1 | import * as chokidar from 'chokidar';
|
2 | import * as glob from 'fast-glob';
|
3 | import * as fs from 'fs-extra-plus';
|
4 | import { spawn } from 'p-spawn';
|
5 | import * as Path from 'path';
|
6 | import { pcssFiles, rollupFiles, tmplFiles } from './processors';
|
7 | import { asNames, now, Partial, printLog, readJsonFileWithComments } from './utils';
|
8 | import { loadVdevConfig } from './vdev-config';
|
9 |
|
10 |
|
11 |
|
12 | export interface Block {
|
13 | name: string;
|
14 | dir: string;
|
15 | webBundles?: WebBundle[];
|
16 | baseDistDir?: string;
|
17 | dbuildDependencies?: string | string[];
|
18 | [key: string]: any;
|
19 |
|
20 | }
|
21 |
|
22 | export interface WebBundle {
|
23 | name: string;
|
24 |
|
25 | entries: string | string[];
|
26 |
|
27 |
|
28 | watch?: string | string[];
|
29 |
|
30 | rollupOptions?: any;
|
31 | dist: string;
|
32 |
|
33 |
|
34 | type?: string;
|
35 | dir?: string;
|
36 | }
|
37 |
|
38 | export type BlockByName = { [name: string]: Block };
|
39 |
|
40 |
|
41 | export async function updateVersions(config?: any) {
|
42 | if (!config) {
|
43 | config = await loadVdevConfig();
|
44 | }
|
45 | const versionFiles = (config.version && config.version.files) ? config.version.files as string[] : null;
|
46 |
|
47 |
|
48 | if (versionFiles == null) {
|
49 | return;
|
50 | }
|
51 |
|
52 | let newAppVersion = config.version.appVersion;
|
53 |
|
54 | if (newAppVersion == null) {
|
55 | const packageJson = await fs.readJSON('./package.json');
|
56 |
|
57 | newAppVersion = packageJson.dropVersion;
|
58 | }
|
59 |
|
60 | let firstUpdate = false;
|
61 |
|
62 | try {
|
63 | for (let file of versionFiles) {
|
64 | const originalContent = (await fs.readFile(file, 'utf8')).toString();
|
65 | const isHTML = file.toLowerCase().endsWith('html');
|
66 |
|
67 | let fileAppVersion = getVersion(originalContent, isHTML);
|
68 |
|
69 | if (newAppVersion !== fileAppVersion) {
|
70 |
|
71 | if (!firstUpdate) {
|
72 | console.log(`----- Version Update: Updating new appVersion to ${newAppVersion} `);
|
73 | firstUpdate = true;
|
74 | }
|
75 |
|
76 | console.log(`Changing appVersion ${fileAppVersion} -> ${newAppVersion} in file: ${file}`);
|
77 | let newContent = replaceVersion(originalContent, newAppVersion, isHTML);
|
78 | await fs.writeFile(file, newContent, 'utf8');
|
79 | } else {
|
80 |
|
81 |
|
82 | }
|
83 | }
|
84 | } catch (ex) {
|
85 | throw new Error(`ERROR while doing versioning files - ${ex.message}`);
|
86 | }
|
87 |
|
88 | if (firstUpdate) {
|
89 | console.log('----- /Version Update: done');
|
90 | }
|
91 | }
|
92 |
|
93 |
|
94 |
|
95 | export async function cleanNodeFiles() {
|
96 | const filesToDelete = ['./package-lock.json', './node_blockules'];
|
97 |
|
98 | const blocks = await loadBlocks();
|
99 |
|
100 |
|
101 |
|
102 | for (const block of Object.values(blocks)) {
|
103 | const dir = block.dir;
|
104 | for (let fName of filesToDelete) {
|
105 | const fileToDelete = Path.join(dir, fName);
|
106 | if ((await fs.pathExists(fileToDelete))) {
|
107 | fs.saferRemove(fileToDelete);
|
108 | }
|
109 | }
|
110 | }
|
111 | }
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | export async function buildBlocksOrBundles(names: string[]) {
|
118 | if (names.length === 0) {
|
119 | const blocks = await loadBlocks();
|
120 | for (let block of Object.values(blocks)) {
|
121 | await _buildBlock(block);
|
122 | }
|
123 | } else {
|
124 | for (let name of names) {
|
125 | const blockAndBundle = name.split('/');
|
126 |
|
127 | await buildBlock(blockAndBundle[0], blockAndBundle[1]);
|
128 | }
|
129 | }
|
130 | }
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | export async function watchBlock(blockName: string) {
|
136 | const blocks = await loadBlocks();
|
137 | const block = blocks[blockName];
|
138 |
|
139 | const webBundles = (block) ? block.webBundles : null;
|
140 | if (webBundles == null) {
|
141 | throw new Error(`Block ${blockName} not found or does not have a '.webBundles'. As of now, can only watch webBundles`)
|
142 | }
|
143 |
|
144 | for (let bundle of webBundles) {
|
145 | await initWebBundle(block, bundle);
|
146 |
|
147 |
|
148 | if (bundle.type === 'js' || bundle.type === 'ts') {
|
149 | if (bundle.watch) {
|
150 | console.log(`WARNING - Ignoring '.watch' property for bundle ${bundle.name} for .js/.ts processing
|
151 | as rollup watches dependencies. (advice: remove this .watch property for clarity)`);
|
152 | }
|
153 | await _buildBlock(block, bundle, { watch: true });
|
154 | }
|
155 |
|
156 |
|
157 | else {
|
158 | await _buildBlock(block, bundle);
|
159 | const toWatch = bundle.watch ?? bundle.entries;
|
160 | let watcher = chokidar.watch(toWatch, { persistent: true });
|
161 | watcher.on('change', async function (filePath: string, stats) {
|
162 | if (filePath.endsWith(`.${bundle.type}`)) {
|
163 | await _buildBlock(block, bundle);
|
164 | }
|
165 | });
|
166 | }
|
167 | }
|
168 | }
|
169 |
|
170 | export interface BuildOptions {
|
171 | watch?: boolean;
|
172 | full?: boolean;
|
173 | }
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 | export async function buildBlock(blockName: string, onlyBundleName?: string, opts?: BuildOptions) {
|
181 | const blockByName = await loadBlocks();
|
182 | const block = blockByName[blockName];
|
183 |
|
184 | if (!block) {
|
185 | throw new Error(`No block found for blockeName ${blockName}.`);
|
186 | }
|
187 |
|
188 | let bundle: WebBundle | undefined;
|
189 | if (onlyBundleName && block.webBundles) {
|
190 | bundle = block.webBundles.find((b) => (b.name == onlyBundleName));
|
191 | if (!bundle) {
|
192 | throw new Error(`No webBundle ${onlyBundleName} found in block ${block.name}`);
|
193 | }
|
194 | await initWebBundle(block, bundle);
|
195 | }
|
196 |
|
197 | await _buildBlock(block, bundle, opts);
|
198 | }
|
199 |
|
200 | async function _buildBlock(block: Block, bundle?: WebBundle, opts?: BuildOptions) {
|
201 |
|
202 | const hasPomXml = await fs.pathExists(Path.join(block.dir, 'pom.xml'));
|
203 | const hasPackageJson = await fs.pathExists(Path.join(block.dir, 'package.json'));
|
204 | const hasDockerFile = await fs.pathExists(Path.join(block.dir, 'Dockerfile'));
|
205 | const hasTsConfig = await fs.pathExists(Path.join(block.dir, 'tsconfig.json'));
|
206 | const hasWebBundles = (block.webBundles) ? true : false;
|
207 |
|
208 |
|
209 | const start = now();
|
210 | if (!bundle) {
|
211 | console.log(`------ Building Block ${block.name} ${block.dir}`);
|
212 | }
|
213 |
|
214 |
|
215 | if (hasPackageJson) {
|
216 | await npmInstall(block);
|
217 | }
|
218 |
|
219 |
|
220 | if (hasWebBundles) {
|
221 |
|
222 | await buildWebBundles(block, bundle, opts);
|
223 | }
|
224 |
|
225 |
|
226 | if (!hasWebBundles && hasTsConfig) {
|
227 | await buildTsSrc(block);
|
228 | }
|
229 |
|
230 | if (hasPomXml) {
|
231 | await runMvn(block, opts ? opts.full : false);
|
232 | }
|
233 |
|
234 | if (!bundle) {
|
235 | await printLog(`------ Building Block ${block.name} DONE`, null, start);
|
236 | console.log();
|
237 | }
|
238 | }
|
239 |
|
240 |
|
241 | async function npmInstall(block: Block) {
|
242 | await spawn('npm', ['install'], { cwd: block.dir });
|
243 | }
|
244 |
|
245 | async function buildTsSrc(block: Block) {
|
246 | const distDirNeedsDelete = false;
|
247 |
|
248 | const distDir = Path.join(block.dir, '/dist/');
|
249 | const distDirExist = await fs.pathExists(distDir);
|
250 |
|
251 |
|
252 | if (distDirExist) {
|
253 | const tsconfigObj = await readJsonFileWithComments(Path.join(block.dir, 'tsconfig.json'));
|
254 | let outDir = tsconfigObj.compilerOptions.outDir as string | undefined | null;
|
255 | outDir = (outDir) ? Path.join(block.dir, outDir, '/') : null;
|
256 | if (outDir === distDir) {
|
257 | console.log(`tsc prep - deleting tsc distDir ${distDir}`);
|
258 | await fs.saferRemove(distDir);
|
259 | } else {
|
260 | console.log(`tss prep - skipping tsc distDir ${distDir} because does not match tsconfig.json compilerOptions.outDir ${outDir}`);
|
261 | }
|
262 | }
|
263 |
|
264 |
|
265 | await spawn('./node_modules/.bin/tsc', ['-p', block.dir]);
|
266 | }
|
267 |
|
268 | async function runMvn(block: Block, full?: boolean) {
|
269 | const args = ['clean', 'package'];
|
270 | if (!full) {
|
271 | args.push('-Dmaven.test.skip=true');
|
272 | }
|
273 |
|
274 | var start = now();
|
275 |
|
276 | await spawn('mvn', args, {
|
277 | toConsole: false,
|
278 | cwd: block.dir,
|
279 | onStderr: function (data: any) {
|
280 | process.stdout.write(data);
|
281 | }
|
282 | });
|
283 |
|
284 | await printLog(`maven build ${full ? 'with test' : ''}`, null, start);
|
285 | }
|
286 |
|
287 |
|
288 |
|
289 | type WebBundler = (block: Block, webBundle: WebBundle, opts?: BuildOptions) => void;
|
290 |
|
291 |
|
292 | const bundlers: { [type: string]: WebBundler } = {
|
293 | ts: buildTsBundler,
|
294 | pcss: buildPcssBundler,
|
295 | tmpl: buildTmplBundler,
|
296 | html: buildTmplBundler,
|
297 | js: buildJsBundler
|
298 | }
|
299 |
|
300 | async function buildWebBundles(block: Block, onlyBundle?: WebBundle, opts?: BuildOptions) {
|
301 |
|
302 | let webBundles: WebBundle[];
|
303 |
|
304 | if (onlyBundle) {
|
305 |
|
306 | webBundles = [onlyBundle];
|
307 | } else {
|
308 | webBundles = block.webBundles!;
|
309 |
|
310 | for (let bundle of webBundles) {
|
311 | await initWebBundle(block, bundle);
|
312 | }
|
313 | }
|
314 |
|
315 | for (let bundle of webBundles!) {
|
316 | await ensureDist(bundle);
|
317 | var start = now();
|
318 | await bundlers[bundle.type!](block, bundle, opts);
|
319 | await printLog(`Building bundle ${block.name}/${bundle.name}`, bundle.dist, start);
|
320 | }
|
321 | }
|
322 |
|
323 | const rollupOptionsDefaults = {
|
324 | ts: {
|
325 | watch: false,
|
326 | ts: true,
|
327 | tsconfig: './tsconfig.json'
|
328 | },
|
329 | js: {
|
330 | watch: false,
|
331 | ts: false
|
332 | }
|
333 | }
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 | async function initWebBundle(block: Block, bundle: WebBundle) {
|
340 |
|
341 | bundle.type = Path.extname(asNames(bundle.entries)[0]).substring(1);
|
342 |
|
343 |
|
344 | bundle.dir = specialPathResolve('', block.dir, bundle.dir);
|
345 |
|
346 |
|
347 | bundle.entries = asNames(bundle.entries).map((f) => specialPathResolve('', bundle.dir!, f));
|
348 |
|
349 |
|
350 | if (bundle.watch) {
|
351 |
|
352 | bundle.watch = asNames(bundle.watch).map((f) => specialPathResolve('', bundle.dir!, f));
|
353 | }
|
354 |
|
355 |
|
356 | bundle.dist = specialPathResolve('', block.baseDistDir!, bundle.dist);
|
357 |
|
358 |
|
359 |
|
360 | if (bundle.type === 'ts' || bundle.type === 'js') {
|
361 |
|
362 | const rollupOptionsDefault = rollupOptionsDefaults[bundle.type];
|
363 |
|
364 |
|
365 | const rollupOptions = (bundle.rollupOptions) ? { ...rollupOptionsDefault, ...bundle.rollupOptions }
|
366 | : { ...rollupOptionsDefault };
|
367 |
|
368 |
|
369 | if (rollupOptions.tsconfig) {
|
370 | rollupOptions.tsconfig = specialPathResolve('', bundle.dir, rollupOptions.tsconfig);
|
371 | }
|
372 |
|
373 |
|
374 | bundle.rollupOptions = rollupOptions;
|
375 | }
|
376 |
|
377 |
|
378 | }
|
379 |
|
380 |
|
381 |
|
382 | async function buildTsBundler(block: Block, bundle: WebBundle, opts?: BuildOptions) {
|
383 |
|
384 | try {
|
385 | if (opts && opts.watch) {
|
386 | bundle.rollupOptions.watch = true;
|
387 | }
|
388 |
|
389 | const allEntries = await resolveGlobs(bundle.entries);
|
390 | await rollupFiles(allEntries, bundle.dist, bundle.rollupOptions);
|
391 | } catch (ex) {
|
392 |
|
393 | console.log("BUILD ERROR - something when wrong on rollup\n\t", ex);
|
394 | console.log("Empty string was save to the app bundle");
|
395 | console.log("Trying saving again...");
|
396 | return;
|
397 | }
|
398 | }
|
399 |
|
400 | async function buildPcssBundler(block: Block, bundle: WebBundle, opts?: BuildOptions) {
|
401 | const allEntries = await resolveGlobs(bundle.entries);
|
402 | await pcssFiles(allEntries, bundle.dist);
|
403 | }
|
404 |
|
405 | async function buildTmplBundler(block: Block, bundle: WebBundle, opts?: BuildOptions) {
|
406 | const allEntries = await resolveGlobs(bundle.entries);
|
407 | await tmplFiles(allEntries, bundle.dist);
|
408 | }
|
409 |
|
410 | async function buildJsBundler(block: Block, bundle: WebBundle, opts?: BuildOptions) {
|
411 | if (opts && opts.watch) {
|
412 | bundle.rollupOptions.watch = true;
|
413 | }
|
414 | const allEntries = await resolveGlobs(bundle.entries);
|
415 | await rollupFiles(allEntries, bundle.dist, bundle.rollupOptions);
|
416 | }
|
417 |
|
418 |
|
419 |
|
420 | export async function loadDockerBlocks(): Promise<BlockByName> {
|
421 | const blocks = await loadBlocks();
|
422 | const dockerBlocks: BlockByName = {};
|
423 | for (let block of Object.values(blocks)) {
|
424 | const hasDockerfile = await fs.pathExists(Path.join(block.dir, 'Dockerfile'));
|
425 | if (hasDockerfile) {
|
426 | dockerBlocks[block.name] = block;
|
427 | }
|
428 | }
|
429 | return dockerBlocks;
|
430 | }
|
431 |
|
432 | export async function loadBlocks(): Promise<BlockByName> {
|
433 | const rawConfig = await loadVdevConfig();
|
434 |
|
435 | const rawBlocks = rawConfig.blocks;
|
436 |
|
437 |
|
438 | const blockByName: { [name: string]: Block } = rawBlocks.map((item: string | any) => {
|
439 | let block: Partial<Block>;
|
440 | let name: string;
|
441 |
|
442 | if (typeof item === 'string') {
|
443 | block = {
|
444 | name: item
|
445 | }
|
446 | } else {
|
447 | if (!item.name) {
|
448 | throw new Error(`the build config file vdev.yaml has a block without '.name'`);
|
449 | }
|
450 |
|
451 | block = { ...item };
|
452 | }
|
453 |
|
454 |
|
455 | if (!block.dir) {
|
456 | block.dir = Path.join(rawConfig.baseBlockDir, `${block.name}/`);
|
457 | }
|
458 | return block as Block;
|
459 | }).reduce((map: { [key: string]: Block }, block: Block) => {
|
460 | map[block.name] = block;
|
461 | return map;
|
462 | }, {});
|
463 |
|
464 | return blockByName;
|
465 | }
|
466 |
|
467 | export async function loadBlock(name: string): Promise<Block> {
|
468 | const blocks = await loadBlocks();
|
469 | const block = blocks[name];
|
470 |
|
471 | if (!block) {
|
472 | throw new Error(`Block ${name} not found`);
|
473 | }
|
474 |
|
475 | return block;
|
476 | }
|
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 | async function resolveGlobs(globs: string | string[]) {
|
484 |
|
485 | return glob(globs);
|
486 | }
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 | function getVersion(content: string, isHtml = false) {
|
493 |
|
494 | if (isHtml) {
|
495 | const rgx = /<.*(href|src).*?v=(.*?)(\"|\&)/gm;
|
496 | const r = rgx.exec(content);
|
497 | if (r != null && r.length > 2) {
|
498 | return r[2];
|
499 | } else {
|
500 | return null;
|
501 | }
|
502 | }
|
503 |
|
504 | else {
|
505 | var rx = new RegExp('appVersion' + '\\s*[=:]\\s*"(.*)"', 'i');
|
506 | var match = content.match(rx);
|
507 | return (match) ? match[1] : null;
|
508 | }
|
509 | }
|
510 |
|
511 | function replaceVersion(content: string, value: string, isHtml = false) {
|
512 | if (isHtml) {
|
513 | const rgxRep = /(<.*(href|src).*?v=).*?(\"|\&.*)/g;
|
514 |
|
515 | return content.replace(rgxRep, '$1' + value + '$3');
|
516 | }
|
517 | else {
|
518 | var rx = new RegExp('(.*' + 'appVersion' + '\\s*[=:]\\s*").*(".*)', 'i');
|
519 | content = content.replace(rx, '$1' + value + '$2');
|
520 | return content;
|
521 | }
|
522 | }
|
523 |
|
524 |
|
525 |
|
526 | async function ensureDist(bundle: WebBundle) {
|
527 | const distDir = Path.dirname(bundle.dist);
|
528 | await fs.ensureDir(distDir);
|
529 | }
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 |
|
540 |
|
541 | export function specialPathResolve(baseDir: string, dir: string, finalPath?: string): string {
|
542 | if (finalPath == null) {
|
543 | return dir;
|
544 | }
|
545 |
|
546 | if (finalPath.startsWith('/')) {
|
547 | return finalPath;
|
548 | }
|
549 | if (finalPath.startsWith('./') || finalPath.startsWith('../')) {
|
550 | return Path.join(dir, finalPath)
|
551 | }
|
552 | return Path.join(baseDir, finalPath);
|
553 | }
|
554 |
|