@@ -7,6 +7,7 @@ import * as path from 'path';
77import * as fsx from 'fs-extra' ;
88import { CommandLineFlagParameter } from '@microsoft/ts-command-line' ;
99
10+ import { PackageManager } from '../../data/RushConfiguration' ;
1011import Utilities from '../../utilities/Utilities' ;
1112import { Stopwatch } from '../../utilities/Stopwatch' ;
1213import 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}
0 commit comments