handle references to external environment variables

use cross-env to enable env variables on Windows
This commit is contained in:
Andrei Listochkin 2022-05-11 13:28:08 +01:00
parent a86db5d0d1
commit 6c769ac00d
4 changed files with 69 additions and 5 deletions

View file

@ -19,6 +19,7 @@
"@typescript-eslint/eslint-plugin": "^5.16.0",
"@typescript-eslint/parser": "^5.16.0",
"@vscode/test-electron": "^2.1.3",
"cross-env": "^7.0.3",
"esbuild": "^0.14.27",
"eslint": "^8.11.0",
"tslib": "^2.3.0",
@ -27,7 +28,7 @@
"vsce": "^2.7.0"
},
"engines": {
"vscode": "^1.65.0"
"vscode": "^1.66.0"
}
},
"node_modules/@eslint/eslintrc": {
@ -790,6 +791,24 @@
"integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==",
"dev": true
},
"node_modules/cross-env": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/cross-env/-/cross-env-7.0.3.tgz",
"integrity": "sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==",
"dev": true,
"dependencies": {
"cross-spawn": "^7.0.1"
},
"bin": {
"cross-env": "src/bin/cross-env.js",
"cross-env-shell": "src/bin/cross-env-shell.js"
},
"engines": {
"node": ">=10.14",
"npm": ">=6",
"yarn": ">=1"
}
},
"node_modules/cross-spawn": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
@ -4663,6 +4682,15 @@
"integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==",
"dev": true
},
"cross-env": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/cross-env/-/cross-env-7.0.3.tgz",
"integrity": "sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==",
"dev": true,
"requires": {
"cross-spawn": "^7.0.1"
}
},
"cross-spawn": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",

View file

@ -33,12 +33,12 @@
"lint": "tsfmt --verify && eslint -c .eslintrc.js --ext ts ./src ./tests",
"fix": " tsfmt -r && eslint -c .eslintrc.js --ext ts ./src ./tests --fix",
"pretest": "tsc && npm run build",
"test": "node ./out/tests/runTests.js"
"test": "cross-env TEST_VARIABLE=test node ./out/tests/runTests.js"
},
"dependencies": {
"vscode-languageclient": "8.0.0-next.14",
"d3": "^7.3.0",
"d3-graphviz": "^4.1.0"
"d3-graphviz": "^4.1.0",
"vscode-languageclient": "8.0.0-next.14"
},
"devDependencies": {
"@types/node": "~14.17.5",
@ -46,6 +46,7 @@
"@typescript-eslint/eslint-plugin": "^5.16.0",
"@typescript-eslint/parser": "^5.16.0",
"@vscode/test-electron": "^2.1.3",
"cross-env": "^7.0.3",
"esbuild": "^0.14.27",
"eslint": "^8.11.0",
"tslib": "^2.3.0",

View file

@ -232,7 +232,30 @@ export function substituteVariablesInEnv(env: Env): Env {
}));
const resolved = new Set<string>();
// TODO: handle missing dependencies
for (const dep of missingDeps) {
const match = /(?<prefix>.*?):(?<body>.+)/.exec(dep);
if (match) {
const { prefix, body } = match.groups!;
if (prefix === 'env') {
const envName = body;
envWithDeps[dep] = {
value: process.env[envName] ?? '',
deps: []
};
resolved.add(dep);
} else {
// we can't handle other prefixes at the moment
// leave values as is, but still mark them as resolved
envWithDeps[dep] = {
value: '${' + dep + '}',
deps: []
};
resolved.add(dep);
}
} else {
// TODO: handle VSCode variables
}
}
const toResolve = new Set(Object.keys(envWithDeps));
let leftToResolveSize;

View file

@ -37,5 +37,17 @@ export async function getTests(ctx: Context) {
const actualEnv = await substituteVariablesInEnv(envJson);
assert.deepStrictEqual(actualEnv, expectedEnv);
});
suite.addTest('Should support external variables', async () => {
const envJson = {
USING_EXTERNAL_VAR: "${env:TEST_VARIABLE} test ${env:TEST_VARIABLE}"
};
const expectedEnv = {
USING_EXTERNAL_VAR: "test test test"
};
const actualEnv = await substituteVariablesInEnv(envJson);
assert.deepStrictEqual(actualEnv, expectedEnv);
});
});
}