1 | const fs = require('fs');
|
2 |
|
3 | function preprocessArgs(args) {
|
4 | let lastNeedsValue = false;
|
5 | let actionIndex = -1;
|
6 | let action = null;
|
7 |
|
8 | const noValueArgs = ['-v', '--verbose', '-p', '--privileged', '-d', '--detached'];
|
9 |
|
10 | args.forEach((str, index) => {
|
11 | if (action === null) {
|
12 |
|
13 | if (index >= 2) {
|
14 | const isFlag = str.substring(0, 1) === '-';
|
15 |
|
16 | if (!isFlag && !lastNeedsValue) {
|
17 | action = str;
|
18 | actionIndex = index;
|
19 | }
|
20 | lastNeedsValue = isFlag && noValueArgs.indexOf(str) === -1;
|
21 | }
|
22 | }
|
23 | });
|
24 |
|
25 | return {
|
26 | action,
|
27 | args: action === null
|
28 | ? []
|
29 | : args.slice(actionIndex + 1),
|
30 | dockerProjectArgs: action === null
|
31 | ? args
|
32 | : args.slice(0, actionIndex)
|
33 | };
|
34 | }
|
35 |
|
36 | function fileExists(file) {
|
37 | try {
|
38 | return fs.statSync(file).size >= 0;
|
39 | } catch (err) {
|
40 | if (err.code === 'ENOENT') {
|
41 | return false;
|
42 | }
|
43 | throw err;
|
44 | }
|
45 | }
|
46 |
|
47 | function parseConfigActions(actions) {
|
48 | const output = {};
|
49 |
|
50 | Object.keys(actions || {}).forEach(action => {
|
51 | const value = actions[action];
|
52 |
|
53 | if (value === true) {
|
54 | output[action] = {};
|
55 | }
|
56 |
|
57 | if (typeof value === 'string') {
|
58 |
|
59 | output[action] = {
|
60 | command: value
|
61 | };
|
62 | } else if (typeof value === 'object' && !Array.isArray(value)) {
|
63 |
|
64 | output[action] = value;
|
65 | }
|
66 | });
|
67 |
|
68 | return output;
|
69 | }
|
70 |
|
71 | function parseConfig(config = {}) {
|
72 | if (!config) {
|
73 | return config;
|
74 | }
|
75 |
|
76 |
|
77 | if (config.actions && typeof config.actions === 'object') {
|
78 | config.actions = parseConfigActions(config.actions);
|
79 | }
|
80 |
|
81 |
|
82 | if (config.env) {
|
83 | Object.keys(config.env).forEach(key => {
|
84 | if (typeof config.env[key] === 'string') {
|
85 |
|
86 | config.env[key] = {
|
87 | file: config.env[key]
|
88 | };
|
89 | } else if (typeof config.env[key].actions === 'object') {
|
90 |
|
91 | config.env[key].actions = parseConfigActions(config.env[key].actions);
|
92 | }
|
93 | });
|
94 | }
|
95 |
|
96 | return config;
|
97 | }
|
98 |
|
99 | module.exports = {
|
100 | preprocessArgs,
|
101 | fileExists,
|
102 | parseConfigActions,
|
103 | parseConfig
|
104 | };
|