@wocker/utils 1.0.1 → 1.0.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/lib/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./demuxOutput";
2
+ export * from "./promptConfig";
2
3
  export * from "./promptConfirm";
3
4
  export * from "./promptGroup";
4
5
  export * from "./promptSelect";
package/lib/index.js CHANGED
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./demuxOutput"), exports);
18
+ __exportStar(require("./promptConfig"), exports);
18
19
  __exportStar(require("./promptConfirm"), exports);
19
20
  __exportStar(require("./promptGroup"), exports);
20
21
  __exportStar(require("./promptSelect"), exports);
@@ -0,0 +1,10 @@
1
+ export type RawOptions = string[] | {
2
+ [value: string]: string;
3
+ } | {
4
+ label?: string;
5
+ value: string;
6
+ }[];
7
+ export declare const normalizeOptions: (rawOptions: RawOptions) => {
8
+ label: string;
9
+ value: string;
10
+ }[];
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.normalizeOptions = void 0;
4
+ const normalizeOptions = (rawOptions) => {
5
+ return Array.isArray(rawOptions)
6
+ ? rawOptions.map((option) => {
7
+ return {
8
+ label: typeof option === "string"
9
+ ? option
10
+ : option.label || option.value || "",
11
+ value: typeof option === "string"
12
+ ? option
13
+ : option.value || ""
14
+ };
15
+ })
16
+ : Object.keys(rawOptions).map((value) => {
17
+ return {
18
+ label: rawOptions[value],
19
+ value
20
+ };
21
+ });
22
+ };
23
+ exports.normalizeOptions = normalizeOptions;
@@ -0,0 +1,14 @@
1
+ import { PromptConfirmOptions } from "./promptConfirm";
2
+ import { PromptSelectOptions } from "./promptSelect";
3
+ import { PromptTextOptions } from "./promptText";
4
+ type ConfirmOptions = PromptConfirmOptions & {
5
+ type: "boolean";
6
+ };
7
+ type SelectOptions = PromptSelectOptions & {
8
+ type: "select";
9
+ };
10
+ type Options = {
11
+ [key: string]: PromptTextOptions | ConfirmOptions | SelectOptions;
12
+ };
13
+ export declare const promptConfig: (options: Options, values?: any) => Promise<any>;
14
+ export {};
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __rest = (this && this.__rest) || function (s, e) {
12
+ var t = {};
13
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14
+ t[p] = s[p];
15
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
16
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
17
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
18
+ t[p[i]] = s[p[i]];
19
+ }
20
+ return t;
21
+ };
22
+ Object.defineProperty(exports, "__esModule", { value: true });
23
+ exports.promptConfig = void 0;
24
+ const normalizeOptions_1 = require("./normalizeOptions");
25
+ const promptConfirm_1 = require("./promptConfirm");
26
+ const promptSelect_1 = require("./promptSelect");
27
+ const promptText_1 = require("./promptText");
28
+ const promptConfig = (options, values = {}) => __awaiter(void 0, void 0, void 0, function* () {
29
+ for (const key in options) {
30
+ const params = options[key];
31
+ switch (params.type) {
32
+ case "number":
33
+ case "int":
34
+ case "string": {
35
+ values[key] = yield (0, promptText_1.promptText)(Object.assign(Object.assign({}, params), { default: values[key] || params.default }));
36
+ break;
37
+ }
38
+ case "boolean": {
39
+ const { type } = params, rest = __rest(params, ["type"]);
40
+ const selected = yield (0, promptConfirm_1.promptConfirm)(Object.assign(Object.assign({}, rest), { default: typeof values[key] === "boolean"
41
+ ? values[key]
42
+ : typeof values[key] !== "undefined"
43
+ ? values[key] === "true"
44
+ : params.default }));
45
+ if (selected) {
46
+ values[key] = true;
47
+ }
48
+ else if (key in values) {
49
+ delete values[key];
50
+ }
51
+ break;
52
+ }
53
+ case "select": {
54
+ if (params.multiple) {
55
+ const { type } = params, rest = __rest(params, ["type"]);
56
+ const options = (0, normalizeOptions_1.normalizeOptions)(params.options);
57
+ const selected = yield (0, promptSelect_1.promptSelect)(Object.assign(Object.assign({}, rest), { default: options.map((option) => {
58
+ return option.value;
59
+ }).filter((name) => {
60
+ return values[name];
61
+ }) }));
62
+ for (const option of options) {
63
+ if (selected.includes(option.value)) {
64
+ values[option.value] = true;
65
+ }
66
+ else if (option.value in values) {
67
+ delete values[option.value];
68
+ }
69
+ }
70
+ }
71
+ else {
72
+ const { type } = params, rest = __rest(params, ["type"]);
73
+ values[key] = yield (0, promptSelect_1.promptSelect)(Object.assign(Object.assign({}, rest), { default: values[key] || params.default }));
74
+ }
75
+ break;
76
+ }
77
+ }
78
+ }
79
+ return values;
80
+ });
81
+ exports.promptConfig = promptConfig;
@@ -1,12 +1,14 @@
1
+ import { RawOptions } from "./normalizeOptions";
1
2
  type Options = {
2
3
  message?: string;
3
- options: string[] | {
4
- [value: string]: string;
5
- } | {
6
- label?: string;
7
- value: string;
8
- }[];
4
+ options: RawOptions;
5
+ } & ({
6
+ multiple?: false;
9
7
  default?: string;
10
- };
11
- export declare const promptSelect: (props: Options) => Promise<string>;
8
+ } | {
9
+ multiple: true;
10
+ default?: string[];
11
+ });
12
+ type ResultType<T extends Options> = T['multiple'] extends true ? string[] : string;
13
+ export declare const promptSelect: <T extends Options>(props: T) => Promise<ResultType<T>>;
12
14
  export { Options as PromptSelectOptions };
