scenv 0.1.1 → 0.2.1

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/index.cjs CHANGED
@@ -25,24 +25,24 @@ __export(index_exports, {
25
25
  getCallbacks: () => getCallbacks,
26
26
  getContextValues: () => getContextValues,
27
27
  loadConfig: () => loadConfig,
28
- parseSenvArgs: () => parseSenvArgs,
28
+ parseScenvArgs: () => parseScenvArgs,
29
29
  resetConfig: () => resetConfig,
30
- senv: () => senv
30
+ scenv: () => scenv
31
31
  });
32
32
  module.exports = __toCommonJS(index_exports);
33
33
 
34
34
  // src/config.ts
35
35
  var import_node_fs = require("fs");
36
36
  var import_node_path = require("path");
37
- var CONFIG_FILENAME = "senv.config.json";
37
+ var CONFIG_FILENAME = "scenv.config.json";
38
38
  var envKeyMap = {
39
- SENV_CONTEXT: "contexts",
40
- SENV_ADD_CONTEXTS: "addContexts",
41
- SENV_PROMPT: "prompt",
42
- SENV_IGNORE_ENV: "ignoreEnv",
43
- SENV_IGNORE_CONTEXT: "ignoreContext",
44
- SENV_SAVE_PROMPT: "savePrompt",
45
- SENV_SAVE_CONTEXT_TO: "saveContextTo"
39
+ SCENV_CONTEXT: "contexts",
40
+ SCENV_ADD_CONTEXTS: "addContexts",
41
+ SCENV_PROMPT: "prompt",
42
+ SCENV_IGNORE_ENV: "ignoreEnv",
43
+ SCENV_IGNORE_CONTEXT: "ignoreContext",
44
+ SCENV_SAVE_PROMPT: "savePrompt",
45
+ SCENV_SAVE_CONTEXT_TO: "saveContextTo"
46
46
  };
47
47
  var programmaticConfig = {};
48
48
  var programmaticCallbacks = {};
@@ -269,7 +269,7 @@ function normalizeValidatorResult(result) {
269
269
  if (result.success === true) return result;
270
270
  return result;
271
271
  }
