scenv-inquirer 0.3.2 → 0.3.3

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
@@ -47,7 +47,7 @@ Variable-level `prompt` overrides this default.
47
47
 
48
48
  ### Save and context callbacks
49
49
 
50
- `askSaveAfterPrompt()` and `askContext()` return functions for scenv's `onAskSaveAfterPrompt` and `onAskContext` callbacks. Use them when `SCENV_SAVE_PROMPT=ask` or `saveContextTo: "ask"`.
50
+ `askWhetherToSave()` and `askContext()` return functions for scenv's `onAskWhetherToSave` and `onAskContext` callbacks. Use them when `shouldSavePrompt` is `ask` or when `saveContextTo: "ask"`.
51
51
 
52
52
  **`callbacks()`** wires all of the above in one go:
53
53
 
@@ -63,17 +63,17 @@ configure({ ...yourConfig, ...callbacks() });
63
63
  This sets:
64
64
 
65
65
  - **defaultPrompt** – inquirer for variable values when no value is resolved
66
- - **onAskSaveAfterPrompt** – "Save '{name}' for next time?" then "Which context?"
67
- - **onAskContext** – "Save to which context?" (list or new)
66
+ - **onAskWhetherToSave** – "Save '{name}' for next time?" (y/n). Only used when `shouldSavePrompt` is "ask".
67
+ - **onAskContext** – "Save to which context?" (list or new). Used when `saveContextTo` is "ask" or when saving after prompt and destination is "ask".
68
68
 
69
69
  ## API
70
70
 
71
71
  | Export | Description |
72
72
  |--------|-------------|
73
73
  | `prompt()` | Returns `(name, defaultValue) => Promise<T>` for use as variable `prompt` or `callbacks.defaultPrompt`. |
74
- | `askSaveAfterPrompt()` | Returns `onAskSaveAfterPrompt`: asks whether to save and which context. |
74
+ | `askWhetherToSave()` | Returns `onAskWhetherToSave`: asks whether to save (y/n). Where to save is handled by `saveContextTo` or `onAskContext`. |
75
75
  | `askContext()` | Returns `onAskContext`: asks which context to save to. |
76
- | `callbacks()` | Returns `{ callbacks: { defaultPrompt, onAskSaveAfterPrompt, onAskContext } }`. |
76
+ | `callbacks()` | Returns `{ callbacks: { defaultPrompt, onAskWhetherToSave, onAskContext } }`. |
77
77
 
78
78
  ## License
79
79
 
package/dist/index.cjs CHANGED
@@ -31,14 +31,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
33
  askContext: () => askContext,
34
- askSaveAfterPrompt: () => askSaveAfterPrompt,
34
+ askWhetherToSave: () => askWhetherToSave,
35
35
  callbacks: () => callbacks,
36
36
  prompt: () => prompt
37
37
  });
38
38
  module.exports = __toCommonJS(index_exports);
39
39
  var import_inquirer = __toESM(require("inquirer"), 1);
