Skip to content

Commit

Permalink
feat: Support Yarn PnP
Browse files Browse the repository at this point in the history
  • Loading branch information
jclab-joseph committed Dec 14, 2023
1 parent 5fa7ff5 commit 2888d82
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/api/core/src/api/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export default autoTrace(
} as NodeJS.ProcessEnv,
};

spawnOpts.env.NODE_OPTIONS = removePnpLoaderArguments(spawnOpts.env.NODE_OPTIONS);
if (runAsNode) {
spawnOpts.env.ELECTRON_RUN_AS_NODE = 'true';
} else {
Expand Down Expand Up @@ -231,3 +232,11 @@ export default autoTrace(
return spawned;
}
);

function removePnpLoaderArguments(input: string | undefined): string | undefined {
if (!input) return input;
return input.replace(
/((--require\s+[^"].+\.pnp\.cjs)|(--experimental-loader\s+[^"].+\.pnp\.loader\.mjs)|(--require\s+".+\.pnp\.cjs")|(--experimental-loader\s+".+\.pnp\.loader\.mjs")) ?/g,
''
);
}
37 changes: 37 additions & 0 deletions packages/api/core/test/fast/start_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,41 @@ describe('start', () => {
})
).to.eventually.equal(fakeChild);
});

it(`should spawn remove pnp environment variable in NODE_OPTIONS`, async () => {
resolveStub.returnsArg(0);
const oldNodeOptions = process.env.NODE_OPTIONS;
try {
process.env.NODE_OPTIONS = '--require /some/workspace/.pnp.cjs --experimental-loader file:///some/workspace/.pnp.loader.mjs';
await start({
dir: __dirname,
interactive: false,
});
expect(spawnStub.firstCall.args[2].env.NODE_OPTIONS).to.equal('');
} finally {
process.env.NODE_OPTIONS = oldNodeOptions;
}
});

const testNodeOptions = [
'--hello --require /some/workspace/.pnp.cjs --experimental-loader file:///some/workspace/.pnp.loader.mjs --require /some/debugConnector.js --world --some args',
'--hello --require "/some/space path/.pnp.cjs" --experimental-loader "file:///some/space path/.pnp.loader.mjs" --require /some/debugConnector.js --world --some args',
'--hello --require "C:\\\\some space path\\\\.pnp.cjs" --experimental-loader "file:///c:/some space path/.pnp.loader.mjs" --require /some/debugConnector.js --world --some args',
];
for (let i = 0; i < testNodeOptions.length; i++) {
it(`should spawn remove pnp environment variable in NODE_OPTIONS case ${i}`, async () => {
resolveStub.returnsArg(0);
const oldNodeOptions = process.env.NODE_OPTIONS;
try {
process.env.NODE_OPTIONS = testNodeOptions[i];
await start({
dir: __dirname,
interactive: false,
});
expect(spawnStub.lastCall.args[2].env.NODE_OPTIONS).to.equal('--hello --require /some/debugConnector.js --world --some args');
} finally {
process.env.NODE_OPTIONS = oldNodeOptions;
}
});
}
});
7 changes: 7 additions & 0 deletions packages/utils/core-utils/src/electron-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ function getElectronModuleName(packageJSON: PackageJSONWithDeps): string {
async function getElectronPackageJSONPath(dir: string, packageName: string): Promise<string | undefined> {
const nodeModulesPath = await determineNodeModulesPath(dir, packageName);
if (!nodeModulesPath) {
try {
// Yarn PnP
// eslint-disable-next-line node/no-missing-require
return require.resolve('electron/package.json');
} catch (e) {
// Ignore
}
throw new PackageNotFoundError(packageName, dir);
}

Expand Down

0 comments on commit 2888d82

Please sign in to comment.