UNPKG

@eljs/create-template

Version:
117 lines (115 loc) 4.14 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/create.ts var create_exports = {}; __export(create_exports, { CreateTemplate: () => CreateTemplate }); module.exports = __toCommonJS(create_exports); var import_create = require("@eljs/create"); var import_utils = require("@eljs/utils"); var import_node_assert = __toESM(require("node:assert")); var import_config = require("./config"); var import_utils2 = require("./utils"); var CreateTemplate = class { /** * 构造函数选项 */ constructorOptions; /** * 当前工作目录 */ cwd; constructor(options) { this.constructorOptions = options; this.cwd = options.cwd || process.cwd(); } async run(projectName) { const template = await this._getTemplate(); const create = new import_create.Create({ ...this.constructorOptions, cwd: this.cwd, template }); await create.run(projectName); } async _getTemplate() { const { scenes, templates } = import_config.defaultConfig; let sceneAnswer = this.constructorOptions.scene; let templateAnswer = this.constructorOptions.template; if (!this.constructorOptions.scene || !(this.constructorOptions.scene in scenes)) { const answer = await (0, import_utils.prompts)( { type: "select", name: "scene", message: "Select the application scene", choices: (0, import_utils2.objectToArray)(scenes) }, { onCancel: import_utils2.onCancel } ); sceneAnswer = answer.scene; } if (!this.constructorOptions.template || !(this.constructorOptions.template in templates[sceneAnswer])) { const answer = await (0, import_utils.prompts)( { type: "select", name: "template", message: "Select the application template", choices: this._formatTemplate(templates[sceneAnswer]) }, { onCancel: import_utils2.onCancel } ); templateAnswer = answer.template; } (0, import_node_assert.default)(sceneAnswer, "Excepted the application scene."); (0, import_node_assert.default)(templateAnswer, "Excepted the application template."); const template = import_config.defaultConfig.templates[sceneAnswer][templateAnswer]; (0, import_node_assert.default)( template, `The selected scene \`${sceneAnswer}\` and template \`${templateAnswer}\` do not corresponding any configuration.` ); return template; } _formatTemplate(template) { return Object.keys(template).map((key) => { const title = template[key].description; return { title, value: key }; }); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { CreateTemplate });