1 | 'use strict';
|
2 | const path = require('path');
|
3 | const PANDORA_LIB_HOME = path.join(__dirname, '../dist');
|
4 | const {cliParamsToApplicationRepresentation} = require(path.join(PANDORA_LIB_HOME, 'common/Helpers'));
|
5 | const { FrontApplicationLoader } = require(path.join(PANDORA_LIB_HOME, 'action/FrontApplicationLoader'));
|
6 | const cliUtils = require('./util/cliUtils');
|
7 | const {consoleLogger} = cliUtils;
|
8 |
|
9 | exports.command = 'start [targetPath]';
|
10 | exports.desc = 'Start an application';
|
11 | exports.builder = (yargs) => {
|
12 |
|
13 | yargs.option('config', {
|
14 | alias: 'c',
|
15 |
|
16 | describe: 'Config'
|
17 | });
|
18 |
|
19 | yargs.option('daemon', {
|
20 | alias: 'd',
|
21 |
|
22 | describe: 'Daemon',
|
23 | boolean: true
|
24 | });
|
25 |
|
26 | yargs.option('name', {
|
27 | alias: 'n',
|
28 | describe: 'App name, it will get a name from [targetPath] by default'
|
29 | });
|
30 |
|
31 | yargs.option('env', {
|
32 | alias: 'E',
|
33 | describe: 'Environment Variables, such as --env="A=1 B=2"'
|
34 | });
|
35 |
|
36 | yargs.option('args', {
|
37 | alias: 'a',
|
38 | describe: 'args, such as --args="--a=b --c=d"'
|
39 | });
|
40 |
|
41 | yargs.option('node-args', {
|
42 | alias: 'A',
|
43 | describe: 'Node.js args, such as --node-args="--expose-gc --max_old_space_size=500"'
|
44 | });
|
45 |
|
46 | yargs.option('npm', {
|
47 | describe: 'Find the Application by require.resolve()',
|
48 | boolean: true
|
49 | });
|
50 |
|
51 | yargs.option('inspect', {
|
52 | describe: 'Activate inspector',
|
53 | type: 'string'
|
54 | });
|
55 |
|
56 | yargs.option('inspect-port', {
|
57 | describe: 'Set inspector port',
|
58 | type: 'string'
|
59 | });
|
60 |
|
61 | };
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | exports.handler = function (argv) {
|
67 |
|
68 | if(argv.npm) {
|
69 | if(!argv.targetPath) {
|
70 | consoleLogger.error('[targetPath] is required when --npm flag enabled');
|
71 | process.exit(1);
|
72 | return;
|
73 | }
|
74 | if(!argv.name) {
|
75 | consoleLogger.error('option --name is required when --npm flag enabled');
|
76 | process.exit(1);
|
77 | return;
|
78 | }
|
79 | const resolveTarget = cliUtils.dirnameUntilPkgJson(require.resolve(argv.targetPath));
|
80 | if(!resolveTarget) {
|
81 | consoleLogger.error('Can\'t found ' + argv.targetPath + ' from ' + __dirname);
|
82 | }
|
83 | consoleLogger.important('Resolve ' + argv.targetPath + ' to ' + resolveTarget);
|
84 | argv.targetPath = resolveTarget;
|
85 | }
|
86 |
|
87 | argv.entry = argv.targetPath;
|
88 |
|
89 | let loaderOpts;
|
90 | try {
|
91 | loaderOpts = cliParamsToApplicationRepresentation('start', argv);
|
92 | } catch(err) {
|
93 | consoleLogger.error(err);
|
94 | process.exit(1);
|
95 | }
|
96 |
|
97 | const loader = new FrontApplicationLoader(loaderOpts);
|
98 | loader.start().catch((err) => {
|
99 | consoleLogger.error(err);
|
100 | process.exit(1);
|
101 | });
|
102 |
|
103 |
|
104 | };
|