|
| 1 | +import { Command } from './base'; |
| 2 | +import * as inquirer from 'inquirer'; |
| 3 | +import { promises as fs } from 'fs'; |
| 4 | +import * as path from 'path'; |
| 5 | +import { version } from '../../package.json'; |
| 6 | +import * as ora from 'ora'; |
| 7 | +import { exec } from 'child_process'; |
| 8 | + |
| 9 | +const validators: Record< |
| 10 | + string, |
| 11 | + { |
| 12 | + regex: RegExp; |
| 13 | + errorMessage: string; |
| 14 | + } |
| 15 | +> = { |
| 16 | + projectName: { |
| 17 | + regex: /^[a-zA-Z0-9_-]+$/, |
| 18 | + errorMessage: `Project name should contain only letters, numbers, or dashes.`, |
| 19 | + }, |
| 20 | +}; |
| 21 | + |
| 22 | +const validateAnswer = (name: string) => (input: string) => { |
| 23 | + const validator = validators[name]; |
| 24 | + if (!validator.regex.test(input)) { |
| 25 | + throw new Error(validator.errorMessage); |
| 26 | + } |
| 27 | + return true; |
| 28 | +}; |
| 29 | + |
| 30 | +interface InitCommandOptions { |
| 31 | + projectName: string; |
| 32 | + version: string; |
| 33 | +} |
| 34 | + |
| 35 | +export class InitCommand extends Command { |
| 36 | + public async handle(options: Partial<InitCommandOptions>): Promise<void> { |
| 37 | + const question = []; |
| 38 | + |
| 39 | + if (!options.projectName) { |
| 40 | + question.push({ |
| 41 | + type: 'input', |
| 42 | + name: 'projectName', |
| 43 | + message: 'Project name:', |
| 44 | + default: 'my-first-vulcan-project', |
| 45 | + validate: validateAnswer('projectName'), |
| 46 | + }); |
| 47 | + } else { |
| 48 | + validateAnswer('projectName')(options.projectName); |
| 49 | + } |
| 50 | + |
| 51 | + options = { |
| 52 | + ...{ version }, |
| 53 | + ...options, |
| 54 | + ...(await inquirer.prompt(question)), |
| 55 | + }; |
| 56 | + |
| 57 | + await this.createProject(options as InitCommandOptions); |
| 58 | + } |
| 59 | + |
| 60 | + public async createProject(options: InitCommandOptions): Promise<void> { |
| 61 | + const projectPath = path.resolve(process.cwd(), options.projectName); |
| 62 | + await fs.mkdir(projectPath); |
| 63 | + const existedFiles = await fs.readdir(projectPath); |
| 64 | + if (existedFiles.length > 0) |
| 65 | + throw new Error(`Path ${projectPath} is not empty`); |
| 66 | + |
| 67 | + const installSpinner = ora('Creating project...').start(); |
| 68 | + try { |
| 69 | + await fs.writeFile( |
| 70 | + path.resolve(projectPath, 'package.json'), |
| 71 | + JSON.stringify( |
| 72 | + { |
| 73 | + name: options.projectName, |
| 74 | + dependencies: { |
| 75 | + '@vulcan-sql/core': options.version, |
| 76 | + }, |
| 77 | + }, |
| 78 | + null, |
| 79 | + 2 |
| 80 | + ), |
| 81 | + 'utf-8' |
| 82 | + ); |
| 83 | + installSpinner.succeed('Project has been created.'); |
| 84 | + installSpinner.start('Installing dependencies...'); |
| 85 | + await this.execAndWait(`yarn --silent`, projectPath); |
| 86 | + installSpinner.succeed(`Dependencies have been installed.`); |
| 87 | + } catch (e) { |
| 88 | + installSpinner.fail(); |
| 89 | + throw e; |
| 90 | + } finally { |
| 91 | + installSpinner.stop(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private async execAndWait(command: string, cwd: string) { |
| 96 | + return new Promise<void>((resolve, reject) => { |
| 97 | + exec(command, { cwd }, (error, _, stderr) => { |
| 98 | + if (error) { |
| 99 | + reject(stderr); |
| 100 | + } |
| 101 | + resolve(); |
| 102 | + }); |
| 103 | + }); |
| 104 | + } |
| 105 | +} |
0 commit comments