1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const fs = require('fs');
|
17 | const { spawn } = require('child_process');
|
18 | const program = require('commander');
|
19 | const chalk = require('chalk');
|
20 | const _ = require('lodash');
|
21 |
|
22 | const welcome = require('../lib/welcome');
|
23 | const { version } = require('../package.json');
|
24 |
|
25 | const terminal = chalk.cyan('athena:');
|
26 | const projects = ['react-spa', 'java'];
|
27 | const environments = ['uat', 'uat2', 'wuxi', 'wuxiB'];
|
28 |
|
29 | function validate() {
|
30 | const params = _.pick(program, ['project', 'env', 'jar']);
|
31 |
|
32 | if (!projects.includes(params.project)) {
|
33 | throw new Error(`none supported project type --> ${params.project}`);
|
34 | }
|
35 |
|
36 | if (!environments.includes(params.env)) {
|
37 | throw new Error(`none supported runtime environment --> ${params.env}`);
|
38 | }
|
39 |
|
40 | if (!fs.existsSync(params.jar)) {
|
41 | throw new Error(`${params.jar} not exists`);
|
42 | }
|
43 |
|
44 | return params;
|
45 | }
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | function execute(params) {
|
51 | const stdout = fs.openSync('./stdout.log', 'w');
|
52 | const stderr = fs.openSync('./stderr.log', 'w');
|
53 | const { env, jar } = params;
|
54 | const success = chalk.magenta(`${params.jar} start success...`);
|
55 |
|
56 | spawn('java', [`-Dspring.profiles.active=${env}`, '-jar', jar], {
|
57 | detached: true,
|
58 | stdio: ['ignore', stdout, stderr],
|
59 | }).unref();
|
60 |
|
61 |
|
62 | console.log(`${terminal} ${success}`);
|
63 |
|
64 | }
|
65 |
|
66 | async function pipeline() {
|
67 | welcome();
|
68 |
|
69 | try {
|
70 | const params = await validate();
|
71 |
|
72 | execute(params);
|
73 | } catch (err) {
|
74 |
|
75 | console.log(
|
76 | `${terminal} ${chalk.red(
|
77 | `spring boot start failed, ${err.message.trim()}`
|
78 | )}`
|
79 | );
|
80 |
|
81 | process.exit(5);
|
82 | }
|
83 | }
|
84 |
|
85 | program
|
86 | .version(version)
|
87 | .option('-p, --project <project>', 'athena available projects')
|
88 | .option('-e, --env <env>', 'spring boot environment')
|
89 | .option('-j, --jar [jar]', 'target jar package')
|
90 | .parse(process.argv);
|
91 |
|
92 | pipeline();
|