@@ -19,27 +19,22 @@ export type FixResult = {
1919
2020/**
2121 * Applies fixes to the .env and .env.example files based on the detected issues.
22- *
22+ *
2323 * This function will:
2424 * - Remove duplicate keys from .env (keeping the last occurrence)
2525 * - Add missing keys to .env with empty values
2626 * - Add missing keys to .env.example (if not already present)
2727 * - Ensure .env is ignored in .gitignore (if in a git repo and ensureGitignore is true)
28- *
28+ *
2929 * @param options - Fix options including file paths and keys to fix
3030 * @returns An object indicating whether changes were made and details of the changes
3131 */
3232export function applyFixes ( options : ApplyFixesOptions ) : {
3333 changed : boolean ;
3434 result : FixResult ;
3535} {
36- const {
37- envPath,
38- examplePath,
39- missingKeys,
40- duplicateKeys,
41- ensureGitignore,
42- } = options ;
36+ const { envPath, examplePath, missingKeys, duplicateKeys, ensureGitignore } =
37+ options ;
4338
4439 const result : FixResult = {
4540 removedDuplicates : [ ] ,
@@ -53,12 +48,12 @@ export function applyFixes(options: ApplyFixesOptions): {
5348 const lines = fs . readFileSync ( envPath , 'utf-8' ) . split ( '\n' ) ;
5449 const seen = new Set < string > ( ) ;
5550 const newLines : string [ ] = [ ] ;
56-
51+
5752 // Process from bottom to top, keeping last occurrence
5853 for ( let i = lines . length - 1 ; i >= 0 ; i -- ) {
5954 const line = lines [ i ] ;
6055 if ( line === undefined ) continue ;
61-
56+
6257 const match = line . match ( / ^ \s * ( [ \w . - ] + ) \s * = / ) ;
6358 if ( match ) {
6459 const key = match [ 1 ] || '' ;
@@ -69,7 +64,7 @@ export function applyFixes(options: ApplyFixesOptions): {
6964 }
7065 newLines . unshift ( line ) ;
7166 }
72-
67+
7368 fs . writeFileSync ( envPath , newLines . join ( '\n' ) ) ;
7469 result . removedDuplicates = duplicateKeys ;
7570 }
@@ -96,7 +91,7 @@ export function applyFixes(options: ApplyFixesOptions): {
9691 . filter ( Boolean ) ,
9792 ) ;
9893 const newExampleKeys = missingKeys . filter ( ( k ) => ! existingExKeys . has ( k ) ) ;
99-
94+
10095 if ( newExampleKeys . length ) {
10196 const newExContent =
10297 exContent +
@@ -125,15 +120,15 @@ export function applyFixes(options: ApplyFixesOptions): {
125120/**
126121 * Ensures .env patterns are present in .gitignore at the git repository root.
127122 * This is a best-effort operation and will not throw errors.
128- *
123+ *
129124 * @param envPath - Path to the .env file to check gitignore for
130125 * @returns true if .gitignore was updated, false otherwise
131126 */
132127function updateGitignoreForEnv ( envPath : string ) : boolean {
133128 try {
134129 const startDir = path . dirname ( envPath ) ;
135130 const gitRoot = findGitRoot ( startDir ) ;
136-
131+
137132 if ( ! gitRoot || ! isGitRepo ( gitRoot ) ) {
138133 return false ;
139134 }
@@ -149,18 +144,17 @@ function updateGitignoreForEnv(envPath: string): boolean {
149144
150145 // Need to add patterns
151146 const patterns = [ '.env' , '.env.*' ] ;
152-
147+
153148 if ( fs . existsSync ( gitignorePath ) ) {
154149 const current = fs . readFileSync ( gitignorePath , 'utf8' ) ;
155150 const existingLines = current . split ( / \r ? \n / ) . map ( ( l ) => l . trim ( ) ) ;
156-
151+
157152 const missingPatterns = patterns . filter (
158- ( pattern ) => ! existingLines . includes ( pattern )
153+ ( pattern ) => ! existingLines . includes ( pattern ) ,
159154 ) ;
160155
161156 if ( missingPatterns . length ) {
162- const toAppend =
163- `${ current . endsWith ( '\n' ) ? '' : '\n' } ${ missingPatterns . join ( '\n' ) } \n` ;
157+ const toAppend = `${ current . endsWith ( '\n' ) ? '' : '\n' } ${ missingPatterns . join ( '\n' ) } \n` ;
164158 fs . appendFileSync ( gitignorePath , toAppend ) ;
165159 return true ;
166160 }
0 commit comments