Skip to content

Commit e9d4394

Browse files
committed
chore: refactor parse env
1 parent 681ee3c commit e9d4394

10 files changed

Lines changed: 49 additions & 33 deletions

File tree

src/core/compare/parseAndFilterEnv.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parseEnvFile } from '../parseEnv.js';
1+
import { parseEnvFile } from '../../services/parseEnvFile.js';
22
import { filterIgnoredKeys } from '../filterIgnoredKeys.js';
33
import type { ComparisonOptions } from '../../config/types.js';
44

src/core/parseEnv.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
import fs from 'fs';
2-
31
/**
4-
* Parses a `.env` file and returns an object with key-value pairs.
2+
* Parses dotenv file content and returns an object with key-value pairs.
53
*
6-
* @param path - The file path to the `.env` file.
4+
* @param content - The raw dotenv file content.
75
* @returns A record object representing parsed environment variables.
86
*
97
* Lines that are empty or start with `#` (comments) are ignored.
108
* Multi-line or quoted values are not supported.
119
*/
12-
export function parseEnvFile(path: string): Record<string, string> {
13-
const content = safeFileSync(path);
10+
export function parseEnvContent(content: string): Record<string, string> {
1411
const lines = content.split('\n');
1512

1613
const result: Record<string, string> = {};
@@ -30,18 +27,3 @@ export function parseEnvFile(path: string): Record<string, string> {
3027

3128
return result;
3229
}
33-
34-
/**
35-
* Safely reads a file and returns its content as a string.
36-
* If the file does not exist or cannot be read, returns an empty string.
37-
*
38-
* @param path - The file path to read.
39-
* @returns The file content as a string, or an empty string if unreadable.
40-
*/
41-
function safeFileSync(path: string): string {
42-
try {
43-
return fs.readFileSync(path, 'utf-8');
44-
} catch {
45-
return '';
46-
}
47-
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
export { parseEnvFile } from './core/parseEnv.js';
1+
export { parseEnvFile } from './services/parseEnvFile.js';
2+
export { parseEnvContent } from './core/parseEnv.js';
23
export { diffEnv, type DiffResult } from './core/diffEnv.js';

src/services/parseEnvFile.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import fs from 'fs';
2+
import { parseEnvContent } from '../core/parseEnv.js';
3+
4+
/**
5+
* Parses a `.env` file and returns an object with key-value pairs.
6+
*
7+
* @param path - The file path to the `.env` file.
8+
* @returns A record object representing parsed environment variables.
9+
*/
10+
export function parseEnvFile(path: string): Record<string, string> {
11+
const content = safeFileSync(path);
12+
return parseEnvContent(content);
13+
}
14+
15+
/**
16+
* Safely reads a file and returns its content as a string.
17+
* If the file does not exist or cannot be read, returns an empty string.
18+
*
19+
* @param path - The file path to read.
20+
* @returns The file content as a string, or an empty string if unreadable.
21+
*/
22+
function safeFileSync(path: string): string {
23+
try {
24+
return fs.readFileSync(path, 'utf-8');
25+
} catch {
26+
return '';
27+
}
28+
}

src/services/processComparisonFile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs';
2-
import { parseEnvFile } from '../core/parseEnv.js';
2+
import { parseEnvFile } from './parseEnvFile.js';
33
import { filterIgnoredKeys } from '../core/filterIgnoredKeys.js';
44
import { compareWithEnvFiles } from '../core/scan/compareScan.js';
55
import { findDuplicateKeys } from '../core/duplicates.js';

test/integration/compare-flow.integration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, it, expect, afterEach } from 'vitest';
22
import path from 'path';
33
import { makeTmpDir, touch, rmrf } from '../utils/fs-helpers.js';
4-
import { parseEnvFile } from '../../src/core/parseEnv.js';
4+
import { parseEnvFile } from '../../src/services/parseEnvFile.js';
55
import { diffEnv } from '../../src/core/diffEnv.js';
66

77
const tmpDirs: string[] = [];

test/unit/commands/compare.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {
1010
} from '../../../src/config/types.js';
1111

1212
// ---- mocks ----
13-
vi.mock('../../../src/core/parseEnv.js', () => ({
13+
vi.mock('../../../src/services/parseEnvFile.js', () => ({
1414
parseEnvFile: vi.fn(),
1515
}));
1616

@@ -78,7 +78,7 @@ vi.mock('../../../src/ui/compare/printErrorNotFound.js', () => ({
7878
printErrorNotFound: vi.fn(),
7979
}));
8080

81-
import { parseEnvFile } from '../../../src/core/parseEnv.js';
81+
import { parseEnvFile } from '../../../src/services/parseEnvFile.js';
8282
import { diffEnv } from '../../../src/core/diffEnv.js';
8383
import { checkGitignoreStatus } from '../../../src/services/git.js';
8484
import { findDuplicateKeys } from '../../../src/core/duplicates.js';

test/unit/core/parseEnv.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
22
import fs from 'fs';
33
import path from 'path';
44
import os from 'os';
5-
import { parseEnvFile } from '../../../src/core/parseEnv.js';
5+
import { parseEnvFile } from '../../../src/services/parseEnvFile.js';
66

77
function tmpDir() {
88
return fs.mkdtempSync(path.join(os.tmpdir(), 'env-test-'));

test/unit/index.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import { describe, it, expect } from 'vitest';
2-
import { diffEnv, parseEnvFile } from '../../src/index.js';
2+
import { diffEnv, parseEnvFile, parseEnvContent } from '../../src/index.js';
33
import { diffEnv as diffEnvDirect } from '../../src/core/diffEnv.js';
4-
import { parseEnvFile as parseEnvFileDirect } from '../../src/core/parseEnv.js';
4+
import { parseEnvFile as parseEnvFileDirect } from '../../src/services/parseEnvFile.js';
5+
import { parseEnvContent as parseEnvContentDirect } from '../../src/core/parseEnv.js';
56

67
describe('index exports', () => {
7-
it('re-exports parseEnvFile from core/parseEnv', () => {
8+
it('re-exports parseEnvFile from services/parseEnvFile', () => {
89
expect(parseEnvFile).toBe(parseEnvFileDirect);
910
});
1011

12+
it('re-exports parseEnvContent from core/parseEnv', () => {
13+
expect(parseEnvContent).toBe(parseEnvContentDirect);
14+
});
15+
1116
it('re-exports diffEnv from core/diffEnv', () => {
1217
expect(diffEnv).toBe(diffEnvDirect);
1318
});

test/unit/services/processComparisonFile.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ vi.mock('fs', () => ({
1111
},
1212
}));
1313

14-
vi.mock('../../../src/core/parseEnv.js', () => ({
14+
vi.mock('../../../src/services/parseEnvFile.js', () => ({
1515
parseEnvFile: vi.fn(() => ({ A: '1', bKey: '2' })),
1616
}));
1717

@@ -58,7 +58,7 @@ vi.mock('../../../src/core/detectInconsistentNaming.js', () => ({
5858

5959
import { processComparisonFile } from '../../../src/services/processComparisonFile.js';
6060
import { applyFixes } from '../../../src/core/fixEnv.js';
61-
import { parseEnvFile } from '../../../src/core/parseEnv.js';
61+
import { parseEnvFile } from '../../../src/services/parseEnvFile.js';
6262

6363
describe('processComparisonFile', () => {
6464
const baseScanResult: ScanResult = {

0 commit comments

Comments
 (0)