diff --git a/src/goods.ts b/src/goods.ts index 511d725527..2d5789d7c1 100644 --- a/src/goods.ts +++ b/src/goods.ts @@ -28,6 +28,7 @@ import { fs, minimist, nodeFetch, + parseDotenv, type RequestInfo, type RequestInit, } from './vendor.js' @@ -224,18 +225,7 @@ export async function spinner( * 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((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, diff --git a/src/util.ts b/src/util.ts index d03e243225..73fe38d93b 100644 --- a/src/util.ts +++ b/src/util.ts @@ -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' @@ -362,4 +357,3 @@ export const toCamelCase = (str: string) => export const parseBool = (v: string): boolean | string => ({ true: true, false: false })[v] ?? v - diff --git a/test/goods.test.js b/test/goods.test.js index 18db48ebe9..e41d8e544f 100644 --- a/test/goods.test.js +++ b/test/goods.test.js @@ -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'), { @@ -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()', () => {