UNPKG

bod

Version:
135 lines (134 loc) 4.19 kB
import { __awaiter } from "tslib"; import { findPackageManager, select, spawn } from '../utils/index.js'; import BaseCommand from './BaseCommand.js'; class CreateCommand extends BaseCommand { 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 __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 __awaiter(this, void 0, void 0, function* () { const templateName = yield select({ message: 'Select template:', choices: CreateCommand.TemplateActions.map(action => ({ name: action.name, value: action.value, })), }); const { command, args, postCommands } = CreateCommand.TemplateActions.find(({ value }) => value === templateName); this.command = command; this.commandArgs = [...args]; this.postCommands = postCommands; }); } resolvePackageManager() { const packageManager = 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 = 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 = 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: [], }, ]; export default CreateCommand;