UNPKG

@tarojs/cli

Version:
214 lines 9.98 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createApp = exports.createPage = exports.TEMPLATE_CREATOR = void 0; const helper_1 = require("@tarojs/helper"); const child_process_1 = require("child_process"); const ora = require("ora"); const path = require("path"); const packagesManagement_1 = require("../config/packagesManagement"); const util_1 = require("../util"); const editTemplate_1 = require("./editTemplate"); exports.TEMPLATE_CREATOR = 'template_creator.js'; const styleExtMap = { sass: 'scss', less: 'less', stylus: 'styl', none: 'css' }; var TemplateType; (function (TemplateType) { TemplateType["rn"] = "react-native"; })(TemplateType || (TemplateType = {})); const doNotCopyFiles = ['.DS_Store', '.npmrc', exports.TEMPLATE_CREATOR]; function createFiles(creator, files, handler, options) { const { description, projectName, version, css, date, typescript, template, templatePath, projectPath, pageName, framework, compiler, isCustomTemplate } = options; const logs = []; // 模板库模板,直接创建,不需要改后缀 const globalChangeExt = Boolean(handler); const currentStyleExt = styleExtMap[css] || 'css'; files.forEach((file) => __awaiter(this, void 0, void 0, function* () { // fileRePath startsWith '/' let fileRePath = file.replace(templatePath, '').replace(new RegExp(`\\${path.sep}`, 'g'), '/'); let externalConfig = null; const isVueFramework = /^vue/.test(framework); if (isVueFramework && file.endsWith('.jsx')) { return; } if (!isVueFramework && file.endsWith('.vue')) { return; } // 跑自定义逻辑,确定是否创建此文件 if (handler && typeof handler[fileRePath] === 'function') { externalConfig = handler[fileRePath](options); if (!externalConfig) return; } let changeExt = globalChangeExt; if (externalConfig && typeof externalConfig === 'object') { if (externalConfig.changeExt === false) { changeExt = false; } } // 合并自定义 config const config = Object.assign({}, { description, projectName, version, css, cssExt: currentStyleExt, date, typescript, template, pageName, framework, compiler }, externalConfig); let destRePath = fileRePath; // createPage 创建页面模式 if (config.setPageName) { destRePath = config.setPageName; } destRePath = destRePath.replace(/^\//, ''); // 处理 .js 和 .css 的后缀 if (typescript && changeExt && (path.extname(destRePath) === '.js' || path.extname(destRePath) === '.jsx') && !(destRePath.endsWith('babel.config.js') || destRePath.endsWith('.eslintrc.js'))) { destRePath = destRePath.replace('.js', '.ts'); } if (changeExt && path.extname(destRePath).includes('.css')) { destRePath = destRePath.replace('.css', `.${currentStyleExt}`); } if (isCustomTemplate) fileRePath = path.join(templatePath, fileRePath); // 创建 creator.template(template, fileRePath, path.join(projectPath, destRePath), config); const destinationPath = creator.destinationPath(path.join(projectPath, destRePath)); logs.push(`${helper_1.chalk.green('✔ ')}${helper_1.chalk.grey(`创建文件: ${destinationPath}`)}`); })); return logs; } function createPage(creator, params, cb) { return __awaiter(this, void 0, void 0, function* () { const { projectDir, template, pageName, isCustomTemplate, customTemplatePath } = params; // path let templatePath; if (isCustomTemplate) { templatePath = customTemplatePath; } else { templatePath = creator.templatePath(template); } if (!helper_1.fs.existsSync(templatePath)) return console.log(helper_1.chalk.red(`创建页面错误:找不到模板${templatePath}`)); // 引入模板编写者的自定义逻辑 const handlerPath = path.join(templatePath, exports.TEMPLATE_CREATOR); const basePageFiles = helper_1.fs.existsSync(handlerPath) ? require(handlerPath).basePageFiles : []; const files = Array.isArray(basePageFiles) ? basePageFiles : []; const handler = helper_1.fs.existsSync(handlerPath) ? require(handlerPath).handler : null; const logs = createFiles(creator, files, handler, Object.assign(Object.assign({}, params), { templatePath, projectPath: projectDir, pageName, isCustomTemplate, period: 'createPage' })); creator.fs.commit(() => { // logs console.log(); logs.forEach(log => console.log(log)); console.log(); typeof cb === 'function' && cb(); }); }); } exports.createPage = createPage; function createApp(creator, params, cb) { return __awaiter(this, void 0, void 0, function* () { const { projectName, projectDir, template, autoInstall = true, framework, npm } = params; const logs = []; // path const projectPath = path.join(projectDir, projectName); const templatePath = creator.templatePath(template); // npm & yarn const version = (0, util_1.getPkgVersion)(); // 遍历出模板中所有文件 const files = yield (0, util_1.getAllFilesInFolder)(templatePath, doNotCopyFiles); // 引入模板编写者的自定义逻辑 const handlerPath = path.join(templatePath, exports.TEMPLATE_CREATOR); const handler = helper_1.fs.existsSync(handlerPath) ? require(handlerPath).handler : null; // 为所有文件进行创建 logs.push(...createFiles(creator, files, handler, Object.assign(Object.assign({}, params), { framework, version, templatePath, projectPath, pageName: 'index', period: 'createApp' }))); // fs commit creator.fs.commit(() => __awaiter(this, void 0, void 0, function* () { // logs console.log(); console.log(`${helper_1.chalk.green('✔ ')}${helper_1.chalk.grey(`创建项目: ${helper_1.chalk.grey.bold(projectName)}`)}`); logs.forEach(log => console.log(log)); // 当选择 rn 模板时,替换默认项目名 if (template === TemplateType.rn) { yield (0, editTemplate_1.changeDefaultNameInTemplate)({ projectName, templatePath, projectPath }); } console.log(); // git init const gitInitSpinner = ora(`cd ${helper_1.chalk.cyan.bold(projectName)}, 执行 ${helper_1.chalk.cyan.bold('git init')}`).start(); process.chdir(projectPath); const gitInit = (0, child_process_1.exec)('git init'); gitInit.on('close', code => { if (code === 0) { gitInitSpinner.color = 'green'; gitInitSpinner.succeed(gitInit.stdout.read()); } else { gitInitSpinner.color = 'red'; gitInitSpinner.fail(gitInit.stderr.read()); } }); const callSuccess = () => { console.log(helper_1.chalk.green(`创建项目 ${helper_1.chalk.green.bold(projectName)} 成功!`)); console.log(helper_1.chalk.green(`请进入项目目录 ${helper_1.chalk.green.bold(projectName)} 开始工作吧!😝`)); if (typeof cb === 'function') { cb(); } }; if (autoInstall) { // packages install const command = packagesManagement_1.default[npm].command; const installSpinner = ora(`执行安装项目依赖 ${helper_1.chalk.cyan.bold(command)}, 需要一会儿...`).start(); const child = (0, child_process_1.exec)(command, (error) => { if (error) { installSpinner.color = 'red'; installSpinner.fail(helper_1.chalk.red('安装项目依赖失败,请自行重新安装!')); console.log(error); } else { installSpinner.color = 'green'; installSpinner.succeed('安装成功'); } callSuccess(); }); child.stdout.on('data', function (data) { installSpinner.stop(); console.log(data.replace(/\n$/, '')); installSpinner.start(); }); child.stderr.on('data', function (data) { installSpinner.warn(data.replace(/\n$/, '')); installSpinner.start(); }); } else { callSuccess(); } })); }); } exports.createApp = createApp; //# sourceMappingURL=init.js.map