Skip to content

Commit 9b0d0d1

Browse files
feat: ${env:ENV} syntax for config (#63)
1 parent 9a2a140 commit 9b0d0d1

File tree

6 files changed

+42
-15
lines changed

6 files changed

+42
-15
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"publisher": "Supabase",
44
"displayName": "postgres-language-server",
55
"description": "Postgres Language Server right in your IDE.",
6-
"version": "1.3.5",
6+
"version": "1.4.0",
77
"repository": {
88
"type": "git",
99
"url": "https://github.com/supabase-community/postgres-language-server-vscode"
@@ -136,27 +136,32 @@
136136
},
137137
"postgres-language-server.configFile": {
138138
"type": "string",
139-
"description": "Path to the `postgres-language-server.jsonc` file. You don't need to set this if the file is on root level of your project.",
139+
"description": "Path to the `postgres-language-server.jsonc` file. Supports environment variables with ${env:VAR_NAME} syntax. You don't need to set this if the file is on root level of your project.",
140+
"examples": [
141+
"./configs/postgres-language-server.jsonc",
142+
"${env:POSTGRES_CONFIG_DIR}/postgres-language-server.jsonc"
143+
],
140144
"scope": "resource"
141145
},
142146
"postgres-language-server.bin": {
143147
"oneOf": [
144148
{
145149
"type": "string",
146-
"description": "Path to the postgres-language-server Language Server binary",
150+
"description": "Path to the postgres-language-server Language Server binary. Supports environment variables with ${env:VAR_NAME} syntax.",
147151
"examples": [
148152
"/path/to/postgres-language-server",
149-
"./path/to/postgres-language-server"
153+
"./path/to/postgres-language-server",
154+
"${env:POSTGRES_TOOLS_DIR}/postgres-language-server"
150155
]
151156
},
152157
{
153158
"type": "object",
154-
"description": "Platform-specific paths to the postgres-language-server Language Server binary",
159+
"description": "Platform-specific paths to the postgres-language-server Language Server binary. Supports environment variables with ${env:VAR_NAME} syntax.",
155160
"examples": [
156161
{
157162
"linux-x64": "/path/to/postgres-language-server",
158163
"darwin-arm64": "./path/to/postgres-language-server",
159-
"win32-x64": "/path/to/postgres-language-server.exe"
164+
"win32-x64": "${env:POSTGRES_TOOLS_DIR}/postgres-language-server.exe"
160165
}
161166
]
162167
}

src/binary-finder-strategies.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Uri, window } from "vscode";
22
import { logger } from "./logger";
33
import { delimiter, dirname, join } from "node:path";
44
import { CONSTANTS, OperatingMode } from "./constants";
5-
import { fileExists } from "./utils";
5+
import { expandEnvVariables, fileExists } from "./utils";
66
import { createRequire } from "node:module";
77
import { getConfig } from "./config";
88
import { downloadPglt, getDownloadedBinary } from "./downloader";
@@ -71,6 +71,8 @@ export const vsCodeSettingsStrategy: BinaryFindStrategy = {
7171
if (typeof binSetting === "string") {
7272
logger.debug("Binary Setting is a string", { binSetting });
7373

74+
binSetting = expandEnvVariables(binSetting);
75+
7476
let resolvedPath: string;
7577

7678
if (binSetting.startsWith(".")) {

src/project.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Uri, window, type WorkspaceFolder } from "vscode";
2-
import { fileExists } from "./utils";
2+
import { expandEnvVariables, fileExists } from "./utils";
33
import { getConfig, isEnabledForFolder } from "./config";
44
import { logger } from "./logger";
55
import { state } from "./state";
@@ -16,7 +16,11 @@ export async function getActiveProjectsForMultiRoot(
1616
let globalConfig: Uri | undefined = undefined;
1717

1818
if (!globalConfig) {
19-
const globalConfigSetting = getConfig<string>("configFile");
19+
let globalConfigSetting = getConfig<string>("configFile");
20+
21+
if (globalConfigSetting) {
22+
globalConfigSetting = expandEnvVariables(globalConfigSetting);
23+
}
2024

2125
if (globalConfigSetting && globalConfigSetting.startsWith(".")) {
2226
window.showErrorMessage(
@@ -90,12 +94,17 @@ export async function getActiveProjectForSingleRoot(
9094
): Promise<Project | null> {
9195
let configPath: Uri;
9296

93-
const userConfig = getConfig<string>("configFile", { scope: first.uri });
97+
let userConfig = getConfig<string>("configFile", { scope: first.uri });
9498
if (userConfig) {
99+
userConfig = expandEnvVariables(userConfig);
95100
logger.info("User has specified path to config file.", {
96101
path: userConfig,
97102
});
98-
configPath = Uri.joinPath(first.uri, userConfig);
103+
if (userConfig.startsWith(".")) {
104+
configPath = Uri.joinPath(first.uri, userConfig);
105+
} else {
106+
configPath = Uri.file(userConfig);
107+
}
99108
} else {
100109
logger.info("User did not specify path to config file. Using default.");
101110

src/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,14 @@ export async function getVersion(bin: Uri): Promise<string | null> {
133133
export function daysToMs(days: number) {
134134
return days * 24 * 60 * 60 * 1000;
135135
}
136+
137+
export function expandEnvVariables(path: string): string {
138+
return path.replace(/\$\{env:([^}]+)\}/g, (match, varName) => {
139+
const value = process.env[varName];
140+
if (value === undefined || value === "") {
141+
logger.warn(`Environment variable '${varName}' is not set`);
142+
return match;
143+
}
144+
return value;
145+
});
146+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"postgres-language-server.configFile": "./configs/conf.jsonc",
3-
"postgres-language-server.bin": "./bin/postgrestools"
2+
"postgres-language-server.configFile": "${env:POSTGRESTOOLS_DIR}/test/fixtures/vscode-settings-strat/configs/conf.jsonc",
3+
"postgres-language-server.bin": "${env:POSTGRESTOOLS_DIR}/test/fixtures/vscode-settings-strat/bin/postgrestools"
44
}

0 commit comments

Comments
 (0)