comb-cli
Version:
comb cli
141 lines (127 loc) • 5.15 kB
JavaScript
const colors = require('colors');
const shell = require("shelljs/global");
const path = require('path');
const fs = require('fs');
const argv = require('yargs').argv;
const process = require('process');
const cliOpt = require('../lib/config.json');
const downLoadGit = require('../lib/downLoadGit.js');
const downLoadFrame = require('../lib/downLoadFrame.js');
const readJson = require('../lib/readJson.js');
const createCombJson = require('../lib/createCombJson.js');
const createJson = require('../lib/createJson.js');
var prompt = require('../lib/prompt.js');
var merge = require('../lib/merge.js');
var createIgnore = require('../lib/createIgnore.js');
const rootPath = process.cwd();
const cmd = argv._[0];
const gitUrl = argv._[1];
const tempDir = cliOpt.project.tempDir;
const targetPath = path.resolve(rootPath, tempDir, '../');
const projFolder = path.resolve(rootPath, tempDir);
function getCombJson(projCombJsonPath, rootCombJsonPath) {
return new Promise((resolve, reject) => {
let jsonPath = test('-f', projCombJsonPath) ? projCombJsonPath : test('-f', rootCombJsonPath) ? rootCombJsonPath : '';
if (jsonPath) {
echo('读取comb.json文件'.yellow);
return readJson(jsonPath).then((data) => {
echo('读取comb.json文件完成'.green);
resolve(data);
}).catch((e) => {
echo('读取Comb.json文件出错'.red);
reject(e);
});
} else {
echo('未检测到Comb.json文件'.yellow);
createCombJson({
version: cliOpt.frame.version
}).then((data) => {
resolve(data);
});
}
});
}
function mergeJson (packagePath, targetPackagePath) {
return new Promise ((resolve, reject) => {
readJson(packagePath).then((projPackage) => {
readJson(targetPackagePath).then((framePackage) => {
const targetPackageData = merge(framePackage, projPackage);
createJson(targetPackageData, targetPackagePath).then(() => {
resolve();
});
})
});
});
}
function finalStep(projTempDir, targetPath) {
echo('拷贝其它项目文件'.yellow);
cd(projTempDir);
mv('-nf', ['./*', './.*'], targetPath);
cd(targetPath);
createIgnore();
rm('-rf', projFolder);
echo('done'.green);
echo('安装项目依赖'.yellow);
echo('安装时间较长,请耐心等待'.yellow);
exec('npm install', (code, stdout, stder) => {
if (0 === parseInt(code)) {
echo('done'.green);
} else {
echo('项目依赖安装失败'.red);
}
});
};
function clone(gitUrl) {
const folderName = path.parse(gitUrl).name;
const projTempDir = path.resolve(rootPath, tempDir, folderName);
let opt = {
gitUrl: gitUrl,
tempDir: tempDir
}
// clone输入的git地址
downLoadGit(opt).then((code, stdout, stderr) => {
const projCombJsonPath = path.resolve(projTempDir, 'comb.json');
const targetCombJsonPath = path.resolve(targetPath, 'comb.json');
const srcPath = path.join(projTempDir, 'src');
const targetSrcPath = path.join(targetPath, 'src');
const staticPath = path.join(projTempDir, 'static');
const targetStaticPath = path.join(targetPath, 'static');
const packagePath = path.join(projTempDir, 'package.json');
const targetPackagePath = path.join(targetPath, 'package.json');
// 根据comb.json下载对应框架
getCombJson(projCombJsonPath, targetCombJsonPath).then((data) => {
const frame = cliOpt.frame;
const opt = Object.assign(cliOpt.frame, data);
downLoadFrame(opt).then(() => { // 安装框架以及packagejson的依赖
if (test('-d', srcPath)) {
echo('拷贝src文件'.yellow);
cd(srcPath);
cp('-rf', './', targetSrcPath);
cd(targetPath);
rm('-rf', srcPath);
echo('done'.green);
}
if (test('-d', staticPath)) {
echo('拷贝static文件'.yellow);
cd(staticPath);
cp('-rf', './', targetStaticPath);
cd(targetPath);
rm('-rf', staticPath);
echo('done'.green);
}
if (test('-f', packagePath)) { // 如果项目中存在package.json,则与框架package.json 合并
echo('合并package.json文件'.yellow);
mergeJson(packagePath, targetPackagePath).then(() => {
rm('-f', packagePath);
echo('合并package.json文件完成'.green);
finalStep(projTempDir, targetPath);
});
} else {
finalStep(projTempDir, targetPath);
}
});
});
});
}
module.exports = clone;