1 | const mkdirp = require('mkdirp');
|
2 |
|
3 | const isEmpty = require('lodash/isEmpty');
|
4 | const pick = require('lodash/pick');
|
5 | const assign = require('lodash/assign');
|
6 |
|
7 | const parsingCommandLineArgs = require('./parsingCommandLineArgs');
|
8 | const displayAppVersion = require('./displayAppVersion');
|
9 | const displayAppHelp = require('./displayAppHelp');
|
10 | const displayCommandHelp = require('./displayCommandHelp');
|
11 | const getWorkingDirectory = require('./getWorkingDirectory');
|
12 | const getExecutionOptions = require('./getExecutionOptions');
|
13 | const getSavedOptions = require('./getSavedOptions');
|
14 | const updateRcFile = require('./updateRcFile');
|
15 | const { loadCommand, executeCommand } = require('../command');
|
16 | const executeAllInstructions = require('../executor/executeAllInstructions');
|
17 | const setDestination = require('../executor/setDestination');
|
18 |
|
19 | const executeApp = async (app, argv = process.argv) => {
|
20 |
|
21 | const input = parsingCommandLineArgs(argv, app.options, getSavedOptions(app));
|
22 |
|
23 | if (input.options.version) {
|
24 | displayAppVersion(app);
|
25 | return;
|
26 | }
|
27 |
|
28 | if (!input.command) {
|
29 | displayAppHelp(app);
|
30 | return;
|
31 | }
|
32 | let command;
|
33 | try {
|
34 | command = loadCommand(app, input.command);
|
35 | } catch(e) {
|
36 | console.log(`\nCommand '${input.command}' not exist.\n`);
|
37 | process.exit(1);
|
38 | }
|
39 |
|
40 | if (input.options.help) {
|
41 | displayCommandHelp(app, input.command, command, input);
|
42 | return;
|
43 | }
|
44 |
|
45 | const wd = getWorkingDirectory(app, command);
|
46 |
|
47 | const executionOptions = getExecutionOptions(argv, app, command, input, wd);
|
48 |
|
49 | let executionParams = assign(pick(executionOptions, [
|
50 | 'options', 'args', 'command'
|
51 | ]), { wd });
|
52 |
|
53 | mkdirp.sync(executionParams.wd);
|
54 | process.chdir(executionParams.wd);
|
55 | if (app.beforeExecution) {
|
56 | const retval = await app.beforeExecution(executionParams);
|
57 | if (retval) executionParams = assign({}, executionParams, retval);
|
58 | }
|
59 |
|
60 | mkdirp.sync(executionParams.wd);
|
61 | process.chdir(executionParams.wd);
|
62 | if (command.beforeExecution) {
|
63 | const retval = await command.beforeExecution(executionParams);
|
64 | if (retval) executionParams = assign({}, executionParams, retval);
|
65 | }
|
66 |
|
67 | mkdirp.sync(executionParams.wd);
|
68 | process.chdir(executionParams.wd);
|
69 | setDestination(executionParams.wd);
|
70 |
|
71 | if (app.rcFile && !isEmpty(executionOptions.undefaultOptions)) {
|
72 | updateRcFile(app, executionOptions.undefaultOptions, wd);
|
73 | }
|
74 | await executeCommand(app, command, executionParams);
|
75 | await executeAllInstructions();
|
76 |
|
77 | mkdirp.sync(executionParams.wd);
|
78 | process.chdir(executionParams.wd);
|
79 | if (command.afterExecution) {
|
80 | const retval = await command.afterExecution(executionParams);
|
81 | if (retval) executionParams = assign({}, executionParams, retval);
|
82 | }
|
83 |
|
84 | mkdirp.sync(executionParams.wd);
|
85 | process.chdir(executionParams.wd);
|
86 | if (app.afterExecution) {
|
87 | await app.afterExecution(executionParams);
|
88 | }
|
89 | };
|
90 |
|
91 | module.exports = executeApp;
|