Skip to content

Commit

Permalink
chore: rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Dec 28, 2024
1 parent 24441a6 commit 94356b5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
14 changes: 2 additions & 12 deletions src/goods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
fs,
minimist,
nodeFetch,
parseDotenv,
type RequestInfo,
type RequestInit,
} from './vendor.js'
Expand Down Expand Up @@ -224,18 +225,7 @@ export async function spinner<T>(
* Read env files and collects it into environment variables
*/
export const dotenv = (() => {
const parse = (content: string | Buffer): NodeJS.ProcessEnv =>
content
.toString()
.split(/\r?\n/)
.reduce<NodeJS.ProcessEnv>((r, line) => {
if (line.startsWith('export ')) line = line.slice(7)
const i = line.indexOf('=')
const k = line.slice(0, i).trim()
const v = line.slice(i + 1).trim()
if (k && v) r[k] = v
return r
}, {})
const parse = parseDotenv

const _load = (
read: (file: string) => string,
Expand Down
8 changes: 1 addition & 7 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@
import os from 'node:os'
import path from 'node:path'
import fs from 'node:fs'
import {
chalk,
type RequestInfo,
type RequestInit,
parseDotenv,
} from './vendor-core.js'
import { chalk, type RequestInfo, type RequestInit } from './vendor-core.js'
import { inspect } from 'node:util'

export { isStringLiteral, parseDotenv } from './vendor-core.js'
Expand Down Expand Up @@ -362,4 +357,3 @@ export const toCamelCase = (str: string) =>

export const parseBool = (v: string): boolean | string =>
({ true: true, false: false })[v] ?? v

23 changes: 18 additions & 5 deletions test/goods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ describe('goods', () => {

describe('dotenv', () => {
test('parse()', () => {
assert.deepEqual(dotenv.parse(''), {})
assert.deepEqual(
dotenv.parse('ENV=v1\nENV2=v2\n\n\n ENV3 = v3 \nexport ENV4=v4'),
{
Expand All @@ -185,15 +186,27 @@ describe('goods', () => {
ENV4: 'v4',
}
)
assert.deepEqual(dotenv.parse(''), {})

// TBD: multiline
const multiline = `SIMPLE=xyz123
NON_INTERPOLATED='raw text without variable interpolation'
# comment ###
NON_INTERPOLATED='raw text without variable interpolation'
MULTILINE = """
long text here,
long text here, # not-comment
e.g. a private SSH key
"""`
"""
ENV=v1\nENV2=v2\n\n\n\t\t ENV3 = v3 \n export ENV4=v4
ENV5=v5 # comment
`
assert.deepEqual(dotenv.parse(multiline), {
SIMPLE: 'xyz123',
NON_INTERPOLATED: 'raw text without variable interpolation',
MULTILINE: 'long text here, # not-comment\ne.g. a private SSH key',
ENV: 'v1',
ENV2: 'v2',
ENV3: 'v3',
ENV4: 'v4',
ENV5: 'v5',
})
})

describe('load()', () => {
Expand Down

0 comments on commit 94356b5

Please sign in to comment.