1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.AssetsManager = void 0;
|
4 | const chokidar = require("chokidar");
|
5 | const path_1 = require("path");
|
6 | const shell = require("shelljs");
|
7 | const copy_path_resolve_1 = require("./helpers/copy-path-resolve");
|
8 | const get_value_or_default_1 = require("./helpers/get-value-or-default");
|
9 | class AssetsManager {
|
10 | constructor() {
|
11 | this.watchAssetsKeyValue = {};
|
12 | this.watchers = [];
|
13 | this.actionInProgress = false;
|
14 | }
|
15 | |
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | closeWatchers() {
|
22 |
|
23 | const timeoutMs = 500;
|
24 | const closeFn = () => {
|
25 | if (this.actionInProgress) {
|
26 | this.actionInProgress = false;
|
27 | setTimeout(closeFn, timeoutMs);
|
28 | }
|
29 | else {
|
30 | this.watchers.forEach((watcher) => watcher.close());
|
31 | }
|
32 | };
|
33 | setTimeout(closeFn, timeoutMs);
|
34 | }
|
35 | copyAssets(configuration, appName, outDir, watchAssetsMode) {
|
36 | const assets = (0, get_value_or_default_1.getValueOrDefault)(configuration, 'compilerOptions.assets', appName) || [];
|
37 | if (assets.length <= 0) {
|
38 | return;
|
39 | }
|
40 | try {
|
41 | let sourceRoot = (0, get_value_or_default_1.getValueOrDefault)(configuration, 'sourceRoot', appName);
|
42 | sourceRoot = (0, path_1.join)(process.cwd(), sourceRoot);
|
43 | const filesToCopy = assets.map((item) => {
|
44 | if (typeof item === 'string') {
|
45 | return {
|
46 | glob: (0, path_1.join)(sourceRoot, item),
|
47 | outDir,
|
48 | };
|
49 | }
|
50 | return {
|
51 | outDir: item.outDir || outDir,
|
52 | glob: (0, path_1.join)(sourceRoot, item.include),
|
53 | exclude: item.exclude ? (0, path_1.join)(sourceRoot, item.exclude) : undefined,
|
54 | flat: item.flat,
|
55 | watchAssets: item.watchAssets,
|
56 | };
|
57 | });
|
58 | const isWatchEnabled = (0, get_value_or_default_1.getValueOrDefault)(configuration, 'compilerOptions.watchAssets', appName) || watchAssetsMode;
|
59 | for (const item of filesToCopy) {
|
60 | const option = {
|
61 | action: 'change',
|
62 | item,
|
63 | path: '',
|
64 | sourceRoot,
|
65 | watchAssetsMode: isWatchEnabled,
|
66 | };
|
67 |
|
68 | const watcher = chokidar
|
69 | .watch(item.glob, { ignored: item.exclude })
|
70 | .on('add', (path) => this.actionOnFile(Object.assign(Object.assign({}, option), { path, action: 'change' })))
|
71 | .on('change', (path) => this.actionOnFile(Object.assign(Object.assign({}, option), { path, action: 'change' })))
|
72 | .on('unlink', (path) => this.actionOnFile(Object.assign(Object.assign({}, option), { path, action: 'unlink' })));
|
73 | this.watchers.push(watcher);
|
74 | }
|
75 | }
|
76 | catch (err) {
|
77 | throw new Error(`An error occurred during the assets copying process. ${err.message}`);
|
78 | }
|
79 | }
|
80 | actionOnFile(option) {
|
81 | const { action, item, path, sourceRoot, watchAssetsMode } = option;
|
82 | const isWatchEnabled = watchAssetsMode || item.watchAssets;
|
83 |
|
84 | if (!isWatchEnabled && this.watchAssetsKeyValue[path]) {
|
85 | return;
|
86 | }
|
87 |
|
88 | this.watchAssetsKeyValue[path] = true;
|
89 |
|
90 | this.actionInProgress = true;
|
91 | const dest = (0, copy_path_resolve_1.copyPathResolve)(path, item.outDir, sourceRoot.split(path_1.sep).length);
|
92 |
|
93 | if (action === 'change') {
|
94 | shell.mkdir('-p', (0, path_1.dirname)(dest));
|
95 | shell.cp(path, dest);
|
96 | }
|
97 | else if (action === 'unlink') {
|
98 |
|
99 | shell.rm(dest);
|
100 | }
|
101 | }
|
102 | }
|
103 | exports.AssetsManager = AssetsManager;
|