@thingjs/xplugin-cli
Version:
UINO ThingJS-X 零代码平台插件二次开发脚手架,用于生成插件模板开发代码
150 lines (131 loc) • 7.13 kB
JavaScript
const { program } = require('commander');
const shell = require('shelljs');
const inquirer = require('inquirer');
const path = require('path');
// const upgrade = require("download-git-repo");
const { name, version } = require('../package.json');
const { create } = require('../dist/create');
const chalk = require('chalk');//设置命令行提示文字颜色
const ora = require('ora');
const { execSync } = require('child_process');
program.version(`${name}-${version}`, '-v,-V');
const commandList = ['create', 'init'];
commandList.forEach(command => {
program.command(`${command} <projectName>`)
.description("构建或初始化一个插件模板工程")
.action((projectName) => {
const createPath = path.join(path.resolve('./'), `/${projectName}`);
const fileTest = shell.test('-d', createPath);
const catalogueState = false;//是否显示目录选项
if (fileTest) {
inquirer
.prompt([{
name: 'template-overwrite',
type: 'confirm',
message: `模板名称${projectName}已存在,是否替换模板?`,
}]).then((answers) => {
if (answers['template-overwrite']) {
create(projectName, catalogueState);
} else {
console.log(chalk.red(`请输入其他名称`));
}
})
} else {
create(projectName, catalogueState);
}
})
});
/**
* add xplugin cli upgrade template code
* @date 2022.10.20
* @author zhangguang@uino.com
*/
program.command(`upgrade`)
.description("升级模板代码")
.action(() => {
//const xpluginTemplatePath = path.join(__dirname, "../", `template/xplugin-template`);
// const xpluginTemplateRealPath = path.join(__dirname, "../", `template/plugIn`);
// console.log('-------------', xpluginTemplatePath)
// console.log('-------------', xpluginTemplateRealPath)
const spinner = ora('Fetch...').start(); // 开始可视加载
setTimeout(() => {//延时3秒造成等待假象,有加载过程,使体验变得较好
//first, 移除克隆目录,克隆项目
// const branch = 'main';
// const xpluginTemplatePath = path.join(__dirname, "../", `template/xplugin-template`);
// shell.rm('-rf', xpluginTemplatePath);
let gitCloneDir;
let gitDirDrive = "pwd"; //磁盘驱动器盘符,主要兼容windows
const npmPrefix = execSync('npm config get prefix').toString().replace(/\r|\n/ig, "");
const platform = process.platform;
spinner.info(`${process.platform} OS `);
switch (platform) {
// case 'aix':
// console.log("IBM AIX platform");
// break;
case 'darwin':
//console.log("Darwin platfrom(MacOS, IOS etc)");
gitCloneDir = `${npmPrefix}${path.sep}lib${path.sep}node_modules${path.sep}${name}${path.sep}template`;
break;
// case 'freebsd':
// console.log("FreeBSD Platform");
// break;
case 'linux':
//console.log("Linux Platform" + `${npmPrefix}`.concat("11"));
gitCloneDir = `${npmPrefix}${path.sep}lib${path.sep}node_modules${path.sep}${name}${path.sep}template`;
break;
// case 'openbsd':
// console.log("OpenBSD platform");
// break;
// case 'sunos':
// console.log("SunOS platform");
// break;
case 'win32':
//console.log("windows platform");
gitCloneDir = `${npmPrefix}${path.sep}node_modules${path.sep}${name}${path.sep}template`;
const temp = gitCloneDir.split(path.sep);//由于项目名称问题,需要进行分割符处理
gitCloneDir = temp.join('\\');
gitDirDrive = temp[0];
break;
default:
//console.log("unknown platform");
gitCloneDir = `${npmPrefix}${path.sep}lib${path.sep}node_modules${path.sep}${name}${path.sep}template`;
}
spinner.info(`git upgrade template code dir: ${gitCloneDir} `);
//拉去模板代码进行模板升级
//Error: 'git clone' failed with status 128
// console.log(`${gitCloneDir}`)
//https://gitee.com/GuangGuangZhang/xplugin-template.git
//https://github.com/GuangGuangZhang/xplugin-template.git
shell.exec(`${gitDirDrive} && cd ${gitCloneDir} && git clone https://gitee.com/GuangGuangZhang/xplugin-template.git`, function (code, stdout, stderr) {
// console.log('Exit code:', code);
// console.log('Program output:', stdout);
// console.log('Program stderr:', stderr);
if (code !== 0) {
spinner.fail(`${chalk.red("模板代码升级失败,检查错误后重新进行升级")}`);
//console.log(chalk.red(stderr));
shell.exit(1);
}
shell.rm('-rf', `${gitCloneDir}${path.sep}plugIn`);//删除原始目录
shell.mv("-f", `${gitCloneDir}${path.sep}xplugin-template`, `${gitCloneDir}${path.sep}plugIn`);//重命名最新模板
spinner.info(`克隆成功`);
spinner.succeed(`${chalk.green("模板代码升级成功")}`);
});
// 方案二:存在相关问题导致git 非公开账户 error code 128
// upgrade(`direct:https://github.com/GuangGuangZhang/xplugin-template.git#${branch}`, gitCloneDir, { clone: true }, function (Error) {
// if (Error) {
// shell.rm('-rf', xpluginTemplatePath);
// spinner.fail(`${chalk.red("模板代码升级失败,检查错误后重新进行升级")}`);
// //console.log(chalk.red(Error));
// console.log(Error);
// return false;
// }
// // const xpluginTemplateRealPath = path.join(__dirname, "../", `template/plugIn`);
// // shell.rm('-rf', xpluginTemplateRealPath);//删除原始目录
// // //shell.mkdir('-p', xpluginTemplateRealPath);
// // shell.mv("-f", xpluginTemplatePath, xpluginTemplateRealPath);//重命名最新模板
// spinner.succeed(`${chalk.green("模板代码升级成功")}`);
// });
}, 3000);
});
program.parse(process.argv);