Skip to content

Commit 92b5b4d

Browse files
committed
Only support fallback for pnpm.
1 parent e0d4706 commit 92b5b4d

File tree

9 files changed

+383
-148
lines changed

9 files changed

+383
-148
lines changed

libraries/rush-lib/src/cli/actions/PublishAction.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export class PublishAction extends BaseRushAction {
239239

240240
this._validate();
241241

242-
this._addNpmPublishHome();
242+
this._addNpmPublishHome(this.rushConfiguration.isPnpm);
243243

244244
const git: Git = new Git(this.rushConfiguration);
245245
const publishGit: PublishGit = new PublishGit(git, this._targetBranch.value);
@@ -582,15 +582,16 @@ export class PublishAction extends BaseRushAction {
582582
}
583583
}
584584

585-
private _addNpmPublishHome(): void {
585+
private _addNpmPublishHome(supportEnvVarFallbackSyntax: boolean): void {
586586
// Create "common\temp\publish-home" folder, if it doesn't exist
587587
Utilities.createFolderWithRetry(this._targetNpmrcPublishFolder);
588588

589589
// Copy down the committed "common\config\rush\.npmrc-publish" file, if there is one
590590
Utilities.syncNpmrc({
591591
sourceNpmrcFolder: this.rushConfiguration.commonRushConfigFolder,
592592
targetNpmrcFolder: this._targetNpmrcPublishFolder,
593-
useNpmrcPublish: true
593+
useNpmrcPublish: true,
594+
supportEnvVarFallbackSyntax
594595
});
595596
}
596597

libraries/rush-lib/src/logic/Autoinstaller.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ export class Autoinstaller {
137137
// Copy: .../common/autoinstallers/my-task/.npmrc
138138
Utilities.syncNpmrc({
139139
sourceNpmrcFolder: this._rushConfiguration.commonRushConfigFolder,
140-
targetNpmrcFolder: autoinstallerFullPath
140+
targetNpmrcFolder: autoinstallerFullPath,
141+
supportEnvVarFallbackSyntax: this._rushConfiguration.isPnpm
141142
});
142143

143144
this._logIfConsoleOutputIsNotRestricted(
@@ -222,7 +223,8 @@ export class Autoinstaller {
222223

223224
Utilities.syncNpmrc({
224225
sourceNpmrcFolder: this._rushConfiguration.commonRushConfigFolder,
225-
targetNpmrcFolder: this.folderFullPath
226+
targetNpmrcFolder: this.folderFullPath,
227+
supportEnvVarFallbackSyntax: this._rushConfiguration.isPnpm
226228
});
227229

228230
await Utilities.executeCommandAsync({

libraries/rush-lib/src/logic/base/BaseInstallManager.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,8 @@ export abstract class BaseInstallManager {
536536
sourceNpmrcFolder: subspace.getSubspaceConfigFolderPath(),
537537
targetNpmrcFolder: subspace.getSubspaceTempFolderPath(),
538538
linesToPrepend: extraNpmrcLines,
539-
createIfMissing: this.rushConfiguration.subspacesFeatureEnabled
539+
createIfMissing: this.rushConfiguration.subspacesFeatureEnabled,
540+
supportEnvVarFallbackSyntax: this.rushConfiguration.isPnpm
540541
});
541542
this._syncNpmrcAlreadyCalled = true;
542543

@@ -912,7 +913,8 @@ ${gitLfsHookHandling}
912913
*/
913914
const isAutoInstallPeersInNpmrc: boolean = isVariableSetInNpmrcFile(
914915
subspace.getSubspaceConfigFolderPath(),
915-
'auto-install-peers'
916+
'auto-install-peers',
917+
this.rushConfiguration.isPnpm
916918
);
917919

918920
let autoInstallPeers: boolean | undefined = this.rushConfiguration.pnpmOptions.autoInstallPeers;
@@ -940,7 +942,8 @@ ${gitLfsHookHandling}
940942
*/
941943
const isResolutionModeInNpmrc: boolean = isVariableSetInNpmrcFile(
942944
subspace.getSubspaceConfigFolderPath(),
943-
'resolution-mode'
945+
'resolution-mode',
946+
this.rushConfiguration.isPnpm
944947
);
945948

946949
let resolutionMode: PnpmResolutionMode | undefined = this.rushConfiguration.pnpmOptions.resolutionMode;

libraries/rush-lib/src/logic/setup/SetupPackageRegistry.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ export class SetupPackageRegistry {
110110
if (!this._options.syncNpmrcAlreadyCalled) {
111111
Utilities.syncNpmrc({
112112
sourceNpmrcFolder: this.rushConfiguration.commonRushConfigFolder,
113-
targetNpmrcFolder: this.rushConfiguration.commonTempFolder
113+
targetNpmrcFolder: this.rushConfiguration.commonTempFolder,
114+
supportEnvVarFallbackSyntax: this.rushConfiguration.isPnpm
114115
});
115116
}
116117

libraries/rush-lib/src/scripts/install-run.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ function _resolvePackageVersion(
169169
syncNpmrc({
170170
sourceNpmrcFolder,
171171
targetNpmrcFolder: rushTempFolder,
172-
logger
172+
logger,
173+
supportEnvVarFallbackSyntax: false
173174
});
174175

175176
const npmPath: string = getNpmPath();
@@ -433,7 +434,8 @@ export function installAndRun(
433434
syncNpmrc({
434435
sourceNpmrcFolder,
435436
targetNpmrcFolder: packageInstallFolder,
436-
logger
437+
logger,
438+
supportEnvVarFallbackSyntax: false
437439
});
438440

439441
_createPackageJson(packageInstallFolder, packageName, packageVersion);

libraries/rush-lib/src/utilities/Utilities.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,8 @@ export class Utilities {
531531
if (commonRushConfigFolder) {
532532
Utilities.syncNpmrc({
533533
sourceNpmrcFolder: commonRushConfigFolder,
534-
targetNpmrcFolder: directory
534+
targetNpmrcFolder: directory,
535+
supportEnvVarFallbackSyntax: false
535536
});
536537
}
537538

libraries/rush-lib/src/utilities/npmrcUtilities.ts

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ export interface ILogger {
2222
// create a global _combinedNpmrc for cache purpose
2323
const _combinedNpmrcMap: Map<string, string> = new Map();
2424

25-
function _trimNpmrcFile(options: {
26-
sourceNpmrcPath: string;
27-
linesToPrepend?: string[];
28-
linesToAppend?: string[];
29-
}): string {
30-
const { sourceNpmrcPath, linesToPrepend, linesToAppend } = options;
25+
function _trimNpmrcFile(
26+
options: Pick<
27+
INpmrcTrimOptions,
28+
'sourceNpmrcPath' | 'linesToAppend' | 'linesToPrepend' | 'supportEnvVarFallbackSyntax'
29+
>
30+
): string {
31+
const { sourceNpmrcPath, linesToPrepend, linesToAppend, supportEnvVarFallbackSyntax } = options;
3132
const combinedNpmrcFromCache: string | undefined = _combinedNpmrcMap.get(sourceNpmrcPath);
3233
if (combinedNpmrcFromCache !== undefined) {
3334
return combinedNpmrcFromCache;
@@ -48,7 +49,7 @@ function _trimNpmrcFile(options: {
4849

4950
npmrcFileLines = npmrcFileLines.map((line) => (line || '').trim());
5051

51-
const resultLines: string[] = trimNpmrcFileLines(npmrcFileLines, process.env);
52+
const resultLines: string[] = trimNpmrcFileLines(npmrcFileLines, process.env, supportEnvVarFallbackSyntax);
5253

5354
const combinedNpmrc: string = resultLines.join('\n');
5455

@@ -58,7 +59,18 @@ function _trimNpmrcFile(options: {
5859
return combinedNpmrc;
5960
}
6061

61-
export function trimNpmrcFileLines(npmrcFileLines: string[], env: NodeJS.ProcessEnv): string[] {
62+
/**
63+
*
64+
* @param npmrcFileLines The npmrc file's lines
65+
* @param env The environment variables object
66+
* @param supportEnvVarFallbackSyntax Whether to support fallback values in the form of `${VAR_NAME:-fallback}`
67+
* @returns
68+
*/
69+
export function trimNpmrcFileLines(
70+
npmrcFileLines: string[],
71+
env: NodeJS.ProcessEnv,
72+
supportEnvVarFallbackSyntax: boolean
73+
): string[] {
6274
const resultLines: string[] = [];
6375

6476
// This finds environment variable tokens that look like "${VAR_NAME}"
@@ -91,18 +103,24 @@ export function trimNpmrcFileLines(npmrcFileLines: string[], env: NodeJS.Process
91103
*/
92104
const nameWithFallback: string = token.substring(2, token.length - 1);
93105

94-
/**
95-
* Get the environment variable name and fallback value.
96-
*
97-
* name fallback
98-
* nameString -> nameString undefined
99-
* nameString-fallbackString -> nameString fallbackString
100-
* nameString:-fallbackString -> nameString fallbackString
101-
*/
102-
const matched: string[] | null = nameWithFallback.match(/^([^:-]+)(?:\:?-(.+))?$/);
103-
// matched: [originStr, variableName, fallback]
104-
const environmentVariableName: string = matched?.[1] ?? nameWithFallback;
105-
const fallback: string | undefined = matched?.[2];
106+
let environmentVariableName: string;
107+
let fallback: string | undefined;
108+
if (supportEnvVarFallbackSyntax) {
109+
/**
110+
* Get the environment variable name and fallback value.
111+
*
112+
* name fallback
113+
* nameString -> nameString undefined
114+
* nameString-fallbackString -> nameString fallbackString
115+
* nameString:-fallbackString -> nameString fallbackString
116+
*/
117+
const matched: string[] | null = nameWithFallback.match(/^([^:-]+)(?:\:?-(.+))?$/);
118+
// matched: [originStr, variableName, fallback]
119+
environmentVariableName = matched?.[1] ?? nameWithFallback;
120+
fallback = matched?.[2];
121+
} else {
122+
environmentVariableName = nameWithFallback;
123+
}
106124

107125
// Is the environment variable and fallback value defined.
108126
if (!env[environmentVariableName] && !fallback) {
@@ -146,18 +164,15 @@ interface INpmrcTrimOptions {
146164
logger: ILogger;
147165
linesToPrepend?: string[];
148166
linesToAppend?: string[];
167+
supportEnvVarFallbackSyntax: boolean;
149168
}
150169

151170
function _copyAndTrimNpmrcFile(options: INpmrcTrimOptions): string {
152-
const { logger, sourceNpmrcPath, targetNpmrcPath, linesToPrepend, linesToAppend } = options;
171+
const { logger, sourceNpmrcPath, targetNpmrcPath } = options;
153172
logger.info(`Transforming ${sourceNpmrcPath}`); // Verbose
154173
logger.info(` --> "${targetNpmrcPath}"`);
155174

156-
const combinedNpmrc: string = _trimNpmrcFile({
157-
sourceNpmrcPath,
158-
linesToPrepend,
159-
linesToAppend
160-
});
175+
const combinedNpmrc: string = _trimNpmrcFile(options);
161176

162177
fs.writeFileSync(targetNpmrcPath, combinedNpmrc);
163178

@@ -176,12 +191,14 @@ function _copyAndTrimNpmrcFile(options: INpmrcTrimOptions): string {
176191
export interface ISyncNpmrcOptions {
177192
sourceNpmrcFolder: string;
178193
targetNpmrcFolder: string;
194+
supportEnvVarFallbackSyntax: boolean;
179195
useNpmrcPublish?: boolean;
180196
logger?: ILogger;
181197
linesToPrepend?: string[];
182198
linesToAppend?: string[];
183199
createIfMissing?: boolean;
184200
}
201+
185202
export function syncNpmrc(options: ISyncNpmrcOptions): string | undefined {
186203
const {
187204
sourceNpmrcFolder,
@@ -193,9 +210,7 @@ export function syncNpmrc(options: ISyncNpmrcOptions): string | undefined {
193210
// eslint-disable-next-line no-console
194211
error: console.error
195212
},
196-
createIfMissing = false,
197-
linesToAppend,
198-
linesToPrepend
213+
createIfMissing = false
199214
} = options;
200215
const sourceNpmrcPath: string = path.join(
201216
sourceNpmrcFolder,
@@ -213,8 +228,7 @@ export function syncNpmrc(options: ISyncNpmrcOptions): string | undefined {
213228
sourceNpmrcPath,
214229
targetNpmrcPath,
215230
logger,
216-
linesToAppend,
217-
linesToPrepend
231+
...options
218232
});
219233
} else if (fs.existsSync(targetNpmrcPath)) {
220234
// If the source .npmrc doesn't exist and there is one in the target, delete the one in the target
@@ -226,15 +240,19 @@ export function syncNpmrc(options: ISyncNpmrcOptions): string | undefined {
226240
}
227241
}
228242

229-
export function isVariableSetInNpmrcFile(sourceNpmrcFolder: string, variableKey: string): boolean {
243+
export function isVariableSetInNpmrcFile(
244+
sourceNpmrcFolder: string,
245+
variableKey: string,
246+
supportEnvVarFallbackSyntax: boolean
247+
): boolean {
230248
const sourceNpmrcPath: string = `${sourceNpmrcFolder}/.npmrc`;
231249

232250
//if .npmrc file does not exist, return false directly
233251
if (!fs.existsSync(sourceNpmrcPath)) {
234252
return false;
235253
}
236254

237-
const trimmedNpmrcFile: string = _trimNpmrcFile({ sourceNpmrcPath });
255+
const trimmedNpmrcFile: string = _trimNpmrcFile({ sourceNpmrcPath, supportEnvVarFallbackSyntax });
238256

239257
const variableKeyRegExp: RegExp = new RegExp(`^${variableKey}=`, 'm');
240258
return trimmedNpmrcFile.match(variableKeyRegExp) !== null;

0 commit comments

Comments
 (0)