@ygyg/yg-cli
Version:
A simple CLI for front-end engineering automation construction tool.
156 lines (131 loc) • 6.03 kB
JavaScript
const { copyFileSync, existsSync } = require('fs');
const { join } = require('path');
const util = require('util');
const chalk = require('chalk');
const execa = require('execa');
const utils = require('./utils');
const output = require('./output');
const packageConfig = require('../package.json');
const fs = require('fs');
const writeJson = async function(params, someFile) {
// 现将json文件读出来
const existFile = await utils.isFileSync(someFile);
if (!existFile) {
const appendFile = util.promisify(fs.appendFile);
await appendFile(someFile, '{}', 'utf8');
utils.logStep(`${someFile}文件创建成功`);
}
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
// let setting = await readFile(someFile);
utils.logStep('setting : 准备写入文件');
let setting = await readFile(someFile).then((data) =>
data.toString() === '' ? {} : JSON.parse(data.toString()),
);
if (setting) {
setting.scripts = params;
const result = JSON.stringify(setting, null, 2);
await writeFile(someFile, result);
}
};
/**
* 使用说明
* 在代码中对所有项目进行批量建立分支,增加配置文件,并在package中增加脚本内容;
* 并推送至gitlab.
* 使用前提,使用者拥有指定项目中的develop权限
*
* 可通过 newEnv 实现 对fat系列 调整
* srcFile 为参考原文件名,将会替换相应的apiGateWay
* @param {*} check
*/
module.exports = async (check) => {
// Ensure minimum supported node version is used
utils.logStep(process.version);
utils.logStep(packageConfig.engines.node);
const pkgList = [
{name: 'ygyg-opmain-web'},
{name: 'ygyg-solution-web'},
{name: 'ygyg-workbench-web'},
{name: 'ygyg-common-web'},
{name: 'ygyg-rule-web'},
{name: 'ygyg-cooperation-web'},
{name: 'ygyg-home-web'},
{name: 'ygyg-services-web'},
{name: 'ygyg-platform-web'},
{name: 'ygyg-dictionary-web'},
{name: 'ygyg-main-web'},
{name: 'ygyg-doc-web'},
{name: 'ygyg-contract-web', group: 'ygyg-contract' },
{name: 'ygyg-platform-mobile'},
];
// const pkgRegistry = {
// local: 'http://registry.ygego.prod/nexus/repository/npm/',
// };
// check project
// check branch
// check file
for (let [index, pkg] of new Map(pkgList.map((pkg, index) => [index, pkg]))) {
// pkg.name
const isExist = existsSync(join(process.cwd(), `${pkg.name}`));
output(`${index}: ${pkg.name}`);
output(`最新版本: ${chalk.green(isExist)}`);
if (!isExist) {
try {
const group = pkg.group ? pkg.group : 'base-platform';
var repo = `https://gitlab.ygego.alpha/${group}/${pkg.name}.git`;
await execa.sync('git', ['clone', repo]).stdout;
output('下载完毕');
// create branch
const newEnv = 'uat2';
const branchName = 'uat2';
const workspace = join(process.cwd());
const projectPath = `${workspace}/${pkg.name}`;
const srcFile = 'config/config.uat1.ts';
const destFile = `config/config.${newEnv}.ts`;
const pwdStatus = await execa.sync('pwd', {cwd: projectPath}).stdout;
output(`pwd: ${chalk.green(pwdStatus)}`);
const listBranch = await execa.sync('git', ['branch', '-a'], {cwd: projectPath}).stdout;
output(`所有分支: ${chalk.green(listBranch)}`);
if (!listBranch.includes(branchName)) {
output(`不存在该分支: ${chalk.green(branchName)}`);
const createBranch = await execa.sync('git', ['branch', branchName], {cwd: projectPath}).stdout;
output(`创建分支: ${chalk.green(createBranch)}`);
}
const switchBranch = await execa.sync('git', ['checkout', branchName], {cwd: projectPath}).stdout;
output(`切换分支: ${chalk.green(switchBranch)}`);
// cp config/config.fat.ts config/config.uat1.ts
// "build:uat"
// "build:uat1": "cross-env UMI_ENV=uat1 ygfish build",
const currPkg = await utils.getPkg(projectPath);
if (!currPkg.scripts.hasOwnProperty('build:uat2') && !currPkg.scripts.hasOwnProperty('dev:uat2')) {
// 增加build:uat1
currPkg.scripts['build:uat2'] = 'cross-env UMI_ENV=uat2 ygfish build';
currPkg.scripts['dev:uat2'] = 'cross-env UMI_ENV=uat2 ygfish dev';
const dictArr = currPkg.scripts;
let keysSorted = Object.keys(dictArr).sort(function(a, b) {return a.charCodeAt(0) - b.charCodeAt(0);}); // 排序健名
const newD = {};
for (let i = 0;i < keysSorted.length;i++) {
newD[keysSorted[i]] = dictArr[keysSorted[i]];
}
await writeJson(newD, `${projectPath}/package.json`);
}
copyFileSync(`${projectPath}/${srcFile}`, `${projectPath}/${destFile}`);
output('copyFile: 成功');
// 替换文件内容 api gateway
let fileContent = fs.readFileSync(`${projectPath}/${destFile}`, 'utf8');
fileContent = fileContent.replace(/api.ygyg.uat1/gm, `api.ygyg.${newEnv}`);
fs.writeFileSync(`${projectPath}/${destFile}`, fileContent, 'utf8');
const gitStatus = await execa.sync('git', ['status'], {cwd: projectPath}).stdout;
// output(`git status: ${chalk.green(JSON.stringify(gitStatus))}`);
const commitFile = await execa.sync('git', ['add', '.'], {cwd: projectPath});
// output(`提交文件内容: ${chalk.green(JSON.stringify(commitFile))}`);
const commitMsg = await execa.sync('git', ['commit', '-m', `fix: 增加${newEnv}环境配置`], {cwd: projectPath});
// output(`提交信息: ${chalk.green(JSON.stringify(commitMsg))}`);
const pushMsg = await execa.sync('git', ['push', '--set-upstream', 'origin', `${newEnv}`], {cwd: projectPath});
output(`推送完毕: ${pkg.name} ${chalk.green(JSON.stringify(pushMsg))}`);
} catch (error) {
console.log(error);
}
}
}
};