Skip to content

Commit 36f013b

Browse files
author
Nicholas Pape
authored
Add support for PNPM (microsoft#449)
* Revert "Remove all PNPM references" This reverts commit f690716. * A few others that needed to be reverted * Add in PNPM stuff, resolve conflicts, rush generate * Refactor linking strategy * Remove debugging line * Add Pete's comment * PR feedback * PR feedback * PR Feedback and fixup the Shrinkwrap API * Add more comments * Add an example * PR Feedback * npm -> NPM and pnpm -> PNPM * Fix a few places where we are referring to npm commands
1 parent a3b5289 commit 36f013b

31 files changed

Lines changed: 829 additions & 147 deletions

‎apps/rush-lib/package.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
"glob": "~7.0.5",
2929
"glob-escape": "~0.0.1",
3030
"inquirer": "~1.2.1",
31+
"js-yaml": "~3.9.1",
3132
"jju": "~1.3.0",
3233
"lodash": "~4.15.0",
3334
"minimatch": "~3.0.2",
3435
"npm-package-arg": "~5.1.2",
3536
"read-package-tree": "~5.1.5",
3637
"rimraf": "~2.5.4",
3738
"semver": "~5.3.0",
39+
"strict-uri-encode": "~2.0.0",
3840
"tar": "~3.1.12",
3941
"wordwrap": "~1.0.0",
4042
"z-schema": "~3.18.3"
@@ -47,6 +49,7 @@
4749
"sinon": "~1.17.3",
4850
"@types/colors": "1.1.3",
4951
"@types/glob": "5.0.30",
52+
"@types/js-yaml": "3.9.1",
5053
"@types/lodash": "4.14.74",
5154
"@types/minimatch": "2.0.29",
5255
"@types/sinon": "1.16.34",

‎apps/rush-lib/src/RushConstants.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ export namespace RushConstants {
5858
*/
5959
export const npmShrinkwrapFilename: string = 'npm-shrinkwrap.json';
6060

61+
/**
62+
* The filename ("shrinkwrap.yaml") used to store state for pnpm
63+
*/
64+
export const pnpmShrinkwrapFilename: string = 'shrinkwrap.yaml';
65+
6166
/**
6267
* The folder name ("node_modules") where NPM installs its packages.
6368
*/

‎apps/rush-lib/src/cli/actions/GenerateAction.ts‎

Lines changed: 82 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as path from 'path';
77
import * as fsx from 'fs-extra';
88
import { CommandLineFlagParameter } from '@microsoft/ts-command-line';
99

10+
import { PackageManager } from '../../data/RushConfiguration';
1011
import Utilities from '../../utilities/Utilities';
1112
import { Stopwatch } from '../../utilities/Stopwatch';
1213
import InstallManager, { InstallType } from '../utilities/InstallManager';
@@ -23,6 +24,7 @@ export default class GenerateAction extends BaseRushAction {
2324
private _lazyParameter: CommandLineFlagParameter;
2425
private _noLinkParameter: CommandLineFlagParameter;
2526
private _forceParameter: CommandLineFlagParameter;
27+
private _cleanParameter: CommandLineFlagParameter;
2628

2729
constructor(parser: RushCommandLineParser) {
2830
super({
@@ -52,18 +54,38 @@ export default class GenerateAction extends BaseRushAction {
5254
parameterLongName: '--force',
5355
parameterShortName: '-f',
5456
description: 'Use this to bypass checking the shrinkwrap file, which forces rush generate to run even if all'
55-
+ ' dependencies already exist in the shrinkwrap file'
57+
+ ' dependencies already exist in the shrinkwrap file. Only applies when package manager is npm.'
5658
});
5759
this._noLinkParameter = this.defineFlagParameter({
5860
parameterLongName: '--no-link',
5961
description: 'Do not automatically run the "rush link" action after "rush generate"'
6062
});
63+
this._cleanParameter = this.defineFlagParameter({
64+
parameterLongName: '--clean',
65+
parameterShortName: '-c',
66+
description: 'When using pnpm, forces a non-incremental clean install which clears the node_module and pnpm'
67+
+ ' store. Use this if any store corruption has occurred.'
68+
});
6169
}
6270

6371
protected run(): void {
6472
const stopwatch: Stopwatch = Stopwatch.start();
6573
const isLazy: boolean = this._lazyParameter.value;
6674

75+
if (this._cleanParameter.value && this._lazyParameter.value) {
76+
throw new Error(`Cannot specify both --clean and --lazy, as these are mutually exclusive operations.`);
77+
}
78+
79+
if (this._lazyParameter.value && this.rushConfiguration.packageManager === 'pnpm') {
80+
console.warn(colors.yellow('The --lazy flag is not required for pnpm'
81+
+ ' because its algorithm inherently incorporates this optimization.'));
82+
}
83+
84+
if (this._cleanParameter.value && this.rushConfiguration.packageManager === 'npm') {
85+
console.warn(colors.yellow('The --clean flag is not required for npm'
86+
+ ' because its algorithm always performs a clean installation.'));
87+
}
88+
6789
ApprovedPackagesChecker.rewriteConfigFiles(this.rushConfiguration);
6890

6991
const installManager: InstallManager = new InstallManager(this.rushConfiguration);
@@ -104,47 +126,52 @@ export default class GenerateAction extends BaseRushAction {
104126
fsx.unlinkSync(tempShrinkwrapFilename);
105127
}
106128

107-
if (isLazy) {
108-
console.log(colors.green(
109-
`${os.EOL}Rush is running in "--lazy" mode. ` +
110-
`You will need to run a normal "rush generate" before committing.`));
129+
const packageManager: PackageManager = this.rushConfiguration.packageManager;
111130

112-
// Do an incremental install
113-
installManager.installCommonModules(InstallType.Normal);
131+
if (this.rushConfiguration.packageManager === 'pnpm') {
114132

115-
console.log(os.EOL + colors.bold('(Skipping "npm shrinkwrap")') + os.EOL);
116-
const packageLogFilePath: string = path.join(this.rushConfiguration.commonTempFolder, 'package.lock');
133+
// Do an incremental install unless --clean is specified
134+
installManager.installCommonModules(this._cleanParameter.value
135+
? InstallType.ForceClean
136+
: InstallType.Normal);
117137

118-
if (fsx.existsSync(packageLogFilePath)) {
119-
console.log('Removing NPM5\'s "package.lock" file');
120-
fsx.removeSync(packageLogFilePath);
121-
}
122-
} else {
123-
// Do a clean install
124-
installManager.installCommonModules(InstallType.ForceClean);
125-
126-
console.log(os.EOL + colors.bold('Running "npm shrinkwrap"...'));
127-
const npmArgs: string[] = ['shrinkwrap'];
128-
installManager.pushConfigurationArgs(npmArgs);
129-
Utilities.executeCommand(this.rushConfiguration.packageManagerToolFilename,
130-
npmArgs, this.rushConfiguration.commonTempFolder);
131-
console.log('"npm shrinkwrap" completed' + os.EOL);
132-
133-
// Copy (or delete) common\temp\npm-shrinkwrap.json --> common\npm-shrinkwrap.json
134-
installManager.syncFile(tempShrinkwrapFilename,
135-
this.rushConfiguration.committedShrinkwrapFilename);
136-
137-
// The flag file is normally created by installCommonModules(), but "rush install" will
138-
// compare its timestamp against the shrinkwrap file. Since we just generated a new
139-
// npm-shrinkwrap file, it's safe to bump the timestamp, which ensures that "rush install"
140-
// won't do anything immediately after "rush generate". This is a minor performance
141-
// optimization, but it helps people to understand the semantics of the commands.
142-
if (fsx.existsSync(installManager.commonNodeModulesMarkerFilename)) {
143-
fsx.writeFileSync(installManager.commonNodeModulesMarkerFilename, '');
138+
this._syncShrinkwrapAndCheckInstallFlag(installManager);
139+
140+
} else if (this.rushConfiguration.packageManager === 'npm') {
141+
142+
if (isLazy) {
143+
console.log(colors.green(
144+
`${os.EOL}Rush is running in "--lazy" mode. ` +
145+
`You will need to run a normal "rush generate" before committing.`));
146+
147+
// Do an incremental install
148+
installManager.installCommonModules(InstallType.Normal);
149+
150+
console.log(os.EOL + colors.bold('(Skipping "npm shrinkwrap")') + os.EOL);
151+
152+
// delete the automatically created npm5 "package.lock" file
153+
const packageLogFilePath: string = path.join(this.rushConfiguration.commonTempFolder, 'package.lock');
154+
if (fsx.existsSync(packageLogFilePath)) {
155+
console.log('Removing NPM 5\'s "package.lock" file');
156+
fsx.removeSync(packageLogFilePath);
157+
}
144158
} else {
145-
// Sanity check -- since we requested a clean install above, this should never occur
146-
throw new Error('The install flag file is missing');
159+
// Do a clean install
160+
installManager.installCommonModules(InstallType.ForceClean);
161+
162+
console.log(os.EOL + colors.bold('Running "npm shrinkwrap"...'));
163+
const npmArgs: string[] = ['shrinkwrap'];
164+
installManager.pushConfigurationArgs(npmArgs);
165+
Utilities.executeCommand(this.rushConfiguration.packageManagerToolFilename,
166+
npmArgs, this.rushConfiguration.commonTempFolder);
167+
console.log('"npm shrinkwrap" completed' + os.EOL);
168+
169+
this._syncShrinkwrapAndCheckInstallFlag(installManager);
147170
}
171+
172+
} else {
173+
// program bug
174+
throw new Error(`Program bug: invalid package manager "${packageManager}"`);
148175
}
149176

150177
stopwatch.stop();
@@ -160,4 +187,22 @@ export default class GenerateAction extends BaseRushAction {
160187
console.log(os.EOL + 'Next you should probably run: "rush link"');
161188
}
162189
}
190+
191+
private _syncShrinkwrapAndCheckInstallFlag(installManager: InstallManager): void {
192+
// Copy (or delete) common\temp\npm-shrinkwrap.json --> common\npm-shrinkwrap.json
193+
installManager.syncFile(this.rushConfiguration.tempShrinkwrapFilename,
194+
this.rushConfiguration.committedShrinkwrapFilename);
195+
196+
// The flag file is normally created by installCommonModules(), but "rush install" will
197+
// compare its timestamp against the shrinkwrap file. Since we just generated a new
198+
// npm-shrinkwrap file, it's safe to bump the timestamp, which ensures that "rush install"
199+
// won't do anything immediately after "rush generate". This is a minor performance
200+
// optimization, but it helps people to understand the semantics of the commands.
201+
if (fsx.existsSync(installManager.commonNodeModulesMarkerFilename)) {
202+
fsx.writeFileSync(installManager.commonNodeModulesMarkerFilename, '');
203+
} else {
204+
// Sanity check -- since we requested a clean install above, this should never occur
205+
throw new Error('The install flag file is missing');
206+
}
207+
}
163208
}

‎apps/rush-lib/src/cli/actions/PublishAction.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ export default class PublishAction extends BaseRushAction {
9696
parameterShortName: '-n',
9797
key: 'TOKEN',
9898
description:
99-
'Provide the default scope npm auth token to be passed into npm publish for global package publishing.'
99+
'Provide the default scope NPM auth token to be passed into npm publish for global package publishing.'
100100
});
101101
this._npmTag = this.defineStringParameter({
102102
parameterLongName: '--tag',
103103
parameterShortName: '-t',
104104
key: 'TAG',
105105
description:
106-
`The tag option to pass to npm publish. By default npm will publish using the 'latest' tag, even if ` +
106+
`The tag option to pass to npm publish. By default NPM will publish using the 'latest' tag, even if ` +
107107
`the package is older than the current latest, so in publishing workflows for older releases, providing ` +
108108
`a tag is important. When hotfix changes are made, this parameter defaults to 'hotfix'.`
109109
});
@@ -196,7 +196,7 @@ export default class PublishAction extends BaseRushAction {
196196
}
197197
}
198198

199-
// NPM publish the things that need publishing.
199+
// npm publish the things that need publishing.
200200
for (const change of orderedChanges) {
201201
if (change.changeType && change.changeType > ChangeType.dependency) {
202202
const project: RushConfigurationProject | undefined = allPackages.get(change.packageName);

‎apps/rush-lib/src/cli/actions/RushCommandLineParser.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default class RushCommandLineParser extends CommandLineParser {
3434
constructor() {
3535
super({
3636
toolFilename: 'rush',
37-
toolDescription: 'Rush helps you to manage a collection of NPM'
37+
toolDescription: 'Rush helps you to manage a collection of npm'
3838
+ ' projects. Rush collects the dependencies for all projects to perform a minimal install,'
3939
+ ' detects which projects can be locally linked, and performs a fast parallel'
4040
+ ' build according to the detected dependency graph. If you want to decompose'

‎apps/rush-lib/src/cli/actions/UnlinkAction.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default class UnlinkAction extends BaseRushAction {
1717
actionVerb: 'unlink',
1818
summary: 'Delete node_modules symlinks for all projects',
1919
documentation: 'This removes the symlinks created by the "rush link" command. This is useful for'
20-
+ ' cleaning a repo using "git clean" without accidentally deleting source files, or for using standard NPM'
20+
+ ' cleaning a repo using "git clean" without accidentally deleting source files, or for using standard npm'
2121
+ ' commands on a project.'
2222
});
2323
this._parser = parser;

‎apps/rush-lib/src/cli/utilities/InstallManager.ts‎

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ export default class InstallManager {
142142
}
143143

144144
/**
145-
* If the "npm-local" symlink hasn't been set up yet, this creates it, installing the
146-
* specified NPM version in the user's home directory if needed.
145+
* If the "(p)npm-local" symlink hasn't been set up yet, this creates it, installing the
146+
* specified (P)npm version in the user's home directory if needed.
147147
*/
148148
public ensureLocalPackageManager(forceReinstall: boolean): void {
149149
// Example: "C:\Users\YourName\.rush"
@@ -157,10 +157,10 @@ export default class InstallManager {
157157
const packageManager: PackageManager = this._rushConfiguration.packageManager;
158158
const packageManagerVersion: string = this._rushConfiguration.packageManagerToolVersion;
159159

160-
// Example: "C:\Users\YourName\.rush\npm-1.2.3"
160+
// Example: "C:\Users\YourName\.rush\pnpm-1.2.3"
161161
const packageManagerToolFolder: string = path.join(rushHomeFolder,
162162
`${packageManager}-${packageManagerVersion}`);
163-
// Example: "C:\Users\YourName\.rush\npm-1.2.3\last-install.flag"
163+
// Example: "C:\Users\YourName\.rush\pnpm-1.2.3\last-install.flag"
164164
const packageManagerToolFlagFile: string = path.join(packageManagerToolFolder, 'last-install.flag');
165165

166166
// NOTE: We don't care about the timestamp for last-install.flag, because nobody will change
@@ -188,7 +188,7 @@ export default class InstallManager {
188188
fsx.mkdirsSync(this._rushConfiguration.commonTempFolder);
189189
}
190190

191-
// Example: "C:\MyRepo\common\temp\npm-local"
191+
// Example: "C:\MyRepo\common\temp\pnpm-local"
192192
const localPackageManagerToolFolder: string =
193193
path.join(this._rushConfiguration.commonTempFolder, `${packageManager}-local`);
194194
if (fsx.existsSync(localPackageManagerToolFolder)) {
@@ -251,7 +251,7 @@ export default class InstallManager {
251251
if (shrinkwrapFile) {
252252
// Check any pinned dependencies first
253253
pinnedVersions.forEach((version: string, dependency: string) => {
254-
if (!shrinkwrapFile.hasCompatibleDependency(dependency, version)) {
254+
if (!shrinkwrapFile.hasCompatibleTopLevelDependency(dependency, version)) {
255255
console.log(colors.yellow(wrap(
256256
`${os.EOL}The NPM shrinkwrap file does not provide "${dependency}"`
257257
+ ` (${version}) required by pinned versions`)));
@@ -527,12 +527,18 @@ export default class InstallManager {
527527
return;
528528
}
529529
} else {
530-
console.log(`Deleting the "npm-cache" folder`);
531-
// This is faster and more thorough than "npm cache clean"
532-
this._asyncRecycler.moveFolder(this._rushConfiguration.npmCacheFolder);
530+
if (this._rushConfiguration.packageManager === 'npm') {
531+
console.log(`Deleting the "npm-cache" folder`);
532+
// This is faster and more thorough than "npm cache clean"
533+
this._asyncRecycler.moveFolder(this._rushConfiguration.npmCacheFolder);
533534

534-
console.log(`Deleting the "npm-tmp" folder`);
535-
this._asyncRecycler.moveFolder(this._rushConfiguration.npmTmpFolder);
535+
console.log(`Deleting the "npm-tmp" folder`);
536+
this._asyncRecycler.moveFolder(this._rushConfiguration.npmTmpFolder);
537+
538+
} else {
539+
console.log(`Deleting the "pnpm-store" folder`);
540+
this._asyncRecycler.moveFolder(this._rushConfiguration.pnpmStoreFolder);
541+
}
536542
}
537543

538544
if (fsx.existsSync(this.commonNodeModulesMarkerFilename)) {
@@ -566,12 +572,15 @@ export default class InstallManager {
566572
} else {
567573
// NO: Do an incremental install in the "node_modules" folder
568574

569-
console.log(`Running "${this._rushConfiguration.packageManager} prune"`
570-
+ ` in ${this._rushConfiguration.commonTempFolder}`);
571-
const args: string[] = ['prune'];
572-
this.pushConfigurationArgs(args);
573-
Utilities.executeCommandWithRetry(packageManagerFilename, args, MAX_INSTALL_ATTEMPTS,
574-
this._rushConfiguration.commonTempFolder);
575+
// note: it is not necessary to run "prune" with pnpm
576+
if (this._rushConfiguration.packageManager === 'npm') {
577+
console.log(`Running "${this._rushConfiguration.packageManager} prune"`
578+
+ ` in ${this._rushConfiguration.commonTempFolder}`);
579+
const args: string[] = ['prune'];
580+
this.pushConfigurationArgs(args);
581+
Utilities.executeCommandWithRetry(packageManagerFilename, args, MAX_INSTALL_ATTEMPTS,
582+
this._rushConfiguration.commonTempFolder);
583+
}
575584

576585
// Delete the (installed image of) the temp projects, since "npm install" does not
577586
// detect changes for "file:./" references.
@@ -599,7 +608,7 @@ export default class InstallManager {
599608

600609
// Run "npm install" in the common folder
601610

602-
// NOTE FOR NPM:
611+
// NOTE FOR npm:
603612
// we do NOT install optional dependencies for Rush, as it seems that optional dependencies do not
604613
// work properly with shrinkwrap. Consider the "fsevents" package. This is a Mac specific package
605614
// which is an optional second-order dependency. Optional dependencies work by attempting to install
@@ -614,8 +623,14 @@ export default class InstallManager {
614623
// One possible solution would be to have the shrinkwrap include information about whether the dependency
615624
// is optional or not, but it does not appear to do so. Also, this would result in strange behavior where
616625
// people would have different node_modules based on their system.
626+
//
627+
// NOTE FOR pnpm:
628+
// PNPM does not appear to support the --no-optional flag at the moment
617629

618-
const installArgs: string[] = ['install', '--no-optional'];
630+
const installArgs: string[] = ['install'];
631+
if (this._rushConfiguration.packageManager === 'npm') {
632+
installArgs.push('--no-optional');
633+
}
619634
this.pushConfigurationArgs(installArgs);
620635

621636
console.log(os.EOL + colors.bold(`Running "${this._rushConfiguration.packageManager} install" in`
@@ -640,8 +655,19 @@ export default class InstallManager {
640655
* to the command-line.
641656
*/
642657
public pushConfigurationArgs(args: string[]): void {
643-
args.push('--cache', this._rushConfiguration.npmCacheFolder);
644-
args.push('--tmp', this._rushConfiguration.npmTmpFolder);
658+
if (this._rushConfiguration.packageManager === 'npm') {
659+
args.push('--cache', this._rushConfiguration.npmCacheFolder);
660+
args.push('--tmp', this._rushConfiguration.npmTmpFolder);
661+
} else if (this._rushConfiguration.packageManager === 'pnpm') {
662+
args.push('--store', this._rushConfiguration.pnpmStoreFolder);
663+
664+
// we are using the --no-lock flag for now, which unfortunately prints a warning, but should be OK
665+
// since rush already has its own install lock file which will invalidate the cache for us.
666+
// we theoretically could use the lock file, but we would need to clean the store if the
667+
// lockfile existed, otherwise PNPM would hang indefinitely. it is simpler to rely on Rush's
668+
// last install flag, which encapsulates the entire installation
669+
args.push('--no-lock');
670+
}
645671
}
646672

647673
/**

‎apps/rush-lib/src/cli/utilities/LinkManagerFactory.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import RushConfiguration from '../../data/RushConfiguration';
22
import { BaseLinkManager } from './base/BaseLinkManager';
33
import { NpmLinkManager } from './npm/NpmLinkManager';
4+
import { PnpmLinkManager } from './pnpm/PnpmLinkManager';
45

56
export class LinkManagerFactory {
67
public static getLinkManager(rushConfiguration: RushConfiguration): BaseLinkManager {
78
if (rushConfiguration.packageManager === 'npm') {
89
return new NpmLinkManager(rushConfiguration);
10+
} else if (rushConfiguration.packageManager === 'pnpm') {
11+
return new PnpmLinkManager(rushConfiguration);
912
}
1013
throw new Error(`Invalid package manager: ${rushConfiguration.packageManager}`);
1114
}

0 commit comments

Comments
 (0)