Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 29x 29x 29x 29x 9x 9x 29x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 3x 3x 3x 3x 5x 4x 4x 4x 3x 4x 5x 5x 3x 3x 3x 5x 3x 3x 3x 3x 1x 1x 1x 1x 1x 3x 3x 3x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 4x 4x 4x 7x 7x 4x 7x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 16x 16x 7x 16x 5x 5x 16x 16x 16x 16x 1x 28x 28x 28x 11x 11x 8x 1x 1x 1x 1x 1x 1x | import * as fs from 'fs';
import * as path from 'path';
import * as vscode from 'vscode';
// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------
export interface Environment {
name: string; // e.g. "dev", "staging", "remote"
envFilePath: string; // absolute path to the http-client.env.json that defines it
}
export interface EnvVariable {
name: string;
value: string;
}
export interface EnvScripts {
/** Absolute path from `$shared.$scripts.pre` */
sharedPre?: string;
/** Absolute path from `$shared.$scripts.post` */
sharedPost?: string;
/** Absolute path from `envName.$scripts.pre` */
envPre?: string;
/** Absolute path from `envName.$scripts.post` */
envPost?: string;
}
// ---------------------------------------------------------------------------
// Internal JSON shape
// ---------------------------------------------------------------------------
// Values can be plain strings or provider objects (AspnetUserSecrets, AzureKeyVault,
// Encrypted). We only support plain strings; provider objects are silently skipped.
type EnvBlock = Record<string, string | object>;
type EnvJson = Record<string, EnvBlock>;
interface ScriptsBlock { pre?: string; post?: string; }
function readJson(filePath: string): EnvJson | undefined {
try {
return JSON.parse(fs.readFileSync(filePath, 'utf8')) as EnvJson;
} catch {
return undefined;
}
}
// ---------------------------------------------------------------------------
// Discovery
// ---------------------------------------------------------------------------
/**
* Discover all named environments by scanning workspace root folders for
* `http-client.env.json`. The `$shared` key is a defaults namespace, not a
* selectable environment, so it is excluded from results.
*/
export function discoverEnvironments(
workspaceFolders: readonly vscode.WorkspaceFolder[] | undefined,
): Environment[] {
if (!workspaceFolders || workspaceFolders.length === 0) { return []; }
const envs: Environment[] = [];
const seen = new Set<string>();
for (const folder of workspaceFolders) {
const envFilePath = path.join(folder.uri.fsPath, 'http-client.env.json');
const json = readJson(envFilePath);
if (!json) { continue; }
for (const name of Object.keys(json)) {
if (name === '$shared') { continue; }
if (!seen.has(name)) {
seen.add(name);
envs.push({ name, envFilePath });
}
}
}
return envs;
}
/**
* Discover all named environments from a single `http-client.env.json` file.
* The `$shared` key is excluded — it is a defaults namespace, not a selectable environment.
*/
export function discoverEnvironmentsFromFile(envFilePath: string): Environment[] {
const json = readJson(envFilePath);
if (!json) { return []; }
return Object.keys(json)
.filter(name => name !== '$shared')
.map(name => ({ name, envFilePath }));
}
/**
* Find the closest `http-client.env.json` by walking up from the directory
* that contains the given `.http` file, stopping at the filesystem root.
* This mirrors Visual Studio's own search strategy.
*/
export function findEnvFileForHttp(httpFilePath: string): string | undefined {
let dir = path.dirname(httpFilePath);
while (true) {
const candidate = path.join(dir, 'http-client.env.json');
if (fs.existsSync(candidate)) { return candidate; }
const parent = path.dirname(dir);
if (parent === dir) { break; }
dir = parent;
}
return undefined;
}
// ---------------------------------------------------------------------------
// Loading
// ---------------------------------------------------------------------------
/**
* Load variables for a specific environment from an `http-client.env.json` file.
*
* Merge order (highest priority wins):
* 1. `http-client.env.json.user` — environment block
* 2. `http-client.env.json` — environment block
* 3. `http-client.env.json.user` — `$shared` block
* 4. `http-client.env.json` — `$shared` block (lowest)
*
* Only plain string values are returned. Provider-based secrets
* (AspnetUserSecrets, AzureKeyVault, Encrypted) are silently skipped —
* resolving them requires runtime infrastructure Laika doesn't have.
*/
export function loadEnvironment(envFilePath: string, envName: string): EnvVariable[] {
const main = readJson(envFilePath) ?? {};
const user = readJson(envFilePath + '.user') ?? {};
const merged: Record<string, string> = {};
applyBlock(main['$shared'], merged);
applyBlock(user['$shared'], merged);
applyBlock(main[envName], merged);
applyBlock(user[envName], merged);
return Object.entries(merged).map(([name, value]) => ({ name, value }));
}
/**
* Load pre/post script paths for the active environment from an `http-client.env.json` file.
* Paths in the JSON are relative to the directory containing the env file; returned paths are absolute.
* Only the main env file is consulted — the `.user` override is not used for scripts.
*/
export function loadEnvScripts(envFilePath: string, envName: string): EnvScripts {
const main = readJson(envFilePath) ?? {};
const dir = path.dirname(envFilePath);
const resolve = (p: string | undefined) => p ? path.resolve(dir, p) : undefined;
const sharedScripts = extractScriptsBlock(main['$shared']);
const envScripts = extractScriptsBlock(main[envName]);
return {
sharedPre: resolve(sharedScripts?.pre),
sharedPost: resolve(sharedScripts?.post),
envPre: resolve(envScripts?.pre),
envPost: resolve(envScripts?.post),
};
}
function extractScriptsBlock(block: EnvBlock | undefined): ScriptsBlock | undefined {
if (!block || typeof block !== 'object') { return undefined; }
const s = (block as Record<string, unknown>)['$scripts'];
if (!s || typeof s !== 'object') { return undefined; }
const scripts = s as Record<string, unknown>;
return {
pre: typeof scripts['pre'] === 'string' ? scripts['pre'] : undefined,
post: typeof scripts['post'] === 'string' ? scripts['post'] : undefined,
};
}
function applyBlock(block: EnvBlock | undefined, target: Record<string, string>): void {
if (!block || typeof block !== 'object') { return; }
for (const [key, val] of Object.entries(block)) {
if (typeof val === 'string') { target[key] = val; }
}
}
/**
* Persist one or more variables into `http-client.env.json.user` under the given env name.
* Existing keys in the file are preserved; only the provided keys are overwritten.
* The `$scripts` key inside the env block is never touched.
*/
export function writeEnvUser(envFilePath: string, envName: string, vars: Record<string, string>): void {
const userPath = envFilePath + '.user';
let json: EnvJson = {};
try {
json = JSON.parse(fs.readFileSync(userPath, 'utf8')) as EnvJson;
} catch {
// File doesn't exist yet — start empty
}
if (!json[envName] || typeof json[envName] !== 'object') {
json[envName] = {};
}
for (const [key, val] of Object.entries(vars)) {
(json[envName] as Record<string, string>)[key] = val;
}
fs.writeFileSync(userPath, JSON.stringify(json, null, 2) + '\n', 'utf8');
}
|