Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(web): use daemon file-watcher for file-server executors #14284

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
51 changes: 21 additions & 30 deletions packages/web/src/executors/file-server/file-server.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down