Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/gentle-sheep-feel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'dotenv-diff': minor
---

added --list-all flag to view all environment variables found in codebase
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ jobs:
commit: 'chore(release): publish to npm'
title: 'chore(release): publish to npm'
env:
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
NPM_CONFIG_PROVENANCE: true
39 changes: 32 additions & 7 deletions .github/workflows/test-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,39 @@ jobs:
run: pip install diff-cover

- name: Run coverage
run: pnpm run coverage
run: pnpm --filter dotenv-diff run coverage

- name: Fix lcov paths
- name: Locate and normalize coverage files
id: coverage_files
run: |
if [ -f "coverage/lcov.info" ]; then
sed -i "s|^SF:|SF:|g" coverage/lcov.info
LCOV_FILE="coverage/lcov.info"
SUMMARY_FILE="coverage/coverage-summary.json"

if [ -f "packages/cli/coverage/lcov.info" ]; then
LCOV_FILE="packages/cli/coverage/lcov.info"
fi

if [ -f "packages/cli/coverage/coverage-summary.json" ]; then
SUMMARY_FILE="packages/cli/coverage/coverage-summary.json"
fi

if [ ! -f "$LCOV_FILE" ]; then
echo "Missing lcov file at $LCOV_FILE"
exit 1
fi

if [ ! -f "$SUMMARY_FILE" ]; then
echo "Missing coverage summary at $SUMMARY_FILE"
exit 1
fi

# Normalize source-file paths for monolith layout so diff-cover can
# map coverage entries to changed files under packages/cli.
sed -i -E 's|^SF:src/|SF:packages/cli/src/|; s|^SF:src\\|SF:packages/cli/src/|; s|^SF:packages\\cli\\src\\|SF:packages/cli/src/|' "$LCOV_FILE"

echo "lcov_file=$LCOV_FILE" >> $GITHUB_OUTPUT
echo "summary_file=$SUMMARY_FILE" >> $GITHUB_OUTPUT

- name: Run diff-cover analysis
id: diff_cover
run: |
Expand All @@ -51,11 +76,11 @@ jobs:
all_passed=true

overall_coverage="N/A"
if [ -f "coverage/coverage-summary.json" ]; then
overall_coverage=$(jq -r '.total.lines.pct' "coverage/coverage-summary.json")
if [ -f "${{ steps.coverage_files.outputs.summary_file }}" ]; then
overall_coverage=$(jq -r '.total.lines.pct' "${{ steps.coverage_files.outputs.summary_file }}")
fi

diff-cover coverage/lcov.info \
diff-cover "${{ steps.coverage_files.outputs.lcov_file }}" \
--compare-branch=origin/${{ github.event.pull_request.base.ref }} \
--diff-range-notation=... \
--json-report diff-coverage.json \
Expand Down
25 changes: 25 additions & 0 deletions docs/configuration_and_flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ CLI flags always take precedence over configuration file values.

### Display Options

