-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildConfig.js
478 lines (428 loc) · 16.1 KB
/
buildConfig.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
const fs = require("fs");
const path = require("path");
const http = require("http");
const https = require("https");
const url = require("url");
const chokidar = require("chokidar");
const minimist = require("minimist");
const babelRegister = require("@babel/register");
const mkdirp = require("mkdirp");
const merge = require("./merge");
const postZip = require("./postZip");
const blankJson = require("./blankJson");
const buildLess = require("./buildLess");
const babel = require("@babel/core");
const babylon = require("@babel/parser");
const git = require("git-rev");
const webpackDevCompile = require("./webpackDevCompile");
const reactJSX = require("@babel/plugin-transform-react-jsx").default;
const packageDirname = require.main.exports.getPath();
const argv = minimist(process.argv.slice(2));
const webAppPathArg = argv["web-app"];
const webAppDevMode = !!webAppPathArg;
const webAppDevDistMode = !!argv["dist"];
const webAppDist = !!argv["with-dist"];
const defaultConfigPath = path.resolve(__dirname, "../config/");
let webAppPath = path.join(packageDirname, "node_modules", "blank-web-app");
let settings = {};
let usedModules = {};
let less = "";
let html = "";
let lessVars = null;
let scripts = null;
let commit = "no git";
let buildTime = new Date();
// let loadModule = false;
const gitShort = () => {
return new Promise(resolve => {
git.short(res => resolve(res));
});
};
// const isWinSep = path.sep === "\\";
// const normalizePath = path => {
// if (isWinSep) {
// return path.replace(/\\/g, "/");
// }
// return path;
// };
module.exports = function(configPath, watch, oneServer) {
babelRegister({
only: [new RegExp(path.resolve(configPath))],
plugins: [reactJSX],
});
if (webAppDevMode) {
webAppPath = path.resolve(configPath, webAppPathArg) + path.sep;
console.log("-----------------RUNNING IN BLANK-WEB-APP DEV MODE-----------------");
console.log(`-----------------${webAppPath}-----------------`);
}
if (webAppDevDistMode) {
console.log("-----------------RUNNING IN BLANK-WEB-APP DEV MODE WITH DIST FRONTEND-----------------");
webpackDevCompile(configPath);
}
usedModules = Object.keys(require.cache);
settings = loadSettings(configPath, oneServer);
console.log(`Building blank from: ${configPath}`);
return prepareConfig(configPath)
.then(() => {
return postLib();
})
.then(() => {
if (watch) {
return createWatchers(configPath);
}
})
.catch(err => {
console.error(err);
throw new Error(err);
});
//assets will be posted when config did build first time and less did render
};
module.exports.makeConfig = function makeConfig(configPath, output) {
babelRegister({
only: [new RegExp(path.resolve(configPath))],
plugins: [reactJSX],
});
usedModules = Object.keys(require.cache);
settings = loadSettings(configPath);
if (output) {
settings.output = output;
}
console.log(`Building blank from: ${configPath}`);
return prepareConfig(configPath)
.then(() => {
return postLib();
})
.catch(err => {
console.error(err);
throw new Error("[makeConfig]: " + err);
});
};
function createWatchers(configPath) {
let configTimer = null;
let libTimer = null;
let assetsTimer = null;
console.log("Starting config watcher on:", settings.confPaths);
const configWatcher = chokidar.watch(settings.confPaths, {
persistent: true,
ignoreInitial: true,
ignored: [/var\//, /tests\//, /lib\//, /interfaces\//, /assets\//, /dist\//],
});
configWatcher.on("change", function() {
clearTimeout(configTimer);
configTimer = setTimeout(() => prepareConfig(configPath), 500);
});
const libWatcher = chokidar.watch(settings.libPaths, { persistent: true, ignoreInitial: true });
libWatcher.on("change", function(_path) {
clearTimeout(libTimer);
libTimer = setTimeout(() => postLib(), 500);
});
const assetsWatchPaths = settings.assetsPaths.map(p => p.path || (typeof p === "string" && p) || "").filter(p => p);
console.log("[assetsWatchPaths]", assetsWatchPaths);
const assetsWatcher = chokidar.watch(assetsWatchPaths, {
persistent: true,
ignoreInitial: true,
});
assetsWatcher.on("change", function(_path) {
console.log("[assetsWatcher] change");
clearTimeout(assetsTimer);
assetsTimer = setTimeout(() => postAssets(), 500);
});
}
function postLib() {
return postZip(settings.libPaths, "lib", settings.output);
}
function postAssets() {
return postZip(
settings.assetsPaths.concat(
{ folder: "blank/css", file: "app.css", data: less },
{ folder: "blank", file: "index.html", data: html }
),
"assets",
settings.output
);
}
function prepareConfig(configPath) {
for (const prop of Object.keys(require.cache)) {
if (usedModules.indexOf(prop) < 0 && Object.prototype.hasOwnProperty.call(require.cache, prop)) {
delete require.cache[prop];
}
}
const config = {};
buildTime = new Date();
return gitShort()
.then(res => {
commit = res;
console.log("Loading default config");
loadFromFolder(config, defaultConfigPath);
console.log("Loading app config");
loadFromFolder(config, configPath, true);
applyProxy(config);
return updateAssets(config);
})
.then(() => {
babelifyConfig(config);
return ship(config);
})
.catch(err => {
throw new Error("[buildConfig:prepareConfig]: " + err);
});
}
function babelifyConfig(config) {
for (const storeName of Object.keys(config)) {
const storeDesc = config[storeName];
babelifyProps(storeDesc.props || {});
}
}
function babelifyProps(props) {
for (const propName of Object.keys(props)) {
const propDesc = props[propName];
if (propDesc.loadComponent) {
propDesc.loadComponent = transformCode(propDesc.loadComponent);
}
if (propDesc.type === "virtual/client" && propDesc.load) {
propDesc.load = transformCode(propDesc.load);
}
if (propDesc.props) {
babelifyProps(propDesc.props);
}
}
}
function transformCode(code) {
const ast = babylon.parse(stringifyFunction(code), {
allowReturnOutsideFunction: true,
});
return babel.transformFromAst(ast, code, {
presets: [require("@babel/preset-env")],
}).code;
}
function updateAssets(config) {
if (config && config._commonSettings && config._commonSettings.entries) {
const promises = [];
const _lessVars = config._commonSettings.entries.lessVars || {};
if (JSON.stringify(_lessVars) !== JSON.stringify(lessVars)) {
console.log("Building LESS...");
lessVars = _lessVars;
const p = buildLess(lessVars, webAppPath).then(
r => {
console.log("LESS build OK");
less = r.css;
},
err => {
console.warn("LESS build error:", err);
}
);
promises.push(p);
}
const _scripts = config._commonSettings.entries.scripts || [];
if (JSON.stringify(_scripts) !== JSON.stringify(scripts)) {
scripts = _scripts;
const p = loadIndex().then(_html => {
let scriptsHtml = "";
for (let s of scripts) {
scriptsHtml += `<script type="text/javascript" src="${s.src}"></script>\n`;
}
const i = _html.indexOf("</body>");
_html = _html.slice(0, i) + scriptsHtml + _html.slice(i);
if (webAppDevMode || webAppDevDistMode) {
_html = _html.replace("/assets/blank/js/vendors.js", "http://localhost:2816/vendors.js");
_html = _html.replace("assets/blank/js/vendors.js", "http://localhost:2816/vendors.js");
_html = _html.replace("blank/js/vendors.js", "http://localhost:2816/vendors.js");
_html = _html.replace("/assets/blank/js/bundle.js", "http://localhost:2816/bundle.js");
_html = _html.replace("assets/blank/js/bundle.js", "http://localhost:2816/bundle.js");
_html = _html.replace("blank/js/bundle.js", "http://localhost:2816/bundle.js");
console.log("HTML:");
console.log(_html);
}
html = _html;
});
promises.push(p);
}
if (promises.length > 0) {
return Promise.all(promises).then(() => {
return postAssets();
});
} else {
return Promise.resolve();
}
}
}
function ship(config) {
const configString = stringifyConfig(config);
if (settings.output.indexOf("http") === 0) {
const confOutput = settings.output + "/config";
console.log(`Posting config to service registry: "${confOutput}"`);
const srUrl = url.parse(confOutput);
const h = srUrl.protocol === "https:" ? https : http;
const req = h.request(
Object.assign(srUrl, {
method: "POST",
}),
res => {
console.log("Post 'config' to service registry result: ", res.statusCode, "/", res.statusMessage);
}
);
req.write(configString);
req.end();
} else {
const confOutput = path.resolve(settings.output);
return new Promise((resolve, reject) => {
mkdirp(confOutput, function(err) {
if (err) {
console.error(err);
return reject(err);
}
resolve();
});
})
.then(() => {
return new Promise((resolve, reject) => {
const outputFile = path.normalize(confOutput + path.sep + "config.json");
console.log(new Date(), "Writing config to: ", outputFile);
fs.writeFile(outputFile, configString, function(err) {
if (err) {
console.log(err);
return reject(err);
}
console.log(new Date(), "The config was saved!");
resolve();
});
});
})
.catch(err => {
throw new Error("[buildConfig:ship]: " + err);
});
}
}
function stringifyConfig(config) {
return JSON.stringify(
config,
function(key, val) {
return stringifyFunction(val);
},
" "
);
}
function stringifyFunction(val, forBabel) {
if (typeof val === "function") {
let fn = val.toString();
fn = fn
.substring(fn.indexOf("{") + 1, fn.lastIndexOf("}"))
.replace(/(\/\*.*\*\/)/g, "")
.replace(/(\/\/.*\n)|(\/\/.*\r\n)|(\/\/.*\r\n)/g, "");
if (forBabel) {
fn = `(function(){${fn}}())`;
}
return fn;
}
return val;
}
function applyProxy(config) {
for (const storeName of Object.keys(config || {})) {
const storeDesc = config[storeName];
if (storeDesc.baseStore) {
const baseStoreDesc = config[storeDesc.baseStore];
if (baseStoreDesc == null) {
throw new Error("Invalid config: no base store desc for proxy:", storeName);
}
delete storeDesc.type;
config[storeName] = merge({}, baseStoreDesc, storeDesc);
delete config[storeName].storeLifeCycle;
}
}
}
function loadFromFolder(config, configPath, recursive) {
let files = [];
try {
files = fs.readdirSync(configPath);
} catch (e) {
return console.log("Error while reading config dir:", configPath, e);
}
for (const file of files) {
const itemPath = path.join(path.resolve(configPath), file);
const stats = fs.lstatSync(itemPath);
if ((stats.isFile(), file.indexOf("_") !== 0 && file.indexOf(".js") === file.length - 3)) {
try {
console.log("Loading:", itemPath);
const c = require(itemPath);
if (c.users) {
if (Array.isArray(c.users.httpHooks) && c.users.httpHooks[0] !== "...") {
c.users.httpHooks.unshift("...");
}
if (Array.isArray(c.users.tasks) && c.users.tasks[0] !== "...") {
c.users.tasks.unshift("...");
}
}
if (c._commonSettings) {
c._commonSettings.entries = c._commonSettings.entries || {};
c._commonSettings.entries.commit = commit;
c._commonSettings.entries.buildTime = buildTime.toISOString();
}
merge(config, c);
} catch (e) {
console.error("\x1b[31m >>>>>>>>>>> Bad config file ", itemPath, e, " <<<<<<<<<<<\x1b[0m");
}
} else if (recursive && stats.isDirectory()) {
const excludeFolders = ["tests", "lib", "templates", "assets", ".git", "node_modules", "var", "dist"];
if (excludeFolders.indexOf(file) < 0) {
loadFromFolder(config, itemPath + path.sep, recursive);
}
}
}
}
function loadSettings(configPath, oneServer) {
const settings = blankJson.read(configPath);
settings.libPaths = Array.isArray(settings.lib.path) ? settings.lib.path : ["./lib"];
settings.assetsPaths = Array.isArray(settings.assets.path) ? settings.assets.path : ["./assets"];
settings.confPaths = [path.resolve(configPath) + path.sep, path.resolve(defaultConfigPath) + path.sep];
const srPort = process.env.BLANK_SERVICE_REGISTRY_PORT || "1234";
settings.output = (argv.out || argv.o || settings.output || `http://localhost:${srPort}`).trim();
for (let i = 0; i < settings.libPaths.length; i++) {
settings.libPaths[i] = path.resolve(configPath, settings.libPaths[i]) + path.sep;
}
settings.libPaths.unshift(path.resolve(__dirname, "../defaultLib/"));
for (let i = 0; i < settings.assetsPaths.length; i++) {
settings.assetsPaths[i] = path.resolve(configPath, settings.assetsPaths[i]) + path.sep;
}
//Load all assets
const bundlePath = webAppDist
? path.join(configPath, "dist") + path.sep
: path.join(webAppPath, "release") + path.sep;
const fontsPath = path.join(webAppPath, "src", "fonts") + path.sep;
const bootstrapCssPath = path.join(webAppPath, "src", "css", "bootstrap.css");
settings.assetsPaths.push({ folder: "blank/js", path: bundlePath });
settings.assetsPaths.push({ folder: "blank/fonts", path: fontsPath });
settings.assetsPaths.push({ folder: "blank/css", file: "bootstrap.css", data: fs.readFileSync(bootstrapCssPath) });
const extraWatch = settings.conf.watch || [];
for (let i = 0; i < extraWatch.length; i++) {
settings.confPaths.push(path.resolve(configPath, extraWatch[i]) + path.sep);
}
return settings;
}
// function findLoadModuleConfig(config) {
// for (let key of Object.keys(config)) {
// if (key === "loadModule") {
// loadModule = true;
// break;
// }
// try {
// let value = config[key];
// if (value && typeof value == "object") {
// findLoadModuleConfig(value);
// }
// } catch (error) {
// console.error("key", key, config[key], error);
// }
// }
// }
function loadIndex() {
const indexPath = path.join(webAppPath, "src", "html", "index.html");
return new Promise((f, r) => {
fs.readFile(indexPath, "UTF8", (err, data) => {
if (err) {
r(err);
} else {
f(data);
}
});
});
}