UNPKG

@procore/core-scripts

Version:

A CLI to enhance your development experience

174 lines • 5.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Generator = void 0; const tslib_1 = require("tslib"); const core_1 = require("@oclif/core"); const cross_spawn_1 = tslib_1.__importDefault(require("cross-spawn")); const debug_1 = tslib_1.__importDefault(require("debug")); const ejs_1 = tslib_1.__importDefault(require("ejs")); const enquirer_1 = require("enquirer"); const fs_1 = tslib_1.__importDefault(require("fs")); const make_dir_1 = tslib_1.__importDefault(require("make-dir")); const path_1 = tslib_1.__importDefault(require("path")); class Generator { constructor(opts) { this.debug = (0, debug_1.default)('@procore/core-scripts/generator'); this.env = opts.env; this.workspace = opts.workspace; this.templatePath = opts.templatePath; this.destPath = opts.destPath; } async render(relativeFromPath, relativeDestPath, data = {}) { const fromPath = this.resolveFromDestTemplate(relativeFromPath); const destPath = this.resolveFromDest(relativeDestPath); const templateData = { fromPath, destPath, data, }; const templateContent = this.readTemplateContent(fromPath); const content = await ejs_1.default.render(templateContent, templateData, { async: true, }); this.writeContent(destPath, content); } async installDependencies(packages, opts = {}) { const args = ['add']; if (opts.exact) { args.push('--exact'); } if (opts.dev) { args.push('--dev'); } if (opts.peer) { args.push('--peer'); } if (opts.cwd) { args.push('--cwd', opts.cwd); } try { core_1.CliUx.ux.action.start('Installing packages, this may take a while'); await this.spawn('yarn', args.concat(packages), { stdio: 'ignore', }); core_1.CliUx.ux.action.stop('Installation completed'); } catch (e) { throw new Error('Failed to install packages'); } } async initializeGit() { const hasGit = await this.hasGit(); if (!hasGit) { return; } const insideGitRepository = await this.isInGitRepository(); if (insideGitRepository) { return; } this.debug('Initalizing Git'); await this.spawn('git', ['init'], { cwd: this.destPath, }); await this.spawn('git', ['add', '.'], { cwd: this.destPath, }); } makeDir(...pathSegments) { const folderPath = this.resolveFromDest(...pathSegments); this.debug(`Creating ${folderPath} folder.`); return (0, make_dir_1.default)(folderPath); } newline() { this.debug(''); } async isInGitRepository() { try { await this.spawn('git', ['rev-parse', '--is-inside-work-tree'], { stdio: 'ignore', cwd: this.destPath, }); return true; } catch (e) { return false; } } async hasGit() { try { await this.spawn('git', ['--version'], { stdio: 'ignore', cwd: this.destPath, }); return true; } catch (e) { return false; } } spawn(command, args, options) { this.newline(); this.debug('Executing Command'); this.debug(` Command name: ${command}`); this.debug(` Command arguments: ${args.join(' ')}`); this.debug(` Command Working Directory: ${options.cwd}`); this.debug(` Command Std IO: ${options.stdio}`); this.newline(); return new Promise((resolve, reject) => { // @ts-ignore Fix type spec const child = (0, cross_spawn_1.default)(command, args, options); child.on('close', (code) => { if (code !== 0) { return reject(new Error(`${command} failed`)); } return resolve(undefined); }); }); } resolveFromDest(...pathSegments) { return path_1.default.resolve(this.destPath, ...pathSegments); } resolveFromDestTemplate(...pathSegments) { return path_1.default.resolve(this.templatePath, ...pathSegments); } readTemplateContent(fromPath) { try { return fs_1.default.readFileSync(fromPath, { encoding: 'utf8' }); } catch (err) { const fileError = err; switch (fileError.code) { case 'ENOENT': throw new Error(`${fromPath} does not exist.`); case 'EISDIR': throw new Error(`${fromPath} template must be a file.`); default: throw err; } } } async confirmAction(message) { const response = await (0, enquirer_1.prompt)({ type: 'confirm', initial: true, name: 'shouldRun', message, }); return response.confirmed; } async writeContent(destPath, content) { const shouldWriteFile = await this.checkDestinationFile(destPath); if (shouldWriteFile) { this.debug(`Creating ${destPath} file.`); fs_1.default.writeFileSync(destPath, content); } } async checkDestinationFile(destPath) { if (fs_1.default.existsSync(destPath)) { return this.confirmAction(`File ${destPath} already exists, are you sure you want to override it?`); } return true; } } exports.Generator = Generator; //# sourceMappingURL=Generator.js.map