comb-cli
Version:
comb cli
51 lines (48 loc) • 1.54 kB
JavaScript
const fs = require('fs');
const path = require('path');
const process = require('process');
const rootPath = process.cwd();
require('shelljs');
/**
* @param opt.gitUrl 框架git地址
* @param opt.version 框架版本号
* @param opt.tempDir 临时存放路径
*/
module.exports = function downLoadGit (opt) {
const folderName = path.parse(opt.gitUrl).name;
const gitPath = path.resolve(rootPath, opt.tempDir, folderName);
const tempDir = path.resolve(rootPath, opt.tempDir);
return new Promise ((resolve, reject) => {
if (test('-L', 'git')) {
throw '请先安装git'.red;
}
if (!opt.tempDir) {
throw '下载框架缺少临时路径'.red;
}
if (test('-d', tempDir)) {
rm('-rf', tempDir);
}
mkdir('-p', tempDir);
cd(tempDir);
exec('git clone ' + opt.gitUrl, (code) => {
if (0 === parseInt(code)) {
cd(gitPath);
if (opt.version) {
echo('切换到版本:', opt.version);
exec('git checkout ' + opt.version + ' -b temp', (code) => {
if (0 === parseInt(code)) {
resolve();
} else {
reject(code);
}
});
} else {
resolve();
}
} else {
reject(code);
}
})
});
}