Skip to content

Commit

Permalink
[Feature] add support for vscode env in dotenv (#1149)
Browse files Browse the repository at this point in the history
* add support for vscode env with dotenv

* fix typo
  • Loading branch information
nicksmider authored Jul 29, 2024
1 parent 9ab5f61 commit f04c419
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/utils/httpVariableProviders/systemVariableProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as fs from 'fs-extra';
import * as path from 'path';
import { Clipboard, commands, env, QuickPickItem, QuickPickOptions, TextDocument, Uri, window } from 'vscode';
import * as Constants from '../../common/constants';
import { EnvironmentController } from '../../controllers/environmentController';
import { HttpRequest } from '../../models/httpRequest';
import { ResolveErrorMessage, ResolveWarningMessage } from '../../models/httpVariableResolveResult';
import { VariableType } from '../../models/variableType';
Expand Down Expand Up @@ -188,13 +189,20 @@ export class SystemVariableProvider implements HttpVariableProvider {
private registerDotenvVariable() {
this.resolveFuncs.set(Constants.DotenvVariableName, async (name, document) => {
let folderPath = path.dirname(document.fileName);
while (!await fs.pathExists(path.join(folderPath, '.env'))) {
const { name : environmentName } = await EnvironmentController.getCurrentEnvironment();

let pathsFound = [false, false];

while ((pathsFound = await Promise.all([
fs.pathExists(path.join(folderPath, `.env.${environmentName}`)),
fs.pathExists(path.join(folderPath, '.env'))
])).every(result => result === false)) {
folderPath = path.join(folderPath, '..');
if (folderPath === path.parse(process.cwd()).root) {
return { warning: ResolveWarningMessage.DotenvFileNotFound };
}
}
const absolutePath = path.join(folderPath, '.env');
const absolutePath = path.join(folderPath, pathsFound[0] ? `.env.${environmentName}` : '.env');
const groups = this.dotenvRegex.exec(name);
if (groups !== null && groups.length === 3) {
const parsed = dotenv.parse(await fs.readFile(absolutePath));
Expand Down

0 comments on commit f04c419

Please sign in to comment.