You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.6 KiB

const program = require('commander');
const Listr = require('listr');
const chalk = require('chalk');
const shell = require('shelljs');
const execa = require('execa');
4 years ago
// const { Config } = require('../utils/config');
const { Config } = require('/Users/config.js');
4 years ago
const buildPath = Config.buildPath;
const codePath = Config.codePath;
program.parse(process.argv);
const env = program.args[0];
const codeBranchArg = program.args[1];
const buildBranchArg = program.args[2];
4 years ago
const mapping = {
'test': { cmd: 'test-build', codeBranch: 'dev', buildBranch: 'dev_aliyun' },
'pre': { cmd: 'pre-build', codeBranch: 'dev', buildBranch: 'predevelop' },
'pro': { cmd: 'build', codeBranch: 'dev', buildBranch: 'develop' },
}
4 years ago
if (!mapping[env]) {
console.log(chalk.red(`not exist ${env} branch`));
return;
}
const cmd = mapping[env].cmd;
const codeBranch = codeBranchArg || mapping[env].codeBranch;
const buildBranch = buildBranchArg || mapping[env].buildBranch;
const runBuild = (task) => new Promise(async resolve => {
try {
4 years ago
shell.cd(codePath);
await execa('git', ['checkout', codeBranch]);
await execa('git', ['pull', 'origin', codeBranch]);
await execa('npm', ['run', cmd]);
resolve();
} catch (e) {
task.skip('Failed run build');
resolve();
}
});
const deleteBuild = (task) => new Promise(async resolve => {
try {
4 years ago
shell.cd(buildPath);
await execa('git', ['checkout', buildBranch]);
await execa('git', ['pull', 'origin', buildBranch]);
4 years ago
shell.rm(`${buildPath}/*`);
shell.rm('-rf', `${buildPath}/static`);
await execa('git', ['add', '.']);
const text = `xjx auto publish ${new Date().toLocaleString()}`;
await execa('git', ['commit', '-m', `'${text}'`]);
await execa('git', ['push', 'origin', buildBranch]);
resolve();
} catch (e) {
task.skip('Failed delete build');
resolve();
}
});
const copyBuild = (task) => new Promise(async resolve => {
try {
4 years ago
shell.cp('-r', `${codePath}/dist/*`, buildPath);
4 years ago
shell.cd(buildPath);
await execa('git', ['add', '.']);
const text = `xjx auto publish ${new Date().toLocaleString()}`;
await execa('git', ['commit', '-m', `'${text}'`]);
await execa('git', ['push', 'origin', buildBranch]);
resolve();
} catch (e) {
task.skip('Failed copy build');
resolve();
}
});
const tasks = new Listr([
{
title: 'run build',
task: (ctx, task) => runBuild(task)
},
{
title: 'delete build',
task: (ctx, task) => deleteBuild(task)
},
{
title: 'copy build',
task: (ctx, task) => copyBuild(task)
}
]);
tasks.run().catch(err => {
});