fix(@angular/build): invalidate com.chrome.devtools.json if project is moved

Ensure that when a project is relocated, the `com.chrome.devtools.json` file is properly invalidated by checking the `root` path.
This commit is contained in:
Alan Agius 2025-03-11 13:05:00 +00:00
parent 5ff4c28e7b
commit 5bea3de4cb

View File

@ -6,18 +6,26 @@
* found in the LICENSE file at https://angular.dev/license
*/
import assert from 'node:assert';
import { randomUUID } from 'node:crypto';
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import type { Connect } from 'vite';
type DevToolsJson = {
workspace: {
root: string;
uuid: string;
};
};
const CHROME_DEVTOOLS_ROUTE = '/.well-known/appspecific/com.chrome.devtools.json';
export function createChromeDevtoolsMiddleware(
cacheDir: string,
projectRoot: string,
): Connect.NextHandleFunction {
let devtoolsConfig: string;
let devtoolsConfig: string | undefined;
const devtoolsConfigPath = join(cacheDir, 'com.chrome.devtools.json');
return function chromeDevtoolsMiddleware(req, res, next) {
@ -27,11 +35,14 @@ export function createChromeDevtoolsMiddleware(
return;
}
if (!devtoolsConfig) {
// We store the UUID and re-use it to ensure Chrome does not repeatedly ask for permissions when restarting the dev server.
try {
devtoolsConfig ??= readFileSync(devtoolsConfigPath, 'utf-8');
const devtoolsConfig = readFileSync(devtoolsConfigPath, 'utf-8');
const devtoolsConfigJson: DevToolsJson = JSON.parse(devtoolsConfig);
assert.equal(projectRoot, devtoolsConfigJson?.workspace.root);
} catch {
const devtoolsConfigJson = {
const devtoolsConfigJson: DevToolsJson = {
workspace: {
root: projectRoot,
uuid: randomUUID(),
@ -39,11 +50,13 @@ export function createChromeDevtoolsMiddleware(
};
devtoolsConfig = JSON.stringify(devtoolsConfigJson, undefined, 2);
try {
mkdirSync(cacheDir, { recursive: true });
writeFileSync(devtoolsConfigPath, devtoolsConfig);
} catch {}
}
}
res.setHeader('Content-Type', 'application/json');
res.end(devtoolsConfig);