Skip to content

Commit f29956f

Browse files
authored
refactor: replace parent-module with process.cwd() for path resolution (#3)
1 parent 8828d8c commit f29956f

File tree

5 files changed

+14
-41
lines changed

5 files changed

+14
-41
lines changed

.pnp.cjs

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ export type SecretSource = "google" | "aws" | "azure";
1111
* @see https://cloud.google.com/secret-manager
1212
* @example
1313
* const [env, secrets] = await loadEnv("development", {
14-
* root: "..",
15-
* schema: "./core/env.ts",
14+
* root: "./apps/backend", // relative to process.cwd()
15+
* schema: "./env.ts", // relative to process.cwd()
1616
* mergeTo: process.env,
1717
* });
1818
*
@@ -21,9 +21,9 @@ export type SecretSource = "google" | "aws" | "azure";
2121
*/
2222
export function loadEnv(
2323
environment: string | undefined,
24-
options: {
24+
options?: {
2525
/**
26-
* Where to look for `.env` files. Defaults to the current working directory.
26+
* Where to look for `.env` files. Defaults to the current working directory (process.cwd()).
2727
*/
2828
root?: string;
2929
/**
@@ -39,7 +39,7 @@ export function loadEnv(
3939
files?: string[] | string;
4040
/**
4141
* Path to the environment schema file exporting environment variables
42-
* required by the app at runtime.
42+
* required by the app at runtime. The path is resolved relative to process.cwd().
4343
*
4444
* @example
4545
* // env.ts

index.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import { expand } from "dotenv-expand";
77
import { build } from "esbuild";
88
import { importFromString } from "module-from-string";
99
import { readFile } from "node:fs/promises";
10-
import { dirname, resolve } from "node:path";
11-
import { fileURLToPath } from "node:url";
12-
import parentModule from "parent-module";
10+
import { resolve } from "node:path";
1311
import { readPackageUp } from "read-pkg-up";
1412

1513
// Matches the following patterns:
@@ -26,8 +24,8 @@ const secretRegExp = /^secret:\/\/(\w+)\/(.*)$/;
2624
* @see https://cloud.google.com/secret-manager
2725
* @example
2826
* const [env, secrets] = await loadEnv("development", {
29-
* root: "..",
30-
* schema: "./core/env.ts",
27+
* root: "./apps/backend", // relative to process.cwd()
28+
* schema: "./env.ts", // relative to process.cwd()
3129
* mergeTo: process.env,
3230
* });
3331
*
@@ -37,7 +35,7 @@ const secretRegExp = /^secret:\/\/(\w+)\/(.*)$/;
3735
* files?: string[] | string;
3836
* schema?: string;
3937
* mergeTo?: Record<string, unknown>;
40-
* }} options Loading options
38+
* }} options Loading options. The `root` defaults to `process.cwd()`, and `schema` is resolved relative to `process.cwd()`.
4139
* @return {Promise<[env: Record<string, string>, secrets: Map<string, { ref: string; source: "google" | "aws" | "azure" }]>}
4240
*/
4341
export async function loadEnv(environment, options) {
@@ -59,11 +57,7 @@ export async function loadEnv(environment, options) {
5957
}
6058

6159
// Load environment variables from the `.env` files
62-
const parentFile = parentModule();
63-
const parentDir = dirname(
64-
parentFile.startsWith("file://") ? fileURLToPath(parentFile) : parentFile,
65-
);
66-
const rootDir = resolve(parentDir, options?.root ?? ".");
60+
const rootDir = resolve(options?.root ?? process.cwd());
6761
const encoding = options?.encoding ?? "utf-8";
6862
const parsed = await Promise.all(
6963
files.map((file) => {
@@ -98,8 +92,8 @@ export async function loadEnv(environment, options) {
9892

9993
if (options?.schema) {
10094
// Transpile the schema file into ESM code
101-
const schemaFile = resolve(parentDir, options?.schema);
102-
const { packageJson } = await readPackageUp({ cwd: parentDir });
95+
const schemaFile = resolve(process.cwd(), options.schema);
96+
const { packageJson } = await readPackageUp({ cwd: process.cwd() });
10397
const {
10498
outputFiles: [{ text: contents }],
10599
} = await build({
@@ -118,7 +112,7 @@ export async function loadEnv(environment, options) {
118112

119113
// Import env schema
120114
const envObj = await importFromString(contents, {
121-
dirname: parentDir,
115+
dirname: process.cwd(),
122116
filename: schemaFile,
123117
useCurrentGlobal: true,
124118
}).then((module) => module.env ?? module.default);

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
"dotenv-expand": "^12.0.1",
5555
"esbuild": "^0.24.2",
5656
"module-from-string": "^3.3.1",
57-
"parent-module": "^3.1.0",
5857
"read-pkg-up": "^11.0.0"
5958
},
6059
"devDependencies": {

yarn.lock

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ __metadata:
11891189
languageName: node
11901190
linkType: hard
11911191

1192-
"callsites@npm:^4.1.0, callsites@npm:^4.2.0":
1192+
"callsites@npm:^4.2.0":
11931193
version: 4.2.0
11941194
resolution: "callsites@npm:4.2.0"
11951195
checksum: 10/9a740675712076a38208967d7f80b525c9c7f4524c2af5d3936c5e278a601af0423a07e91f79679fec0546f3a52514d56969c6fe65f84d794e64a36b1f5eda8a
@@ -1518,7 +1518,6 @@ __metadata:
15181518
eslint-config-prettier: "npm:^9.1.0"
15191519
globals: "npm:^15.14.0"
15201520
module-from-string: "npm:^3.3.1"
1521-
parent-module: "npm:^3.1.0"
15221521
prettier: "npm:^3.4.2"
15231522
read-pkg-up: "npm:^11.0.0"
15241523
typescript: "npm:^5.7.2"
@@ -2837,15 +2836,6 @@ __metadata:
28372836
languageName: node
28382837
linkType: hard
28392838

2840-
"parent-module@npm:^3.1.0":
2841-
version: 3.1.0
2842-
resolution: "parent-module@npm:3.1.0"
2843-
dependencies:
2844-
callsites: "npm:^4.1.0"
2845-
checksum: 10/8c089b0077b45f13f17acaa9e4fca34d76c6eba3c628778b54a8caf6bdafeb63fbce2f082063841df279cad427e3ac9a202f69dbf56d68bb9e17473268dbd10c
2846-
languageName: node
2847-
linkType: hard
2848-
28492839
"parse-json@npm:^8.0.0":
28502840
version: 8.1.0
28512841
resolution: "parse-json@npm:8.1.0"

0 commit comments

Comments
 (0)