Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 additions & 11 deletions packages/varlock/src/cli/helpers/find-env-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,54 @@
// our tool may generate some additional files which we want to ignore
const SKIP_FILE_TYPES = ['.md', '.d.ts'];

// Directories to skip when searching for .env files
const SKIP_DIRECTORIES = ['node_modules', '.git', 'dist', 'build', '.next', '.nuxt', 'out'];

export async function findEnvFiles(opts?: {
cwd?: string,
searchSubdirectories?: boolean,
}) {
const cwd = opts?.cwd || process.cwd();
const searchSubdirectories = opts?.searchSubdirectories ?? false;

const envFiles = [];
const envFiles: string[] = [];

Check failure on line 17 in packages/varlock/src/cli/helpers/find-env-files.ts

View workflow job for this annotation

GitHub Actions / build

Array type using 'string[]' is forbidden. Use 'Array<string>' instead

Check failure on line 17 in packages/varlock/src/cli/helpers/find-env-files.ts

View workflow job for this annotation

GitHub Actions / build

Array type using 'string[]' is forbidden. Use 'Array<string>' instead
const envFilesInSubdirs: string[] = [];

Check failure on line 18 in packages/varlock/src/cli/helpers/find-env-files.ts

View workflow job for this annotation

GitHub Actions / build

Array type using 'string[]' is forbidden. Use 'Array<string>' instead

Check failure on line 18 in packages/varlock/src/cli/helpers/find-env-files.ts

View workflow job for this annotation

GitHub Actions / build

Array type using 'string[]' is forbidden. Use 'Array<string>' instead

const filesWithinDir = await fs.readdir(cwd);
async function scanDirectory(dir: string, isRootDir: boolean) {
const filesWithinDir = await fs.readdir(dir, { withFileTypes: true });

// Filter for files starting with .env and check if they exist
for (const fileName of filesWithinDir) {
if (fileName === '.env' || fileName.startsWith('.env.')) { // this ignores `.envrc` files
let skip = false;
for (const fileType of SKIP_FILE_TYPES) {
if (fileName.endsWith(fileType)) skip = true;
// Filter for files starting with .env and check if they exist
for (const entry of filesWithinDir) {
if (entry.isFile()) {
const fileName = entry.name;
if (fileName === '.env' || fileName.startsWith('.env.')) { // this ignores `.envrc` files
let skip = false;
for (const fileType of SKIP_FILE_TYPES) {
if (fileName.endsWith(fileType)) skip = true;
}
if (skip) continue;

Check failure on line 33 in packages/varlock/src/cli/helpers/find-env-files.ts

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed

Check failure on line 33 in packages/varlock/src/cli/helpers/find-env-files.ts

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
const filePath = path.join(dir, fileName);
if (isRootDir) {
envFiles.push(filePath);
} else {
envFilesInSubdirs.push(filePath);
}
}
} else if (entry.isDirectory() && searchSubdirectories && !isRootDir) {
// Only search subdirectories if we're looking for monorepo detection
// Skip common directories that shouldn't contain .env files
if (!SKIP_DIRECTORIES.includes(entry.name)) {
await scanDirectory(path.join(dir, entry.name), false);
}
}
if (skip) continue;
envFiles.push(path.join(cwd, fileName));
}
}

await scanDirectory(cwd, true);

// TODO: we may want to look up or down the folder tree?
// TODO: we could support looking within specific directories ("config", "env", etc)

return envFiles;
return { envFiles, envFilesInSubdirs };
}
Loading