fix(misc): make Nx 16 migration safer when treee throws an exception (#16782)

This commit is contained in:
Craigory Coppola 2023-05-05 15:46:09 -04:00 committed by GitHub
parent c7ce767298
commit 95594d21f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 9 deletions

View File

@ -4,6 +4,7 @@ import { requireNx } from '../../nx';
import { visitNotIgnoredFiles } from '../generators/visit-not-ignored-files';
import { basename } from 'path';
import { isBinaryPath } from './binary-extensions';
const { logger } = requireNx();
const {
getProjects,
@ -162,15 +163,23 @@ function replaceMentions(
return;
}
const contents = tree.read(path).toString();
try {
const contents = tree.read(path).toString();
if (!contents.includes(oldPackageName)) {
return;
if (!contents.includes(oldPackageName)) {
return;
}
tree.write(
path,
contents.replace(new RegExp(oldPackageName, 'g'), newPackageName)
);
} catch {
// Its **probably** ok, contents can be null if the file is too large or
// there was an access exception.
logger.warn(
`An error was thrown when trying to update ${path}. If you believe the migration should have updated it, be sure to review the file and open an issue.`
);
}
tree.write(
path,
contents.replace(new RegExp(oldPackageName, 'g'), newPackageName)
);
});
}

View File

@ -1558,7 +1558,7 @@ async function runNxMigration(
name
);
const fn = require(implPath)[fnSymbol];
const host = new FsTree(root, false);
const host = new FsTree(root, process.env.NX_VERBOSE_LOGGING === 'true');
await fn(host, {});
host.lock();
const changes = host.listChanges();