UNPKG

@semo/cli

Version:

A command line tools dispatcher

106 lines (102 loc) 3.86 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.handler = exports.builder = exports.aliases = exports.desc = exports.command = exports.plugin = void 0; const path_1 = __importDefault(require("path")); const core_1 = require("@semo/core"); const fs_extra_1 = __importDefault(require("fs-extra")); exports.plugin = 'semo'; exports.command = 'command <name> [description]'; exports.desc = 'Generate a command template'; exports.aliases = ['com']; const builder = function (yargs) { yargs.option('extend', { default: false, alias: 'E', describe: 'generate command in extend directory', }); yargs.option('plugin', { default: false, alias: 'P', describe: 'generate command in plugin directory', }); yargs.option('typescript', { alias: 'ts', describe: 'generate typescript style code', }); }; exports.builder = builder; const handler = function (argv) { const scriptName = argv.scriptName || 'semo'; let commandDir; if (argv.extend) { let extendName = argv.extend; if (extendName !== scriptName && extendName.indexOf(`${scriptName}-plugin-`) === -1) { extendName = `${scriptName}-plugin-${extendName}`; } commandDir = `${argv.extendMakeDir || argv.extendDir}/${extendName}/src/commands`; } else if (argv.plugin) { let pluginName = argv.plugin; if (pluginName.indexOf(`${scriptName}-plugin-`) !== 0) { pluginName = `${scriptName}-plugin-${pluginName}`; } commandDir = `${argv.pluginMakeDir || argv.pluginDir}/${pluginName}/src/commands`; } else { commandDir = argv.commandMakeDir || argv.commandDir; } if (!commandDir) { core_1.Utils.error('"commandDir" missing in config file!'); } const commandFilePath = path_1.default.resolve(commandDir, `${argv.name}.${argv.typescript ? 'ts' : 'js'}`); const commandFileDir = path_1.default.dirname(commandFilePath); fs_extra_1.default.ensureDirSync(commandFileDir); if (core_1.Utils.fileExistsSyncCache(commandFilePath)) { core_1.Utils.error('Command file exist!'); } const name = argv.name.split('/').pop(); let handerTpl, code; if (argv.typescript) { code = `export const disabled = false // Set to true to disable this command temporarily // export const plugin = '' // Set this for importing plugin config export const command = '${name}' export const desc = '${argv.description || name}' // export const aliases = '' // export const middleware = (argv) => {} export const builder = function (yargs: any) { // yargs.option('option', { default, describe, alias }) // yargs.commandDir('${name}') } export const handler = async function (argv: any) { console.log('Start to draw your dream code!') } `; } else { handerTpl = `exports.handler = async function (argv) { console.log('Start to draw your dream code!') }`; code = `exports.disabled = false // Set to true to disable this command temporarily // exports.plugin = '' // Set this for importing plugin config exports.command = '${name}' exports.desc = '${argv.description || name}' // exports.aliases = '' // exports.middleware = (argv) => {} exports.builder = function (yargs) { // yargs.option('option', { default, describe, alias }) // yargs.commandDir('${name}') } ${handerTpl} `; } if (!core_1.Utils.fileExistsSyncCache(commandFilePath)) { fs_extra_1.default.writeFileSync(commandFilePath, code); console.log(core_1.Utils.success(`${commandFilePath} created!`)); } }; exports.handler = handler; //# sourceMappingURL=command.js.map