UNPKG

16.4 kBJavaScriptView Raw
1#!/usr/bin/env node
2var program=require("commander"),shell=require("shelljs"),path=require("path"),fs=require("fs"),os=require("os"),readdirp=require("readdirp"),colors=require("colors/safe"),scaffold=require("./scaffold"),extend=require("./extend"),utils=require("./utils"),awsClient=require("./aws"),auth=require("./auth"),template=require("./template"),vectors=require("./vectors"),deploy=require("./deploy"),dotenv=require("dotenv"),BASE_URL="https://turbo-dashboard.herokuapp.com",VERTEX_BASE_URL="https://vertex360.herokuapp.com",PLATFORM_VECTOR_URL="https://platform.turbo360-vector.com";program.arguments("<action> <arg>").option("-e, --express","with express").option("-s, --static","static site").option("-r, --react","react/redux app").option("-v, --vertex","vertex project").option("-vc, --vector","vectors app").option("-i, --app_id <app_id>","APP Id").action(function(e,n){}),program.command("help").description("show cheatsheet of basic commands").action(function(){utils.readFile(path.join(__dirname,"package.json")).then(function(e){var n=JSON.parse(e);return console.log(colors.white.bold("\nTURBO CLI VERSION")),console.log(colors.cyan("-> "+n.version)),console.log(colors.white.bold("\nFULL DOCUMENTATION")),console.log(colors.cyan("-> https://docs.turbo360.co")+"\n"),utils.getRequest("https://cdn.turbo360-dev.com/dist/help.json")}).then(function(e){if("success"!==e.confirmation)throw new Error("Help JSON request failed.");var n=e.data;Object.keys(n).forEach(function(e){console.log(colors.white.bold(e.toUpperCase())),n[e].forEach(function(e){console.log(colors.cyan(e.cmd)),console.log(e.desc+"\n")})})}).catch(function(e){console.log(colors.red("\nError: "+e.message+"\n"))})}),program.command("new <name>").description("create new Turbo 360 project").action(function(e){shell.mkdir("-p",e),shell.cd(e),program.express?scaffold(shell,e,"fullstack"):program.react?scaffold(shell,e,"react"):(program.vertex,scaffold(shell,e,"vertex"))}),program.command("app <appId>").description("connect app to staging environment").action(function(e){if(0==auth.isLoggedIn())return console.log("\nPlease Log In to Connect Site:"),void console.log("$ turbo login\n");var n=null,o=null,r=null;auth.currentUser().then(function(n){return r=n,utils.getRequest(BASE_URL+"/api/site/"+e)}).then(function(t){if("success"!=t.confirmation)throw new Error(t.message);if(0==auth.isAuthorized(t.result,r))throw new Error("\nYou must be the admin or a collaborator for this project. To login:\n$ turbo login\n");return o=t.result,n={siteId:e,apiKey:o.api.key},utils.readFile(".env","\n\n.env file not found. Please add .env file.\n\n")}).then(function(e){var r=dotenv.parse(e);r.TURBO_APP_ID=n.siteId,r.TURBO_API_KEY=n.apiKey,r.TURBO_APP_SLUG=o.slug,r.TURBO_CDN="https://cdn.turbo360-vertex.com/"+o.slug+"/public",r.TMP_DIR="/tmp",null==r.SESSION_SECRET&&(r.SESSION_SECRET="YOUR_SESSION_SECRET"),null==r.TURBO_ENV&&(r.TURBO_ENV="dev");var t="";return Object.keys(r).forEach(function(e,n){t+=e+"="+r[e]+"\n"}),utils.write(".env",t),console.log(colors.cyan("\nApp credentials successfully configured.\n")),e}).catch(function(e){console.log(colors.red("\nError: "+e.message+"\n"))})}),program.command("extend <format>").description("extend Turbo 360 project").action(function(e){console.log(colors.cyan("\nExtending project...\n")),extend(shell,e).then(function(n){const o=null==n.message?"\nProject successfully extended: "+e+"\n":"\n"+n.message+"\n";console.log(colors.cyan(o))}).catch(function(e){console.log(colors.red("\nError: "+e.message+"\n"))})}),program.command("template <format>").description("Create Vertex 360 template").action(function(e){if(null==template[e])return console.log("\nInvalid Template. Please Select From the following: "),console.log(Object.keys(template).join("\n")),void console.log("\n");var n=utils.isTurboApp();if(1==n.isTurboApp)return void scaffold(shell,e,"template",null);if("package.json"!=n.missing)return void console.log("\nInvalid Turbo 360 app: "+n.reason+"\n");if(0==auth.isLoggedIn())return console.log("\nPlease Log In:"),void console.log("$ turbo login\n");console.log("Creating template...");var o=null,r=null,t=null;auth.currentUser().then(function(e){return t=e,auth.connectApp()}).then(function(e){if(r=e,0==e.siteId.length)throw new Error("App Id required");if(0==e.apiKey.length)throw new Error("API Key required");return utils.getRequest(BASE_URL+"/api/site/"+e.siteId)}).then(function(n){if(o=n.result,0==auth.isAuthorized(o,t))throw new Error("\nYou must be the admin or a collaborator for this project. To login:\n$ turbo login\n");return r.slug=o.slug,utils.postRequest(PLATFORM_VECTOR_URL+"/seedtemplate",{source:e,app:o.slug})}).then(function(n){var r={category:e,status:"dev"};return utils.putRequest(VERTEX_BASE_URL+"/api/site/"+o.id,{origin:"vertex360",template:r})}).then(function(n){shell.mkdir("-p",r.slug),shell.cd(r.slug),scaffold(shell,e,"template",r)}).catch(function(e){console.log(colors.red("\nError: "+e.message+"\n"))})}),program.command("resource <name>").description("Add resource").action(function(e){extend(shell,"resource",{name:e}).then(function(e){shell.echo(colors.cyan("\n"+e.message+"\n"))}).catch(function(e){shell.echo("Error: "+e.message)})}),program.command("page <name>").description("Upload page").action(function(e){var n=null,o=null,r=null;if(0==auth.isLoggedIn())return console.log("\nPlease Log In to Add Page:"),void console.log("$ turbo login\n");auth.currentUser().then(function(e){return n=e,utils.readFile(".env")}).then(function(e){o=dotenv.parse(e);var n=o.TURBO_APP_ID;if(null==n)throw new Error('Missing App ID. Please add to .env file under "TURBO_APP_ID" key.');if(n.length<20)throw new Error('Please specify Turbo App ID in the .env file under "TURBO_APP_ID" key.');return utils.getRequest(BASE_URL+"/api/site/"+n)}).then(function(o){if(r=o.result,0==auth.isAuthorized(r,n))throw new Error("\nYou must be the admin or a collaborator to deploy this project. To login:\n$ turbo login\n");return shell.cd("pages"),utils.readFile(e+".json")}).then(function(n){if(shell.cd(".."),"global"==e)return utils.putRequest(VERTEX_BASE_URL+"/api/site/"+r.id,{globalConfig:n});var o=JSON.stringify(n);return utils.postRequest(PLATFORM_VECTOR_URL+"/pageconfig",{app:r.slug,page:e,config:o})}).then(function(n){return"global"==e?null:utils.postRequest(BASE_URL+"/admin/resetpage",{page:e,appslug:r.slug})}).then(function(n){console.log(colors.cyan("\nPage Successfully Configured: "+e+"\n"))}).catch(function(e){console.log(colors.red("\nError: "+e.message+"\n"))})}),program.command("connect").description("Connect to Vertex 360 site").action(function(){var e=null,n=null,o=null,r=null;if(0==auth.isLoggedIn())return console.log("\nPlease Log In to Connect Site:"),void console.log("$ turbo login\n");auth.connectApp().then(function(e){if(0==e.siteId.length)throw new Error("App Id required");if(0==e.apiKey.length)throw new Error("API Key required");return n=e,auth.currentUser()}).then(function(e){return r=e,utils.getRequest(BASE_URL+"/api/site/"+n.siteId)}).then(function(t){if("success"!=t.confirmation)throw new Error(t.message);if(0==auth.isAuthorized(t.result,r))throw new Error("\nYou must be the admin or a collaborator for this project. To login:\n\n$ turbo login\n");if(o=t.result,e=n,o.api.key!==e.apiKey)throw new Error("Invalid API Key.");return utils.readFile(".env","\n\n.env file not found. Please add .env file.\n\n")}).then(function(n){var r=dotenv.parse(n);r.TURBO_APP_ID=e.siteId,r.TURBO_API_KEY=e.apiKey,r.TURBO_APP_SLUG=o.slug,r.TURBO_CDN="https://cdn.turbo360-vertex.com/"+o.slug+"/public",r.TMP_DIR="/tmp",null==r.SESSION_SECRET&&(r.SESSION_SECRET="YOUR_SESSION_SECRET"),null==r.TURBO_ENV&&(r.TURBO_ENV="dev");var t="";return Object.keys(r).forEach(function(e,n){t+=e+"="+r[e]+"\n"}),utils.write(".env",t),console.log("\nApp successfully connected to Turbo 360 hosting environment! To deploy:\n\n$ turbo deploy\n"),n}).catch(function(e){console.log(colors.red("\nError: "+e.message+"\n"))})}),program.command("logs").description("show logs").action(function(e,n){if(0==auth.isLoggedIn())return console.log("\nPlease Log In to Connect Site:"),void console.log("$ turbo login\n");var o=null,r=null;auth.currentUser().then(function(e){return r=e,utils.readFile(".env","\n\n.env file not found. Please add .env file.\n\n")}).then(function(e){var n=dotenv.parse(e),o=[{text:"TURBO_APP_ID",min:20,placeholder:"<TURBO_APP_ID>"}];if(null!=utils.validateEnv(n,o))throw new Error("\nEnv mis-configuration. Connect project to Turbo site then try again:\n\n$ turbo connect\n\n");return utils.getRequest(BASE_URL+"/api/site/"+n.TURBO_APP_ID)}).then(function(e){if("success"!=e.confirmation)throw new Error(e.message);if(0==auth.isAuthorized(e.result,r))throw new Error("\nYou must be the admin or a collaborator for this project. To login:\n$ turbo login\n");return o=e.result,utils.postRequest(PLATFORM_VECTOR_URL+"/logs",{site:{id:o.id,slug:o.slug}})}).then(function(e){const n=e.data.join("");console.log("\n"+n)}).catch(function(e){console.log(colors.red("\nError: "+e.message+"\n"))})}),program.command("version").description("show current version").action(function(e,n){utils.readFile(path.join(__dirname,"package.json")).then(function(e){var n=JSON.parse(e);console.log(colors.cyan("\nTurbo CLI Version: "+n.version+"\n"))}).catch(function(e){console.log(colors.red("\nError: "+e.message+"\n"))})}),program.command("login").description("login to Turbo 360 account").action(function(e,n){auth.showPrompt().then(function(e){return utils.postRequest(BASE_URL+"/account/login",{email:e.email,password:e.password})}).then(function(e){if("success"!=e.confirmation)throw new Error(e.message);var n={id:e.user._id,username:e.user.username,image:e.user.image,slug:e.user.slug,firstName:e.user.firstName,lastName:e.user.lastName};return shell.cd(""),utils.write(".turbo_user",JSON.stringify(n,null,2)+"\n"),console.log(colors.cyan("\nSuccess: Currently Logged In As "+n.username+"\n")),n}).catch(function(e){console.log(colors.red("\nError: "+e.message+"\n"))})}),program.command("check-env").description("check env file").action(function(e,n){try{fs.readFileSync(".env","utf8")}catch(e){var o="TURBO_ENV=dev";return o+="\nTURBO_CDN=https://cdn.turbo360-vertex.com",o+="\nSESSION_SECRET=YOUR_SESSION_SECRET",o+="\nTURBO_APP_ID=<TURBO_APP_ID>",o+="\nTMP_DIR=/tmp",o+="\nTURBO_API_KEY=<TURBO_API_KEY>",o+="\nTURBO_APP_SLUG=<TURBO_APP_SLUG>",void utils.write(".env",o)}}),program.command("postinstall").description("npm post install hook").action(function(e,n){try{fs.readFileSync(".env","utf8")}catch(e){var o="TURBO_ENV=dev";return o+="\nTURBO_CDN=https://cdn.turbo360-vertex.com",o+="\nSESSION_SECRET=YOUR_SESSION_SECRET",o+="\nTURBO_APP_ID=<TURBO_APP_ID>",o+="\nTMP_DIR=/tmp",o+="\nTURBO_API_KEY=<TURBO_API_KEY>",o+="\nTURBO_APP_SLUG=<TURBO_APP_SLUG>",void utils.write(".env",o)}}),program.command("devserver").option("-p, --python","run flask server").description("run development server").action(function(e,n){var o=process.platform,r=e.python?"python":"node";return utils.readFile("package.json","\n\npackage.json file not found. make sure you are in the root directory of the project.\n\n").then(function(e){if("vertex"==JSON.parse(e).format)"win32"==o?shell.exec("node_modules\\.bin\\nodemon node_modules\\vertex360\\www\\vertex_server.js --quiet"):shell.exec("node_modules/.bin/nodemon node_modules/vertex360/www/vertex_server.js --quiet");else{if("python"==r)return void shell.exec("python3 node_modules/turbo360/www/pyserver.py",function(e,n,o){o?console.log("\n* * * * * Please install Flask: $ pip install flask * * * * * \n"):console.log("Program output:",n)});"win32"==o?shell.exec("node_modules\\.bin\\nodemon node_modules\\turbo360\\www\\server.js --quiet"):shell.exec("node_modules/.bin/nodemon node_modules/turbo360/www/server.js --quiet")}}).catch(function(e){console.log(colors.red("\nError: "+e.message+"\n"))})}),program.command("deploy").option("-d, --dev","deploy to dev").option("-p, --prod","deploy to production").description("deploy app").action(function(e,n){var o=e.prod?"prod":"dev",r=null,t=null,l=null,s=null,i=null,c=null,a=null;if(0==auth.isLoggedIn())return console.log("\nPlease Log In to Deploy:"),void console.log("$ turbo login\n");auth.currentUser().then(function(e){return c=e,utils.readFile("package.json","\n\npackage.json file not found. Make sure you are in the root directory of the project.\n\n")}).then(function(e){return l=JSON.parse(e),a=null==l.format?null==l.dependencies.react?"static":null==l.dependencies.express?"react":"fullstack":l.format,"vertex"==a&&(o="vertex"),utils.readFile(".env","\n\n.env file not found. Please add .env file then run:\n\n$ turbo connect\n\n")}).then(function(e){r=dotenv.parse(e);var n=[{text:"TURBO_CDN",min:20,placeholder:"https://cdn.turbo360-vertex.com"},{text:"TURBO_APP_SLUG",min:6,placeholder:"<TURBO_APP_SLUG>"},{text:"TURBO_API_KEY",min:20,placeholder:"<TURBO_API_KEY>"},{text:"TURBO_APP_ID",min:20,placeholder:"<TURBO_APP_ID>"}];if(null!=utils.validateEnv(r,n))throw new Error("\nEnv mis-configuration. Connect project to Turbo site then try again:\n\n$ turbo connect\n\n");return awsClient.deployConfig({app:r.TURBO_APP_ID,env:o})}).then(function(e){if(void 0==e)throw new Error("Turbo app not found. Please check your APP_ID.");if(t=e.app,0==auth.isAuthorized(t,c))throw new Error("\nYou must be the admin or a collaborator to deploy this project. To login:\n$ turbo login\n");return s=e.deploy,i=e.bucket,awsClient.deleteObject({Bucket:i,Key:t.slug},s)}).then(function(e){deploy.deployToVertex(s,i,t)}).catch(function(e){console.log(colors.red("\nError: "+e.message+"\n"))})}),program.command("customDeploy").description("Deploy to custom environment").action(function(){if(0==auth.awsConfigSet())return console.log("\nPlease Configure AWS Setting:"),void console.log("$ turbo awsConfig\n");var e=null,n=null;auth.awsConfig().then(function(o){return e=o.key+"::"+o.secret,n=o.role,auth.showAWSPrompt()}).then(function(o){if(0==o.bucket.length)throw new Error("Please specify a bucket");if(0==o.folder.length)throw new Error("Please enter a folder");var r=o.bucket.trim(),t=o.folder.trim();console.log(JSON.stringify(o)),deploy.deployToCustom(e,r,{slug:t,role:n})}).catch(function(e){console.log(colors.red("\nError: "+e.message+"\n"))})}),program.command("awsConfig").description("Configure AWS credentials").action(function(){auth.showAWSConfigInputs().then(function(e){if(0==e.awsKey.length)throw new Error("Please enter your AWS key");if(0==e.awsSecret.length)throw new Error("Please enter your AWS secret");if(0==e.awsRole.length)throw new Error("Please enter your AWS role");var n={key:e.awsKey,secret:e.awsSecret,role:e.awsRole};shell.cd(""),utils.write(".turbo_aws_config",JSON.stringify(n,null,2)+"\n"),console.log("AWS Settings Successfully Configured.")}).catch(function(e){console.log(colors.red("\nError: "+e.message+"\n"))})}),program.command("package").description("build package").action(function(e,n){if(0==auth.isLoggedIn())return console.log("\nPlease Log In:"),void console.log("$ turbo login\n");var o=null,r=null,t=null,l=null,s=e.prod?"prod":"dev",i=null,c=null;auth.currentUser().then(function(e){return o=e,utils.readFile(".env")}).then(function(e){r=dotenv.parse(e);var n=r.TURBO_APP_ID;if(null==n)throw new Error('Missing App ID. Please add to .env file under "TURBO_APP_ID" key.');if(n.length<20)throw new Error('Please specify Turbo App ID in the .env file under "TURBO_APP_ID" key.');return utils.readFile("package.json")}).then(function(e){return t=JSON.parse(e),null==t.format?null==t.dependencies.react?format="static":null==t.dependencies.express?format="react":format="fullstack":format=t.format,"vertex"==format&&(s="vertex"),awsClient.deployConfig({app:r.TURBO_APP_ID,env:s})}).then(function(e){if(void 0==e)throw new Error("Turbo app not found. Please check your APP_ID.");return l=e.app,i=e.deploy,c=e.bucket,awsClient.deleteObject({Bucket:c,Key:l.slug},i)}).then(function(e){if("vertex"==format)return void deploy.build(i,c,l)}).catch(function(e){console.log(colors.red("\nError: "+e.message+"\n"))})}),program.command("s3-config").description("Show S3 configuration").action(function(e,n){utils.readFile(path.join(__dirname,"aws/s3-configuration.txt")).then(function(e){console.log(e)}).catch(function(e){console.log(colors.red("\nError: "+e.message+"\n"))})}),program.parse(process.argv);
\No newline at end of file