Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
36 changes: 36 additions & 0 deletions .yarn/versions/3e62b777.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
releases:
"@yarnpkg/core": patch

declined:
- "@yarnpkg/plugin-catalog"
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-http"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-jsr"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- "@yarnpkg/builder"
- "@yarnpkg/cli"
- "@yarnpkg/doctor"
- "@yarnpkg/extensions"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
8 changes: 5 additions & 3 deletions packages/yarnpkg-core/sources/miscUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,11 +469,13 @@ export function buildIgnorePattern(ignorePatterns: Array<string>) {
}

export function replaceEnvVariables(value: string, {env}: {env: {[key: string]: string | undefined}}) {
const regex = /\${(?<variableName>[\d\w_]+)(?<colon>:)?(?:-(?<fallback>[^}]*))?}/g;
const regex = /\\?\${(?<variableName>[\d\w_]+)(?<colon>:)?(?:-(?<fallback>[^}]*))?}/g;

return value.replace(regex, (...args) => {
const {variableName, colon, fallback} = args[args.length - 1];
return value.replace(regex, (match, ...args) => {
if (match.startsWith(`\\`))
return match.slice(1);

const {variableName, colon, fallback} = args[args.length - 1];
const variableExist = Object.hasOwn(env, variableName);
const variableValue = env[variableName];

Expand Down
49 changes: 47 additions & 2 deletions packages/yarnpkg-core/tests/miscUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,45 @@ import CJSON from 'comment-json';
import * as miscUtils from '../sources/miscUtils';

describe(`miscUtils`, () => {
describe(`replaceEnvVariables`, () => {
it(`should replace environment variables with their values`, () => {
expect(
miscUtils.replaceEnvVariables(
`VAR_A: \${VAR_A}, VAR_B: \${VAR_B}`,
{
env: {
VAR_A: `ValueA`,
VAR_B: `ValueB`,
},
},
),
).toBe(`VAR_A: ValueA, VAR_B: ValueB`);
});

it(`should use fallback values when environment variables are not set`, () => {
expect(
miscUtils.replaceEnvVariables(
`VAR_A: \${VAR_A:-ValueA}, VAR_B: \${VAR_B:-ValueB}`,
{env: {}},
),
).toBe(`VAR_A: ValueA, VAR_B: ValueB`);
});

it(`should not replace escaped environment variables`, () => {
expect(
miscUtils.replaceEnvVariables(
`VAR_A: \\\${VAR_A}, VAR_B: \\\${VAR_B}`,
{
env: {
VAR_A: `ValueA`,
VAR_B: `ValueB`,
},
},
),
).toBe(`VAR_A: \${VAR_A}, VAR_B: \${VAR_B}`);
});
});

describe(`mapAndFind`, () => {
it(`should work with a simple example`, () => {
expect(
Expand Down Expand Up @@ -135,7 +174,9 @@ describe(`miscUtils`, () => {
const b = {n: [4, 5, 6]};
const c = miscUtils.mergeIntoTarget(a, b);

expect(CJSON.stringify(c, null, 2)).toStrictEqual(CJSON.stringify(CJSON.parse(`{
expect(CJSON.stringify(c, null, 2)).toStrictEqual(
CJSON.stringify(
CJSON.parse(`{
// n
"n":
// array
Expand All @@ -150,7 +191,11 @@ describe(`miscUtils`, () => {
5,
6
]
}`), null, 2));
}`),
null,
2,
),
);
});
});
});