- [--list-all](#--list-all)
- [--show-unused](#--show-unused)
- [--no-show-unused](#--no-show-unused)
- [--show-stats](#--show-stats)
Expand Down Expand Up @@ -437,6 +438,30 @@ If you later want to scan files from one of the default excluded paths, use `--i

## Display Options

### `--list-all`

Scans the codebase and prints all unique environment variable names found.

This is useful when you want a quick overview of every environment variable your project references.

The list is sorted alphabetically and deduplicated across all usages.

Example usage:

```bash
dotenv-diff --list-all
```

Usage in the configuration file:

```json
{
"listAll": true
}
```

---

### `--show-unused`

List variables that are defined in `.env` but not used in the codebase (enabled by default).
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@types/prompts": "^2.4.9",
"@typescript-eslint/eslint-plugin": "^8.59.1",
"@typescript-eslint/parser": "^8.59.1",
"@vitest/coverage-v8": "4.1.4",
"@vitest/coverage-v8": "4.1.5",
"eslint": "^10.2.1",
"husky": "^9.1.7",
"lint-staged": "^16.4.0",
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/cli/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,9 @@ export function createProgram() {
'Disable inconsistent naming pattern warnings',
)
.option('--init', 'Create a sample dotenv-diff.config.json file')
.option(
'--list-all',
'List all unique environment variable keys found in codebase',
)
);
}
1 change: 1 addition & 0 deletions packages/cli/src/cli/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ async function runScanMode(opts: Options): Promise<boolean> {
uppercaseKeys: opts.uppercaseKeys,
expireWarnings: opts.expireWarnings,
inconsistentNamingWarnings: opts.inconsistentNamingWarnings,
listAll: opts.listAll,
});

return exitWithError;
Expand Down
6 changes: 5 additions & 1 deletion packages/cli/src/commands/scanUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ export async function scanUsage(opts: ScanUsageOptions): Promise<ExitResult> {

// JSON output
if (opts.json) {
const jsonOutput = scanJsonOutput(scanResult, comparedAgainst);
const jsonOutput = scanJsonOutput(
scanResult,
comparedAgainst,
opts.listAll ?? false,
);
console.log(JSON.stringify(jsonOutput, null, 2));

// Check for high severity secrets
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/config/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export function normalizeOptions(raw: RawOptions): Options {
const uppercaseKeys = raw.uppercaseKeys !== false;
const expireWarnings = raw.expireWarnings !== false;
const inconsistentNamingWarnings = raw.inconsistentNamingWarnings !== false;
const listAll = toBool(raw.listAll);

const cwd = process.cwd();
const envFlag =
Expand Down Expand Up @@ -96,6 +97,7 @@ export function normalizeOptions(raw: RawOptions): Options {
uppercaseKeys,
expireWarnings,
inconsistentNamingWarnings,
listAll,
};
}

Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export interface RawOptions {
uppercaseKeys?: boolean;
expireWarnings?: boolean;
inconsistentNamingWarnings?: boolean;
listAll?: boolean;
}

/**
Expand Down Expand Up @@ -135,6 +136,7 @@ export interface Options {
uppercaseKeys: boolean;
expireWarnings: boolean;
inconsistentNamingWarnings: boolean;
listAll: boolean;
}

export type EnvPatternName = 'process.env' | 'import.meta.env' | 'sveltekit';
Expand Down Expand Up @@ -228,6 +230,7 @@ export interface ScanUsageOptions extends ScanOptions {
uppercaseKeys?: boolean;
expireWarnings?: boolean;
inconsistentNamingWarnings?: boolean;
listAll?: boolean;
}

/**
Expand Down
7 changes: 6 additions & 1 deletion packages/cli/src/services/printScanResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { computeHealthScore } from '../core/scan/computeHealthScore.js';
import { printHealthScore } from '../ui/scan/printHealthScore.js';
import { printExpireWarnings } from '../ui/scan/printExpireWarnings.js';
import { printInconsistentNamingWarning } from '../ui/scan/printInconsistentNamingWarning.js';
import { printListAll } from '../ui/scan/printListAll.js';

/**
* Prints the scan result to the console.
Expand All @@ -46,6 +47,10 @@ export function printScanResult(
// Determine if output should be in JSON format
const isJson = opts.json;

if (opts.listAll) {
printListAll(scanResult.used);
}

printHeader(comparedAgainst);

// Show stats if requested
Expand Down Expand Up @@ -102,7 +107,7 @@ export function printScanResult(
printSecrets(scanResult.secrets, opts.strict);
}
// Console log usage warning
if (scanResult.logged) {
if (scanResult.logged?.length) {
printConsolelogWarning(scanResult.logged, opts.strict);
}

Expand Down
27 changes: 27 additions & 0 deletions packages/cli/src/ui/scan/printListAll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { EnvUsage } from '../../config/types.js';
import { accent, dim, header, divider } from '../theme.js';

/**
* Prints all unique environment variable names found in the codebase scan.
* @param usages - All environment variable usages found during the scan.
*/
export function printListAll(usages: EnvUsage[]): void {
const uniqueVars = [...new Set(usages.map((u) => u.variable))].sort();

if (uniqueVars.length === 0) {
console.log(dim('\nNo environment variables found in codebase.\n'));
return;
}

console.log(
`\n${accent('▸')} ${header('Environment variables found in codebase')}`,
);
console.log(`${divider}`);

for (const key of uniqueVars) {
console.log(`${accent(key)}`);
}

console.log(`${divider}`);
console.log(dim(`${uniqueVars.length} unique variable(s)\n`));
}
8 changes: 8 additions & 0 deletions packages/cli/src/ui/scan/scanJsonOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { normalizePath } from '../../core/helpers/normalizePath.js';
*/
interface ScanJsonOutput {
stats?: ScanStats;
listAll?: string[];
missing?: Array<{
variable: string;
usages: Array<{
Expand Down Expand Up @@ -64,9 +65,16 @@ interface ScanJsonOutput {
export function scanJsonOutput(
scanResult: ScanResult,
comparedAgainst: string,
listAll: boolean = false,
): ScanJsonOutput {
const output: ScanJsonOutput = {};

if (listAll) {
output.listAll = [
...new Set(scanResult.used.map((usage) => usage.variable)),
].sort();
}

// Add comparison info if we compared against a file
if (comparedAgainst) {
output.comparedAgainst = comparedAgainst;
Expand Down
1 change: 1 addition & 0 deletions packages/cli/test/unit/cli/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function createBaseOptions(overrides: Partial<Options> = {}): Options {
uppercaseKeys: true,
expireWarnings: true,
inconsistentNamingWarnings: true,
listAll: false,
...overrides,
};
}
Expand Down
Loading