Skip to content

Commit

Permalink
fix: env dir is resolved correctly (AnWeber/vscode-httpyac#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnWeber committed Jun 9, 2023
1 parent ee0af6c commit 33ef59d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Fixes

- default headers do not overwrite headers with other casing (Anweber/vscode-httpyac#200)
- env dir is resolved correctly (Anweber/vscode-httpyac#198)

## [6.4.5]

Expand Down
61 changes: 36 additions & 25 deletions src/plugins/dotenv/dotenvVariableProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,8 @@ const defaultFiles: Array<string> = ['.env'];
export async function provideDotenvEnvironments(context: VariableProviderContext): Promise<string[]> {
const files: Array<string> = [];

const globalEnv = process.env.HTTPYAC_ENV;
if (globalEnv && utils.isString(globalEnv)) {
const globalEnvAbsolute = await utils.toAbsoluteFilename(globalEnv, context.httpFile.rootDir);
if (globalEnvAbsolute) {
files.push(...(await fileProvider.readdir(globalEnvAbsolute)));
}
}
if (context.config?.envDirName) {
const absolute = await utils.toAbsoluteFilename(context.config.envDirName, context.httpFile.rootDir);
if (absolute) {
files.push(...(await fileProvider.readdir(absolute)));
}
}
files.push(...(await readEnvDir(process.env.HTTPYAC_ENV, context)));
files.push(...(await readEnvDir(context.config?.envDirName, context)));

const dirOfFile = fileProvider.dirname(context.httpFile.fileName);
if (dirOfFile) {
Expand All @@ -40,6 +29,17 @@ export async function provideDotenvEnvironments(context: VariableProviderContext
});
}

async function readEnvDir(dir: string | undefined, context: VariableProviderContext): Promise<Array<string>> {
const files = [];
if (dir && utils.isString(dir)) {
const absoluteDir = await utils.toAbsoluteFilename(dir, context.httpFile.rootDir);
if (absoluteDir) {
files.push(...(await fileProvider.readdir(absoluteDir)));
}
}
return files;
}

export async function provideDotenvVariables(
env: string[] | undefined,
context: VariableProviderContext
Expand All @@ -51,27 +51,38 @@ export async function provideDotenvVariables(
if (globalEnv && utils.isString(globalEnv)) {
const globalEnvAbsolute = await utils.toAbsoluteFilename(globalEnv, context.httpFile.rootDir);
if (globalEnvAbsolute) {
variables.push(...(await getVariablesOfFolder(searchFiles, globalEnvAbsolute)));
variables.push(...(await getEnvVariables(searchFiles, globalEnvAbsolute)));
}
}

if (context.config?.envDirName) {
const absolute = await utils.toAbsoluteFilename(context.config.envDirName, context.httpFile.rootDir);
if (absolute) {
variables.push(...(await getVariablesOfFolder(searchFiles, absolute)));
}
}
await getEnvFolderVariables(context.config?.envDirName, searchFiles, context.httpFile.rootDir);
const dirOfFile = fileProvider.dirname(context.httpFile.fileName);
if (dirOfFile) {
const vars: Array<Variables> = [];
await utils.iterateUntilRoot(dirOfFile, context.httpFile.rootDir, async (dir: PathLike) => {
vars.unshift(...(await getVariablesOfFolder(searchFiles, dir)));
vars.unshift(...(await getEnvFolderVariables(context.config?.envDirName, searchFiles, dir)));
vars.unshift(...(await getEnvVariables(searchFiles, dir)));
});
variables.push(...vars);
}
return Object.assign({}, ...variables);
}

async function getEnvFolderVariables(
envDirName: string | undefined,
searchFiles: string[],
dir: PathLike | undefined
): Promise<Array<Variables>> {
const variables: Array<Variables> = [];
if (envDirName) {
const absolute = await utils.toAbsoluteFilename(envDirName, dir);
if (absolute) {
variables.push(...(await getEnvVariables(searchFiles, absolute)));
}
}
return variables;
}

function getSearchFiles(env: string[] | undefined) {
const searchFiles = [...defaultFiles];
if (env) {
Expand All @@ -82,20 +93,20 @@ function getSearchFiles(env: string[] | undefined) {
return searchFiles;
}

async function getVariablesOfFolder(searchFiles: string[], workingDir: PathLike) {
const files = await fileProvider.readdir(workingDir);
async function getEnvVariables(searchFiles: string[], dir: PathLike) {
const files = await fileProvider.readdir(dir);
const foundFiles = searchFiles.filter(file => files.indexOf(file) >= 0);
const vars = [];

for (const fileName of foundFiles) {
const envFileName = fileProvider.joinPath(workingDir, fileName);
const envFileName = fileProvider.joinPath(dir, fileName);
log.trace(`.env environment file found: ${envFileName}`);
try {
const content = await fileProvider.readFile(envFileName, 'utf-8');
const variables = parse(content);
vars.push(variables);
} catch (err) {
log.trace(`${fileProvider.toString(workingDir)}/${fileName} not found`);
log.trace(`${fileProvider.toString(dir)}/${fileName} not found`);
}
}
return vars;
Expand Down
3 changes: 2 additions & 1 deletion src/store/httpFileStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ export class HttpFileStore implements models.HttpFileStore {
options.workingDir,
'package.json',
options.config?.envDirName || 'env'
));
)) ||
options.workingDir;

const httpFile = new HttpFile(absoluteFileName, rootDir);
httpFile.activeEnvironment = options.activeEnvironment;
Expand Down
6 changes: 1 addition & 5 deletions src/utils/fsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,7 @@ export async function findRootDirOfFile(
file = fileProvider.joinPath(workingDir, fsPath);
}
const dirName = fileProvider.dirname(file);
const dir = await findRootDir(dirName, ...files);
if (dir) {
return dir;
}
return dir || workingDir;
return await findRootDir(dirName, ...files);
}

export async function findRootDir(
Expand Down

0 comments on commit 33ef59d

Please sign in to comment.