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]; const codeBranchArg = program.args[1]; const buildBranchArg = program.args[2]; 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' }, } 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 { 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 { shell.cd(buildPath); await execa('git', ['checkout', buildBranch]); await execa('git', ['pull', 'origin', buildBranch]); 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 { shell.cp('-r', `${codePath}/dist/*`, buildPath); 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 => { });