From bb8f35d1d87301dec170b35c9e7f503099437524 Mon Sep 17 00:00:00 2001 From: Kamil Mysliwiec Date: Thu, 29 Dec 2022 16:07:47 +0100 Subject: [PATCH] Revert "fix(): copy all assets on build" --- actions/build.action.ts | 1 + lib/compiler/assets-manager.ts | 26 +++++++++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/actions/build.action.ts b/actions/build.action.ts index 84e19fca8..7553afc0e 100644 --- a/actions/build.action.ts +++ b/actions/build.action.ts @@ -152,6 +152,7 @@ export class BuildAction extends AbstractAction { ); } else { this.compiler.run(configuration, pathToTsconfig, appName, onSuccess); + this.assetsManager.closeWatchers(); } } diff --git a/lib/compiler/assets-manager.ts b/lib/compiler/assets-manager.ts index fe1b3384f..51cd5f09b 100644 --- a/lib/compiler/assets-manager.ts +++ b/lib/compiler/assets-manager.ts @@ -13,9 +13,27 @@ import { getValueOrDefault } from './helpers/get-value-or-default'; export class AssetsManager { private watchAssetsKeyValue: { [key: string]: boolean } = {}; private watchers: chokidar.FSWatcher[] = []; + private actionInProgress = false; + /** + * Using on `nest build` to close file watch or the build process will not end + * Interval like process + * If no action has been taken recently close watchers + * If action has been taken recently flag and try again + */ public closeWatchers() { - this.watchers.forEach((watcher) => watcher.close()); + // Consider adjusting this for larger files + const timeoutMs = 500; + const closeFn = () => { + if (this.actionInProgress) { + this.actionInProgress = false; + setTimeout(closeFn, timeoutMs); + } else { + this.watchers.forEach((watcher) => watcher.close()); + } + }; + + setTimeout(closeFn, timeoutMs); } public copyAssets( @@ -78,10 +96,6 @@ export class AssetsManager { .on('change', (path: string) => this.actionOnFile({ ...option, path, action: 'change' })) .on('unlink', (path: string) => this.actionOnFile({ ...option, path, action: 'unlink' })); - if (!isWatchEnabled) { - watcher.on('ready', () => watcher.close()); - } - this.watchers.push(watcher); } } catch (err) { @@ -101,6 +115,8 @@ export class AssetsManager { } // Set path value to true for watching the first time this.watchAssetsKeyValue[path] = true; + // Set action to true to avoid watches getting cutoff + this.actionInProgress = true; const dest = copyPathResolve( path,