Skip to content

chore(plugin-pnp): add enum NodeLinker #5848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .yarn/versions/bd149584.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/core": patch
"@yarnpkg/plugin-nm": patch
"@yarnpkg/plugin-pnp": patch
"@yarnpkg/plugin-pnpm": patch
6 changes: 3 additions & 3 deletions packages/plugin-nm/sources/NodeModulesLinker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {ZipOpenFS} from
import {buildNodeModulesTree} from '@yarnpkg/nm';
import {NodeModulesLocatorMap, buildLocatorMap, NodeModulesHoistingLimits} from '@yarnpkg/nm';
import {parseSyml} from '@yarnpkg/parsers';
import {jsInstallUtils} from '@yarnpkg/plugin-pnp';
import {NodeLinker, jsInstallUtils} from '@yarnpkg/plugin-pnp';
import {PnpApi, PackageInformation} from '@yarnpkg/pnp';
import cmdShim from '@zkochan/cmd-shim';
import {UsageError} from 'clipanion';
Expand Down Expand Up @@ -108,7 +108,7 @@ export class NodeModulesLinker implements Linker {
}

private isEnabled(opts: MinimalLinkOptions) {
return opts.project.configuration.get(`nodeLinker`) === `node-modules`;
return opts.project.configuration.get(`nodeLinker`) === NodeLinker.NODE_MODULES;
}
}

Expand Down Expand Up @@ -218,7 +218,7 @@ class NodeModulesInstaller implements Installer {
}