272
- function senv(name, options = {}) {
272
+ function scenv(name, options = {}) {
273
273
  const key = options.key ?? defaultKeyFromName(name);
274
274
  const envKey = options.env ?? defaultEnvFromKey(key);
275
275
  const validator = options.validator;
@@ -305,7 +305,8 @@ function senv(name, options = {}) {
305
305
  let value;
306
306
  if (shouldPrompt(config, hadValue, hadEnv)) {
307
307
  const defaultForPrompt = raw !== void 0 ? raw : defaultValue;
308
- const fn = promptFn ?? defaultPrompt;
308
+ const callbacks = getCallbacks();
309
+ const fn = promptFn ?? callbacks.defaultPrompt ?? defaultPrompt;
309
310
  value = await Promise.resolve(fn(name, defaultForPrompt));
310
311
  wasPrompted = true;
311
312
  } else if (raw !== void 0) {
@@ -391,7 +392,7 @@ function senv(name, options = {}) {
391
392
  }
392
393
 
393
394
  // src/cli-args.ts
394
- function parseSenvArgs(argv) {
395
+ function parseScenvArgs(argv) {
395
396
  const config = {};
396
397
  let i = 0;
397
398
  while (i < argv.length) {
@@ -442,7 +443,7 @@ function parseSenvArgs(argv) {
442
443
  getCallbacks,
443
444
  getContextValues,
444
445
  loadConfig,
445
- parseSenvArgs,
446
+ parseScenvArgs,
446
447
  resetConfig,
447
- senv
448
+ scenv
448
449
  });
package/dist/index.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  type PromptMode = "always" | "never" | "fallback" | "no-env";
2
2
  type SavePromptMode = "always" | "never" | "ask";
3
- interface SenvConfig {
3
+ interface ScenvConfig {
4
4
  /** Replace all contexts with this list (CLI: --context a,b,c) */
5
5
  contexts?: string[];
6
6
  /** Merge these contexts with existing (CLI: --add-context a,b,c) */
@@ -20,22 +20,26 @@ interface SenvConfig {
20
20
  /** Root directory for config/context search (default: cwd) */
21
21
  root?: string;
22
22
  }
23
- interface SenvCallbacks {
23
+ /** (name, defaultValue) => value; used when a variable has no prompt option. Overridable per variable. */
24
+ type DefaultPromptFn = (name: string, defaultValue: unknown) => unknown | Promise<unknown>;
25
+ interface ScenvCallbacks {
26
+ /** Default prompt when a variable does not provide its own `prompt`. Variable's `prompt` overrides this. */
27
+ defaultPrompt?: DefaultPromptFn;
24
28
  /** When user was just prompted for a value and savePrompt is ask/always: (variableName, value, contextNames) => context name to save to, or null to skip */
25
29
  onAskSaveAfterPrompt?: (name: string, value: unknown, contextNames: string[]) => Promise<string | null>;
26
30
  /** When saveContextTo is "ask": (variableName, contextNames) => context name to save to */
27
31
  onAskContext?: (name: string, contextNames: string[]) => Promise<string>;
28
32
  }
29
- declare function getCallbacks(): SenvCallbacks;
33
+ declare function getCallbacks(): ScenvCallbacks;
30
34
  /**
31
35
  * Load full config: file (from root or cwd) <- env <- programmatic. Precedence: programmatic > env > file.
32
36
  */
33
- declare function loadConfig(root?: string): SenvConfig;
37
+ declare function loadConfig(root?: string): ScenvConfig;
34
38
  /**
35
39
  * Set programmatic config (e.g. from CLI flags). Merged on top of env and file in loadConfig().
36
40
  */
37
- declare function configure(partial: Partial<SenvConfig> & {
38
- callbacks?: SenvCallbacks;
41
+ declare function configure(partial: Partial<ScenvConfig> & {
42
+ callbacks?: ScenvCallbacks;
39
43
  }): void;
40
44
  /**
41
45
  * Reset programmatic config (mainly for tests).
@@ -59,14 +63,14 @@ type ValidatorResult<T> = boolean | {
59
63
  error?: unknown;
60
64
  };
61
65
  type PromptFn<T> = (name: string, defaultValue: T) => T | Promise<T>;
62
- interface SenvVariableOptions<T> {
66
+ interface ScenvVariableOptions<T> {
63
67
  key?: string;
64
68
  env?: string;
65
69
  default?: T;
66
70
  validator?: (val: T) => ValidatorResult<T>;
67
71
  prompt?: PromptFn<T>;
68
72
  }
69
- interface SenvVariable<T> {
73
+ interface ScenvVariable<T> {
70
74
  get(): Promise<T>;
71
75
  safeGet(): Promise<{
72
76
  success: true;
@@ -77,13 +81,13 @@ interface SenvVariable<T> {
77
81
  }>;
78
82
  save(value?: T): Promise<void>;
79
83
  }
80
- declare function senv<T>(name: string, options?: SenvVariableOptions<T>): SenvVariable<T>;
84
+ declare function scenv<T>(name: string, options?: ScenvVariableOptions<T>): ScenvVariable<T>;
81
85
 
82
86
  /**
83
- * Parse argv (e.g. process.argv.slice(2)) into SenvConfig for configure().
87
+ * Parse argv (e.g. process.argv.slice(2)) into ScenvConfig for configure().
84
88
  * Supports: --context a,b,c --add-context x,y --prompt fallback --ignore-env --ignore-context
85
89
  * --set key=value --save-prompt ask --save-context-to prod
86
90
  */
87
- declare function parseSenvArgs(argv: string[]): Partial<SenvConfig>;
91
+ declare function parseScenvArgs(argv: string[]): Partial<ScenvConfig>;
88
92
 
89
- export { type PromptMode, type SavePromptMode, type SenvCallbacks, type SenvConfig, type SenvVariable, configure, discoverContextPaths, getCallbacks, getContextValues, loadConfig, parseSenvArgs, resetConfig, senv };
93
+ export { type DefaultPromptFn, type PromptMode, type SavePromptMode, type ScenvCallbacks, type ScenvConfig, type ScenvVariable, configure, discoverContextPaths, getCallbacks, getContextValues, loadConfig, parseScenvArgs, resetConfig, scenv };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  type PromptMode = "always" | "never" | "fallback" | "no-env";
2
2
  type SavePromptMode = "always" | "never" | "ask";
3
- interface SenvConfig {
3
+ interface ScenvConfig {
4
4
  /** Replace all contexts with this list (CLI: --context a,b,c) */
5
5
  contexts?: string[];
6
6
  /** Merge these contexts with existing (CLI: --add-context a,b,c) */
@@ -20,22 +20,26 @@ interface SenvConfig {
20
20
  /** Root directory for config/context search (default: cwd) */
21
21
  root?: string;
22
22
  }
23
- interface SenvCallbacks {
23
+ /** (name, defaultValue) => value; used when a variable has no prompt option. Overridable per variable. */
24
+ type DefaultPromptFn = (name: string, defaultValue: unknown) => unknown | Promise<unknown>;
25
+ interface ScenvCallbacks {
26
+ /** Default prompt when a variable does not provide its own `prompt`. Variable's `prompt` overrides this. */
27
+ defaultPrompt?: DefaultPromptFn;
24
28
  /** When user was just prompted for a value and savePrompt is ask/always: (variableName, value, contextNames) => context name to save to, or null to skip */
25
29
  onAskSaveAfterPrompt?: (name: string, value: unknown, contextNames: string[]) => Promise<string | null>;
26
30
  /** When saveContextTo is "ask": (variableName, contextNames) => context name to save to */
27
31
  onAskContext?: (name: string, contextNames: string[]) => Promise<string>;
28
32
  }
29
- declare function getCallbacks(): SenvCallbacks;
33
+ declare function getCallbacks(): ScenvCallbacks;
30
34
  /**
31
35
  * Load full config: file (from root or cwd) <- env <- programmatic. Precedence: programmatic > env > file.
32
36
  */
33
- declare function loadConfig(root?: string): SenvConfig;
37
+ declare function loadConfig(root?: string): ScenvConfig;
34
38
  /**
35
39
  * Set programmatic config (e.g. from CLI flags). Merged on top of env and file in loadConfig().
36
40
  */
37
- declare function configure(partial: Partial<SenvConfig> & {
38
- callbacks?: SenvCallbacks;
41
+ declare function configure(partial: Partial<ScenvConfig> & {
42
+ callbacks?: ScenvCallbacks;
39
43
  }): void;
40
44
  /**
41
45
  * Reset programmatic config (mainly for tests).
@@ -59,14 +63,14 @@ type ValidatorResult<T> = boolean | {
59
63
  error?: unknown;
60
64
  };
61
65
  type PromptFn<T> = (name: string, defaultValue: T) => T | Promise<T>;
62
- interface SenvVariableOptions<T> {
66
+ interface ScenvVariableOptions<T> {
63
67
  key?: string;
64
68
  env?: string;
65
69
  default?: T;
66
70
  validator?: (val: T) => ValidatorResult<T>;
67
71
  prompt?: PromptFn<T>;
68
72
  }
69
- interface SenvVariable<T> {
73
+ interface ScenvVariable<T> {
70
74
  get(): Promise<T>;
71
75
  safeGet(): Promise<{
72
76
  success: true;
@@ -77,13 +81,13 @@ interface SenvVariable<T> {
77
81
  }>;
78
82
  save(value?: T): Promise<void>;
79
83
  }
80
- declare function senv<T>(name: string, options?: SenvVariableOptions<T>): SenvVariable<T>;
84
+ declare function scenv<T>(name: string, options?: ScenvVariableOptions<T>): ScenvVariable<T>;
81
85
 
82
86
  /**
83
- * Parse argv (e.g. process.argv.slice(2)) into SenvConfig for configure().
87
+ * Parse argv (e.g. process.argv.slice(2)) into ScenvConfig for configure().
84
88
  * Supports: --context a,b,c --add-context x,y --prompt fallback --ignore-env --ignore-context
85
89
  * --set key=value --save-prompt ask --save-context-to prod
86
90
  */
87
- declare function parseSenvArgs(argv: string[]): Partial<SenvConfig>;
91
+ declare function parseScenvArgs(argv: string[]): Partial<ScenvConfig>;
88
92
 
89
- export { type PromptMode, type SavePromptMode, type SenvCallbacks, type SenvConfig, type SenvVariable, configure, discoverContextPaths, getCallbacks, getContextValues, loadConfig, parseSenvArgs, resetConfig, senv };
93
+ export { type DefaultPromptFn, type PromptMode, type SavePromptMode, type ScenvCallbacks, type ScenvConfig, type ScenvVariable, configure, discoverContextPaths, getCallbacks, getContextValues, loadConfig, parseScenvArgs, resetConfig, scenv };
package/dist/index.js CHANGED
@@ -1,15 +1,15 @@
1
1
  // src/config.ts
2
2
  import { readFileSync, existsSync } from "fs";
3
3
  import { dirname, join } from "path";
4
- var CONFIG_FILENAME = "senv.config.json";
4
+ var CONFIG_FILENAME = "scenv.config.json";
5
5
  var envKeyMap = {
6
- SENV_CONTEXT: "contexts",
7
- SENV_ADD_CONTEXTS: "addContexts",
8
- SENV_PROMPT: "prompt",
9
- SENV_IGNORE_ENV: "ignoreEnv",
10
- SENV_IGNORE_CONTEXT: "ignoreContext",
11
- SENV_SAVE_PROMPT: "savePrompt",
12
- SENV_SAVE_CONTEXT_TO: "saveContextTo"
6
+ SCENV_CONTEXT: "contexts",
7
+ SCENV_ADD_CONTEXTS: "addContexts",
8
+ SCENV_PROMPT: "prompt",
9
+ SCENV_IGNORE_ENV: "ignoreEnv",
10
+ SCENV_IGNORE_CONTEXT: "ignoreContext",
11
+ SCENV_SAVE_PROMPT: "savePrompt",
12
+ SCENV_SAVE_CONTEXT_TO: "saveContextTo"
13
13
  };
14
14
  var programmaticConfig = {};
15
15
  var programmaticCallbacks = {};
@@ -242,7 +242,7 @@ function normalizeValidatorResult(result) {
242
242
  if (result.success === true) return result;
243
243
  return result;
244
244
  }
245
- function senv(name, options = {}) {
245
+ function scenv(name, options = {}) {
246
246
  const key = options.key ?? defaultKeyFromName(name);
247
247
  const envKey = options.env ?? defaultEnvFromKey(key);
248
248
  const validator = options.validator;
@@ -278,7 +278,8 @@ function senv(name, options = {}) {
278
278
  let value;
279
279
  if (shouldPrompt(config, hadValue, hadEnv)) {
280
280
  const defaultForPrompt = raw !== void 0 ? raw : defaultValue;
281
- const fn = promptFn ?? defaultPrompt;
281
+ const callbacks = getCallbacks();
282
+ const fn = promptFn ?? callbacks.defaultPrompt ?? defaultPrompt;
282
283
  value = await Promise.resolve(fn(name, defaultForPrompt));
283
284
  wasPrompted = true;
284
285
  } else if (raw !== void 0) {
@@ -364,7 +365,7 @@ function senv(name, options = {}) {
364
365
  }
365
366
 
366
367
  // src/cli-args.ts
367
- function parseSenvArgs(argv) {
368
+ function parseScenvArgs(argv) {
368
369
  const config = {};
369
370
  let i = 0;
370
371
  while (i < argv.length) {
@@ -414,7 +415,7 @@ export {
414
415
  getCallbacks,
415
416
  getContextValues,
416
417
  loadConfig,
417
- parseSenvArgs,
418
+ parseScenvArgs,
418
419
  resetConfig,
419
- senv
420
+ scenv
420
421
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "scenv",
3
- "version": "0.1.1",
3
+ "version": "0.2.1",
4
4
  "description": "Environment and context variables with runtime-configurable resolution",
5
5
  "repository": {
6
6
  "type": "git",