sdata-cli
Version:
smardaten二次开发插件脚手架
173 lines (168 loc) • 6.47 kB
JavaScript
;
const {prompt} = require("enquirer");
const ora = require("ora");
const execa = require("execa");
const chalk = require("chalk");
const execSync = require('child_process').execSync;
const path = require("path");
const {v4: uuidv4} = require("uuid");
const fse = require("fs-extra");
const os = require('os');
const {
getModuleList,
urlObjMap,
frameMap,
} = require("../config/gitUrlMap");
const {errLog} = require("../src/utils/log.js");
const getFirstDate = require("./getFirstDate.js")
const axios = require('axios');
function executeCommand(command, cwd = process.cwd()) {
return new Promise((resolve, reject) => {
const child = execa.command(command, {cwd});
child.on("close", code => {
if (code !== 0) {
reject(new Error(`command failed: ${command}, ${code}`));
return;
}
resolve();
});
});
}
module.exports = (options) => {
let promptList = [
{
type: "input",
name: "name",
message: "请输入项目名称",
default: "sdata-custom-plugin",
validate(val) {
if (fse.existsSync(val)) {
return "文件名已经存在,请确认文件名....";
}
if (val.trim() !== "") {
return true;
}
return "项目名称不能为空!";
},
},
{
type: "autocomplete",
name: "pluginType",
limit: 7,
initial: 0,
message: "请选择插件类型",
choices: () => {
return getModuleList(effectiveObj);
},
},
{
type: "select",
name: "templeteType",
message: "请选择开发框架",
choices() {
let {pluginType} = this.enquirer.answers
return effectiveObj[pluginType].frame
},
},
];
let effectiveObj
let {branch, origin} = options
axios.get(`${origin ? origin : "https://gitee.com/njsmartdata/sdata-plugins"}/raw/${branch
? branch
: "master"}/templates.json`).then(async ({data}) => {
// axios.get("https://gitee.com/njsmartdata/sdata-plugins/raw/master/templates.json").then(async ({data}) => {
effectiveObj = data || urlObjMap
const latestVersion = execSync(`npm view sdata-cli version`, {"encoding": "utf8"}).replace("\n", "");
if (require("../package.json").version != latestVersion) {
console.log(chalk.red(`${require("../package.json").version}-->${latestVersion},请执行npm i sdata-cli -g 获取最新版本,以达到最佳使用体验\n`));
let {description} = (await axios.get("https://gitee.com/njsmartdata/sdata-cli/raw/master/update_log.json"))?.data[latestVersion] || {}
description && console.log(`${latestVersion}更新:${description}`);
}
prompt(promptList).then(async answer => {
const spinner = ora();
spinner.text = "开始生成项目,请等待...";
spinner.start();
try {
await executeCommand(`mkdir ${answer.name}`);
await executeCommand("git init", path.join(process.cwd(), answer.name));
await executeCommand(
//此处设置新增可自定义二开源
`git remote add -f origin ${origin ? origin + ".git" : "https://gitee.com/njsmartdata/sdata-plugins.git"}`,
path.join(process.cwd(), answer.name)
);
await executeCommand(
`git config core.sparsecheckout true`,
path.join(process.cwd(), answer.name)
);
let infoPath = path.join(process.cwd(), answer.name, ".git/info");
if (!fse.existsSync(infoPath)) {
fse.mkdirSync(infoPath);
}
const packagePath = `packages/${answer.pluginType}${
frameMap[answer.templeteType]
}-plugin`;
fse.writeFileSync(
path.join(process.cwd(), answer.name, ".git/info/sparse-checkout"),
packagePath
);
await executeCommand(
`git pull origin ${branch ? branch : "master"}`,
path.join(process.cwd(), answer.name)
);
fse.copySync(
path.join(process.cwd(), answer.name, packagePath),
path.join(process.cwd(), answer.name)
);
fse.removeSync(path.join(process.cwd(), answer.name, "/packages"));
fse.removeSync(path.join(process.cwd(), answer.name, "/.git"));
let configJson;
try {
configJson = require(path.join(
process.cwd(),
`${answer.name}/pluginTemp/config.json`
));
configJson.id = uuidv4();
fse.writeFileSync(
path.join(process.cwd(), `${answer.name}/pluginTemp/config.json`),
JSON.stringify(configJson, null, 2),
"utf8"
);
} catch (error) {
configJson = require(path.join(
process.cwd(),
`${answer.name}/config.json`
));
configJson.id = uuidv4();
fse.writeFileSync(
path.join(process.cwd(), `${answer.name}/config.json`),
JSON.stringify(configJson, null, 2),
"utf8"
);
}
spinner.succeed(chalk.green(`${answer.name} 项目生成完毕!`));
console.log(chalk.green(`cd ${answer.name} && npm install \n`));
try {
if (os.type() === "Darwin") {
execSync(`open ${path.resolve(process.cwd(), answer.name)}`)
} else {
execSync(`start ${path.resolve(process.cwd(), answer.name)}`)
}
} catch (e) {
console.log("自动打开文件夹失败,请手动打开,目录名:" + path.resolve(process.cwd(), answer.name))
}
} catch (error) {
spinner.fail("模板下载失败,请先按照以下步骤排查问题");
spinner.fail("1.请使用管理员权限运行命令行程序");
spinner.fail("2.本程序依赖git运行,请确保安装git并将git命令添加到系统环境变量");
spinner.fail("3.请检查网络是否连接正常,如有VPN请关闭后重试");
spinner.fail("4.请检查命令行是否使用utf-8编码运行");
spinner.fail("以上排查项均ok,请联系管理员");
errLog(error);
process.exit();
}
});
}).catch((reason) => {
console.log(reason);
console.log("请检查网络是否连接正常,如有VPN请关闭后重试");
})
};