UNPKG

@tarojs/cli

Version:
129 lines 5.88 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; }; Object.defineProperty(exports, "__esModule", { value: true }); const helper_1 = require("@tarojs/helper"); const lodash_1 = require("lodash"); const path = require("path"); const creator_1 = require("./creator"); const fetchTemplate_1 = require("./fetchTemplate"); const init_1 = require("./init"); const DEFAULT_TEMPLATE_INFO = { name: 'default', css: 'none', typescript: false, compiler: 'webpack5' }; class Page extends creator_1.default { constructor(args) { super(); this.rootPath = this._rootPath; const { modifyCustomTemplateConfig } = args, otherOptions = __rest(args, ["modifyCustomTemplateConfig"]); this.conf = Object.assign({ projectDir: '', projectName: '', template: '', description: '' }, otherOptions); this.modifyCustomTemplateConfig = modifyCustomTemplateConfig; this.conf.projectName = path.basename(this.conf.projectDir); } getPkgPath() { const projectDir = this.conf.projectDir; let pkgPath = path.join(projectDir, 'package.json'); if (!helper_1.fs.existsSync(pkgPath)) { // 适配 云开发 项目 pkgPath = path.join(projectDir, 'client', 'package.json'); if (!helper_1.fs.existsSync(pkgPath)) { console.log(helper_1.chalk.yellow('请在项目根目录下执行 taro create 命令!')); process.exit(0); } } return pkgPath; } getPkgTemplateInfo() { const pkg = helper_1.fs.readJSONSync(this.getPkgPath()); const templateInfo = pkg.templateInfo || DEFAULT_TEMPLATE_INFO; // set template name templateInfo.template = templateInfo.name; delete templateInfo.name; return templateInfo; } setCustomTemplateConfig(customTemplateConfig) { const pkgTemplateInfo = this.getPkgTemplateInfo(); const { compiler, css, customTemplatePath, typescript } = customTemplateConfig; const conf = { compiler: compiler || pkgTemplateInfo.compiler, css: css || pkgTemplateInfo.css, typescript: !(0, lodash_1.isNil)(typescript) ? typescript : pkgTemplateInfo.typescript, customTemplatePath, isCustomTemplate: true, }; this.setTemplateConfig(conf); } setTemplateConfig(templateInfo) { this.conf = Object.assign(this.conf, templateInfo); } fetchTemplates() { return __awaiter(this, void 0, void 0, function* () { const homedir = (0, helper_1.getUserHomeDir)(); let templateSource = helper_1.DEFAULT_TEMPLATE_SRC; if (!homedir) helper_1.chalk.yellow('找不到用户根目录,使用默认模版源!'); const taroConfigPath = path.join(homedir, helper_1.TARO_CONFIG_FOLDER); const taroConfig = path.join(taroConfigPath, helper_1.TARO_BASE_CONFIG); if (helper_1.fs.existsSync(taroConfig)) { const config = yield helper_1.fs.readJSON(taroConfig); templateSource = config && config.templateSource ? config.templateSource : helper_1.DEFAULT_TEMPLATE_SRC; } else { yield helper_1.fs.createFile(taroConfig); yield helper_1.fs.writeJSON(taroConfig, { templateSource: helper_1.DEFAULT_TEMPLATE_SRC }); templateSource = helper_1.DEFAULT_TEMPLATE_SRC; } // 从模板源下载模板 yield (0, fetchTemplate_1.default)(templateSource, this.templatePath('')); }); } create() { return __awaiter(this, void 0, void 0, function* () { const date = new Date(); this.conf.date = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`; // apply 插件,由插件设置自定义模版 config yield this.modifyCustomTemplateConfig(this.setCustomTemplateConfig.bind(this)); if (!this.conf.isCustomTemplate) { const pkgTemplateInfo = this.getPkgTemplateInfo(); this.setTemplateConfig(pkgTemplateInfo); if (!helper_1.fs.existsSync(this.templatePath(this.conf.template))) { yield this.fetchTemplates(); } } this.write(); }); } write() { (0, init_1.createPage)(this, this.conf, () => { console.log(`${helper_1.chalk.green('✔ ')}${helper_1.chalk.grey(`创建页面 ${this.conf.pageName} 成功!`)}`); }).catch(err => console.log(err)); } } exports.default = Page; //# sourceMappingURL=page.js.map