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 +5 -5
- package/dist/index.cjs +12 -31
- package/dist/index.d.cts +41 -19
- package/dist/index.d.ts +41 -19
- package/dist/index.js +11 -30
- package/package.json +9 -12
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
|
-
`
|
|
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
|
-
- **
|
|
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
|
-
| `
|
|
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,
|
|
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
|
-
|
|
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
|
|
55
|
-
return async (name, _value
|
|
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
|
-
|
|
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
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
|
-
|
|
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
|
|
3
|
-
*
|
|
4
|
-
*
|
|
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>():
|
|
16
|
+
declare function prompt<T = string>(): DefaultPromptFn;
|
|
7
17
|
/**
|
|
8
|
-
* Returns a function suitable for scenv's
|
|
9
|
-
*
|
|
10
|
-
*
|
|
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
|
|
25
|
+
declare function askWhetherToSave(): NonNullable<ScenvCallbacks["onAskWhetherToSave"]>;
|
|
13
26
|
/**
|
|
14
|
-
* Returns a function suitable for scenv's onAskContext callback.
|
|
15
|
-
*
|
|
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():
|
|
34
|
+
declare function askContext(): NonNullable<ScenvCallbacks["onAskContext"]>;
|
|
18
35
|
/**
|
|
19
|
-
* Returns an object
|
|
20
|
-
*
|
|
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,
|
|
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
|
|
3
|
-
*
|
|
4
|
-
*
|
|
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>():
|
|
16
|
+
declare function prompt<T = string>(): DefaultPromptFn;
|
|
7
17
|
/**
|
|
8
|
-
* Returns a function suitable for scenv's
|
|
9
|
-
*
|
|
10
|
-
*
|
|
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
|
|
25
|
+
declare function askWhetherToSave(): NonNullable<ScenvCallbacks["onAskWhetherToSave"]>;
|
|
13
26
|
/**
|
|
14
|
-
* Returns a function suitable for scenv's onAskContext callback.
|
|
15
|
-
*
|
|
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():
|
|
34
|
+
declare function askContext(): NonNullable<ScenvCallbacks["onAskContext"]>;
|
|
18
35
|
/**
|
|
19
|
-
* Returns an object
|
|
20
|
-
*
|
|
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,
|
|
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
|
|
18
|
-
return async (name, _value
|
|
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
|
-
|
|
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
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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
|
-
|
|
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.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"description": "Inquirer prompt for scenv variables",
|
|
5
|
-
"repository": {
|
|
6
|
-
"publishConfig": {
|
|
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
|
}
|