From 6c769ac00d12cc1436bdbb2bbbce80db0bb8d2d5 Mon Sep 17 00:00:00 2001 From: Andrei Listochkin Date: Wed, 11 May 2022 13:28:08 +0100 Subject: [PATCH] handle references to external environment variables use cross-env to enable env variables on Windows --- editors/code/package-lock.json | 30 +++++++++++++++++++++++- editors/code/package.json | 7 +++--- editors/code/src/config.ts | 25 +++++++++++++++++++- editors/code/tests/unit/settings.test.ts | 12 ++++++++++ 4 files changed, 69 insertions(+), 5 deletions(-) diff --git a/editors/code/package-lock.json b/editors/code/package-lock.json index 3c7c643798e..74cda037d8b 100644 --- a/editors/code/package-lock.json +++ b/editors/code/package-lock.json @@ -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", diff --git a/editors/code/package.json b/editors/code/package.json index 241cafd369a..9d0f9b89c4e 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -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", diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index 87cc2a395ba..cfbdd696652 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -232,7 +232,30 @@ export function substituteVariablesInEnv(env: Env): Env { })); const resolved = new Set(); - // TODO: handle missing dependencies + for (const dep of missingDeps) { + const match = /(?.*?):(?.+)/.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; diff --git a/editors/code/tests/unit/settings.test.ts b/editors/code/tests/unit/settings.test.ts index 12734d15667..f4b022b421c 100644 --- a/editors/code/tests/unit/settings.test.ts +++ b/editors/code/tests/unit/settings.test.ts @@ -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); + }); }); }