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.

94 lines
2.4 KiB

const program = require('commander');
const Listr = require('listr');
const chalk = require('chalk');
const shell = require('shelljs');
const execa = require('execa');
// const { Config } = require('../utils/config');
const { Config } = require('/Users/config.js');
const buildPath = Config.buildPath;
const codePath = Config.codePath;
program.parse(process.argv);
const env = program.args[0];
console.log('env: ', env);
const mappingBranch = {
'test': { code: 'dev', build: 'dev_aliyun' },
'pre': { code: 'dev', build: 'predevelop' },
'pro': { code: 'dev', build: 'develop' },
}
if (!mappingBranch[env]) {
console.log(chalk.red(`not exist ${env} branch`));
return;
}
const runBuild = (task) => new Promise(async resolve => {
try {
shell.cd(codePath);
await execa('git', ['checkout', mappingBranch[env].code]);
await execa('git', ['pull', 'origin', mappingBranch[env].code]);
await execa('npm', ['run', 'build', 'test-build']);
resolve();
} catch (e) {
console.log('run build error: ', e);
task.skip('Failed run build');
resolve();
}
});
const deleteBuild = (task) => new Promise(async resolve => {
try {
shell.cd(buildPath);
await execa('git', ['checkout', mappingBranch[env].build]);
await execa('git', ['pull', 'origin', mappingBranch[env].build]);
shell.rm(`${buildPath}/*`);
shell.rm('-rf', `${buildPath}/static`);
await execa('git', ['add', '.']);
await execa('git', ['commit', '-m', `'发布新版本'`]);
await execa('git', ['push', 'origin', mappingBranch[env].build]);
resolve();
} catch (e) {
console.log('delete build error: ', e);
task.skip('Failed delete build');
resolve();
}
});
const copyBuild = (task) => new Promise(async resolve => {
try {
shell.cp('-r', `${codePath}/dist/*`, buildPath);
shell.cd(buildPath);
await execa('git', ['add', '.']);
await execa('git', ['commit', '-m', `'发布新版本'`]);
await execa('git', ['push', 'origin', mappingBranch[env].build]);
resolve();
} catch (e) {
console.log('copyBuild error: ', 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 => {
console.log(err)
});