code.js 1.76 KB
let _ = require('lodash');
let chalk = require('chalk');
let path = require('path');
let shelljs = require('shelljs');
let fs = require('fs');
let util = require('./util');
let opn = require('opn')
let config = require('../config')

if (!process.env.npm_config_module || !process.env.npm_config_page) {
    console.log(chalk.red('缺少-module或者-page参数'));
    return;
}

let templateData = {
    moduleName: process.env.npm_config_module,
    pageName: process.env.npm_config_page
};
let codeMode = process.env.npm_config_mode || 'list';
let restPath = path.join(__dirname, '../../app/pages', `/${templateData.moduleName}/${templateData.pageName}`);

if (fs.existsSync(restPath)) {
    console.log(chalk.red(`已经存在/${templateData.moduleName}/${templateData.pageName}目录结构`));
    return;
}
shelljs.exec(`mkdir -p ${restPath}`); // 创建页面目录

// 递归编译模板文件并copy到restPath目录
util.complieFile(templateData, '/', codeMode, restPath) && console.log(chalk.green(`模板编译完成`)); 

// 模块入口文件路径
let moduleIndexPath = path.join(__dirname, '../../app/pages', `/${templateData.moduleName}/index.js`); 
// 全局入口文件路径
let indexPath = path.join(__dirname, '../../app/pages', `/index.js`);


// 模块入口中添加page引用
util.complieRef(moduleIndexPath, templateData.pageName) && console.log(chalk.green(`页面引用已添加`));
// 全局入口中添加module的引用
util.complieRef(indexPath, templateData.moduleName) && console.log(chalk.green(`模块引用已添加`));

console.log(chalk.green(`创建完成`));
console.log(chalk.green(`正在打开新页面...`));
setTimeout(() => {
    var uri = `http://localhost:${config.dev.port}/${templateData.moduleName}/${templateData.pageName}.html`;
    opn(uri)
}, 1000);