async finalizeInstall() {
if (this.opts.project.configuration.get(`nodeLinker`) !== `node-modules`)
if (this.opts.project.configuration.get(`nodeLinker`) !== NodeLinker.NODE_MODULES)
return undefined;

const defaultFsLayer = new VirtualFS({
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-pnp/sources/PnpLinker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {AliasFS, CwdFS, PortablePath, VirtualFS, npath, ppath, xfs}
import {generateInlinedScript, generateSplitScript, PackageRegistry, PnpApi, PnpSettings, getESMLoaderTemplate} from '@yarnpkg/pnp';
import {UsageError} from 'clipanion';

import {getPnpPath} from './index';
import {NodeLinker, getPnpPath} from './index';
import * as jsInstallUtils from './jsInstallUtils';
import * as pnpUtils from './pnpUtils';

Expand Down Expand Up @@ -76,7 +76,7 @@ export class PnpLinker implements Linker {
}

private isEnabled(opts: MinimalLinkOptions) {
if (opts.project.configuration.get(`nodeLinker`) !== `pnp`)
if (opts.project.configuration.get(`nodeLinker`) !== NodeLinker.PNP)
return false;

if (opts.project.configuration.get(`pnpMode`) !== this.mode)
Expand Down Expand Up @@ -239,7 +239,7 @@ export class PnpInstaller implements Installer {
if (!this.isEsmEnabled())
await xfs.removePromise(pnpPath.esmLoader);

if (this.opts.project.configuration.get(`nodeLinker`) !== `pnp`) {
if (this.opts.project.configuration.get(`nodeLinker`) !== NodeLinker.PNP) {
await xfs.removePromise(pnpPath.cjs);
await xfs.removePromise(pnpPath.data);
await xfs.removePromise(pnpPath.esmLoader);
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-pnp/sources/commands/unplug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {structUtils, semverUtils}
import {Command, Option, Usage, UsageError} from 'clipanion';
import micromatch from 'micromatch';

import {NodeLinker} from '../index';
import * as pnpUtils from '../pnpUtils';

// eslint-disable-next-line arca/no-default-export
Expand Down Expand Up @@ -71,7 +72,7 @@ export default class UnplugCommand extends BaseCommand {
if (!workspace)
throw new WorkspaceRequiredError(project.cwd, this.context.cwd);

if (configuration.get(`nodeLinker`) !== `pnp`)
if (configuration.get(`nodeLinker`) !== NodeLinker.PNP)
throw new UsageError(`This command can only be used if the \`nodeLinker\` option is set to \`pnp\``);

await project.restoreInstallState();
Expand Down
12 changes: 9 additions & 3 deletions packages/plugin-pnp/sources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async function setupScriptEnvironment(project: Project, env: {[key: string]: str

// We remove the PnP hook from NODE_OPTIONS because the process can have
// NODE_OPTIONS set while changing linkers, which affects build scripts.
if (project.configuration.get(`nodeLinker`) !== `pnp`) {
if (project.configuration.get(`nodeLinker`) !== NodeLinker.PNP) {
env.NODE_OPTIONS = nodeOptions;
return;
}
Expand Down Expand Up @@ -77,16 +77,22 @@ declare module '@yarnpkg/core' {
}
}

export enum NodeLinker {
PNP = `pnp`,
PNPM = `pnpm`,
NODE_MODULES = `node-modules`,
}

const plugin: Plugin<CoreHooks & StageHooks> = {
hooks: {
populateYarnPaths,
setupScriptEnvironment,
},
configuration: {
nodeLinker: {
description: `The linker used for installing Node packages, one of: "pnp", "node-modules"`,
description: `The linker used for installing Node packages, one of: ${Object.values(NodeLinker).map(v => `"${v}"`).join(`, `)}}`,
type: SettingsType.STRING,
default: `pnp`,
default: NodeLinker.PNP,
},
winLinkType: {
description: `Whether Yarn should use Windows Junctions or symlinks when creating links on Windows.`,
Expand Down
10 changes: 5 additions & 5 deletions packages/plugin-pnpm/sources/PnpmLinker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Descriptor, FetchResult, formatUtils, Installer, InstallPackageExtraApi, Linker, LinkOptions, LinkType, Locator, LocatorHash, Manifest, MessageName, MinimalLinkOptions, Package, Project, miscUtils, structUtils, WindowsLinkType} from '@yarnpkg/core';
import {Filename, PortablePath, setupCopyIndex, ppath, xfs, DirentNoPath} from '@yarnpkg/fslib';
import {jsInstallUtils} from '@yarnpkg/plugin-pnp';
import {NodeLinker, jsInstallUtils} from '@yarnpkg/plugin-pnp';
import {UsageError} from 'clipanion';

export type PnpmCustomData = {
Expand Down Expand Up @@ -76,7 +76,7 @@ export class PnpmLinker implements Linker {
}

private isEnabled(opts: MinimalLinkOptions) {
return opts.project.configuration.get(`nodeLinker`) === `pnpm`;
return opts.project.configuration.get(`nodeLinker`) === NodeLinker.PNPM;
}
}

Expand Down Expand Up @@ -170,7 +170,7 @@ class PnpmInstaller implements Installer {
}

async attachInternalDependencies(locator: Locator, dependencies: Array<[Descriptor, Locator]>) {
if (this.opts.project.configuration.get(`nodeLinker`) !== `pnpm`)
if (this.opts.project.configuration.get(`nodeLinker`) !== NodeLinker.PNPM)
return;

// We don't install those packages at all, because they can't be used anyway
Expand Down Expand Up @@ -264,7 +264,7 @@ class PnpmInstaller implements Installer {
async finalizeInstall() {
const storeLocation = getStoreLocation(this.opts.project);

if (this.opts.project.configuration.get(`nodeLinker`) !== `pnpm`) {
if (this.opts.project.configuration.get(`nodeLinker`) !== NodeLinker.PNPM) {
await xfs.removePromise(storeLocation);
} else {
let extraneous: Set<Filename>;
Expand Down Expand Up @@ -295,7 +295,7 @@ class PnpmInstaller implements Installer {
await this.asyncActions.wait();

await removeIfEmpty(storeLocation);
if (this.opts.project.configuration.get(`nodeLinker`) !== `node-modules`)
if (this.opts.project.configuration.get(`nodeLinker`) !== NodeLinker.NODE_MODULES)
await removeIfEmpty(getNodeModulesLocation(this.opts.project));

return {
Expand Down