@@ -14,27 +14,29 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.promptSelect = void 0;
16
16
  const inquirer_1 = __importDefault(require("inquirer"));
17
+ const normalizeOptions_1 = require("./normalizeOptions");
17
18
  const promptSelect = (props) => __awaiter(void 0, void 0, void 0, function* () {
18
- const { message, options: rawOptions, default: defaultValue } = props;
19
- const options = Array.isArray(rawOptions)
20
- ? rawOptions.map((option) => {
21
- return {
22
- label: typeof option === "string"
23
- ? option
24
- : option.label || option.value || "",
25
- value: typeof option === "string"
26
- ? option
27
- : option.value || ""
28
- };
29
- })
30
- : Object.keys(rawOptions).map((value) => {
31
- return {
32
- label: rawOptions[value],
33
- value
34
- };
19
+ const { message, options: rawOptions, } = props;
20
+ const options = (0, normalizeOptions_1.normalizeOptions)(rawOptions);
21
+ if (props.multiple) {
22
+ const { value } = yield inquirer_1.default.prompt({
23
+ message,
24
+ name: "value",
25
+ type: "checkbox",
26
+ choices: options.map((option) => {
27
+ return {
28
+ name: option.label,
29
+ value: option.value,
30
+ checked: Array.isArray(props.default)
31
+ ? props.default.includes(option.value)
32
+ : false
33
+ };
34
+ })
35
35
  });
36
+ return value;
37
+ }
36
38
  const defaultOption = options.find((option) => {
37
- return option.value === defaultValue;
39
+ return option.value === props.default;
38
40
  });
39
41
  const { value } = yield inquirer_1.default.prompt({
40
42
  choices: options.map((option) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wocker/utils",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "author": "Kris Papercut <krispcut@gmail.com>",
5
5
  "description": "Utils for @wocker",
6
6
  "license": "MIT",
@@ -1,31 +0,0 @@
1
- name: Publish
2
-
3
- on:
4
- release:
5
- types:
6
- - published
7
-
8
- jobs:
9
- publish:
10
- runs-on: ubuntu-latest
11
- environment: publish
12
- steps:
13
- - name: Checkout code
14
- uses: actions/checkout@v2
15
-
16
- - name: Set up Node.js
17
- uses: actions/setup-node@v3
18
- with:
19
- node-version: '18'
20
- registry-url: 'https://registry.npmjs.org'
21
-
22
- - name: Install dependencies
23
- run: npm install
24
-
25
- - name: Build
26
- run: npm run build
27
-
28
- - name: Publish to NPM
29
- env:
30
- NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}
31
- run: npm publish