40
40
  function prompt() {
41
- return async (name, defaultValue) => {
41
+ return (async (name, defaultValue) => {
42
42
  const defaultStr = defaultValue !== void 0 && defaultValue !== null ? String(defaultValue) : "";
43
43
  const { value } = await import_inquirer.default.prompt([
44
44
  {
@@ -49,10 +49,10 @@ function prompt() {
49
49
  }
50
50
  ]);
51
51
  return value;
52
- };
52
+ });
53
53
  }
54
- function askSaveAfterPrompt() {
55
- return async (name, _value, contextNames) => {
54
+ function askWhetherToSave() {
55
+ return async (name, _value) => {
56
56
  const { save } = await import_inquirer.default.prompt([
57
57
  {
58
58
  type: "confirm",
@@ -61,25 +61,7 @@ function askSaveAfterPrompt() {
61
61
  default: true
62
62
  }
63
63
  ]);
64
- if (!save) return null;
65
- const choices = [...contextNames];
66
- if (choices.length === 0) choices.push("default");
67
- choices.push("(new context)");
68
- const { context } = await import_inquirer.default.prompt([
69
- {
70
- type: "list",
71
- name: "context",
72
- message: "Which context?",
73
- choices
74
- }
75
- ]);
76
- if (context === "(new context)") {
77
- const { newContext } = await import_inquirer.default.prompt([
78
- { type: "input", name: "newContext", message: "Context name:", default: "default" }
79
- ]);
80
- return newContext.trim() || "default";
81
- }
82
- return context;
64
+ return save;
83
65
  };
84
66
  }
85
67
  function askContext() {
@@ -105,18 +87,17 @@ function askContext() {
105
87
  };
106
88
  }
107
89
  function callbacks() {
108
- return {
109
- callbacks: {
110
- defaultPrompt: prompt(),
111
- onAskSaveAfterPrompt: askSaveAfterPrompt(),
112
- onAskContext: askContext()
113
- }
90
+ const cbs = {
91
+ defaultPrompt: prompt(),
92
+ onAskWhetherToSave: askWhetherToSave(),
93
+ onAskContext: askContext()
114
94
  };
95
+ return { callbacks: cbs };
115
96
  }
116
97
  // Annotate the CommonJS export names for ESM import in node:
117
98
  0 && (module.exports = {
118
99
  askContext,
119
- askSaveAfterPrompt,
100
+ askWhetherToSave,
120
101
  callbacks,
121
102
  prompt
122
103
  });
package/dist/index.d.cts CHANGED
@@ -1,30 +1,52 @@
1
+ import { ScenvCallbacks, DefaultPromptFn } from 'scenv';
2
+
1
3
  /**
2
- * Returns a prompt function for use with scenv(): (name, defaultValue) => Promise<T>.
3
- * Scenv passes the variable name and default (from env/context/default); this uses inquirer to ask for input.
4
- * Returns string; use a validator (e.g. scenv-zod) for type/coercion.
4
+ * Returns an inquirer-based prompt function for use with scenv variables. Use as the
5
+ * variable's `prompt` option or as `callbacks.defaultPrompt`. Scenv calls it with the
6
+ * variable name and default value when prompting; this shows an inquirer input prompt.
7
+ * Returned value is a string; use a validator (e.g. scenv-zod) for type coercion.
8
+ *
9
+ * @typeParam T - Value type (default string). Cast or validate in your validator.
10
+ * @returns A function `(name, defaultValue) => Promise<T>` suitable for scenv's prompt or defaultPrompt.
11
+ *
12
+ * @example
13
+ * const apiUrl = scenv("API URL", { default: "http://localhost:4000", prompt: prompt() });
14
+ * const url = await apiUrl.get(); // prompts via inquirer if no env/context
5
15
  */
6
- declare function prompt<T = string>(): (name: string, defaultValue: T) => Promise<T>;
16
+ declare function prompt<T = string>(): DefaultPromptFn;
7
17
  /**
8
- * Returns a function suitable for scenv's onAskSaveAfterPrompt callback.
9
- * Asks "Save '{name}' for next time?" and, if yes, "Which context?" (from contextNames or new).
10
- * @returns Context name to save to, or null to skip saving
18
+ * Returns a function suitable for scenv's {@link ScenvCallbacks.onAskWhetherToSave} callback.
19
+ * Uses inquirer confirm to ask "Save '{name}' for next time?" (y/n). Where to save is
20
+ * determined by config.saveContextTo or the onAskContext callback when saveContextTo is "ask".
21
+ * Only called when {@link ScenvConfig.shouldSavePrompt} is "ask" and the user was just prompted. ("always" saves without asking.)
22
+ *
23
+ * @returns A function `(name, value) => Promise<boolean>`: true to save, false to skip.
11
24
  */
12
- declare function askSaveAfterPrompt(): (name: string, value: unknown, contextNames: string[]) => Promise<string | null>;
25
+ declare function askWhetherToSave(): NonNullable<ScenvCallbacks["onAskWhetherToSave"]>;
13
26
  /**
14
- * Returns a function suitable for scenv's onAskContext callback.
15
- * Asks user to choose a context from contextNames or enter a new one.
27
+ * Returns a function suitable for scenv's {@link ScenvCallbacks.onAskContext} callback.
28
+ * Uses inquirer list to choose a context from contextNames, with a "(new context)" option
29
+ * that prompts for a new name. Used when {@link ScenvConfig.saveContextTo} is "ask" or
30
+ * when saving after a prompt and the destination is "ask".
31
+ *
32
+ * @returns A function `(name, contextNames) => Promise<string>` that returns the chosen context name.
16
33
  */
17
- declare function askContext(): (name: string, contextNames: string[]) => Promise<string>;
34
+ declare function askContext(): NonNullable<ScenvCallbacks["onAskContext"]>;
18
35
  /**
19
- * Returns an object suitable for scenv's configure(): { callbacks: { defaultPrompt, onAskSaveAfterPrompt, onAskContext } }.
20
- * Use as configure(callbacks()) or configure({ ...config, ...callbacks() }) to wire a default inquirer prompt for variables and save/context prompts via inquirer. Variable-level prompt overrides defaultPrompt.
36
+ * Returns an object with all inquirer-based callbacks for scenv. Pass to {@link configure}
37
+ * to use inquirer for variable prompts, "save for next time?", and "which context?".
38
+ * Variable-level `prompt` overrides defaultPrompt.
39
+ *
40
+ * @returns `{ callbacks: { defaultPrompt, onAskWhetherToSave, onAskContext } }` – spread into configure() or merge with other config.
41
+ *
42
+ * @example
43
+ * import { configure } from "scenv";
44
+ * import { callbacks } from "scenv-inquirer";
45
+ * configure(callbacks());
46
+ * // or: configure({ ...parseScenvArgs(process.argv.slice(2)), ...callbacks() });
21
47
  */
22
48
  declare function callbacks(): {
23
- callbacks: {
24
- defaultPrompt: ReturnType<typeof prompt>;
25
- onAskSaveAfterPrompt: ReturnType<typeof askSaveAfterPrompt>;
26
- onAskContext: ReturnType<typeof askContext>;
27
- };
49
+ callbacks: ScenvCallbacks;
28
50
  };
29
51
 
30
- export { askContext, askSaveAfterPrompt, callbacks, prompt };
52
+ export { askContext, askWhetherToSave, callbacks, prompt };
package/dist/index.d.ts CHANGED
@@ -1,30 +1,52 @@
1
+ import { ScenvCallbacks, DefaultPromptFn } from 'scenv';
2
+
1
3
  /**
2
- * Returns a prompt function for use with scenv(): (name, defaultValue) => Promise<T>.
3
- * Scenv passes the variable name and default (from env/context/default); this uses inquirer to ask for input.
4
- * Returns string; use a validator (e.g. scenv-zod) for type/coercion.
4
+ * Returns an inquirer-based prompt function for use with scenv variables. Use as the
5
+ * variable's `prompt` option or as `callbacks.defaultPrompt`. Scenv calls it with the
6
+ * variable name and default value when prompting; this shows an inquirer input prompt.
7
+ * Returned value is a string; use a validator (e.g. scenv-zod) for type coercion.
8
+ *
9
+ * @typeParam T - Value type (default string). Cast or validate in your validator.
10
+ * @returns A function `(name, defaultValue) => Promise<T>` suitable for scenv's prompt or defaultPrompt.
11
+ *
12
+ * @example
13
+ * const apiUrl = scenv("API URL", { default: "http://localhost:4000", prompt: prompt() });
14
+ * const url = await apiUrl.get(); // prompts via inquirer if no env/context
5
15
  */
6
- declare function prompt<T = string>(): (name: string, defaultValue: T) => Promise<T>;
16
+ declare function prompt<T = string>(): DefaultPromptFn;
7
17
  /**
8
- * Returns a function suitable for scenv's onAskSaveAfterPrompt callback.
9
- * Asks "Save '{name}' for next time?" and, if yes, "Which context?" (from contextNames or new).
10
- * @returns Context name to save to, or null to skip saving
18
+ * Returns a function suitable for scenv's {@link ScenvCallbacks.onAskWhetherToSave} callback.
19
+ * Uses inquirer confirm to ask "Save '{name}' for next time?" (y/n). Where to save is
20
+ * determined by config.saveContextTo or the onAskContext callback when saveContextTo is "ask".
21
+ * Only called when {@link ScenvConfig.shouldSavePrompt} is "ask" and the user was just prompted. ("always" saves without asking.)
22
+ *
23
+ * @returns A function `(name, value) => Promise<boolean>`: true to save, false to skip.
11
24
  */
12
- declare function askSaveAfterPrompt(): (name: string, value: unknown, contextNames: string[]) => Promise<string | null>;
25
+ declare function askWhetherToSave(): NonNullable<ScenvCallbacks["onAskWhetherToSave"]>;
13
26
  /**
14
- * Returns a function suitable for scenv's onAskContext callback.
15
- * Asks user to choose a context from contextNames or enter a new one.
27
+ * Returns a function suitable for scenv's {@link ScenvCallbacks.onAskContext} callback.
28
+ * Uses inquirer list to choose a context from contextNames, with a "(new context)" option
29
+ * that prompts for a new name. Used when {@link ScenvConfig.saveContextTo} is "ask" or
30
+ * when saving after a prompt and the destination is "ask".
31
+ *
32
+ * @returns A function `(name, contextNames) => Promise<string>` that returns the chosen context name.
16
33
  */
17
- declare function askContext(): (name: string, contextNames: string[]) => Promise<string>;
34
+ declare function askContext(): NonNullable<ScenvCallbacks["onAskContext"]>;
18
35
  /**
19
- * Returns an object suitable for scenv's configure(): { callbacks: { defaultPrompt, onAskSaveAfterPrompt, onAskContext } }.
20
- * Use as configure(callbacks()) or configure({ ...config, ...callbacks() }) to wire a default inquirer prompt for variables and save/context prompts via inquirer. Variable-level prompt overrides defaultPrompt.
36
+ * Returns an object with all inquirer-based callbacks for scenv. Pass to {@link configure}
37
+ * to use inquirer for variable prompts, "save for next time?", and "which context?".
38
+ * Variable-level `prompt` overrides defaultPrompt.
39
+ *
40
+ * @returns `{ callbacks: { defaultPrompt, onAskWhetherToSave, onAskContext } }` – spread into configure() or merge with other config.
41
+ *
42
+ * @example
43
+ * import { configure } from "scenv";
44
+ * import { callbacks } from "scenv-inquirer";
45
+ * configure(callbacks());
46
+ * // or: configure({ ...parseScenvArgs(process.argv.slice(2)), ...callbacks() });
21
47
  */
22
48
  declare function callbacks(): {
23
- callbacks: {
24
- defaultPrompt: ReturnType<typeof prompt>;
25
- onAskSaveAfterPrompt: ReturnType<typeof askSaveAfterPrompt>;
26
- onAskContext: ReturnType<typeof askContext>;
27
- };
49
+ callbacks: ScenvCallbacks;
28
50
  };
29
51
 
30
- export { askContext, askSaveAfterPrompt, callbacks, prompt };
52
+ export { askContext, askWhetherToSave, callbacks, prompt };
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // src/index.ts
2
2
  import inquirer from "inquirer";
3
3
  function prompt() {
4
- return async (name, defaultValue) => {
4
+ return (async (name, defaultValue) => {
5
5
  const defaultStr = defaultValue !== void 0 && defaultValue !== null ? String(defaultValue) : "";
6
6
  const { value } = await inquirer.prompt([
7
7
  {
@@ -12,10 +12,10 @@ function prompt() {
12
12
  }
13
13
  ]);
14
14
  return value;
15
- };
15
+ });
16
16
  }
17
- function askSaveAfterPrompt() {
18
- return async (name, _value, contextNames) => {
17
+ function askWhetherToSave() {
18
+ return async (name, _value) => {
19
19
  const { save } = await inquirer.prompt([
20
20
  {
21
21
  type: "confirm",
@@ -24,25 +24,7 @@ function askSaveAfterPrompt() {
24
24
  default: true
25
25
  }
26
26
  ]);
27
- if (!save) return null;
28
- const choices = [...contextNames];
29
- if (choices.length === 0) choices.push("default");
30
- choices.push("(new context)");
31
- const { context } = await inquirer.prompt([
32
- {
33
- type: "list",
34
- name: "context",
35
- message: "Which context?",
36
- choices
37
- }
38
- ]);
39
- if (context === "(new context)") {
40
- const { newContext } = await inquirer.prompt([
41
- { type: "input", name: "newContext", message: "Context name:", default: "default" }
42
- ]);
43
- return newContext.trim() || "default";
44
- }
45
- return context;
27
+ return save;
46
28
  };
47
29
  }
48
30
  function askContext() {
@@ -68,17 +50,16 @@ function askContext() {
68
50
  };
69
51
  }
70
52
  function callbacks() {
71
- return {
72
- callbacks: {
73
- defaultPrompt: prompt(),
74
- onAskSaveAfterPrompt: askSaveAfterPrompt(),
75
- onAskContext: askContext()
76
- }
53
+ const cbs = {
54
+ defaultPrompt: prompt(),
55
+ onAskWhetherToSave: askWhetherToSave(),
56
+ onAskContext: askContext()
77
57
  };
58
+ return { callbacks: cbs };
78
59
  }
79
60
  export {
80
61
  askContext,
81
- askSaveAfterPrompt,
62
+ askWhetherToSave,
82
63
  callbacks,
83
64
  prompt
84
65
  };
package/package.json CHANGED
@@ -1,24 +1,20 @@
1
1
  {
2
2
  "name": "scenv-inquirer",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "description": "Inquirer prompt for scenv variables",
5
- "repository": { "type": "git", "url": "https://github.com/PKWadsy/scenv" },
6
- "publishConfig": { "access": "public", "provenance": true },
5
+ "repository": {"type": "git", "url": "https://github.com/PKWadsy/scenv"},
6
+ "publishConfig": {"access": "public", "provenance": true},
7
7
  "keywords": ["scenv", "inquirer", "prompt"],
8
8
  "type": "module",
9
9
  "main": "dist/index.cjs",
10
10
  "module": "dist/index.js",
11
11
  "types": "dist/index.d.ts",
12
- "exports": {
13
- ".": {
14
- "import": "./dist/index.js",
15
- "require": "./dist/index.cjs",
16
- "types": "./dist/index.d.ts"
17
- }
18
- },
12
+ "exports": {".": {"import": "./dist/index.js", "require": "./dist/index.cjs", "types": "./dist/index.d.ts"}},
19
13
  "files": ["dist"],
20
14
  "scripts": {
21
- "build": "tsup src/index.ts --format cjs,esm --dts --clean"
15
+ "build": "tsup src/index.ts --format cjs,esm --dts --clean",
16
+ "test": "vitest run",
17
+ "test:watch": "vitest"
22
18
  },
23
19
  "peerDependencies": {
24
20
  "inquirer": "^8.0.0 || ^9.0.0",
@@ -29,6 +25,7 @@
29
25
  "inquirer": "^9.2.0",
30
26
  "scenv": "workspace:*",
31
27
  "tsup": "^8.0.0",
32
- "typescript": "^5.3.0"
28
+ "typescript": "^5.3.0",
29
+ "vitest": "^2.0.0"
33
30
  }
34
31
  }