bm_scaffold_async_router
Version:
本木前端脚手架-异步路由版
88 lines (75 loc) • 3 kB
JavaScript
var fs = require('fs'),
path = require('path'),
fse = require('fs-extra'),
prompt = require('prompt'),
argv = require('yargs').argv,
print = require('../../../utils/print'),
Config = require('../../../utils/config'),
erosUtils = require('./util');
var shell = require('shelljs');
var schema = {
properties: {
name: {
pattern: /^[0-9a-z\-_]+$/i,
message: '只允许输入字母、连接符、下划线',
required: false,
description: '请输入创建工程的名字',
default: 'weex-eros-demo'
},
version: {
pattern: /^\d{1,2}\.\d{1,2}\.\d{1,2}$/,
message: '只允许输入如 0.0.x',
required: false,
description: '请输入工程的初始版本号,默认0.0.0,初始化工程建议不修改',
default: '0.0.0'
}
}
};
function changeFile(path, oldText, newText) {
if (!fs.existsSync(path)) return
var result = fs.readFileSync(path, 'utf8').replace(new RegExp(oldText, "g"), newText);
if (result) {
fs.writeFileSync(path, result, 'utf8');
}
};
function create() {
prompt.start();
return prompt.get(schema, function(err, result) {
var configJson, projectPath;
if (err) {
print.info('创建失败');
return;
}
configJson = Config.createEmptySchema();
configJson.name = result.name;
configJson.version = result.version;
configJson.platform = 'weex-eros';
projectPath = path.join(process.cwd(), configJson.name);
createProject(configJson, projectPath);
});
};
function createProject(configJson, projectPath) {
var frame = (argv.f || argv.frame) || 'weex-eros-template',
fullFrame = 'bmfe-' + frame;
fse.ensureDir(projectPath, function() {
console.time('[' + 'bm-eros'.blue + '] ' + '模板加载耗时:'.green);
shell.cd(projectPath).exec('cnpm install ' + fullFrame);
shell.cp('-R', projectPath + '/node_modules/' + fullFrame + '/template/*', projectPath);
shell.rm('-rf', projectPath + '/node_modules');
var _name = configJson.name
erosUtils.erosConsole('修改对应配置文件信息')
changeFile(projectPath + '/fe/dev.json', frame, _name);
changeFile(projectPath + '/config.json', frame, _name);
changeFile(projectPath + '/config.json', "weex-eros-version", configJson.version);
changeFile(projectPath + '/fe/package.json', frame, _name);
fs.rename(projectPath + '/gitignore', projectPath + '/.gitignore');
shell.cd(projectPath + '/fe').exec('cnpm install');
shell.chmod('-R', '777', projectPath);
console.timeEnd('[' + 'bm-eros'.blue + '] ' + '模板加载耗时:'.green);
erosUtils.erosConsole('初始化成功,enjoy it !'.green);
});
};
module.exports = {
create: create,
createProject: createProject
}