UNPKG

bod

Version:
138 lines (137 loc) 4.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const utils_1 = require("../utils"); const BaseCommand_1 = tslib_1.__importDefault(require("./BaseCommand")); class CreateCommand extends BaseCommand_1.default { constructor() { super({ name: 'create', description: 'Create a new project powered by @sabertazimi/react-scripts', usage: 'create <appName>', }); this.command = 'npm'; this.commandArgs = []; this.postCommands = []; this.resolvePackageManager(); } run(appName_1) { return tslib_1.__awaiter(this, arguments, void 0, function* (appName, additionalOptions = []) { yield this.processTemplateAction(); this.resolveAppPath(appName); this.execute(additionalOptions); }); } getCommand() { return this.command; } getCommandArgs() { return this.commandArgs; } processTemplateAction() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const { templateName } = yield utils_1.inquirer.prompt([ { name: 'templateName', type: 'list', message: 'Select template:', choices: [...CreateCommand.TemplateActions], }, ]); const { command, args, postCommands } = CreateCommand.TemplateActions.find(({ value }) => value === templateName); this.command = command; this.commandArgs = [...args]; this.postCommands = postCommands; }); } resolvePackageManager() { const packageManager = (0, utils_1.findPackageManager)(); CreateCommand.TemplateActions.forEach((action) => { if (action.command === 'npm') action.command = packageManager; }); } resolveAppPath(appName) { this.commandArgs.push(appName); CreateCommand.TemplateActions.forEach(({ postCommands }) => { for (const postCommand of postCommands) { postCommand.args = postCommand.args.map((arg) => { return arg.replace('appPath', appName); }); } }); } execute(additionalOptions) { const proc = utils_1.spawn.sync(this.command, [...this.commandArgs, ...additionalOptions], { stdio: 'inherit', }); if (proc.status !== 0) { throw new Error(`\n\`${this.command} ${this.commandArgs.join(' ')}\` exited.`); } this.postCommands.forEach((postCommand) => { const proc = utils_1.spawn.sync(postCommand.command, postCommand.args, { stdio: 'inherit', }); if (proc.status !== 0) { throw new Error(`\n\`${postCommand.command} ${postCommand.args.join(' ')}\` exited.`); } }); } } CreateCommand.TemplateActions = [ { name: 'Vanilla', value: 'vanilla', command: 'git', args: ['clone', '--depth=1', 'https://github.com/sabertazimi/bod'], postCommands: [ { command: 'mv', args: ['appPath', 'appPath.bak'], }, { command: 'mv', args: ['appPath.bak/packages/webpack-template', 'appPath'], }, { command: 'rm', args: ['-rf', 'appPath.bak'], }, ], }, { name: 'React Framework', value: 'react', command: 'npm', args: [ 'create', 'react-app@latest', '--template', 'cra-template-bod@latest', '--scripts-version', '@sabertazimi/react-scripts@latest', ], postCommands: [], }, { name: 'Vue Framework', value: 'vue', command: 'npm', args: [ 'create', 'vue@latest', ], postCommands: [], }, { name: 'Vite Framework', value: 'vite', command: 'npm', args: [ 'create', 'vite@latest', ], postCommands: [], }, ]; exports.default = CreateCommand;