From 660f0a6d60cbde234ae4e2728dc3ecec39b2cc78 Mon Sep 17 00:00:00 2001 From: Phillip Barta Date: Wed, 30 Aug 2023 15:27:17 +0200 Subject: [PATCH] feat(web): use daemon file-watcher for file-server executors (#14284) --- packages/web/package.json | 2 - .../executors/file-server/file-server.impl.ts | 51 ++++++++----------- 2 files changed, 21 insertions(+), 32 deletions(-) diff --git a/packages/web/package.json b/packages/web/package.json index de0a32e3029af..301b47700eac1 100644 --- a/packages/web/package.json +++ b/packages/web/package.json @@ -31,10 +31,8 @@ }, "dependencies": { "chalk": "^4.1.0", - "chokidar": "^3.5.1", "detect-port": "^1.5.1", "http-server": "^14.1.0", - "ignore": "^5.0.4", "tslib": "^2.3.0", "@nx/devkit": "file:../devkit", "@nx/js": "file:../js" diff --git a/packages/web/src/executors/file-server/file-server.impl.ts b/packages/web/src/executors/file-server/file-server.impl.ts index d2995326c9d7e..f09afa45e1c60 100644 --- a/packages/web/src/executors/file-server/file-server.impl.ts +++ b/packages/web/src/executors/file-server/file-server.impl.ts @@ -2,18 +2,16 @@ import { execFileSync, fork } from 'child_process'; import * as chalk from 'chalk'; import { ExecutorContext, - joinPathFragments, parseTargetString, readTargetOptions, } from '@nx/devkit'; -import ignore from 'ignore'; -import { copyFileSync, readFileSync, unlinkSync } from 'fs'; +import { copyFileSync, unlinkSync } from 'fs'; import { Schema } from './schema'; -import { watch } from 'chokidar'; import { platform } from 'os'; import { join, resolve } from 'path'; import { readModulePackageJson } from 'nx/src/utils/package-json'; import * as detectPort from 'detect-port'; +import { daemonClient } from 'nx/src/daemon/client/client'; // platform specific command name const pmCmd = platform() === 'win32' ? `npx.cmd` : 'npx'; @@ -89,33 +87,26 @@ function getBuildTargetOutputPath(options: Schema, context: ExecutorContext) { return outputPath; } -function getIgnoredGlobs(root: string) { - const ig = ignore(); - try { - ig.add(readFileSync(`${root}/.gitignore`, 'utf-8')); - } catch {} - try { - ig.add(readFileSync(`${root}/.nxignore`, 'utf-8')); - } catch {} - return ig; -} - function createFileWatcher( - root: string, - projectRoot: string, + project: string | undefined, changeHandler: () => void -): () => void { - const ignoredGlobs = getIgnoredGlobs(root); - - const watcher = watch([joinPathFragments(projectRoot, '**')], { - cwd: root, - ignoreInitial: true, - }); - watcher.on('all', (_event: string, path: string) => { - if (ignoredGlobs.ignores(path)) return; - changeHandler(); - }); - return () => watcher.close(); +) { + return daemonClient.registerFileWatcher( + { + watchProjects: project ? [project] : 'all', + includeGlobalWorkspaceFiles: true, + includeDependentProjects: true, + }, + async (error, { changedFiles }) => { + if (error === 'closed') { + throw new Error('Watch error: Daemon closed the connection'); + } else if (error) { + throw new Error(`Watch error: ${error?.message ?? 'Unknown'}`); + } else if (changedFiles.length > 0) { + changeHandler(); + } + } + ); } export default async function* fileServerExecutor( @@ -146,7 +137,7 @@ export default async function* fileServerExecutor( if (options.watch) { const projectRoot = context.projectsConfigurations.projects[context.projectName].root; - disposeWatch = createFileWatcher(context.root, projectRoot, run); + disposeWatch = await createFileWatcher(context.projectName, run); } // perform initial run