1
1
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2
2
// See LICENSE in the project root for license information.
3
3
4
+ import * as path from 'path' ;
4
5
import colors from 'colors/safe' ;
6
+ import { FileSystem , AlreadyReportedError , Executable , EnvironmentMap } from '@rushstack/node-core-library' ;
7
+ import { PrintUtilities } from '@rushstack/terminal' ;
5
8
6
9
import { RushConfiguration } from '../api/RushConfiguration' ;
7
10
import { NodeJsCompatibility } from '../logic/NodeJsCompatibility' ;
11
+ import { SpawnSyncReturns } from 'child_process' ;
8
12
9
13
export interface ILaunchRushPnpmInternalOptions {
10
14
isManaged : boolean ;
@@ -31,11 +35,78 @@ export class RushPnpmCommandLine {
31
35
rushConfiguration
32
36
} ) ;
33
37
34
- console . log ( 'Success' ) ;
35
- process . exitCode = 0 ;
38
+ if ( ! rushConfiguration ) {
39
+ throw new Error (
40
+ 'The "rush-pnpm" command must be executed in a folder that is under a Rush workspace folder'
41
+ ) ;
42
+ }
43
+
44
+ if ( rushConfiguration . packageManager !== 'pnpm' ) {
45
+ throw new Error (
46
+ 'The "rush-pnpm" command requires your rush.json to be configured to use the PNPM package manager'
47
+ ) ;
48
+ }
49
+
50
+ if ( ! rushConfiguration . pnpmOptions . useWorkspaces ) {
51
+ throw new Error (
52
+ 'The "rush-pnpm" command requires the "useWorkspaces" setting to be enabled in rush.json'
53
+ ) ;
54
+ }
55
+
56
+ const workspaceFolder : string = rushConfiguration . commonTempFolder ;
57
+ const workspaceFilePath : string = path . join ( workspaceFolder , 'pnpm-workspace.yaml' ) ;
58
+
59
+ if ( ! FileSystem . exists ( workspaceFilePath ) ) {
60
+ console . error ( colors . red ( 'Error: The PNPM workspace file has not been generated:' ) ) ;
61
+ console . error ( ` ${ colors . red ( workspaceFilePath ) } \n` ) ;
62
+ console . error ( colors . cyan ( `Do you need to run "rush install" or "rush update"?` ) ) ;
63
+ throw new AlreadyReportedError ( ) ;
64
+ }
65
+
66
+ if ( ! FileSystem . exists ( rushConfiguration . packageManagerToolFilename ) ) {
67
+ console . error ( colors . red ( 'Error: The PNPM local binary has not been installed yet.' ) ) ;
68
+ console . error ( '\n' + colors . cyan ( `Do you need to run "rush install" or "rush update"?` ) ) ;
69
+ throw new AlreadyReportedError ( ) ;
70
+ }
36
71
72
+ // 0 = node.exe
73
+ // 1 = rush-pnpm
74
+ const originalArgs : string [ ] = process . argv . slice ( 2 ) ;
75
+
76
+ const pnpmArgs : string [ ] = [ ] ;
77
+
78
+ if ( rushConfiguration . pnpmOptions . pnpmStorePath ) {
79
+ pnpmArgs . push ( '--store' ) ;
80
+ pnpmArgs . push ( rushConfiguration . pnpmOptions . pnpmStorePath ) ;
81
+ }
82
+
83
+ pnpmArgs . push ( ...originalArgs ) ;
84
+
85
+ const pnpmEnvironmentMap : EnvironmentMap = new EnvironmentMap ( process . env ) ;
86
+ pnpmEnvironmentMap . set ( 'NPM_CONFIG_WORKSPACE_DIR' , workspaceFolder ) ;
87
+
88
+ const result : SpawnSyncReturns < string > = Executable . spawnSync (
89
+ rushConfiguration . packageManagerToolFilename ,
90
+ pnpmArgs ,
91
+ {
92
+ environmentMap : pnpmEnvironmentMap ,
93
+ stdio : 'inherit'
94
+ }
95
+ ) ;
96
+ if ( result . error ) {
97
+ throw new Error ( 'Failed to invoke PNPM: ' + result . error ) ;
98
+ }
99
+ if ( result . status === null ) {
100
+ throw new Error ( 'Failed to invoke PNPM: Spawn completed without an exit code' ) ;
101
+ }
102
+ process . exitCode = result . status ;
103
+
104
+ console . log ( '\nFinished rush-pnpm' ) ;
37
105
} catch ( error ) {
38
- console . log ( colors . red ( 'Error: ' + error . message ) ) ;
106
+ if ( ! ( error instanceof AlreadyReportedError ) ) {
107
+ const prefix : string = 'ERROR: ' ;
108
+ console . error ( '\n' + colors . red ( PrintUtilities . wrapWords ( prefix + error . message ) ) ) ;
109
+ }
39
110
}
40
111
}
41
112
}
0 commit comments