UNPKG

templates-mo

Version:

Templates is a scaffolding framework that makes code generation simple, dynamic, and reusable. Generate files, parts of your app, or whole project structures—without the repetitive copy-pasting

127 lines (126 loc) • 5.12 kB
"use strict"; 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()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createHandler = exports.options = void 0; /* eslint-disable no-prototype-builtins */ const is_1 = __importDefault(require("is")); const templates_1 = __importDefault(require("../../templates")); const logger_1 = __importDefault(require("../../utilities/logger")); const helpers_1 = require("./helpers"); // TODO: async is not working. need to debug. Completely ignores this function when its async const options = (yargs) => { const { argv } = yargs; const template = argv._[0]; yargs.options({ use: { alias: 'u', describe: 'Template package to create your with', type: 'string', }, packages: { alias: 'p', describe: 'Additional Packages to use when building your template', type: 'array', }, default: { alias: 'd', type: 'boolean', describe: 'Use all default answers to all prompts', }, newFolder: { alias: 'f', describe: 'Create a new folder', type: 'boolean', }, force: { describe: 'force the template to be made. This will override any files that tps needs to create', type: 'boolean', }, wipe: { describe: 'force the template to be made. This will delete the directory if exists', type: 'boolean', }, hidden: { describe: 'Prompt all hidden prompts', type: 'boolean', }, }); if (!template) return yargs; const templateOptions = (0, helpers_1.getCliArgsFromTemplate)(template); yargs.options(templateOptions); return yargs; }; exports.options = options; const createHandler = (argv) => __awaiter(void 0, void 0, void 0, function* () { /** * if we ever want to be able to pass a name in then we should add this to the flags. * @example * { * "name": { * "alias": "n", * "describe": "Name for template rendering. defaults to base name of the destination path", * "type": "string" * }, * } */ const dest = process.cwd(); const { packages, buildPaths } = argv, answers = __rest(argv, ["packages", "buildPaths"]); const tpsConfig = {}; if (argv.hasOwnProperty('newFolder')) tpsConfig.newFolder = argv.newFolder; if (argv.hasOwnProperty('force')) tpsConfig.force = argv.force; if (argv.hasOwnProperty('wipe')) tpsConfig.wipe = argv.wipe; if (argv.hasOwnProperty('default')) tpsConfig.default = argv.default; if (argv.hasOwnProperty('hidden')) tpsConfig.hidden = argv.hidden; logger_1.default.cli.info('Tps Config: %n', tpsConfig); const tps = new templates_1.default(argv.use, tpsConfig); // @ts-expect-error wrong types for `is` if (is_1.default.array(packages) && !is_1.default.array.empty(packages)) { logger_1.default.cli.info('Loading packages:', packages); tps.loadPackages(packages); } if (tps.hasPrompts()) { logger_1.default.cli.info('Answers to prompts: %n', answers); tps.setAnswers(answers); } // @ts-expect-error wrong types for `is` const hasBuildPaths = !is_1.default.array.empty(buildPaths); const renderItems = hasBuildPaths ? buildPaths : null; const renderData = { name: argv.name, }; logger_1.default.cli.info('Build paths: %n', buildPaths); const results = yield tps.render(dest, renderItems, renderData); const templatesBuilt = Array.isArray(results) ? results : [results]; templatesBuilt.forEach((template) => { console.log(template); }); }); exports.createHandler = createHandler;