Skip to content

Commit

Permalink
Improve typesafety in PnpmfileShim.
Browse files Browse the repository at this point in the history
iclanton committed Jul 3, 2020
1 parent 1771839 commit 0e8d05a
Showing 3 changed files with 34 additions and 31 deletions.
29 changes: 15 additions & 14 deletions apps/rush-lib/src/logic/installManager/WorkspaceInstallManager.ts
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ import { Utilities } from '../../utilities/Utilities';
import { InstallHelpers } from './InstallHelpers';
import { CommonVersionsConfiguration } from '../../api/CommonVersionsConfiguration';
import { RepoStateFile } from '../RepoStateFile';
import { IPnpmfileShimSettings } from '../pnpm/IPnpmfileShimSettings';

/**
* This class implements common logic between "rush install" and "rush update".
@@ -434,21 +435,21 @@ export class WorkspaceInstallManager extends BaseInstallManager {
}
}

const pnpmfileShimSettings: IPnpmfileShimSettings = {
allPreferredVersions: MapExtensions.toObject(
InstallHelpers.collectPreferredVersions(this.rushConfiguration, this.options)
),
allowedAlternativeVersions: MapExtensions.toObject(
this.rushConfiguration.getCommonVersions(this.options.variant).allowedAlternativeVersions
),
semverPath: require.resolve('semver'),
useClientPnpmfile: pnpmfileExists
};

// Write the settings to be consumed by the pnpmfile
await JsonFile.saveAsync(
{
allPreferredVersions: MapExtensions.toObject(
InstallHelpers.collectPreferredVersions(this.rushConfiguration, this.options)
),
allowedAlternativeVersions: MapExtensions.toObject(
this.rushConfiguration.getCommonVersions(this.options.variant).allowedAlternativeVersions
),
semverPath: require.resolve('semver'),
useClientPnpmfile: pnpmfileExists
},
path.resolve(pnpmfileDir, 'pnpmfileSettings.json'),
{ ensureFolderExists: true }
);
await JsonFile.saveAsync(pnpmfileShimSettings, path.resolve(pnpmfileDir, 'pnpmfileSettings.json'), {
ensureFolderExists: true
});

// Copy the shim pnpmfile to the original path
await FileSystem.copyFileAsync({
9 changes: 9 additions & 0 deletions apps/rush-lib/src/logic/pnpm/IPnpmfileShimSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

export interface IPnpmfileShimSettings {
allPreferredVersions: { [dependencyName: string]: string };
allowedAlternativeVersions: { [dependencyName: string]: ReadonlyArray<string> };
semverPath: string;
useClientPnpmfile: boolean;
}
27 changes: 10 additions & 17 deletions apps/rush-lib/src/logic/pnpm/PnpmfileShim.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

interface ILockfile {}
// Uncomment "/* type */" when we upgrade to TS 3.9
import { /* type */ IPackageJson } from '@rushstack/node-core-library';
import { /* type */ IPnpmfileShimSettings } from './IPnpmfileShimSettings';
import /* type */ * as TSemver from 'semver';

interface IPackageJson {
dependencies?: { [dependencyName: string]: string };
devDependencies?: { [dependencyName: string]: string };
optionalDependencies?: { [dependencyName: string]: string };
}
interface ILockfile {}

interface IPnpmfile {
hooks?: {
@@ -18,18 +17,10 @@ interface IPnpmfile {
};
}

interface IPnpmfileSettings {
allPreferredVersions: { [dependencyName: string]: string };
allowedAlternativeVersions: { [dependencyName: string]: string[] };
semverPath: string;
useClientPnpmfile: boolean;
}

// Load in the generated settings file
const pnpmfileSettings: IPnpmfileSettings = require('./pnpmfileSettings.json');
const pnpmfileSettings: IPnpmfileShimSettings = require('./pnpmfileSettings.json');
// We will require semver from this path on disk, since this is the version of semver shipping with Rush
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const semver: any = require(pnpmfileSettings.semverPath);
const semver: typeof TSemver = require(pnpmfileSettings.semverPath);
// Only require the client pnpmfile if requested
const clientPnpmfile: IPnpmfile | undefined = pnpmfileSettings.useClientPnpmfile
? require('./clientPnpmfile')
@@ -44,7 +35,8 @@ function setPreferredVersions(dependencies?: { [dependencyName: string]: string
const preferredVersion: string = pnpmfileSettings.allPreferredVersions[name];
const version: string = dependencies![name];
if (pnpmfileSettings.allowedAlternativeVersions.hasOwnProperty(name)) {
const allowedAlternatives: string[] | undefined = pnpmfileSettings.allowedAlternativeVersions[name];
const allowedAlternatives: ReadonlyArray<string> | undefined =
pnpmfileSettings.allowedAlternativeVersions[name];
if (allowedAlternatives && allowedAlternatives.indexOf(version) > -1) {
continue;
}
@@ -55,6 +47,7 @@ function setPreferredVersions(dependencies?: { [dependencyName: string]: string
} catch {
// Swallow invalid range errors
}

if (isValidRange && semver.subset(preferredVersion, version)) {
dependencies![name] = preferredVersion;
}

0 comments on commit 0e8d05a

Please sign in to comment.