1 | #!/usr/bin/env node
|
2 |
|
3 | const path = require('path');
|
4 | const fs = require('fs');
|
5 | const program = require('commander');
|
6 | const glob = require('glob');
|
7 | const { spawn } = require('child_process');
|
8 | const chalk = require('chalk');
|
9 |
|
10 | const welcome = require('../lib/welcome');
|
11 | const { version } = require('../package.json');
|
12 |
|
13 | const basedir = process.cwd();
|
14 | const outDir = path.resolve(basedir, 'ansible');
|
15 | const terminal = chalk.cyan('athena:');
|
16 |
|
17 | function validate(module) {
|
18 | const project = path.resolve(basedir, module);
|
19 |
|
20 | return fs.existsSync(project)
|
21 | ? Promise.resolve(module)
|
22 | : Promise.reject(new Error(`The package --> ${module} not exist`));
|
23 | }
|
24 |
|
25 | function make(module) {
|
26 | const mvn = spawn('mvn', [
|
27 | 'clean',
|
28 | 'install',
|
29 | '-Dmaven.test.skip=true',
|
30 | `-pl=${module}`,
|
31 | '-am',
|
32 | ]);
|
33 |
|
34 | mvn.stdout.pipe(process.stdout);
|
35 | mvn.stderr.pipe(process.stderr);
|
36 |
|
37 | return new Promise((resolve, reject) => {
|
38 | mvn.on('close', () => resolve(module));
|
39 | mvn.on('error', (error) => reject(error));
|
40 | });
|
41 | }
|
42 |
|
43 | function generate(module) {
|
44 | const pattern = '*.jar';
|
45 | const context = `${module}/target`;
|
46 |
|
47 | const [jar] = glob.sync(pattern, { cwd: context });
|
48 | const destiny = path.resolve(outDir, jar);
|
49 | const manifest = path.resolve(outDir, 'manifest.json');
|
50 | const mapping = { [module]: destiny };
|
51 |
|
52 | fs.copyFileSync(`${context}/${jar}`, destiny);
|
53 | fs.writeFileSync(manifest, JSON.stringify(mapping, null, 2));
|
54 |
|
55 | console.log(chalk.cyan(`athena make ${jar}, manifest success `));
|
56 |
|
57 | }
|
58 |
|
59 | async function pipeline(module) {
|
60 | try {
|
61 | await welcome();
|
62 | await validate(module);
|
63 | await make(module);
|
64 | await generate(module);
|
65 | } catch (err) {
|
66 |
|
67 | console.log(
|
68 | `${terminal} ${chalk.red(`make pipeline failed, ${err.message.trim()}`)}`
|
69 | );
|
70 |
|
71 | process.exit(5);
|
72 | }
|
73 | }
|
74 |
|
75 | program
|
76 | .version(version)
|
77 | .usage('<module> [options]')
|
78 | .option('-c, --config <file>', 'declare athena make config file')
|
79 | .action(pipeline)
|
80 | .parse(process.argv);
|