UNPKG

@agatee/cli

Version:

CLI for Agatee App

124 lines (123 loc) 6.1 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CreateCMD = void 0; var fs = __importStar(require("fs")); var starter_code_1 = require("./starter-code"); var file_1 = require("../../utils/file"); var workers_1 = require("../../utils/workers"); var console_1 = require("../../utils/console"); var KNOW_ADDITIONAL_PARAMS = [ 'api-style', 'api' ]; var CreateCMD = /** @class */ (function () { function CreateCMD() { this.longCmd = 'create'; this.shortCmd = 'create'; } CreateCMD.prototype.exec = function (params) { var _this = this; // check if there is an error and stop cmd if so this.params = params; this.checkError(); var appName = params.params[0]; var packageJsonContent = (0, starter_code_1.packageJsonStarterCode)(appName); var appContent = (0, starter_code_1.appStarterCode)(appName, { apiStyle: this.getApiStyle() }); var serverContent = (0, starter_code_1.serverStarterCode)(appName); var indexContent = (0, starter_code_1.indexStarterCode)(appName); var tsConfigContent = (0, starter_code_1.tsConfigCode)(appName); var dotenvContent = (0, starter_code_1.dotEnvCode)(appName); var environmentContent = (0, starter_code_1.environmentStarterCode)(appName); var agateeJsonContent = (0, starter_code_1.agateeJsonCode)(appName, { apiStyle: this.getApiStyle() }); // Creating directories fs.mkdirSync("./" + appName + "/app", { recursive: true }); fs.mkdirSync("./" + appName + "/environments", { recursive: true }); fs.mkdirSync("./" + appName + "/node_modules"); // Writting filContent (0, file_1.createFile)("./" + appName + "/index.ts", indexContent.trim()); (0, file_1.createFile)("./" + appName + "/environments/index.ts", environmentContent.trim()); (0, file_1.createFile)("./" + appName + "/app/server.ts", serverContent.trim()); (0, file_1.createFile)("./" + appName + "/app/app.ts", appContent.trim()); (0, file_1.createFile)("./" + appName + "/package.json", packageJsonContent.trim()); (0, file_1.createFile)("./" + appName + "/tsconfig.json", tsConfigContent.trim()); (0, file_1.createFile)("./" + appName + "/agatee.json", agateeJsonContent.trim()); (0, file_1.createFile)("./" + appName + "/.env", dotenvContent.trim()); console.log('> npm install'); (0, workers_1.runSpawnWorker)("npm", ['install', '--prefix', "./" + appName, "./" + appName], { onExit: function () { return setTimeout(function () { _this.clearDir(appName); if (_this.getApiStyle() == 'graphql') { (0, console_1.logSuccess)("[...] Adding graphql"); process.chdir(appName); (0, workers_1.runSpawnWorker)("gat", ['add', 'graphql'], ({ onDataErr: function () { }, onExit: function () { (0, console_1.logSuccess)("[✔] App created"); } })); } else { (0, console_1.logSuccess)("[✔] App created"); } }, 1000); }, onDataErr: function (err) { if (err.toLowerCase().includes('npm warn') || err.toLowerCase().includes('npm notice')) { // console.log(err); } else { (0, console_1.logError)(err); } } }); }; CreateCMD.prototype.checkError = function () { var appName = this.params.params[0]; if (!appName) { throw new Error('Project name is required'); } if (fs.existsSync('./' + appName)) { throw new Error('A directory with the same name already exist'); } // checking additionalParams for (var key in this.params.additionalParams) { if (!KNOW_ADDITIONAL_PARAMS.includes(key)) { throw new Error("Unkown option : " + (this.params.additionalParams[key].isShort ? '-' : '--') + key); } } }; // this function is need in widows due to some undesired generated files CreateCMD.prototype.clearDir = function (appName) { var authorizedPath = ['app', '.env', 'node_modules', 'package-lock.json', 'package.json', 'tsconfig.json', 'agatee.json', 'environments', 'index.ts']; var _path = './' + appName + '/'; fs.readdirSync(_path) .filter(function (f) { return !(authorizedPath.includes(f)); }) .map(function (f) { return fs.unlinkSync(_path + f); }); }; CreateCMD.prototype.getApiStyle = function () { var apiStyleParams = this.params.additionalParams['api-style'] || this.params.additionalParams['api']; if (!(apiStyleParams === null || apiStyleParams === void 0 ? void 0 : apiStyleParams.values[0])) return 'rest'; var apiStyle = apiStyleParams.values[0]; return ['graphql', 'rest'].includes(apiStyle) ? apiStyle : 'rest'; }; return CreateCMD; }()); exports.CreateCMD = CreateCMD;