@mono-labs/project 0.1.286 → 0.1.289

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -57,7 +57,9 @@ import {
57
57
  import { loadMergedEnv } from "@mono-labs/project";
58
58
  ```
59
59
 
60
- - **`loadMergedEnv()`** -- Loads `.env` and `.env.local` from the project root, merges them (`.env.local` takes precedence), and injects into `process.env` without overwriting existing variables.
60
+ - **`loadMergedEnv(files?)`** -- Loads env files from the project root, merges them (later files take precedence), and injects into `process.env` without overwriting existing variables. The file list is `files` if given, else `MONOLABS_ENV_FILES` (set by `mono` for its child processes), else `.env` + `.env.local`. A missing `.env`/`.env.local` is skipped; any other missing file throws.
61
+ - **`readEnvFiles(files?)`** -- Same merge as above but returns the values without touching `process.env`.
62
+ - **`getInjectedEnvKeys()`** / **`getLoadedEnvFiles()`** -- Keys injected and files considered by the last `loadMergedEnv()` call.
61
63
 
62
64
  ### Environment Filtering
63
65
 
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { findProjectRoot, getRootDirectory, getRootJson, resolveMonoDirectory, getMonoFiles, getMonoConfig, clearMonoConfigCache, } from './loadFromRoot';
2
2
  export type { MonoWorkspaceConfig, MonoProjectConfig, MonoFiles, MonoConfig, } from './loadFromRoot';
3
- export { loadAppConfig, loadProjectConfig, loadMergedEnv, filterEnvByPrefixes, filterEnvByConfig } from './project/index';
3
+ export { loadAppConfig, loadProjectConfig, loadMergedEnv, readEnvFiles, resolveEnvFiles, envFilesFromEnv, getInjectedEnvKeys, getLoadedEnvFiles, DEFAULT_ENV_FILES, ENV_FILES_VAR, INJECTED_KEYS_VAR, filterEnvByPrefixes, filterEnvByConfig, } from './project/index';
4
+ export type { ResolvedEnvFile } from './project/index';
4
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,eAAe,EACf,gBAAgB,EAChB,WAAW,EACX,oBAAoB,EACpB,YAAY,EACZ,aAAa,EACb,oBAAoB,GACrB,MAAM,gBAAgB,CAAA;AAEvB,YAAY,EACV,mBAAmB,EACnB,iBAAiB,EACjB,SAAS,EACT,UAAU,GACX,MAAM,gBAAgB,CAAA;AAEvB,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,aAAa,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,eAAe,EACf,gBAAgB,EAChB,WAAW,EACX,oBAAoB,EACpB,YAAY,EACZ,aAAa,EACb,oBAAoB,GACrB,MAAM,gBAAgB,CAAA;AAEvB,YAAY,EACV,mBAAmB,EACnB,iBAAiB,EACjB,SAAS,EACT,UAAU,GACX,MAAM,gBAAgB,CAAA;AAEvB,OAAO,EACL,aAAa,EACb,iBAAiB,EACjB,aAAa,EACb,YAAY,EACZ,eAAe,EACf,eAAe,EACf,kBAAkB,EAClB,iBAAiB,EACjB,iBAAiB,EACjB,aAAa,EACb,iBAAiB,EACjB,mBAAmB,EACnB,iBAAiB,GAClB,MAAM,iBAAiB,CAAA;AACxB,YAAY,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA"}
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  // @mono-labs/project barrel export
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.filterEnvByConfig = exports.filterEnvByPrefixes = exports.loadMergedEnv = exports.loadProjectConfig = exports.loadAppConfig = exports.clearMonoConfigCache = exports.getMonoConfig = exports.getMonoFiles = exports.resolveMonoDirectory = exports.getRootJson = exports.getRootDirectory = exports.findProjectRoot = void 0;
4
+ exports.filterEnvByConfig = exports.filterEnvByPrefixes = exports.INJECTED_KEYS_VAR = exports.ENV_FILES_VAR = exports.DEFAULT_ENV_FILES = exports.getLoadedEnvFiles = exports.getInjectedEnvKeys = exports.envFilesFromEnv = exports.resolveEnvFiles = exports.readEnvFiles = exports.loadMergedEnv = exports.loadProjectConfig = exports.loadAppConfig = exports.clearMonoConfigCache = exports.getMonoConfig = exports.getMonoFiles = exports.resolveMonoDirectory = exports.getRootJson = exports.getRootDirectory = exports.findProjectRoot = void 0;
5
5
  var loadFromRoot_1 = require("./loadFromRoot");
6
6
  Object.defineProperty(exports, "findProjectRoot", { enumerable: true, get: function () { return loadFromRoot_1.findProjectRoot; } });
7
7
  Object.defineProperty(exports, "getRootDirectory", { enumerable: true, get: function () { return loadFromRoot_1.getRootDirectory; } });
@@ -14,5 +14,13 @@ var index_1 = require("./project/index");
14
14
  Object.defineProperty(exports, "loadAppConfig", { enumerable: true, get: function () { return index_1.loadAppConfig; } });
15
15
  Object.defineProperty(exports, "loadProjectConfig", { enumerable: true, get: function () { return index_1.loadProjectConfig; } });
16
16
  Object.defineProperty(exports, "loadMergedEnv", { enumerable: true, get: function () { return index_1.loadMergedEnv; } });
17
+ Object.defineProperty(exports, "readEnvFiles", { enumerable: true, get: function () { return index_1.readEnvFiles; } });
18
+ Object.defineProperty(exports, "resolveEnvFiles", { enumerable: true, get: function () { return index_1.resolveEnvFiles; } });
19
+ Object.defineProperty(exports, "envFilesFromEnv", { enumerable: true, get: function () { return index_1.envFilesFromEnv; } });
20
+ Object.defineProperty(exports, "getInjectedEnvKeys", { enumerable: true, get: function () { return index_1.getInjectedEnvKeys; } });
21
+ Object.defineProperty(exports, "getLoadedEnvFiles", { enumerable: true, get: function () { return index_1.getLoadedEnvFiles; } });
22
+ Object.defineProperty(exports, "DEFAULT_ENV_FILES", { enumerable: true, get: function () { return index_1.DEFAULT_ENV_FILES; } });
23
+ Object.defineProperty(exports, "ENV_FILES_VAR", { enumerable: true, get: function () { return index_1.ENV_FILES_VAR; } });
24
+ Object.defineProperty(exports, "INJECTED_KEYS_VAR", { enumerable: true, get: function () { return index_1.INJECTED_KEYS_VAR; } });
17
25
  Object.defineProperty(exports, "filterEnvByPrefixes", { enumerable: true, get: function () { return index_1.filterEnvByPrefixes; } });
18
26
  Object.defineProperty(exports, "filterEnvByConfig", { enumerable: true, get: function () { return index_1.filterEnvByConfig; } });
@@ -38,6 +38,7 @@ export declare function loadAppConfig<TCustom = unknown, TType extends string =
38
38
  meta: WorkspaceDetectResult;
39
39
  };
40
40
  export declare const loadProjectConfig: typeof loadAppConfig;
41
- export { loadMergedEnv } from './merge-env';
41
+ export { loadMergedEnv, readEnvFiles, resolveEnvFiles, envFilesFromEnv, getInjectedEnvKeys, getLoadedEnvFiles, DEFAULT_ENV_FILES, ENV_FILES_VAR, INJECTED_KEYS_VAR, } from './merge-env';
42
+ export type { ResolvedEnvFile } from './merge-env';
42
43
  export { filterEnvByPrefixes, filterEnvByConfig } from './filter-env';
43
44
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/project/index.ts"],"names":[],"mappings":"AAQA,KAAK,qBAAqB,GAAG;IAC3B,GAAG,EAAE,MAAM,CAAA;IACX,aAAa,EAAE,MAAM,GAAG,IAAI,CAAA;IAC5B,eAAe,EAAE,OAAO,CAAA;IACxB,SAAS,EAAE,MAAM,CAAA;IACjB,UAAU,EAAE,MAAM,CAAA;CACnB,CAAA;AAED,KAAK,gBAAgB,GAAG;IACtB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,CAAA;AAED,KAAK,mBAAmB,GAAG;IACzB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,cAAc,CAAC,EAAE,MAAM,CAA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E,MAAM,aAAa,CAAA;AAClD,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA"}
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.filterEnvByConfig = exports.filterEnvByPrefixes = exports.loadMergedEnv = exports.loadProjectConfig = void 0;
6
+ exports.filterEnvByConfig = exports.filterEnvByPrefixes = exports.INJECTED_KEYS_VAR = exports.ENV_FILES_VAR = exports.DEFAULT_ENV_FILES = exports.getLoadedEnvFiles = exports.getInjectedEnvKeys = exports.envFilesFromEnv = exports.resolveEnvFiles = exports.readEnvFiles = exports.loadMergedEnv = exports.loadProjectConfig = void 0;
7
7
  exports.loadAppConfig = loadAppConfig;
8
8
  const node_fs_1 = __importDefault(require("node:fs"));
9
9
  const node_path_1 = __importDefault(require("node:path"));
@@ -98,6 +98,14 @@ function loadAppConfig(configType = 'app', startDir = process.cwd()) {
98
98
  exports.loadProjectConfig = loadAppConfig;
99
99
  var merge_env_1 = require("./merge-env");
100
100
  Object.defineProperty(exports, "loadMergedEnv", { enumerable: true, get: function () { return merge_env_1.loadMergedEnv; } });
101
+ Object.defineProperty(exports, "readEnvFiles", { enumerable: true, get: function () { return merge_env_1.readEnvFiles; } });
102
+ Object.defineProperty(exports, "resolveEnvFiles", { enumerable: true, get: function () { return merge_env_1.resolveEnvFiles; } });
103
+ Object.defineProperty(exports, "envFilesFromEnv", { enumerable: true, get: function () { return merge_env_1.envFilesFromEnv; } });
104
+ Object.defineProperty(exports, "getInjectedEnvKeys", { enumerable: true, get: function () { return merge_env_1.getInjectedEnvKeys; } });
105
+ Object.defineProperty(exports, "getLoadedEnvFiles", { enumerable: true, get: function () { return merge_env_1.getLoadedEnvFiles; } });
106
+ Object.defineProperty(exports, "DEFAULT_ENV_FILES", { enumerable: true, get: function () { return merge_env_1.DEFAULT_ENV_FILES; } });
107
+ Object.defineProperty(exports, "ENV_FILES_VAR", { enumerable: true, get: function () { return merge_env_1.ENV_FILES_VAR; } });
108
+ Object.defineProperty(exports, "INJECTED_KEYS_VAR", { enumerable: true, get: function () { return merge_env_1.INJECTED_KEYS_VAR; } });
101
109
  var filter_env_1 = require("./filter-env");
102
110
  Object.defineProperty(exports, "filterEnvByPrefixes", { enumerable: true, get: function () { return filter_env_1.filterEnvByPrefixes; } });
103
111
  Object.defineProperty(exports, "filterEnvByConfig", { enumerable: true, get: function () { return filter_env_1.filterEnvByConfig; } });
@@ -1,2 +1,32 @@
1
- export declare function loadMergedEnv(): NodeJS.ProcessEnv;
1
+ /** Files loaded when nothing else is configured. Missing defaults are skipped silently. */
2
+ export declare const DEFAULT_ENV_FILES: string[];
3
+ /** Env var carrying the resolved env file list to child processes (path-delimiter separated). */
4
+ export declare const ENV_FILES_VAR = "MONOLABS_ENV_FILES";
5
+ /** Env var carrying the comma-separated list of keys mono injected from env files. */
6
+ export declare const INJECTED_KEYS_VAR = "MONOLABS_INJECTED_KEYS";
7
+ export type ResolvedEnvFile = {
8
+ path: string;
9
+ optional: boolean;
10
+ };
11
+ /**
12
+ * Resolve env file names to absolute paths. The default names (.env, .env.local) are optional;
13
+ * any other explicitly listed file is required and must exist.
14
+ */
15
+ export declare function resolveEnvFiles(files?: string[], cwd?: string): ResolvedEnvFile[];
16
+ /**
17
+ * Read and merge env files in order (later files win). Pure: does not touch process.env.
18
+ * Throws if a required (non-default) file is missing.
19
+ */
20
+ export declare function readEnvFiles(files?: string[], cwd?: string): Record<string, string>;
21
+ /** Parse the MONOLABS_ENV_FILES value; returns undefined when unset. Empty string means "no files". */
22
+ export declare function envFilesFromEnv(env?: NodeJS.ProcessEnv): string[] | undefined;
23
+ /** Keys the last loadMergedEnv() call injected into process.env (i.e. not real shell vars). */
24
+ export declare function getInjectedEnvKeys(): string[];
25
+ /** Absolute paths of the files the last loadMergedEnv() call considered. */
26
+ export declare function getLoadedEnvFiles(): string[];
27
+ /**
28
+ * Load env files into process.env without overwriting existing (real) variables.
29
+ * File list: `files` arg → MONOLABS_ENV_FILES (set by a parent mono process) → .env + .env.local.
30
+ */
31
+ export declare function loadMergedEnv(files?: string[]): NodeJS.ProcessEnv;
2
32
  //# sourceMappingURL=merge-env.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"merge-env.d.ts","sourceRoot":"","sources":["../../src/project/merge-env.ts"],"names":[],"mappings":"AAIA,wBAAgB,aAAa,IAAI,MAAM,CAAC,UAAU,CA2BjD"}
1
+ {"version":3,"file":"merge-env.d.ts","sourceRoot":"","sources":["../../src/project/merge-env.ts"],"names":[],"mappings":"AAIA,2FAA2F;AAC3F,eAAO,MAAM,iBAAiB,UAAyB,CAAC;AAExD,iGAAiG;AACjG,eAAO,MAAM,aAAa,uBAAuB,CAAC;AAElD,sFAAsF;AACtF,eAAO,MAAM,iBAAiB,2BAA2B,CAAC;AAE1D,MAAM,MAAM,eAAe,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,OAAO,CAAA;CAAE,CAAC;AAElE;;;GAGG;AACH,wBAAgB,eAAe,CAC9B,KAAK,GAAE,MAAM,EAAsB,EACnC,GAAG,GAAE,MAAsB,GACzB,eAAe,EAAE,CAOnB;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAC3B,KAAK,GAAE,MAAM,EAAsB,EACnC,GAAG,GAAE,MAAsB,GACzB,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAUxB;AAED,uGAAuG;AACvG,wBAAgB,eAAe,CAC9B,GAAG,GAAE,MAAM,CAAC,UAAwB,GAClC,MAAM,EAAE,GAAG,SAAS,CAItB;AAKD,+FAA+F;AAC/F,wBAAgB,kBAAkB,IAAI,MAAM,EAAE,CAE7C;AAED,4EAA4E;AAC5E,wBAAgB,iBAAiB,IAAI,MAAM,EAAE,CAE5C;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC,UAAU,CAcjE"}
@@ -3,28 +3,80 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.INJECTED_KEYS_VAR = exports.ENV_FILES_VAR = exports.DEFAULT_ENV_FILES = void 0;
7
+ exports.resolveEnvFiles = resolveEnvFiles;
8
+ exports.readEnvFiles = readEnvFiles;
9
+ exports.envFilesFromEnv = envFilesFromEnv;
10
+ exports.getInjectedEnvKeys = getInjectedEnvKeys;
11
+ exports.getLoadedEnvFiles = getLoadedEnvFiles;
6
12
  exports.loadMergedEnv = loadMergedEnv;
7
13
  const fs_1 = __importDefault(require("fs"));
8
14
  const path_1 = __importDefault(require("path"));
9
15
  const dotenv_1 = __importDefault(require("dotenv"));
10
- function loadMergedEnv() {
11
- const ENV_PATH = path_1.default.resolve(process.cwd(), '.env');
12
- const ENV_LOCAL_PATH = path_1.default.resolve(process.cwd(), '.env.local');
13
- // Load base .env
14
- const base = fs_1.default.existsSync(ENV_PATH) ? dotenv_1.default.parse(fs_1.default.readFileSync(ENV_PATH)) : {};
15
- // Load overrides .env.local
16
- const local = fs_1.default.existsSync(ENV_LOCAL_PATH) ?
17
- dotenv_1.default.parse(fs_1.default.readFileSync(ENV_LOCAL_PATH))
18
- : {};
19
- // Merge: local overrides base
20
- const merged = {
21
- ...base,
22
- ...local,
23
- };
24
- // Inject into process.env (do NOT overwrite existing real env vars)
16
+ /** Files loaded when nothing else is configured. Missing defaults are skipped silently. */
17
+ exports.DEFAULT_ENV_FILES = ['.env', '.env.local'];
18
+ /** Env var carrying the resolved env file list to child processes (path-delimiter separated). */
19
+ exports.ENV_FILES_VAR = 'MONOLABS_ENV_FILES';
20
+ /** Env var carrying the comma-separated list of keys mono injected from env files. */
21
+ exports.INJECTED_KEYS_VAR = 'MONOLABS_INJECTED_KEYS';
22
+ /**
23
+ * Resolve env file names to absolute paths. The default names (.env, .env.local) are optional;
24
+ * any other explicitly listed file is required and must exist.
25
+ */
26
+ function resolveEnvFiles(files = exports.DEFAULT_ENV_FILES, cwd = process.cwd()) {
27
+ return files
28
+ .filter((f) => typeof f === 'string' && f.trim().length > 0)
29
+ .map((f) => ({
30
+ path: path_1.default.resolve(cwd, f.trim()),
31
+ optional: exports.DEFAULT_ENV_FILES.includes(path_1.default.basename(f.trim())),
32
+ }));
33
+ }
34
+ /**
35
+ * Read and merge env files in order (later files win). Pure: does not touch process.env.
36
+ * Throws if a required (non-default) file is missing.
37
+ */
38
+ function readEnvFiles(files = exports.DEFAULT_ENV_FILES, cwd = process.cwd()) {
39
+ const merged = {};
40
+ for (const file of resolveEnvFiles(files, cwd)) {
41
+ if (!fs_1.default.existsSync(file.path)) {
42
+ if (file.optional)
43
+ continue;
44
+ throw new Error(`Env file not found: ${file.path}`);
45
+ }
46
+ Object.assign(merged, dotenv_1.default.parse(fs_1.default.readFileSync(file.path)));
47
+ }
48
+ return merged;
49
+ }
50
+ /** Parse the MONOLABS_ENV_FILES value; returns undefined when unset. Empty string means "no files". */
51
+ function envFilesFromEnv(env = process.env) {
52
+ const raw = env[exports.ENV_FILES_VAR];
53
+ if (raw === undefined)
54
+ return undefined;
55
+ return raw.split(path_1.default.delimiter).filter(Boolean);
56
+ }
57
+ let injectedKeys = [];
58
+ let loadedFiles = [];
59
+ /** Keys the last loadMergedEnv() call injected into process.env (i.e. not real shell vars). */
60
+ function getInjectedEnvKeys() {
61
+ return [...injectedKeys];
62
+ }
63
+ /** Absolute paths of the files the last loadMergedEnv() call considered. */
64
+ function getLoadedEnvFiles() {
65
+ return [...loadedFiles];
66
+ }
67
+ /**
68
+ * Load env files into process.env without overwriting existing (real) variables.
69
+ * File list: `files` arg → MONOLABS_ENV_FILES (set by a parent mono process) → .env + .env.local.
70
+ */
71
+ function loadMergedEnv(files) {
72
+ const list = files ?? envFilesFromEnv() ?? exports.DEFAULT_ENV_FILES;
73
+ const merged = readEnvFiles(list);
74
+ loadedFiles = resolveEnvFiles(list).map((f) => f.path);
75
+ injectedKeys = [];
25
76
  for (const [key, value] of Object.entries(merged)) {
26
77
  if (process.env[key] === undefined) {
27
78
  process.env[key] = value;
79
+ injectedKeys.push(key);
28
80
  }
29
81
  }
30
82
  return process.env;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mono-labs/project",
3
- "version": "0.1.286",
3
+ "version": "0.1.289",
4
4
  "type": "commonjs",
5
5
  "description": "Project configuration utilities for mono-labs",
6
6
  "main": "dist/index.js",
@@ -32,7 +32,7 @@
32
32
  },
33
33
  "license": "MIT",
34
34
  "dependencies": {
35
- "@mono-labs/shared": "0.1.286",
35
+ "@mono-labs/shared": "0.1.289",
36
36
  "dotenv": "^17.2.0"
37
37
  },
38
38
  "peerDependencies": {
package/src/index.ts CHANGED
@@ -17,4 +17,19 @@ export type {
17
17
  MonoConfig,
18
18
  } from './loadFromRoot'
19
19
 
20
- export { loadAppConfig, loadProjectConfig, loadMergedEnv, filterEnvByPrefixes, filterEnvByConfig } from './project/index'
20
+ export {
21
+ loadAppConfig,
22
+ loadProjectConfig,
23
+ loadMergedEnv,
24
+ readEnvFiles,
25
+ resolveEnvFiles,
26
+ envFilesFromEnv,
27
+ getInjectedEnvKeys,
28
+ getLoadedEnvFiles,
29
+ DEFAULT_ENV_FILES,
30
+ ENV_FILES_VAR,
31
+ INJECTED_KEYS_VAR,
32
+ filterEnvByPrefixes,
33
+ filterEnvByConfig,
34
+ } from './project/index'
35
+ export type { ResolvedEnvFile } from './project/index'
@@ -164,5 +164,16 @@ export function loadAppConfig<TCustom = unknown, TType extends string = 'app'>(
164
164
 
165
165
  export const loadProjectConfig = loadAppConfig
166
166
 
167
- export { loadMergedEnv } from './merge-env'
167
+ export {
168
+ loadMergedEnv,
169
+ readEnvFiles,
170
+ resolveEnvFiles,
171
+ envFilesFromEnv,
172
+ getInjectedEnvKeys,
173
+ getLoadedEnvFiles,
174
+ DEFAULT_ENV_FILES,
175
+ ENV_FILES_VAR,
176
+ INJECTED_KEYS_VAR,
177
+ } from './merge-env'
178
+ export type { ResolvedEnvFile } from './merge-env'
168
179
  export { filterEnvByPrefixes, filterEnvByConfig } from './filter-env'
@@ -2,30 +2,89 @@ import fs from 'fs';
2
2
  import path from 'path';
3
3
  import dotenv from 'dotenv';
4
4
 
5
- export function loadMergedEnv(): NodeJS.ProcessEnv {
6
- const ENV_PATH = path.resolve(process.cwd(), '.env');
7
- const ENV_LOCAL_PATH = path.resolve(process.cwd(), '.env.local');
8
-
9
- // Load base .env
10
- const base =
11
- fs.existsSync(ENV_PATH) ? dotenv.parse(fs.readFileSync(ENV_PATH)) : {};
12
-
13
- // Load overrides .env.local
14
- const local =
15
- fs.existsSync(ENV_LOCAL_PATH) ?
16
- dotenv.parse(fs.readFileSync(ENV_LOCAL_PATH))
17
- : {};
18
-
19
- // Merge: local overrides base
20
- const merged = {
21
- ...base,
22
- ...local,
23
- };
24
-
25
- // Inject into process.env (do NOT overwrite existing real env vars)
5
+ /** Files loaded when nothing else is configured. Missing defaults are skipped silently. */
6
+ export const DEFAULT_ENV_FILES = ['.env', '.env.local'];
7
+
8
+ /** Env var carrying the resolved env file list to child processes (path-delimiter separated). */
9
+ export const ENV_FILES_VAR = 'MONOLABS_ENV_FILES';
10
+
11
+ /** Env var carrying the comma-separated list of keys mono injected from env files. */
12
+ export const INJECTED_KEYS_VAR = 'MONOLABS_INJECTED_KEYS';
13
+
14
+ export type ResolvedEnvFile = { path: string; optional: boolean };
15
+
16
+ /**
17
+ * Resolve env file names to absolute paths. The default names (.env, .env.local) are optional;
18
+ * any other explicitly listed file is required and must exist.
19
+ */
20
+ export function resolveEnvFiles(
21
+ files: string[] = DEFAULT_ENV_FILES,
22
+ cwd: string = process.cwd()
23
+ ): ResolvedEnvFile[] {
24
+ return files
25
+ .filter((f) => typeof f === 'string' && f.trim().length > 0)
26
+ .map((f) => ({
27
+ path: path.resolve(cwd, f.trim()),
28
+ optional: DEFAULT_ENV_FILES.includes(path.basename(f.trim())),
29
+ }));
30
+ }
31
+
32
+ /**
33
+ * Read and merge env files in order (later files win). Pure: does not touch process.env.
34
+ * Throws if a required (non-default) file is missing.
35
+ */
36
+ export function readEnvFiles(
37
+ files: string[] = DEFAULT_ENV_FILES,
38
+ cwd: string = process.cwd()
39
+ ): Record<string, string> {
40
+ const merged: Record<string, string> = {};
41
+ for (const file of resolveEnvFiles(files, cwd)) {
42
+ if (!fs.existsSync(file.path)) {
43
+ if (file.optional) continue;
44
+ throw new Error(`Env file not found: ${file.path}`);
45
+ }
46
+ Object.assign(merged, dotenv.parse(fs.readFileSync(file.path)));
47
+ }
48
+ return merged;
49
+ }
50
+
51
+ /** Parse the MONOLABS_ENV_FILES value; returns undefined when unset. Empty string means "no files". */
52
+ export function envFilesFromEnv(
53
+ env: NodeJS.ProcessEnv = process.env
54
+ ): string[] | undefined {
55
+ const raw = env[ENV_FILES_VAR];
56
+ if (raw === undefined) return undefined;
57
+ return raw.split(path.delimiter).filter(Boolean);
58
+ }
59
+
60
+ let injectedKeys: string[] = [];
61
+ let loadedFiles: string[] = [];
62
+
63
+ /** Keys the last loadMergedEnv() call injected into process.env (i.e. not real shell vars). */
64
+ export function getInjectedEnvKeys(): string[] {
65
+ return [...injectedKeys];
66
+ }
67
+
68
+ /** Absolute paths of the files the last loadMergedEnv() call considered. */
69
+ export function getLoadedEnvFiles(): string[] {
70
+ return [...loadedFiles];
71
+ }
72
+
73
+ /**
74
+ * Load env files into process.env without overwriting existing (real) variables.
75
+ * File list: `files` arg → MONOLABS_ENV_FILES (set by a parent mono process) → .env + .env.local.
76
+ */
77
+ export function loadMergedEnv(files?: string[]): NodeJS.ProcessEnv {
78
+ const list = files ?? envFilesFromEnv() ?? DEFAULT_ENV_FILES;
79
+ const merged = readEnvFiles(list);
80
+
81
+ loadedFiles = resolveEnvFiles(list).map((f) => f.path);
82
+ injectedKeys = [];
83
+
26
84
  for (const [key, value] of Object.entries(merged)) {
27
85
  if (process.env[key] === undefined) {
28
86
  process.env[key] = value;
87
+ injectedKeys.push(key);
29
88
  }
30
89
  }
31
90
  return process.env;