Skip to content

Commit 3722b92

Browse files
committed
Support escaping template variables
1 parent 59d931b commit 3722b92

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

packages/yarnpkg-core/sources/miscUtils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,11 +469,13 @@ export function buildIgnorePattern(ignorePatterns: Array<string>) {
469469
}
470470

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

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

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

packages/yarnpkg-core/tests/miscUtils.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ describe(`miscUtils`, () => {
2626
),
2727
).toBe(`VAR_A: ValueA, VAR_B: ValueB`);
2828
});
29+
30+
it(`should not replace escaped environment variables`, () => {
31+
expect(
32+
miscUtils.replaceEnvVariables(
33+
`VAR_A: \\\${VAR_A}, VAR_B: \\\${VAR_B}`,
34+
{
35+
env: {
36+
VAR_A: `ValueA`,
37+
VAR_B: `ValueB`,
38+
},
39+
},
40+
),
41+
).toBe(`VAR_A: \${VAR_A}, VAR_B: \${VAR_B}`);
42+
});
2943
});
3044

3145
describe(`mapAndFind`, () => {

0 commit comments

Comments
 (0)