diff --git a/CHANGELOG.md b/CHANGELOG.md index db7edb49..ff01f1ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/src/plugins/dotenv/dotenvVariableProvider.ts b/src/plugins/dotenv/dotenvVariableProvider.ts index 6c81436d..9d34a131 100644 --- a/src/plugins/dotenv/dotenvVariableProvider.ts +++ b/src/plugins/dotenv/dotenvVariableProvider.ts @@ -8,19 +8,8 @@ const defaultFiles: Array = ['.env']; export async function provideDotenvEnvironments(context: VariableProviderContext): Promise { const files: Array = []; - 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) { @@ -40,6 +29,17 @@ export async function provideDotenvEnvironments(context: VariableProviderContext }); } +async function readEnvDir(dir: string | undefined, context: VariableProviderContext): Promise> { + 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 @@ -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 = []; 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> { + const variables: Array = []; + 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) { @@ -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; diff --git a/src/store/httpFileStore.ts b/src/store/httpFileStore.ts index fa655ba6..c9d0b639 100644 --- a/src/store/httpFileStore.ts +++ b/src/store/httpFileStore.ts @@ -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; diff --git a/src/utils/fsUtils.ts b/src/utils/fsUtils.ts index a44cfe93..105dd691 100644 --- a/src/utils/fsUtils.ts +++ b/src/utils/fsUtils.ts @@ -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(