@zenera/engine 1.1.44 → 1.1.46

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/dist/env.d.ts CHANGED
@@ -1,15 +1,31 @@
1
1
  /** The file, relative to the project directory. */
2
2
  export declare const ENV_FILE = ".env";
3
+ /**
4
+ * Names another file to read instead of `.env` — relative to the project
5
+ * directory, or absolute. A host running the same project for different
6
+ * purposes points each process at its own file this way.
7
+ */
8
+ export declare const ENV_FILE_ENV = "ZEN_ENV_FILE";
3
9
  export interface ProjectEnv {
4
10
  /** the file it was read from */
5
11
  path: string;
6
12
  /** every name the file declares, whether or not its value won */
7
13
  names: string[];
14
+ /** what the file says, credential paths made absolute */
15
+ values: Record<string, string>;
8
16
  }
17
+ /** The env file a project in `dir` reads: `$ZEN_ENV_FILE` when set, `.env` otherwise. */
18
+ export declare function projectEnvPath(dir: string): string;
19
+ /**
20
+ * Reads the project's env file without touching `process.env`. `undefined`
21
+ * when there is no such file.
22
+ */
23
+ export declare function readProjectEnv(dir: string): ProjectEnv | undefined;
9
24
  /**
10
- * Reads `<dir>/.env` into `process.env`, without overwriting anything already
11
- * set, and returns the names it found. A project with no such file is not a
12
- * problem to report — most have none — so the answer is simply `undefined`.
25
+ * Reads the project's env file (`.env`, or `$ZEN_ENV_FILE`) into
26
+ * `process.env`, without overwriting anything already set, and returns the
27
+ * names it found. A project with no such file is not a problem to report —
28
+ * most have none — so the answer is simply `undefined`.
13
29
  *
14
30
  * The names are returned in full, including the ones the environment already
15
31
  * answered: the variable is in play either way, and the sandbox forwards by
package/dist/env.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { existsSync, readFileSync } from 'node:fs';
2
- import { isAbsolute, join, resolve } from 'node:path';
2
+ import { isAbsolute, resolve } from 'node:path';
3
3
  import { parseEnv } from 'node:util';
4
4
  import { FILE_ENVS } from "./keys.js";
5
5
  // ---------------------------------------------------------------------------
@@ -24,9 +24,37 @@ import { FILE_ENVS } from "./keys.js";
24
24
  /** The file, relative to the project directory. */
25
25
  export const ENV_FILE = '.env';
26
26
  /**
27
- * Reads `<dir>/.env` into `process.env`, without overwriting anything already
28
- * set, and returns the names it found. A project with no such file is not a
29
- * problem to report — most have none — so the answer is simply `undefined`.
27
+ * Names another file to read instead of `.env` — relative to the project
28
+ * directory, or absolute. A host running the same project for different
29
+ * purposes points each process at its own file this way.
30
+ */
31
+ export const ENV_FILE_ENV = 'ZEN_ENV_FILE';
32
+ /** The env file a project in `dir` reads: `$ZEN_ENV_FILE` when set, `.env` otherwise. */
33
+ export function projectEnvPath(dir) {
34
+ return resolve(dir, process.env[ENV_FILE_ENV]?.trim() || ENV_FILE);
35
+ }
36
+ /**
37
+ * Reads the project's env file without touching `process.env`. `undefined`
38
+ * when there is no such file.
39
+ */
40
+ export function readProjectEnv(dir) {
41
+ const path = projectEnvPath(dir);
42
+ if (!existsSync(path)) {
43
+ return undefined;
44
+ }
45
+ const values = {};
46
+ for (const [name, value] of Object.entries(parseEnv(readFileSync(path, 'utf8')))) {
47
+ if (value !== undefined) {
48
+ values[name] = located(dir, name, value);
49
+ }
50
+ }
51
+ return { path, names: Object.keys(values), values };
52
+ }
53
+ /**
54
+ * Reads the project's env file (`.env`, or `$ZEN_ENV_FILE`) into
55
+ * `process.env`, without overwriting anything already set, and returns the
56
+ * names it found. A project with no such file is not a problem to report —
57
+ * most have none — so the answer is simply `undefined`.
30
58
  *
31
59
  * The names are returned in full, including the ones the environment already
32
60
  * answered: the variable is in play either way, and the sandbox forwards by
@@ -34,24 +62,16 @@ export const ENV_FILE = '.env';
34
62
  * two sources supplied them.
35
63
  */
36
64
  export function loadProjectEnv(dir) {
37
- const path = join(dir, ENV_FILE);
38
- if (!existsSync(path)) {
39
- return undefined;
40
- }
41
- const names = [];
42
- for (const [name, value] of Object.entries(parseEnv(readFileSync(path, 'utf8')))) {
43
- if (value === undefined) {
44
- continue;
45
- }
46
- names.push(name);
65
+ const env = readProjectEnv(dir);
66
+ for (const [name, value] of Object.entries(env?.values ?? {})) {
47
67
  // An empty string is not an answer: tools test for absence, and a
48
68
  // commented-out line someone half-filled in should not shadow a key
49
69
  // the keyring has.
50
70
  if (!process.env[name]) {
51
- process.env[name] = located(dir, name, value);
71
+ process.env[name] = value;
52
72
  }
53
73
  }
54
- return { path, names };
74
+ return env;
55
75
  }
56
76
  /**
57
77
  * A service-account file is normally kept beside the `.env` that names it, so
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenera/engine",
3
- "version": "1.1.44",
3
+ "version": "1.1.46",
4
4
  "description": "Runs a @zenera/neo project on this machine: keyring, sandbox, sessions and the record each turn leaves behind.",
5
5
  "keywords": [
6
6
  "agents",
@@ -44,6 +44,6 @@
44
44
  "prepack": "tsc -b"
45
45
  },
46
46
  "dependencies": {
47
- "@zenera/neo": "^1.1.44"
47
+ "@zenera/neo": "^1.1.46"
48
48
  }
49
49
  }