diff --git a/cli-tool/commands/index.js b/cli-tool/commands/index.js index 84b384b8b..8b78dc04c 100644 --- a/cli-tool/commands/index.js +++ b/cli-tool/commands/index.js @@ -1,2 +1,3 @@ exports.init = require('./init'); exports.deploy = require('./deploy'); +exports.update = require('./update'); diff --git a/cli-tool/commands/update/index.js b/cli-tool/commands/update/index.js new file mode 100644 index 000000000..13b1fe046 --- /dev/null +++ b/cli-tool/commands/update/index.js @@ -0,0 +1,123 @@ +const checkHerokuLogin = require('../deploy/checkHerokuLogin'); +const gitCheckoutHApp = require('../deploy/gitCheckoutHApp'); +const createTMP = require('../deploy/createTMP'); +const downloadUnzip = require('../deploy/downloadUnzip'); +const initBackendnCommit = require('../deploy/initBackendnCommit'); +const inputPrompts = require('../deploy/inputPrompts'); +const pushBackend = require('./pushBackend'); +const herokuNotAuthMess = require('../init/post-init'); +const spinner = require('../../helpers/spinner'); + +const chalk = require('chalk'); + +module.exports = async () => { + spinner(true, 'Check for Heroku Login Status', 0, false, async(herokuSpin) => { + const checkHeroku = await checkHerokuLogin(); + herokuSpin.stop(); + if(checkHeroku.res){ + console.log( + '\n'+ + chalk.bold.green("Heroku Login Successfull")+'\n\n'+ + chalk.white(checkHeroku.output)+ + '\n' + ); + const appname = await inputPrompts.appName(); + if(appname.length > 1){ + createTMP(async (tempPath, resPath) => { + spinner(true, 'Downloading latest Backend Repo and Unzipping Now', 0, false, async(downSpin) => { + const downloadUnzipBackend = downloadUnzip(tempPath, async () => { + downSpin.stop(); + console.log( + '\n'+ + chalk.bold.green("Download and Unzip of Backend Successfull")+'\n\n'+ + chalk.white("Backend downloaded and Unzipped in "+tempPath+". Don\'t Worry this will be Autocleaned after this Process.")+ + '\n' + ); + spinner(true, 'Initializing Repo for Deploying to Heroku', 0, false, async(initSpin) => { + const gitInitBack = await initBackendnCommit(resPath); + initSpin.stop(); + if(gitInitBack.res){ + console.log( + '\n'+ + chalk.bold.green("Initialized Backend and Ready to Deploy to Heroku")+'\n\n'+ + chalk.white(gitInitBack.output)+ + '\n' + ); + spinner(true, 'Checking Heroku App for Deployment', 0, false, async(checkSpin) => { + const checkOutHeroku = await gitCheckoutHApp(appname, resPath); + checkSpin.stop(); + if(checkOutHeroku.res){ + console.log( + '\n'+ + chalk.bold.green("Sucessfully Initialized the Heroku app for Backend Deployment")+'\n\n'+ + chalk.white(checkOutHeroku.output)+ + '\n' + ); + spinner(true, 'Pushing the Latest Backend to Heroku App', 0, false, async(pushSpin) => { + const pushHeroku = await pushBackend(resPath); + pushSpin.stop(); + if(pushHeroku.res){ + console.log( + '\n'+ + chalk.bold.green("Pushing to Backend Successfull. Here is the Build Process.")+'\n\n'+ + chalk.white(pushHeroku.output)+ + '\n' + ); + console.log( + '\n'+ + chalk.yellow.bold(`You can Access the backend in the following Address for Creating the Super User\n>> https://${appname}.herokuapp.com`)+ + '\n' + ); + process.exit(); + } else { + console.log( + '\n'+ + chalk.bold.red("Pushing to Backend Failed with the Following Error")+'\n\n'+ + chalk.white(pushHeroku.output)+ + '\n' + ); + process.exit(); + } + }) + } else { + console.log( + '\n'+ + chalk.bold.red("Initializing Heroku App Failed with the Following Error")+'\n\n'+ + chalk.white(checkOutHeroku.output)+ + '\n' + ); + process.exit(); + } + }) + } else { + console.log( + '\n'+ + chalk.bold.red("Initializing Backend Zip Failed with the Following Error")+'\n\n'+ + chalk.white(gitInitBack.output)+ + '\n' + ); + process.exit(); + } + }) + }) + }) + }) + } else { + console.log( + '\n'+ + chalk.bold.red("Appname Should be More than Atleast 1 Chars")+'\n' + ); + process.exit(); + } + } else { + console.log( + '\n'+ + chalk.bold.red("It Looks like You are not Logged in to Heroku")+'\n\n'+ + chalk.white(checkHeroku.output)+ + '\n'+ + herokuNotAuthMess + ); + process.exit(); + } + }) +} diff --git a/cli-tool/commands/update/pushBackend.js b/cli-tool/commands/update/pushBackend.js new file mode 100644 index 000000000..be9a67c1f --- /dev/null +++ b/cli-tool/commands/update/pushBackend.js @@ -0,0 +1,31 @@ +const execa = require('execa'); +const path = require('path'); + +module.exports = async (cwdPath) => { + const backPath = path.resolve(cwdPath); + try { + const result = await execa('git', ['push','heroku', 'master', '--force'], { cwd: backPath }); + if(!result.failed && !result.killed && !result.timedOut && !result.isCancelled){ + return { + res: true, + output: result.stdout+'\n'+result.stderr, + cmd: result.command, + exitCode: result.exitCode + }; + } else { + return { + res: false, + output: result.stdout+'\n'+result.stderr, + cmd: result.command, + exitCode: result.exitCode + }; + } + } catch(e) { + return { + res: false, + output: e.stdout+'\n'+e.stderr, + cmd: e.command, + exitCode: e.exitCode + }; + } +} diff --git a/cli-tool/index.js b/cli-tool/index.js index 7e8fd795e..a720f2e6d 100644 --- a/cli-tool/index.js +++ b/cli-tool/index.js @@ -4,7 +4,7 @@ const spinner = require('./helpers/spinner'); const yargs = require('yargs') const { hideBin } = require('yargs/helpers') const initialRender = require('./displays/initial-render'); -const { init, deploy } = require('./commands'); +const { init, deploy, update } = require('./commands'); console.log( initialRender() @@ -19,6 +19,11 @@ yargs(hideBin(process.argv)) deploy(); }); }) + .command('update', 'Update Your Backend to latest Version', {}, (argv) => { + spinner(false, `Initializing Now`, 2, false, function(){ + update(); + }); + }) .argv if(yargs.argv._.length < 1){ diff --git a/cli-tool/package.json b/cli-tool/package.json index 75119197a..4bff2c56e 100644 --- a/cli-tool/package.json +++ b/cli-tool/package.json @@ -1,6 +1,6 @@ { "name": "gindex-cli-tool", - "version": "1.0.0", + "version": "1.1.0", "description": "A Cli Tool to Publish Google Drive Index's Frontend and Backend all in One Tool", "main": "index.js", "scripts": {