fdm
Version:
基于gulp的1688前端集成化工具
140 lines (129 loc) • 3.82 kB
JavaScript
var path = require('path');
var commander = require('commander');
var grunt = require('grunt');
var fs = require('fs');
var spawn = require('win-spawn');
var helper = require('../utils/helper');
var config = helper.getConfig();
var markdown = require('markdown-js');
var tempPath = '',
tempLocalPath = '',
projDest = '';
/*
模板安装逻辑
if(grunt模板) 安装
if(普通模板) 直接复制
*/
function tempSetup() {
if (fs.existsSync(tempLocalPath + '/template.js')) {
grunt.task.loadTasks(path.join(__dirname, '../node_modules/grunt-init/tasks'));
// fix windows directory by replace C:\ to C\:\
var taskName = 'init:' + tempLocalPath.replace(/^([a-zA-Z]):\\/, '$1\\:\\');
grunt.cli.tasks = [taskName];
grunt.cli({
force: true
}, function() {
spawn('npm', ['install'], {
stdio: 'inherit'
});
showTempReadme();
});
}else if (fs.existsSync(tempLocalPath)) {
// copy template to current dir
console.log('正在复制模板文件'.green);
helper.copyFile(tempLocalPath, projDest,showTempReadme);
} else {
console.log('模板获取失败,请检查模板路径'.green);
process.exit();
}
}
/*
输出目录下的readme内容
*/
function showTempReadme(){
if(fs.existsSync(path.join(projDest,'readme.md'))){
console.log(fs.readFileSync(path.join(projDest,'readme.md'), "utf8"));
}
}
/*
模板获取逻辑
if(本地路径) 直接安装
if(远程路径) 远程获取,clone到.fdm/init/template,安装
if(别名) 从.fdm/init/template查询,没有从git仓库获取,clone到.fdm/init/template,安装
*/
function initTemp() {
if (!fs.existsSync(tempLocalPath + '/template.js')) {
switch (helper.parsePath(tempPath)) {
case 'local':
tempSetup();
break;
case 'github':
console.log('开始从GitHub获取模板...'.green);
var gitDest = projDest;
if(projDest=='./'){
try{
gitDest = tempPath.match(/\/(.*).git$/)[1];
}catch(e){
console.log('git路径不正确...'.green);
return;
}
}
helper.getFromGit(tempPath,gitDest).on('close', function(){
if(fs.existsSync(gitDest + '/package.json')){
spawn('npm', ['install'], {
stdio: 'inherit',
cwd:gitDest
}).on('close',showTempReadme);
}
});
break;
case 'alias':
console.log('开始从模板仓库获取模板...'.green);
// 创建项目目录
if(projDest!=='./' && !fs.existsSync(projDest)){
fs.mkdirSync(projDest);
}
// 从npm获取最新模板
helper.getFromNPM('fd-init-' + tempPath,helper.getFdmDir())
.on('close', function() {
// 临时模板路径
var _tplPath = path.join(helper.getFdmDir(),'node_modules', 'fd-init-' + tempPath);
// 将模板从临时目录复制到当前项目目录
helper.copyFile(_tplPath, projDest,showTempReadme);
});
break;
default:
break;
}
} else {
tempSetup();
}
}
module.exports = {
help: function() {
console.log();
// console.log('从公共仓库构建,仓库源地址:' + config.tempSource);
// console.log(' $', 'fdm init'.green, 'lofty');
console.log('从公共仓库构建,查看可用模板 fdm -t');
console.log(' $', 'fdm init'.green, 'lofty');
console.log();
console.log('从指定模板路径构建,支持远程Git地址');
console.log(' $', 'fdm init'.green, './模板路径');
console.log();
// console.log('模板安装到指定路径');
// console.log(' $', 'fdm init'.green, 'H5App', './目标路径'.green);
console.log();
},
run: function(argv) {
commander.parse(argv);
tempPath = commander.args[1];
if(!tempPath){
this.help();
return;
}
// 获取模板本地路径
tempLocalPath = helper.getTempDir(tempPath);
projDest = commander.args[2] || './';
initTemp();
}
};