-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqtools-parse-command-line.js
186 lines (156 loc) · 4.75 KB
/
qtools-parse-command-line.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
'use strict';
const fs = require('fs');
//START OF moduleFunction() ============================================================
const moduleFunction = function (args = {}) {
const workingFunctionActual = (localArgs) => (operatingArgs={}) => {
var noFunctions = args && args.noFunctions ? args.noFunctions : false;
var doNotRetrieveFiles = args && args.getFileData ? args.getFileData : true;
var valuesSplitCharacter =
args && args.valuesSplitCharacter ? args.valuesSplitCharacter : ',';
const { injectedCommandString, injectFirstTwoElements } = operatingArgs;
let workingArgValues = process.argv;
if (injectedCommandString) {
let workingString = injectedCommandString;
if (injectFirstTwoElements) {
workingString = `unused unused ${injectedCommandString}`;
}
workingArgValues = workingString.split(/ +/);
}
/* someday add
arg.allowedItems={
fileList:3, //number of files allowed
functions:['tmp', 'otherThing']
values:['a', 'b'],
switches:['x', 'y']
}
also, args.errorIfNotAllowedItem:true
These seem like they could be useful.
*/
var figureOutWhatItIs = function (filePath) {
var isNotFile;
var trialString;
var statResult;
var fileString;
try {
var statResult = fs.statSync(filePath);
} catch (err) {
if (err && err.code == 'ENOENT') {
isNotFile = true;
} else if (err) {
console.log(`qtools-parse-command-line.js says, ${err.toString()}`);
process.exit(1);
}
}
if (statResult && statResult.isDirectory()) {
fileString = filePath;
isNotFile = true;
}
if (!isNotFile) {
fileString = fs.readFileSync(filePath, 'utf8');
if (fileString == '') {
return '';
}
}
if (typeof fileString != 'undefined') {
trialString = fileString;
} else {
trialString = filePath;
}
if (isNotFile) {
result = trialString;
} else {
result = fileString;
}
try {
//example: (function(){ return {aaa:'bbb',ccc:'ddd'} })()
//example: (function(item, inx, all){ return new Date(); }) //this one will be evaluated at substitution time
if (noFunctions) {
throw 'not processing functions';
}
result = eval(trialString);
} catch (err) {
if (cmdLineSwitches.v) {
console.log('filePath=' + filePath + '\n');
qtools.dump({ '=-=== eval err =====': err });
}
try {
var result = JSON.parse(trialString);
} catch (err) {
if (cmdLineSwitches.v) {
qtools.dump({ '=-=== JSON err =====': err });
}
}
}
return result;
};
var fileList = [];
var replaceObject = {};
var transformations = {};
var cmdLineSwitches = {};
for (var i = 2, len = workingArgValues.length; i < len; i++) {
var element = workingArgValues[i];
if (element.match(/^--/)) {
var explosion = element.match(/^--(\w+)=(.+)/);
if (explosion) {
var switchName = explosion[1];
if (doNotRetrieveFiles) {
var replacement = explosion[2];
} else {
var replacement = figureOutWhatItIs(explosion[2]);
}
} else {
var explosion = element.match(/^--(\w+)/);
if (explosion) {
var switchName = explosion[1];
if (i < len) {
i++;
var replacement = workingArgValues[i];
} else {
var replacement = '';
}
}
}
if (replacement == '' && !cmdLineSwitches.q) {
console.log(element + ' is empty', 'red');
}
if (typeof replacement == 'number') {
replaceObject[switchName] = replacement.toString();
} else if (typeof replacement == 'string') {
if (valuesSplitCharacter) {
replacement = replacement.split(valuesSplitCharacter);
}
replaceObject[switchName] = replacement;
} else if (
typeof replacement == 'object' &&
typeof replacement.length == 'undefined'
) {
replaceObject = Object.assign(replaceObject, replacement);
} else if (typeof replacement == 'function') {
transformations[switchName] = replacement;
} else {
replaceObject[switchName] = true;
}
} else if (element.match(/^-\w/)) {
var explosion = element.match(/-(\w+)/);
cmdLineSwitches[explosion[1]] = true;
} else {
fileList.push(element);
}
}
//systemd has shell level 0; nodemon shell level 2
return {
calledFromCommandLine:
process.env.SHLVL == 1 || cmdLineSwitches.forceCalledFromCommandLine,
fileList: fileList,
values: replaceObject,
functions: transformations,
switches: cmdLineSwitches,
};
};
this.parseCommandLine = workingFunctionActual(Object.assign({}, args));
this.getParameters = workingFunctionActual(Object.assign({}, args));
return this;
};
//END OF moduleFunction() ============================================================
//module.exports = moduleFunction;
module.exports = new moduleFunction();