UNPKG

@mountainpass/hooked-cli

Version:
78 lines (76 loc) 3.56 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 fs from 'fs'; import inquirer from 'inquirer'; import YAML from 'yaml'; import defaults from './defaults.js'; import logger from './utils/logger.js'; const HEADER = `# # Hooked configuration file # See https://github.com/mountain-pass/hooked for more information. # # To install the cli: npm i -g @mountainpass/hooked-cli # To enable yaml validation: https://github.com/mountain-pass/hooked/blob/main/_CONFIG.md#recommended---enable-yaml-schema # `; /** * Generates a blank hooked.yaml file contents. */ export const generateBlankTemplateFileContents = () => { return `${HEADER}${YAML.stringify(defaults.CONFIG_BLANK(), { blockQuote: 'literal' })}`; }; export const generateAdvancedBlankTemplateFileContents = () => { return `${HEADER}${YAML.stringify(defaults.CONFIG_ADVANCED_GREETING())}`; }; /** Writes the config to the file location. */ export const writeConfig = (contents, overwrite) => __awaiter(void 0, void 0, void 0, function* () { const filepath = defaults.getDefaults().HOOKED_FILE; if (!overwrite && fs.existsSync(filepath)) { throw new Error(`Cannot create - file already exists '${filepath}'`); } logger.info(`Writing config file - ${filepath}`); fs.writeFileSync(filepath, contents, { encoding: 'utf-8' }); }); export const writeBlankConfig = () => __awaiter(void 0, void 0, void 0, function* () { yield writeConfig(generateBlankTemplateFileContents(), false); }); /** * Facilitates creating configuration files. */ export const init = (options) => __awaiter(void 0, void 0, void 0, function* () { // if batch and init, create a blank config if (options.batch === true && options.init === true) { yield writeConfig(generateBlankTemplateFileContents(), options.force); return; } // throw error if (options.batch === true) { throw new Error(`Interactive prompts not supported in batch mode. [2] No config file found - "${defaults.getDefaults().HOOKED_FILE}".`); } // ask user which hooked.yaml template to use (NOTE: even if only one option, still ask user the chance to escape without creating a file!) yield inquirer.prompt([ { type: 'list', name: 'init', message: 'Create new config from:', pageSize: defaults.getDefaults().PAGE_SIZE, choices: [ { name: 'new Blank template', value: 'blank' }, { name: 'new Advanced Blank template', value: 'advanced' } ], loop: true } ]).then((answers) => __awaiter(void 0, void 0, void 0, function* () { if (['blank', 'advanced'].includes(answers.init)) { const contents = answers.init === 'advanced' ? generateAdvancedBlankTemplateFileContents() : generateBlankTemplateFileContents(); yield writeConfig(contents, options.force); } })); });