-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqtools-config-file-processor.js
279 lines (237 loc) · 8.32 KB
/
qtools-config-file-processor.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
'use strict';
const multiIni = require('multi-ini');
const fs = require('fs');
const path = require('path');
const qtLib = require('qtools-functional-library');
const multiIniGen = require('multi-ini');
const os=require('os');
//START OF moduleFunction() ============================================================
var moduleFunction = function(args = {}) {
const { logger = {} } = args;
logger.warn = logger.warn
? logger.warn
: message => console.log(`WARNING: ${message}`);
logger.error = logger.error
? logger.error
: message => console.log(`ERROR: ${message}`);
const findGoodConfigPath = (pathParm, workingDirectory, resolve) => {
let result = fs.existsSync(pathParm) ? fs.realpathSync(pathParm) : '';
workingDirectory = workingDirectory
? fs.realpathSync(workingDirectory)
: '';
if (result) {
return result;
} else if (workingDirectory) {
while (
workingDirectory != '/' &&
!fs.existsSync(path.join(workingDirectory, pathParm))
) {
if (resolve) {
logger.warn(`tried: ${path.join(workingDirectory, pathParm)}`);
}
workingDirectory = path.dirname(workingDirectory);
}
const result =
workingDirectory != '/' ? path.join(workingDirectory, pathParm) : '';
if (resolve) {
logger.warn(`finished with: ${result}`);
}
return result;
}
return result;
};
const multiIni = new multiIniGen.Class({
filters: [
value => {
if (value == /^''$/) {
return value;
}
if (value.match(/^''/)) {
return value.replace(/^''/, '');
}
switch (value) {
case 'true':
return true;
case 'false':
return false;
case 'null':
return null;
}
if (!isNaN(+value)) {
return +value;
}
return value;
}
]
});
let configurationSourceFilePath = '';
this.getRecentConfigPath = () => configurationSourceFilePath;
this.getConfig = (configPath, workingDirectory, options = {}) => {
const { resolve = false, userSubstitutions, injectedItems } = options;
if (!workingDirectory) {
workingDirectory = '.';
}
configurationSourceFilePath = findGoodConfigPath(
configPath,
workingDirectory,
resolve
);
if (!configurationSourceFilePath) {
logger.error(`no config file found ${configPath}`);
return;
}
const configurationModificationDate = fs
.statSync(configurationSourceFilePath)
.mtime.toLocaleString();
const raw = multiIni.read(configurationSourceFilePath);
let config = raw.qtNumberKeysToArray().qtMerge({
_meta: {
configurationSourceFilePath,
configurationDirectoryPath:path.dirname(configurationSourceFilePath),
configurationModificationDate
}
});
const configString = JSON.stringify(config);
const includedFiles = [];
const mergeBeforeFiles = [];
const mergeAfterFiles = [];
const includedRaw = [];
const mergeBeforeRaw = [];
const mergeAfterRaw = [];
if (config._mergeBefore && config._mergeBefore.length) {
const appendages = config._mergeBefore.map(filePath => {
if (!fs.existsSync(filePath)) {
const newPath = path.resolve(
path.dirname(configurationSourceFilePath),
filePath
);
if (!fs.existsSync(newPath)) {
throw `Bad relative file path in _mergeBefore. ${filePath} resolves to ${newPath} which does not exist (From config ${configurationSourceFilePath}.)`;
}
filePath = newPath;
}
const appendageRaw = multiIni.read(filePath);
const appendage = appendageRaw.qtNumberKeysToArray();
if (Object.keys(appendage).length == 0) {
throw `No properties in _mergeBefore file. This is usually because there is no top level .ini [section]. multiIni insists on this. Bad filepath is ${filePath}`;
}
mergeBeforeFiles.push(filePath);
mergeBeforeRaw.push(appendageRaw);
return appendage;
});
config = appendages
.reduce((result, component) => {
return result.qtMerge(component);
}, {})
.qtMerge(config);
}
if (config._includes && config._includes.length) {
const appendages = config._includes.map(filePath => {
if (!fs.existsSync(filePath)) {
const newPath = path.resolve(
path.dirname(configurationSourceFilePath),
filePath
);
if (!fs.existsSync(newPath)) {
throw `Bad relative file path. ${filePath} resolves to ${newPath} which does not exist (From config ${configurationSourceFilePath}.)`;
}
filePath = newPath;
}
const appendageRaw = multiIni.read(filePath);
const appendage = appendageRaw.qtNumberKeysToArray();
if (Object.keys(appendage).length == 0) {
throw `No properties in _merge file. This is usually because there is no top level .ini [section]. multiIni insists on this. Bad filepath is ${filePath}`;
}
includedFiles.push(filePath);
includedRaw.push(appendageRaw);
return appendage;
});
config = appendages.reduce((result, component) => {
return result.qtMerge(component);
}, config);
}
if (config._mergeAfter && config._mergeAfter.length) {
const appendages = config._mergeAfter.map(filePath => {
if (!fs.existsSync(filePath)) {
const newPath = path.resolve(
path.dirname(configurationSourceFilePath),
filePath
);
if (!fs.existsSync(newPath)) {
throw `Bad relative file path in _mergeAfter. ${filePath} resolves to ${newPath} which does not exist (From config ${configurationSourceFilePath}.)`;
}
filePath = newPath;
}
const appendageRaw = multiIni.read(filePath);
const appendage = appendageRaw.qtNumberKeysToArray();
if (Object.keys(appendage).length == 0) {
throw `No properties in _mergeAfter file. This is usually because there is no top level .ini [section]. multiIni insists on this. Bad filepath is ${filePath}`;
}
mergeAfterFiles.push(filePath);
mergeAfterRaw.push(appendageRaw);
return appendage;
});
config = config.qtMerge(
appendages.reduce((result, component) => {
return result.qtMerge(component);
}, {})
);
}
//substitutions supplied in args are assumed to be client overrides and are processed
//first. That means that the tokens are not available for subsitution by the
//values in the file.
if (typeof options.userSubstitutions == 'object') {
const configString = JSON.stringify(config);
const revisedConfigString = configString.qtTemplateReplace(
options.userSubstitutions
);
try {
config = JSON.parse(revisedConfigString);
config._meta.userSubstitutions = options.userSubstitutions;
} catch (err) {
throw `qtools-config-files-processor says, 'args._substitutions' processing is actually string processing on JSON.stringify(config). The result does not JSON.parse(revisedConfigString). The error message is ${err.toString()}.`;
}
}
if (typeof config._substitutions == 'object') {
const configString = JSON.stringify(config);
const revisedConfigString = configString.qtTemplateReplace(
config._substitutions
);
try {
config = JSON.parse(revisedConfigString);
} catch (err) {
throw `qtools-config-files-processor says, 'config._substitutions' processing is actually string processing on JSON.stringify(config). The result does not JSON.parse(revisedConfigString). The error message is ${err.toString()}.`;
}
}
if ('always substitute system items') {
const configString = JSON.stringify(config);
const revisedConfigString = configString.qtTemplateReplace(
{
userHomeDir:os.homedir(),
configsDir:path.dirname(configurationSourceFilePath)
}
);
try {
config = JSON.parse(revisedConfigString);
} catch (err) {
throw `qtools-config-files-processor says, 'config._substitutions' processing is actually string processing on JSON.stringify(config). The result does not JSON.parse(revisedConfigString). The error message is ${err.toString()}.`;
}
}
if (injectedItems) {
config.injectedItems = injectedItems;
}
config._meta._substitutions = config._substitutions;
config._meta._includes = includedFiles;
config._meta._mergeBefore = mergeBeforeFiles;
config._meta._mergeAfter = mergeAfterFiles;
config._meta._mainRaw = raw;
config._meta._includedRaw = includedRaw;
config._meta._mergeBeforeRaw = mergeBeforeRaw;
config._meta._mergeAfterRaw = mergeAfterRaw;
return config;
};
return this;
};
//END OF moduleFunction() ============================================================
//module.exports = moduleFunction;
module.exports = new moduleFunction();