@pecometer/peco-cli
Version:
Pecometer Command Line Interface Application
1 lines • 4.33 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.CCommandLineApp=void 0;const _=require("lodash"),moment=require("moment"),path=require("path"),base_command_1=require("./base-command"),create_app_1=require("../commands/create-app"),file_1=require("./file"),ts_node_1=require("ts-node"),generate_env_1=require("../commands/generate-env"),version_1=require("../commands/version"),check_update_1=require("../commands/check-update"),check_reg_version_1=require("../helpers/check-reg-version"),tsconfig_paths_1=require("tsconfig-paths"),package_json_1=require("../package");class CCommandLineApp{constructor(){this._output=new base_command_1.CBaseCommand,this._fileHelper=new file_1.CFileHelper,this._isTypescript=!1,this._cwd=process.cwd(),this._commandsMap={"check-update":check_update_1.CheckUpdate,"create-app":create_app_1.CreateApp,"generate-env":generate_env_1.GenerateEnv,version:version_1.Version},process.on("unhandledRejection",((reason,promise)=>{console.error(reason),process.exit()}))}async _parseArguments(args){let index=_.findIndex(args,(o=>o.includes("node")));if(-1===index||index+1>=args.length)throw new Error("Unable to process request. Unknown Error during command parse.");if(index+=2,index>=args.length){if(await this._checkHasBackendApplication())return!0;this._output.outputNewLine(),this._output.warn("No command selected, use a command below to execute the action:");for(const key in this._commandsMap)if(this._commandsMap[key]){const command=new this._commandsMap[key];this._output.warn(` -- ${key} - ${command.description}`)}throw this._output.outputNewLine(),new Error("No command was passed to PecoCLI.")}const cls=args[index].split(".")[0],method=args[index].split(".")[1]||"execute";if(void 0===this._commandsMap[cls]){if(await this._checkHasBackendApplication())return!0;throw new Error(`Unknown command call \`${cls}\``)}return{cls,method,params:_.slice(args,index+1)}}async _checkHasBackendApplication(){return await this._fileHelper.exists(path.join(process.cwd(),"index.ts"))?(this._location=path.join(process.cwd(),"index.ts"),this._isTypescript=!0,!0):await this._fileHelper.exists(path.join(process.cwd(),"backend","index.ts"))?(this._location=path.join(process.cwd(),"backend","index.ts"),this._isTypescript=!0,!0):await this._fileHelper.exists(path.join(process.cwd(),"index.js"))?(this._location=path.join(process.cwd(),"index.js"),!0):!!await this._fileHelper.exists(path.join(process.cwd(),"backend","index.js"))&&(this._location=path.join(process.cwd(),"backend","index.js"),!0)}async run(){this._output.notice(`PecoTS CLI, © ${moment().year()} Pecometer Software Limited, All rights reserved.`);try{const paramData=await this._parseArguments(process.argv);if(!0===paramData){if(process.__pecoCli=!0,this._isTypescript){this._location.includes("backend")&&process.chdir(path.join(process.cwd(),"backend")),ts_node_1.register({project:"./tsconfig.json",transpileOnly:!0});const config=tsconfig_paths_1.loadConfig("./tsconfig.json");tsconfig_paths_1.register({baseUrl:config.absoluteBaseUrl,paths:config.paths}),process.__pecoCli_isTypeScript=!0}this._output.notice("Executing project command line app."),await Promise.resolve().then((()=>require(this._location))),this._isTypescript&&this._location.includes("backend")&&process.chdir(this._cwd)}else{const{cls,method,params}=paramData,command=new this._commandsMap[cls];if(!command[method])throw new Error(`Method \`${method}\` does not exists on class \`${_.capitalize(cls)}\`.`);const result=command[method](...params);if(result)return result.then((async()=>{await this._checkVersion(cls),this._output.outputNewLine()})).catch((err=>{throw this._output.outputNewLine(),err}));await this._checkVersion(cls),this._output.outputNewLine()}}catch(err){err instanceof Error?(this._output.error(err.message),this._output.error(err.stack)):this._output.error(err),this._output.outputNewLine()}}async _checkVersion(cls=""){if("check-update"===cls)return;const result=await check_reg_version_1.CheckRegistryVersion();!0!==result&&(this._output.outputNewLine(),this._output.info("There is an update ready to be installed.",!0),this._output.info(`${package_json_1.name} => ${result}`,!0),this._output.info(`Run npm i -g ${package_json_1.name}`,!0),this._output.outputNewLine())}}exports.CCommandLineApp=CCommandLineApp;