Skip to content

Commit f4752d7

Browse files
committed
Initial working prototype of rush-pnpm logic
1 parent 36ddb4d commit f4752d7

File tree

1 file changed

+74
-3
lines changed

1 file changed

+74
-3
lines changed

apps/rush-lib/src/cli/RushPnpmCommandLine.ts

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
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 * as path from 'path';
45
import colors from 'colors/safe';
6+
import { FileSystem, AlreadyReportedError, Executable, EnvironmentMap } from '@rushstack/node-core-library';
7+
import { PrintUtilities } from '@rushstack/terminal';
58

69
import { RushConfiguration } from '../api/RushConfiguration';
710
import { NodeJsCompatibility } from '../logic/NodeJsCompatibility';
11+
import { SpawnSyncReturns } from 'child_process';
812

913
export interface ILaunchRushPnpmInternalOptions {
1014
isManaged: boolean;
@@ -31,11 +35,78 @@ export class RushPnpmCommandLine {
3135
rushConfiguration
3236
});
3337

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+
}
3671

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');
37105
} 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+
}
39110
}
40111
}
41112
}

0 commit comments

Comments
 (0)