UNPKG

create-typescript-project-scaffolding

Version:
79 lines (78 loc) 3.69 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import Name from './answerHandlers/name.js'; import BuildPackageJson from './answerHandlers/buildPackageJson.js'; import { CreateNewProjectFiles } from './answerHandlers/createNewProjectFiles.js'; import { spawn } from 'child_process'; class AnswerHandler { handler(answers) { return __awaiter(this, void 0, void 0, function* () { this.answers = answers; this.prepare(); return this.run(); }); } prepare() { console.log('\n\nPreparing...'); new Name(this.answers['project-name']); console.log('Creating package.json file...'); this.prepare_package_json = new BuildPackageJson(Name.PROJECT_NAME, this.answers); console.log('Creating project files...'); this.prepare_CreateNewProjectFiles = new CreateNewProjectFiles(this.answers, Name.PROJECT_PATH, Name.PROJECT_NAME); } run() { return __awaiter(this, void 0, void 0, function* () { if (this.answers['project-ncu-packages']) { console.log('Updating packages...'); try { yield this.prepare_package_json.fetchLatestPackageVersions(); } catch (error) { console.log(error); return error; } } console.log('Saving package.json file...'); this.prepare_package_json.save(Name.PROJECT_PATH); console.log('Writing project files to disk...'); this.prepare_CreateNewProjectFiles.run(); if (this.answers['project-npm-install-packages']) { console.log('Installing packages (running "npm install")...'); try { let npmCommand = 'npm'; if (process.platform === 'win32') { npmCommand = 'npm.cmd'; } const r = spawn(npmCommand, ['install'], { cwd: Name.PROJECT_PATH }); r.stdout.on('data', (x) => console.log(x.toString())); r.stderr.on('data', (x) => console.log(x.toString())); r.on('error', (x) => console.error(x)); } catch (e) { console.log(e); } } if (this.answers['project-git-init']) { console.log('Initializing git (running "git init")...'); try { const r = spawn('git', ['init'], { cwd: Name.PROJECT_PATH }); r.stdout.on('data', (x) => console.log(x.toString())); r.stderr.on('data', (x) => console.log(x.toString())); r.on('error', (x) => console.error(x)); } catch (e) { console.log(e); } } return `\n\nSuccessfully created new project (${Name.PROJECT_NAME})\n\n:)`; }); } } export default new AnswerHandler();