feat(web): use daemon file-watcher for file-server executors (#14284)
This commit is contained in:
parent
885765ef96
commit
660f0a6d60
@ -31,10 +31,8 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"chalk": "^4.1.0",
|
"chalk": "^4.1.0",
|
||||||
"chokidar": "^3.5.1",
|
|
||||||
"detect-port": "^1.5.1",
|
"detect-port": "^1.5.1",
|
||||||
"http-server": "^14.1.0",
|
"http-server": "^14.1.0",
|
||||||
"ignore": "^5.0.4",
|
|
||||||
"tslib": "^2.3.0",
|
"tslib": "^2.3.0",
|
||||||
"@nx/devkit": "file:../devkit",
|
"@nx/devkit": "file:../devkit",
|
||||||
"@nx/js": "file:../js"
|
"@nx/js": "file:../js"
|
||||||
|
|||||||
@ -2,18 +2,16 @@ import { execFileSync, fork } from 'child_process';
|
|||||||
import * as chalk from 'chalk';
|
import * as chalk from 'chalk';
|
||||||
import {
|
import {
|
||||||
ExecutorContext,
|
ExecutorContext,
|
||||||
joinPathFragments,
|
|
||||||
parseTargetString,
|
parseTargetString,
|
||||||
readTargetOptions,
|
readTargetOptions,
|
||||||
} from '@nx/devkit';
|
} from '@nx/devkit';
|
||||||
import ignore from 'ignore';
|
import { copyFileSync, unlinkSync } from 'fs';
|
||||||
import { copyFileSync, readFileSync, unlinkSync } from 'fs';
|
|
||||||
import { Schema } from './schema';
|
import { Schema } from './schema';
|
||||||
import { watch } from 'chokidar';
|
|
||||||
import { platform } from 'os';
|
import { platform } from 'os';
|
||||||
import { join, resolve } from 'path';
|
import { join, resolve } from 'path';
|
||||||
import { readModulePackageJson } from 'nx/src/utils/package-json';
|
import { readModulePackageJson } from 'nx/src/utils/package-json';
|
||||||
import * as detectPort from 'detect-port';
|
import * as detectPort from 'detect-port';
|
||||||
|
import { daemonClient } from 'nx/src/daemon/client/client';
|
||||||
|
|
||||||
// platform specific command name
|
// platform specific command name
|
||||||
const pmCmd = platform() === 'win32' ? `npx.cmd` : 'npx';
|
const pmCmd = platform() === 'win32' ? `npx.cmd` : 'npx';
|
||||||
@ -89,33 +87,26 @@ function getBuildTargetOutputPath(options: Schema, context: ExecutorContext) {
|
|||||||
return outputPath;
|
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(
|
function createFileWatcher(
|
||||||
root: string,
|
project: string | undefined,
|
||||||
projectRoot: string,
|
|
||||||
changeHandler: () => void
|
changeHandler: () => void
|
||||||
): () => void {
|
) {
|
||||||
const ignoredGlobs = getIgnoredGlobs(root);
|
return daemonClient.registerFileWatcher(
|
||||||
|
{
|
||||||
const watcher = watch([joinPathFragments(projectRoot, '**')], {
|
watchProjects: project ? [project] : 'all',
|
||||||
cwd: root,
|
includeGlobalWorkspaceFiles: true,
|
||||||
ignoreInitial: true,
|
includeDependentProjects: true,
|
||||||
});
|
},
|
||||||
watcher.on('all', (_event: string, path: string) => {
|
async (error, { changedFiles }) => {
|
||||||
if (ignoredGlobs.ignores(path)) return;
|
if (error === 'closed') {
|
||||||
changeHandler();
|
throw new Error('Watch error: Daemon closed the connection');
|
||||||
});
|
} else if (error) {
|
||||||
return () => watcher.close();
|
throw new Error(`Watch error: ${error?.message ?? 'Unknown'}`);
|
||||||
|
} else if (changedFiles.length > 0) {
|
||||||
|
changeHandler();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function* fileServerExecutor(
|
export default async function* fileServerExecutor(
|
||||||
@ -146,7 +137,7 @@ export default async function* fileServerExecutor(
|
|||||||
if (options.watch) {
|
if (options.watch) {
|
||||||
const projectRoot =
|
const projectRoot =
|
||||||
context.projectsConfigurations.projects[context.projectName].root;
|
context.projectsConfigurations.projects[context.projectName].root;
|
||||||
disposeWatch = createFileWatcher(context.root, projectRoot, run);
|
disposeWatch = await createFileWatcher(context.projectName, run);
|
||||||
}
|
}
|
||||||
|
|
||||||
// perform initial run
|
// perform initial run
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user