Skip to content

Commit

Permalink
chore: move custom dotenv parser to external pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Dec 24, 2024
1 parent 1852d43 commit 8bcbcde
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 61 deletions.
6 changes: 3 additions & 3 deletions .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{
"name": "zx/index",
"path": "build/*.{js,cjs}",
"limit": "805.5 kB",
"limit": "806 kB",
"brotli": false,
"gzip": false
},
Expand All @@ -23,14 +23,14 @@
{
"name": "vendor",
"path": "build/vendor-*",
"limit": "761 kB",
"limit": "762 kB",
"brotli": false,
"gzip": false
},
{
"name": "all",
"path": "build/*",
"limit": "842.5 kB",
"limit": "843 kB",
"brotli": false,
"gzip": false
}
Expand Down
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"create-require": "^1.1.1",
"depseek": "^0.4.1",
"dts-bundle-generator": "^9.5.1",
"envapi": "^0.0.0",
"esbuild": "^0.24.2",
"esbuild-node-externals": "^1.16.0",
"esbuild-plugin-entry-chunks": "^0.1.15",
Expand Down
1 change: 1 addition & 0 deletions scripts/build-dts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const entries = [
'@webpod/ingrid',
'chalk',
'zurk',
'envapi'
], // args['external-inlines'],
},
output,
Expand Down
59 changes: 2 additions & 57 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import os from 'node:os'
import path from 'node:path'
import fs from 'node:fs'
import { chalk, type RequestInfo, type RequestInit } from './vendor-core.js'
import { chalk, type RequestInfo, type RequestInit, parseDotenv } from './vendor-core.js'
import { inspect } from 'node:util'

export { isStringLiteral } from './vendor-core.js'
export { isStringLiteral, parseDotenv } from './vendor-core.js'

export function tempdir(prefix: string = `zx-${randomId()}`): string {
const dirpath = path.join(os.tmpdir(), prefix)
Expand Down Expand Up @@ -358,61 +358,6 @@ export const toCamelCase = (str: string) =>
export const parseBool = (v: string): boolean | string =>
({ true: true, false: false })[v] ?? v

// prettier-ignore
export const parseDotenv = (content: string): NodeJS.ProcessEnv => {
const kr = /^[a-zA-Z_]+\w*$/
const sr = /\s/
const e: Record<string, string> = {}
let k = ''
let b = ''
let q = ''
let i = 0
const cap = () => { if (b && k) {
if (!kr.test(k)) throw new Error(`Invalid identifier: ${k}`)
e[k] = b; b = k = ''
}}

for (const c of content.replace(/\r\n?/mg, '\n')) {
if (i) {
if (c === '\n') i = 0
continue
}
if (!q) {
if (c === '#') {
i = 1
continue
}
if (c === '\n') {
cap()
continue
}
if (sr.test(c)) {
if (!k && b === 'export') b = ''
continue
}
if (c === '=') {
if (!k) { k = b; b = ''; continue }
}
}

if (c === '"' || c === "'" || c === '`') {
if (!q) {
q = c
continue
}
if (q === c) {
q = ''
cap()
continue
}
}
b += c
}
cap()

return e
}

export const readEnvFromFile = (
filepath: string,
env: NodeJS.ProcessEnv = process.env
Expand Down
1 change: 1 addition & 0 deletions src/vendor-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ export type RequestInit = Parameters<typeof globalThis.fetch>[1]
export { default as chalk, type ChalkInstance } from 'chalk'
export { default as which } from 'which'
export { default as ps } from '@webpod/ps'
export { parse as parseDotenv } from 'envapi'
2 changes: 1 addition & 1 deletion test/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ ENV5=v5 # comment
assert.deepEqual(parseDotenv(multiline), {
SIMPLE: 'xyz123',
NON_INTERPOLATED: 'raw text without variable interpolation',
MULTILINE: '\nlong text here, # not-comment\ne.g. a private SSH key\n',
MULTILINE: 'long text here, # not-comment\ne.g. a private SSH key',
ENV: 'v1',
ENV2: 'v2',
ENV3: 'v3',
Expand Down

0 comments on commit 8bcbcde

Please sign in to comment.