Skip to content

Support fallback syntax in .npmrc file #5030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/rush",
"comment": "Support fallback syntax in .npmrc file",
"type": "none"
}
],
"packageName": "@microsoft/rush"
}
54 changes: 43 additions & 11 deletions libraries/rush-lib/src/utilities/npmrcUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,33 @@ function _trimNpmrcFile(options: {
if (combinedNpmrcFromCache !== undefined) {
return combinedNpmrcFromCache;
}

let npmrcFileLines: string[] = [];
if (linesToPrepend) {
npmrcFileLines.push(...linesToPrepend);
}

if (fs.existsSync(sourceNpmrcPath)) {
npmrcFileLines.push(...fs.readFileSync(sourceNpmrcPath).toString().split('\n'));
}

if (linesToAppend) {
npmrcFileLines.push(...linesToAppend);
}

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

const resultLines: string[] = trimNpmrcFileLines(npmrcFileLines, process.env);

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

//save the cache
_combinedNpmrcMap.set(sourceNpmrcPath, combinedNpmrc);

return combinedNpmrc;
}

export function trimNpmrcFileLines(npmrcFileLines: string[], env: NodeJS.ProcessEnv): string[] {
const resultLines: string[] = [];

// This finds environment variable tokens that look like "${VAR_NAME}"
Expand All @@ -66,11 +82,30 @@ function _trimNpmrcFile(options: {
const environmentVariables: string[] | null = line.match(expansionRegExp);
if (environmentVariables) {
for (const token of environmentVariables) {
// Remove the leading "${" and the trailing "}" from the token
const environmentVariableName: string = token.substring(2, token.length - 1);

// Is the environment variable defined?
if (!process.env[environmentVariableName]) {
/**
* Remove the leading "${" and the trailing "}" from the token
*
* ${nameString} -> nameString
* ${nameString-fallbackString} -> name-fallbackString
* ${nameString:-fallbackString} -> name:-fallbackString
*/
const nameWithFallback: string = token.substring(2, token.length - 1);

/**
* Get the environment variable name and fallback value.
*
* name fallback
* nameString -> nameString undefined
* nameString-fallbackString -> nameString fallbackString
* nameString:-fallbackString -> nameString fallbackString
*/
const matched: string[] | null = nameWithFallback.match(/^([^:-]+)(?:\:?-(.+))?$/);
// matched: [originStr, variableName, fallback]
const environmentVariableName: string = matched?.[1] ?? nameWithFallback;
const fallback: string | undefined = matched?.[2];

// Is the environment variable and fallback value defined.
if (!env[environmentVariableName] && !fallback) {
// No, so trim this line
lineShouldBeTrimmed = true;
break;
Expand All @@ -88,12 +123,7 @@ function _trimNpmrcFile(options: {
}
}

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

//save the cache
_combinedNpmrcMap.set(sourceNpmrcPath, combinedNpmrc);

return combinedNpmrc;
return resultLines;
}

/**
Expand All @@ -117,6 +147,7 @@ interface INpmrcTrimOptions {
linesToPrepend?: string[];
linesToAppend?: string[];
}

function _copyAndTrimNpmrcFile(options: INpmrcTrimOptions): string {
const { logger, sourceNpmrcPath, targetNpmrcPath, linesToPrepend, linesToAppend } = options;
logger.info(`Transforming ${sourceNpmrcPath}`); // Verbose
Expand Down Expand Up @@ -177,6 +208,7 @@ export function syncNpmrc(options: ISyncNpmrcOptions): string | undefined {
if (!fs.existsSync(targetNpmrcFolder)) {
fs.mkdirSync(targetNpmrcFolder, { recursive: true });
}

return _copyAndTrimNpmrcFile({
sourceNpmrcPath,
targetNpmrcPath,
Expand Down
114 changes: 114 additions & 0 deletions libraries/rush-lib/src/utilities/test/npmrcUtilities.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import { trimNpmrcFileLines } from '../npmrcUtilities';

describe('npmrcUtilities', () => {
it(trimNpmrcFileLines.name, () => {
// Normal
expect(trimNpmrcFileLines(['var1=${foo}'], {})).toMatchInlineSnapshot(`
Array [
"; MISSING ENVIRONMENT VARIABLE: var1=\${foo}",
]
`);
expect(trimNpmrcFileLines(['var1=${foo}'], { foo: 'test' })).toMatchInlineSnapshot(`
Array [
"var1=\${foo}",
]
`);
expect(trimNpmrcFileLines(['var1=${foo-fallback_value}'], {})).toMatchInlineSnapshot(`
Array [
"var1=\${foo-fallback_value}",
]
`);
expect(trimNpmrcFileLines(['var1=${foo-fallback_value}'], { foo: 'test' })).toMatchInlineSnapshot(`
Array [
"var1=\${foo-fallback_value}",
]
`);
expect(trimNpmrcFileLines(['var1=${foo:-fallback_value}'], {})).toMatchInlineSnapshot(`
Array [
"var1=\${foo:-fallback_value}",
]
`);
expect(trimNpmrcFileLines(['var1=${foo:-fallback_value}'], { foo: 'test' })).toMatchInlineSnapshot(`
Array [
"var1=\${foo:-fallback_value}",
]
`);

// Multiple environment variables
expect(trimNpmrcFileLines(['var1=${foo}-${bar}'], { foo: 'test' })).toMatchInlineSnapshot(`
Array [
"; MISSING ENVIRONMENT VARIABLE: var1=\${foo}-\${bar}",
]
`);
expect(trimNpmrcFileLines(['var1=${foo}-${bar}'], { bar: 'test' })).toMatchInlineSnapshot(`
Array [
"; MISSING ENVIRONMENT VARIABLE: var1=\${foo}-\${bar}",
]
`);
expect(trimNpmrcFileLines(['var1=${foo}-${bar}'], { foo: 'test', bar: 'test' })).toMatchInlineSnapshot(`
Array [
"var1=\${foo}-\${bar}",
]
`);
expect(trimNpmrcFileLines(['var1=${foo:-fallback_value}-${bar-fallback_value}'], {}))
.toMatchInlineSnapshot(`
Array [
"var1=\${foo:-fallback_value}-\${bar-fallback_value}",
]
`);

// Multiline
expect(trimNpmrcFileLines(['var1=${foo}', 'var2=${bar}'], { foo: 'test' })).toMatchInlineSnapshot(`
Array [
"var1=\${foo}",
"; MISSING ENVIRONMENT VARIABLE: var2=\${bar}",
]
`);
expect(trimNpmrcFileLines(['var1=${foo}', 'var2=${bar}'], { foo: 'test', bar: 'test' }))
.toMatchInlineSnapshot(`
Array [
"var1=\${foo}",
"var2=\${bar}",
]
`);
expect(trimNpmrcFileLines(['var1=${foo}', 'var2=${bar-fallback_value}'], { foo: 'test' }))
.toMatchInlineSnapshot(`
Array [
"var1=\${foo}",
"var2=\${bar-fallback_value}",
]
`);
expect(trimNpmrcFileLines(['var1=${foo:-fallback_value}', 'var2=${bar-fallback_value}'], {}))
.toMatchInlineSnapshot(`
Array [
"var1=\${foo:-fallback_value}",
"var2=\${bar-fallback_value}",
]
`);

// Malformed
expect(trimNpmrcFileLines(['var1=${foo_fallback_value}'], {})).toMatchInlineSnapshot(`
Array [
"; MISSING ENVIRONMENT VARIABLE: var1=\${foo_fallback_value}",
]
`);
expect(trimNpmrcFileLines(['var1=${foo:fallback_value}'], {})).toMatchInlineSnapshot(`
Array [
"; MISSING ENVIRONMENT VARIABLE: var1=\${foo:fallback_value}",
]
`);
expect(trimNpmrcFileLines(['var1=${foo:_fallback_value}'], {})).toMatchInlineSnapshot(`
Array [
"; MISSING ENVIRONMENT VARIABLE: var1=\${foo:_fallback_value}",
]
`);
expect(trimNpmrcFileLines(['var1=${foo'], {})).toMatchInlineSnapshot(`
Array [
"var1=\${foo",
]
`);
});
});
Loading