UNPKG

4.52 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.AssetsManager = void 0;
4const chokidar = require("chokidar");
5const path_1 = require("path");
6const shell = require("shelljs");
7const copy_path_resolve_1 = require("./helpers/copy-path-resolve");
8const get_value_or_default_1 = require("./helpers/get-value-or-default");
9class AssetsManager {
10 constructor() {
11 this.watchAssetsKeyValue = {};
12 this.watchers = [];
13 this.actionInProgress = false;
14 }
15 /**
16 * Using on `nest build` to close file watch or the build process will not end
17 * Interval like process
18 * If no action has been taken recently close watchers
19 * If action has been taken recently flag and try again
20 */
21 closeWatchers() {
22 // Consider adjusting this for larger files
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 // prettier-ignore
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 // Allow to do action for the first time before check watchMode
84 if (!isWatchEnabled && this.watchAssetsKeyValue[path]) {
85 return;
86 }
87 // Set path value to true for watching the first time
88 this.watchAssetsKeyValue[path] = true;
89 // Set action to true to avoid watches getting cutoff
90 this.actionInProgress = true;
91 const dest = (0, copy_path_resolve_1.copyPathResolve)(path, item.outDir, sourceRoot.split(path_1.sep).length);
92 // Copy to output dir if file is changed or added
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 // Remove from output dir if file is deleted
99 shell.rm(dest);
100 }
101 }
102}
103exports.AssetsManager = AssetsManager;