Skip to content

Commit fdfe46a

Browse files
authored
fix: dot notation when line break after process.env (#425)
* chore(deps): updated dev depen and pnpm * chore: updated pnpm * fix: dot notation when line break after process.env * fix(cli): adjusted --explain ui * fix: prevent extra blank line when adding missing keys to empty .env files * chore: removed trust downgrade * chore: updated node version in workflow
1 parent 48136fc commit fdfe46a

17 files changed

Lines changed: 257 additions & 180 deletions

File tree

.changeset/bright-parrots-start.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'dotenv-diff': patch
3+
---
4+
5+
adjustet --explain ui

.changeset/little-dolls-walk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'dotenv-diff': patch
3+
---
4+
5+
detects dot notation when line break appears after process.env

.changeset/polite-pants-cheer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'dotenv-diff': patch
3+
---
4+
5+
prevent extra blank line when adding missing keys to empty .env files

.github/workflows/build-test-lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- name: Install pnpm
2323
uses: pnpm/action-setup@0e279bb959325dab635dd2c09392533439d90093
2424
with:
25-
version: 10.33.0
25+
version: 11.3.0
2626
run_install: false
2727

2828
- name: Install dependencies

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
- name: Install pnpm
3535
uses: pnpm/action-setup@0e279bb959325dab635dd2c09392533439d90093
3636
with:
37-
version: 10.33.0
37+
version: 11.3.0
3838

3939
- uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e
4040
with:

.github/workflows/test-coverage.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ jobs:
2020

2121
- uses: pnpm/action-setup@0e279bb959325dab635dd2c09392533439d90093
2222
with:
23-
version: 10.33.0
23+
version: 11.3.0
2424

2525
- uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e
2626
with:
27-
node-version: 20
27+
node-version: 24
2828
cache: 'pnpm'
2929

3030
- run: pnpm install --frozen-lockfile

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@
3232
"@repo/eslint-config": "workspace:*",
3333
"@repo/tsconfig": "workspace:*",
3434
"@repo/vitest": "workspace:*",
35-
"@types/node": "^25.9.0",
35+
"@types/node": "^25.9.1",
3636
"@types/prompts": "^2.4.9",
37-
"@typescript-eslint/eslint-plugin": "^8.59.3",
38-
"@typescript-eslint/parser": "^8.59.3",
39-
"@vitest/coverage-v8": "4.1.6",
37+
"@typescript-eslint/eslint-plugin": "^8.59.4",
38+
"@typescript-eslint/parser": "^8.59.4",
39+
"@vitest/coverage-v8": "4.1.7",
4040
"eslint": "^10.4.0",
4141
"husky": "^9.1.7",
4242
"lint-staged": "^17.0.5",
4343
"prettier": "^3.8.3",
4444
"turbo": "^2.9.14",
4545
"typescript": "^6.0.3",
46-
"vitest": "^4.1.6"
46+
"vitest": "^4.1.7"
4747
},
48-
"packageManager": "pnpm@10.33.0"
48+
"packageManager": "pnpm@11.3.0"
4949
}

packages/@repo/eslint-config/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
"format": "prettier --write \"**/*.{ts,js,json,md,yml}\""
1313
},
1414
"devDependencies": {
15-
"@typescript-eslint/eslint-plugin": "^8.59.3",
16-
"@typescript-eslint/parser": "^8.59.3",
15+
"@typescript-eslint/eslint-plugin": "^8.59.4",
16+
"@typescript-eslint/parser": "^8.59.4",
1717
"eslint": "^10.4.0"
1818
}
1919
}

packages/cli/src/core/scan/patterns.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ export function buildSveltekitAliasPatterns(
5353
export const ENV_PATTERNS: Pattern[] = [
5454
/**
5555
* Matches process.env.KEY references in source code.
56-
* Supports both dot notation and bracket notation.
56+
* Supports both dot notation and bracket notation, including whitespace
57+
* and line breaks between `env` and the accessor.
5758
*
5859
* Examples:
5960
* process.env.MY_KEY
@@ -63,7 +64,7 @@ export const ENV_PATTERNS: Pattern[] = [
6364
{
6465
name: 'process.env' as const,
6566
regex:
66-
/process\.env\.([A-Z_][A-Z0-9_]*)|process\.env\[['"]([A-Z_][A-Z0-9_]*)['"]]/g,
67+
/process\.env\s*\.([A-Z_][A-Z0-9_]*)|process\.env\s*\[['"]([A-Z_][A-Z0-9_]*)['"]]/g,
6768
processor: (match) => {
6869
// match[1] covers dot notation: process.env.KEY
6970
// match[2] covers bracket notation: process.env['KEY']
@@ -103,7 +104,8 @@ export const ENV_PATTERNS: Pattern[] = [
103104

104105
/**
105106
* Matches import.meta.env.KEY references in source code.
106-
* Supports both dot notation and bracket notation.
107+
* Supports both dot notation and bracket notation, including whitespace
108+
* and line breaks between `env` and the accessor.
107109
*
108110
* Examples:
109111
* import.meta.env.MY_KEY
@@ -113,7 +115,7 @@ export const ENV_PATTERNS: Pattern[] = [
113115
{
114116
name: 'import.meta.env' as const,
115117
regex:
116-
/import\.meta\.env\.([A-Z_][A-Z0-9_]*)|import\.meta\.env\[['"]([A-Z_][A-Z0-9_]*)['"]]/g,
118+
/import\.meta\.env\s*\.([A-Z_][A-Z0-9_]*)|import\.meta\.env\s*\[['"]([A-Z_][A-Z0-9_]*)['"]]/g,
117119
processor: (match) => {
118120
const variable = match[1] || match[2];
119121
return variable ? [variable] : [];

packages/cli/src/services/fixEnv.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,9 @@ export function applyFixes(options: ApplyFixesOptions): {
7979
if (missingKeys.length) {
8080
const content = fs.readFileSync(envPath, 'utf-8');
8181
const eol = content.includes('\r\n') ? '\r\n' : '\n';
82+
const separator = content.length > 0 && !content.endsWith('\n') ? eol : '';
8283
const newContent =
83-
content +
84-
(content.endsWith('\n') ? '' : eol) +
85-
missingKeys.map((k) => `${k}=`).join(eol) +
86-
eol;
84+
content + separator + missingKeys.map((k) => `${k}=`).join(eol) + eol;
8785
fs.writeFileSync(envPath, newContent);
8886
result.addedEnv = missingKeys;
8987
}

0 commit comments

Comments
 (0)