@rushstack/lockfile-explorer
Version:
Rush Lockfile Explorer: The UI for solving version conflicts quickly in a large monorepo
86 lines • 3.34 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { Worker } from 'node:worker_threads';
import * as path from 'node:path';
/**
* Evals `.pnpmfile.cjs` in an isolated thread, so `transformPackageAsync()` can be used to rewrite
* package.json files. Calling `disposeAsync()` will free the loaded modules.
*/
export class PnpmfileRunner {
constructor(pnpmfilePath) {
this._nextId = 1000;
this._promisesById = new Map();
this._disposed = false;
this.logger = undefined;
this._worker = new Worker(path.join(`${__dirname}/pnpmfileRunnerWorkerThread.js`), {
workerData: { pnpmfilePath }
});
this._worker.on('message', (message) => {
const id = message.id;
const promise = this._promisesById.get(id);
if (!promise) {
return;
}
if (message.kind === 'return') {
this._promisesById.delete(id);
// TODO: Validate the user's readPackage() return value
const result = message.result;
promise.resolve(result);
}
else if (message.kind === 'log') {
// No this._promisesById.delete(id) for this case
if (this.logger) {
this.logger(message.log);
}
else {
console.log('.pnpmfile.cjs: ' + message.log);
}
}
else {
this._promisesById.delete(id);
promise.reject(new Error(message.error || 'An unknown error occurred'));
}
});
this._worker.on('error', (err) => {
for (const promise of this._promisesById.values()) {
promise.reject(err);
}
this._promisesById.clear();
});
this._worker.on('exit', (code) => {
if (!this._disposed) {
const error = new Error(`PnpmfileRunner worker thread terminated unexpectedly with exit code ${code}`);
console.error(error);
for (const promise of this._promisesById.values()) {
promise.reject(error);
}
this._promisesById.clear();
}
});
}
/**
* Invokes the readPackage() hook from .pnpmfile.cjs
*/
transformPackageAsync(packageJson, packageJsonFullPath) {
if (this._disposed) {
return Promise.reject(new Error('The operation failed because PnpmfileRunner has been disposed'));
}
const id = this._nextId++;
return new Promise((resolve, reject) => {
this._promisesById.set(id, { resolve, reject });
this._worker.postMessage({ id, packageJson, packageJsonFullPath });
});
}
async disposeAsync() {
if (this._disposed) {
return;
}
for (const pending of this._promisesById.values()) {
pending.reject(new Error('Aborted because PnpmfileRunner was disposed'));
}
this._promisesById.clear();
this._disposed = true;
await this._worker.terminate();
}
}
//# sourceMappingURL=PnpmfileRunner.js.map