-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add out-of-box support for
.env
files (#527)
- Loading branch information
1 parent
e9ae81b
commit 4ef3ecf
Showing
13 changed files
with
434 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
HOST="123.123.123.123" | ||
# USER0="user" | ||
USER1='#user' # main user | ||
USER2=support # TT1 # TT2 | ||
PASS1="nmnm@!sdf&*#@'RBUY3efPZpsqufHdhgdfhU!Bi90q.Zm.b7.C-8fpOIUSH&%GN" | ||
PASS2='nmnm@!sdf&*#@"RBUY3efPZpsqufHdhgdfhU!Bi90q.Zm.b7.C-8fpOIUSH&%GN' | ||
PASS3=nmnm@!sdf*@RBUY3efPZpsqufHdhgdfhU!Bi90q.Zm.b7.C-8fpOIUSH%GN | ||
PORT1=8080 | ||
PORT2=#8081 | ||
PORT3=${MY_PORT} # undefined variable | ||
WHO_AM_I="I'm ${MY_VAR}" | ||
SPACED1 = yes | ||
SPACED2 = "yes" | ||
SPACED3 = 'yes' | ||
NO_VALUE1 | ||
NO_VALUE2= | ||
NO_VALUE3 = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* c8 ignore start */ // ? | ||
import { readFile } from '../../polyfills/fs.js'; | ||
import { sanitizePath } from './list-files.js'; | ||
import { | ||
parseEnvLine, | ||
removeComments, | ||
resolveEnvVariables, | ||
} from '../../services/env.js'; | ||
|
||
const regex = { | ||
comment: /^\s*#/, | ||
}; | ||
|
||
/** Reads an environment file and sets the environment variables. */ | ||
export const envFile = async (filePath = '.env') => { | ||
/* c8 ignore stop */ | ||
const mapEnv = new Map<string, string>(); | ||
const env = await readFile(sanitizePath(filePath), 'utf8'); | ||
const lines = env | ||
.split('\n') | ||
.map((line) => removeComments(line.trim())) | ||
.filter((line) => line.length > 0 && !regex.comment.test(line)); | ||
|
||
for (const line of lines) { | ||
const parsedLine = parseEnvLine(line); | ||
|
||
if (parsedLine) { | ||
const { arg, value } = parsedLine; | ||
|
||
mapEnv.set(arg, value ? resolveEnvVariables(value, process.env) : value); | ||
} | ||
} | ||
|
||
for (const [arg, value] of mapEnv) { | ||
process.env[arg] = value; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* c8 ignore next */ // ? | ||
export const removeComments = (input: string) => { | ||
let output = ''; | ||
let quoteChar = ''; | ||
let inQuote = false; | ||
|
||
for (let i = 0; i < input.length; i++) { | ||
const char = input[i]; | ||
|
||
if (inQuote) { | ||
output += char; | ||
|
||
if (char === quoteChar && input[i - 1] !== '\\') { | ||
inQuote = false; | ||
} | ||
} else if (char === '"' || char === "'") { | ||
inQuote = true; | ||
quoteChar = char; | ||
output += char; | ||
} else if (char === '#') { | ||
break; | ||
} else { | ||
output += char; | ||
} | ||
} | ||
|
||
return output.trim(); | ||
}; | ||
|
||
/* c8 ignore net */ // ? | ||
export const parseEnvLine = (line: string) => { | ||
const index = line.indexOf('='); | ||
|
||
if (index === -1) { | ||
return null; | ||
} | ||
|
||
const arg = line.substring(0, index).trim(); | ||
const value = line | ||
.substring(index + 1) | ||
.trim() | ||
.replace(/^['"]|['"]$/g, ''); | ||
|
||
return { arg, value }; | ||
}; | ||
|
||
/* c8 ignore next */ // ? | ||
export const resolveEnvVariables = (str: string, env: typeof process.env) => { | ||
let result = ''; | ||
let i = 0; | ||
|
||
while (i < str.length) { | ||
if (str[i] === '$' && str[i + 1] === '{') { | ||
i += 2; | ||
|
||
let varName = ''; | ||
|
||
while (i < str.length && str[i] !== '}') { | ||
varName += str[i]; | ||
i++; | ||
} | ||
|
||
i++; | ||
result += env[varName] || ''; | ||
} else { | ||
result += str[i]; | ||
i++; | ||
} | ||
} | ||
|
||
return result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import process from 'node:process'; | ||
import { test } from '../../../src/modules/helpers/test.js'; | ||
import { skip } from '../../../src/modules/helpers/skip.js'; | ||
import { assert } from '../../../src/modules/essentials/assert.js'; | ||
|
||
if (process.env.My_VAR !== 'Poku') { | ||
skip("Skip tests when MY_VAR is not set to 'Poku'"); | ||
} | ||
|
||
test('Defining Variables', () => { | ||
assert.strictEqual(process.env.HOST, '123.123.123.123', 'Basic'); | ||
assert.strictEqual(process.env.USER0, undefined, 'Comented Line'); | ||
assert.strictEqual(process.env.USER1, '#user', 'Using quoted #'); | ||
assert.strictEqual( | ||
process.env.USER2, | ||
'support', | ||
'Comments after the variable' | ||
); | ||
assert.strictEqual( | ||
process.env.PASS1, | ||
"nmnm@!sdf&*#@'RBUY3efPZpsqufHdhgdfhU!Bi90q.Zm.b7.C-8fpOIUSH&%GN", | ||
'Complex string double quoted' | ||
); | ||
assert.strictEqual( | ||
process.env.PASS2, | ||
'nmnm@!sdf&*#@"RBUY3efPZpsqufHdhgdfhU!Bi90q.Zm.b7.C-8fpOIUSH&%GN', | ||
'Complex single double quoted' | ||
); | ||
assert.strictEqual( | ||
process.env.PASS3, | ||
'nmnm@!sdf*@RBUY3efPZpsqufHdhgdfhU!Bi90q.Zm.b7.C-8fpOIUSH%GN', | ||
'Complex string no quoted' | ||
); | ||
assert.strictEqual(process.env.PORT1, '8080', 'Using a number'); | ||
assert.strictEqual( | ||
process.env.PORT2, | ||
'', | ||
'Valid env with full commented value' | ||
); | ||
assert.strictEqual(process.env.PORT3, '', 'Undefined local variable'); | ||
assert.strictEqual( | ||
process.env.WHO_AM_I, | ||
"I'm Poku", | ||
'Resolved Env Variables' | ||
); | ||
assert.strictEqual( | ||
process.env.SPACED1, | ||
'yes', | ||
'Using space between env and value' | ||
); | ||
assert.strictEqual( | ||
process.env.SPACED2, | ||
'yes', | ||
'Using space between env and value with double quotes' | ||
); | ||
assert.strictEqual( | ||
process.env.SPACED3, | ||
'yes', | ||
'Using space between env and value with single quotes' | ||
); | ||
assert.strictEqual( | ||
process.env.NO_VALUE1, | ||
undefined, | ||
'Undefined value (invalid)' | ||
); | ||
assert.strictEqual(process.env.NO_VALUE2, '', 'No value (valid)'); | ||
assert.strictEqual(process.env.NO_VALUE3, '', 'No value (valid with spaces)'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.