11// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22// See LICENSE in the project root for license information.
33
4- import colors from 'colors/safe' ;
5- import * as path from 'path' ;
6-
7- import { FileSystem , type IPackageJson , JsonFile , LockFile , NewlineKind } from '@rushstack/node-core-library' ;
4+ import * as path from 'node:path' ;
5+
6+ import {
7+ FileSystem ,
8+ type IPackageJson ,
9+ JsonFile ,
10+ LockFile ,
11+ NewlineKind ,
12+ PackageName ,
13+ type IParsedPackageNameOrError
14+ } from '@rushstack/node-core-library' ;
15+ import { Colorize } from '@rushstack/terminal' ;
16+
17+ import { AsyncRecycler } from '../utilities/AsyncRecycler' ;
818import { Utilities } from '../utilities/Utilities' ;
9-
10- import { PackageName , type IParsedPackageNameOrError } from '@rushstack/node-core-library' ;
1119import type { RushConfiguration } from '../api/RushConfiguration' ;
1220import { PackageJsonEditor } from '../api/PackageJsonEditor' ;
1321import { InstallHelpers } from './installManager/InstallHelpers' ;
@@ -17,7 +25,7 @@ import { LastInstallFlag } from '../api/LastInstallFlag';
1725import { RushCommandLineParser } from '../cli/RushCommandLineParser' ;
1826import type { PnpmPackageManager } from '../api/packageManager/PnpmPackageManager' ;
1927
20- interface IAutoinstallerOptions {
28+ export interface IAutoinstallerOptions {
2129 autoinstallerName : string ;
2230 rushConfiguration : RushConfiguration ;
2331 rushGlobalFolder : RushGlobalFolder ;
@@ -79,7 +87,7 @@ export class Autoinstaller {
7987 ) ;
8088 }
8189
82- await InstallHelpers . ensureLocalPackageManager (
90+ await InstallHelpers . ensureLocalPackageManagerAsync (
8391 this . _rushConfiguration ,
8492 this . _rushGlobalFolder ,
8593 RushConstants . defaultMaxInstallAttempts ,
@@ -94,7 +102,7 @@ export class Autoinstaller {
94102
95103 this . _logIfConsoleOutputIsNotRestricted ( `Acquiring lock for "${ relativePathForLogs } " folder...` ) ;
96104
97- const lock : LockFile = await LockFile . acquire ( autoinstallerFullPath , 'autoinstaller' ) ;
105+ const lock : LockFile = await LockFile . acquireAsync ( autoinstallerFullPath , 'autoinstaller' ) ;
98106
99107 try {
100108 // Example: .../common/autoinstallers/my-task/.rush/temp
@@ -118,30 +126,39 @@ export class Autoinstaller {
118126 // Example: ../common/autoinstallers/my-task/node_modules
119127 const nodeModulesFolder : string = `${ autoinstallerFullPath } /${ RushConstants . nodeModulesFolderName } ` ;
120128 const flagPath : string = `${ nodeModulesFolder } /rush-autoinstaller.flag` ;
121- const isLastInstallFlagDirty : boolean = ! lastInstallFlag . isValid ( ) || ! FileSystem . exists ( flagPath ) ;
129+ const isLastInstallFlagDirty : boolean =
130+ ! ( await lastInstallFlag . isValidAsync ( ) ) || ! FileSystem . exists ( flagPath ) ;
122131
123132 if ( isLastInstallFlagDirty || lock . dirtyWhenAcquired ) {
124133 if ( FileSystem . exists ( nodeModulesFolder ) ) {
125134 this . _logIfConsoleOutputIsNotRestricted ( 'Deleting old files from ' + nodeModulesFolder ) ;
126- FileSystem . ensureEmptyFolder ( nodeModulesFolder ) ;
135+ const recycler = new AsyncRecycler (
136+ path . join ( this . _rushConfiguration . commonTempFolder , RushConstants . rushRecyclerFolderName )
137+ ) ;
138+ recycler . moveFolder ( nodeModulesFolder ) ;
139+ await recycler . startDeleteAllAsync ( ) ;
127140 }
128141
129142 // Copy: .../common/autoinstallers/my-task/.npmrc
130- Utilities . syncNpmrc ( this . _rushConfiguration . commonRushConfigFolder , autoinstallerFullPath ) ;
143+ Utilities . syncNpmrc ( {
144+ sourceNpmrcFolder : this . _rushConfiguration . commonRushConfigFolder ,
145+ targetNpmrcFolder : autoinstallerFullPath ,
146+ supportEnvVarFallbackSyntax : this . _rushConfiguration . isPnpm
147+ } ) ;
131148
132149 this . _logIfConsoleOutputIsNotRestricted (
133150 `Installing dependencies under ${ autoinstallerFullPath } ...\n`
134151 ) ;
135152
136- Utilities . executeCommand ( {
153+ await Utilities . executeCommandAsync ( {
137154 command : this . _rushConfiguration . packageManagerToolFilename ,
138155 args : [ 'install' , '--frozen-lockfile' ] ,
139156 workingDirectory : autoinstallerFullPath ,
140157 keepEnvironment : true
141158 } ) ;
142159
143160 // Create file: ../common/autoinstallers/my-task/.rush/temp/last-install.flag
144- lastInstallFlag . create ( ) ;
161+ await lastInstallFlag . createAsync ( ) ;
145162
146163 FileSystem . writeFile (
147164 flagPath ,
@@ -159,7 +176,7 @@ export class Autoinstaller {
159176 }
160177
161178 public async updateAsync ( ) : Promise < void > {
162- await InstallHelpers . ensureLocalPackageManager (
179+ await InstallHelpers . ensureLocalPackageManagerAsync (
163180 this . _rushConfiguration ,
164181 this . _rushGlobalFolder ,
165182 RushConstants . defaultMaxInstallAttempts ,
@@ -182,7 +199,7 @@ export class Autoinstaller {
182199 oldFileContents = FileSystem . readFile ( this . shrinkwrapFilePath , { convertLineEndings : NewlineKind . Lf } ) ;
183200 this . _logIfConsoleOutputIsNotRestricted ( 'Deleting ' + this . shrinkwrapFilePath ) ;
184201 await FileSystem . deleteFileAsync ( this . shrinkwrapFilePath ) ;
185- if ( this . _rushConfiguration . packageManager === 'pnpm' ) {
202+ if ( this . _rushConfiguration . isPnpm ) {
186203 // Workaround for https://github.com/pnpm/pnpm/issues/1890
187204 //
188205 // When "rush update-autoinstaller" is run, Rush deletes "common/autoinstallers/my-task/pnpm-lock.yaml"
@@ -198,7 +215,7 @@ export class Autoinstaller {
198215 }
199216
200217 // Detect a common mistake where PNPM prints "Already up-to-date" without creating a shrinkwrap file
201- const packageJsonEditor : PackageJsonEditor = PackageJsonEditor . load ( this . packageJsonPath ) ;
218+ const packageJsonEditor : PackageJsonEditor = await PackageJsonEditor . loadAsync ( this . packageJsonPath ) ;
202219 if ( packageJsonEditor . dependencyList . length === 0 ) {
203220 throw new Error (
204221 'You must add at least one dependency to the autoinstaller package' +
@@ -209,9 +226,13 @@ export class Autoinstaller {
209226
210227 this . _logIfConsoleOutputIsNotRestricted ( ) ;
211228
212- Utilities . syncNpmrc ( this . _rushConfiguration . commonRushConfigFolder , this . folderFullPath ) ;
229+ Utilities . syncNpmrc ( {
230+ sourceNpmrcFolder : this . _rushConfiguration . commonRushConfigFolder ,
231+ targetNpmrcFolder : this . folderFullPath ,
232+ supportEnvVarFallbackSyntax : this . _rushConfiguration . isPnpm
233+ } ) ;
213234
214- Utilities . executeCommand ( {
235+ await Utilities . executeCommandAsync ( {
215236 command : this . _rushConfiguration . packageManagerToolFilename ,
216237 args : [ 'install' ] ,
217238 workingDirectory : this . folderFullPath ,
@@ -221,8 +242,8 @@ export class Autoinstaller {
221242 this . _logIfConsoleOutputIsNotRestricted ( ) ;
222243
223244 if ( this . _rushConfiguration . packageManager === 'npm' ) {
224- this . _logIfConsoleOutputIsNotRestricted ( colors . bold ( 'Running "npm shrinkwrap"...' ) ) ;
225- Utilities . executeCommand ( {
245+ this . _logIfConsoleOutputIsNotRestricted ( Colorize . bold ( 'Running "npm shrinkwrap"...' ) ) ;
246+ await Utilities . executeCommandAsync ( {
226247 command : this . _rushConfiguration . packageManagerToolFilename ,
227248 args : [ 'shrinkwrap' ] ,
228249 workingDirectory : this . folderFullPath ,
@@ -243,11 +264,11 @@ export class Autoinstaller {
243264 } ) ;
244265 if ( oldFileContents !== newFileContents ) {
245266 this . _logIfConsoleOutputIsNotRestricted (
246- colors . green ( 'The shrinkwrap file has been updated.' ) + ' Please commit the updated file:'
267+ Colorize . green ( 'The shrinkwrap file has been updated.' ) + ' Please commit the updated file:'
247268 ) ;
248269 this . _logIfConsoleOutputIsNotRestricted ( `\n ${ this . shrinkwrapFilePath } ` ) ;
249270 } else {
250- this . _logIfConsoleOutputIsNotRestricted ( colors . green ( 'Already up to date.' ) ) ;
271+ this . _logIfConsoleOutputIsNotRestricted ( Colorize . green ( 'Already up to date.' ) ) ;
251272 }
252273 }
253274
0 commit comments