Smart configuration loader
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

perf: use native dotenv parser (#296)

authored by

Pooya Parsa and committed by
GitHub
2afe0059 25ee78fb

+37 -12
+5 -1
package.json
··· 26 26 "dependencies": { 27 27 "confbox": "^0.2.4", 28 28 "defu": "^6.1.4", 29 - "dotenv": "^17.2.4", 30 29 "exsolve": "^1.0.8", 31 30 "pathe": "^2.0.3", 32 31 "pkg-types": "^2.3.0", ··· 39 38 "automd": "^0.4.3", 40 39 "changelogen": "^0.6.2", 41 40 "chokidar": "^5.0.0", 41 + "dotenv": "^17.2.4", 42 42 "eslint-config-unjs": "^0.6.2", 43 43 "expect-type": "^1.3.0", 44 44 "giget": "^3.1.2", ··· 54 54 }, 55 55 "peerDependencies": { 56 56 "chokidar": "^5", 57 + "dotenv": "*", 57 58 "giget": "*", 58 59 "jiti": "*", 59 60 "magicast": "*" 60 61 }, 61 62 "peerDependenciesMeta": { 63 + "dotenv": { 64 + "optional": true 65 + }, 62 66 "magicast": { 63 67 "optional": true 64 68 },
+6 -6
pnpm-lock.yaml
··· 14 14 defu: 15 15 specifier: ^6.1.4 16 16 version: 6.1.4 17 - dotenv: 18 - specifier: ^17.2.4 19 - version: 17.2.4 20 17 exsolve: 21 18 specifier: ^1.0.8 22 19 version: 1.0.8 23 - giget: 24 - specifier: ^3.1.2 25 - version: 3.1.2 26 20 pathe: 27 21 specifier: ^2.0.3 28 22 version: 2.0.3 ··· 51 45 chokidar: 52 46 specifier: ^5.0.0 53 47 version: 5.0.0 48 + dotenv: 49 + specifier: ^17.2.4 50 + version: 17.2.4 54 51 eslint-config-unjs: 55 52 specifier: ^0.6.2 56 53 version: 0.6.2(eslint@9.39.2(jiti@2.6.1))(typescript@5.9.3) 57 54 expect-type: 58 55 specifier: ^1.3.0 59 56 version: 1.3.0 57 + giget: 58 + specifier: ^3.1.2 59 + version: 3.1.2 60 60 jiti: 61 61 specifier: ^2.6.1 62 62 version: 2.6.1
+24 -3
src/dotenv.ts
··· 1 - import { promises as fsp, statSync } from "node:fs"; 1 + import { readFileSync, statSync } from "node:fs"; 2 + import * as nodeUtil from "node:util"; 2 3 import { resolve } from "pathe"; 3 - import * as dotenv from "dotenv"; 4 4 5 5 export interface DotenvOptions { 6 6 /** ··· 88 88 if (!statSync(dotenvFile, { throwIfNoEntry: false })?.isFile()) { 89 89 continue; 90 90 } 91 - const parsed = dotenv.parse(await fsp.readFile(dotenvFile, "utf8")); 91 + const parsed = await readEnvFile(dotenvFile); 92 92 for (const key in parsed) { 93 93 if (key in environment && !dotenvVars.has(key)) { 94 94 continue; // Do not override existing env variables ··· 104 104 } 105 105 106 106 return environment; 107 + } 108 + 109 + // --- readEnvFile --- 110 + 111 + type ParseEnvFn = (src: string) => Record<string, string>; 112 + 113 + let _parseEnv = nodeUtil.parseEnv as ParseEnvFn | undefined; 114 + 115 + async function readEnvFile(path: string): Promise<Record<string, string>> { 116 + const src = readFileSync(path, "utf8"); 117 + if (!_parseEnv) { 118 + try { 119 + const dotenv = await import("dotenv"); 120 + _parseEnv = (src: string) => dotenv.parse(src) as Record<string, string>; 121 + } catch { 122 + throw new Error( 123 + "Failed to parse .env file: `node:util.parseEnv` is not available and `dotenv` package is not installed. Please upgrade your runtime or install `dotenv` as a dependency.", 124 + ); 125 + } 126 + } 127 + return _parseEnv(src); 107 128 } 108 129 109 130 // Based on https://github.com/motdotla/dotenv-expand
+2 -2
test/dotenv.test.ts
··· 12 12 13 13 describe("update config file", () => { 14 14 beforeEach(async () => { 15 - await rm(tmpDir, { recursive: true, force: true }); 15 + await rm(tmpDir, { recursive: true, force: true }).catch(() => {}); 16 16 await mkdir(tmpDir, { recursive: true }); 17 17 }); 18 18 afterAll(async () => { 19 - await rm(tmpDir, { recursive: true, force: true }); 19 + // await rm(tmpDir, { recursive: true, force: true }); 20 20 await unlink(cwdEnvPath).catch(console.error); 21 21 }); 22 22 it("should read .env file into process.env", async () => {