Skip to content

Commit

Permalink
Merge pull request #1866 from nestjs/revert-1829-fix-build-assets
Browse files Browse the repository at this point in the history
Revert "fix(): copy all assets on build"
  • Loading branch information
kamilmysliwiec committed Dec 29, 2022
2 parents 4eb8384 + bb8f35d commit 4b73b95
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions actions/build.action.ts
Expand Up @@ -152,6 +152,7 @@ export class BuildAction extends AbstractAction {
);
} else {
this.compiler.run(configuration, pathToTsconfig, appName, onSuccess);
this.assetsManager.closeWatchers();
}
}

Expand Down
26 changes: 21 additions & 5 deletions lib/compiler/assets-manager.ts
Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand Down

0 comments on commit 4b73b95

Please sign in to comment.