@wocker/utils 1.0.0
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/LICENSE +21 -0
- package/README.md +11 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +20 -0
- package/lib/promptConfirm.d.ts +6 -0
- package/lib/promptConfirm.js +27 -0
- package/lib/promptGroup.d.ts +14 -0
- package/lib/promptGroup.js +54 -0
- package/lib/promptSelect.d.ts +12 -0
- package/lib/promptSelect.js +56 -0
- package/lib/promptText.d.ts +10 -0
- package/lib/promptText.js +52 -0
- package/package.json +30 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Kris Papercut
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./promptConfirm"), exports);
|
|
18
|
+
__exportStar(require("./promptGroup"), exports);
|
|
19
|
+
__exportStar(require("./promptSelect"), exports);
|
|
20
|
+
__exportStar(require("./promptText"), exports);
|
|
@@ -0,0 +1,27 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.promptConfirm = void 0;
|
|
16
|
+
const inquirer_1 = __importDefault(require("inquirer"));
|
|
17
|
+
const promptConfirm = (options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
18
|
+
const { message, default: defaultValue = true } = options;
|
|
19
|
+
const { confirm } = yield inquirer_1.default.prompt({
|
|
20
|
+
type: "confirm",
|
|
21
|
+
name: "confirm",
|
|
22
|
+
message,
|
|
23
|
+
default: defaultValue
|
|
24
|
+
});
|
|
25
|
+
return confirm;
|
|
26
|
+
});
|
|
27
|
+
exports.promptConfirm = promptConfirm;
|
|
@@ -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 promptGroup: (options: Options, values?: any) => Promise<any>;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
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.promptGroup = void 0;
|
|
24
|
+
const promptConfirm_1 = require("./promptConfirm");
|
|
25
|
+
const promptSelect_1 = require("./promptSelect");
|
|
26
|
+
const promptText_1 = require("./promptText");
|
|
27
|
+
const promptGroup = (options, values = {}) => __awaiter(void 0, void 0, void 0, function* () {
|
|
28
|
+
const res = {};
|
|
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
|
+
res[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
|
+
res[key] = yield (0, promptConfirm_1.promptConfirm)(Object.assign(Object.assign({}, rest), { default: typeof values[key] !== "undefined"
|
|
41
|
+
? values[key] === "true"
|
|
42
|
+
: params.default }));
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
case "select": {
|
|
46
|
+
const { type } = params, rest = __rest(params, ["type"]);
|
|
47
|
+
res[key] = yield (0, promptSelect_1.promptSelect)(Object.assign(Object.assign({}, rest), { default: values[key] || params.default }));
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return res;
|
|
53
|
+
});
|
|
54
|
+
exports.promptGroup = promptGroup;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type Options = {
|
|
2
|
+
message?: string;
|
|
3
|
+
options: string[] | {
|
|
4
|
+
[value: string]: string;
|
|
5
|
+
} | {
|
|
6
|
+
label?: string;
|
|
7
|
+
value: string;
|
|
8
|
+
}[];
|
|
9
|
+
default?: string;
|
|
10
|
+
};
|
|
11
|
+
export declare const promptSelect: (props: Options) => Promise<string>;
|
|
12
|
+
export { Options as PromptSelectOptions };
|
|
@@ -0,0 +1,56 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.promptSelect = void 0;
|
|
16
|
+
const inquirer_1 = __importDefault(require("inquirer"));
|
|
17
|
+
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
|
+
};
|
|
35
|
+
});
|
|
36
|
+
const defaultOption = options.find((option) => {
|
|
37
|
+
return option.value === defaultValue;
|
|
38
|
+
});
|
|
39
|
+
const { value } = yield inquirer_1.default.prompt({
|
|
40
|
+
choices: options.map((option) => {
|
|
41
|
+
return option.label;
|
|
42
|
+
}),
|
|
43
|
+
message,
|
|
44
|
+
name: "value",
|
|
45
|
+
type: "list",
|
|
46
|
+
default: defaultOption ? (defaultOption.label || defaultOption.value) : ""
|
|
47
|
+
});
|
|
48
|
+
const selected = options.find((option) => {
|
|
49
|
+
return option.label === value;
|
|
50
|
+
});
|
|
51
|
+
if (selected) {
|
|
52
|
+
return selected.value;
|
|
53
|
+
}
|
|
54
|
+
return "";
|
|
55
|
+
});
|
|
56
|
+
exports.promptSelect = promptSelect;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
type Options = {
|
|
2
|
+
required?: boolean;
|
|
3
|
+
message?: string;
|
|
4
|
+
prefix?: string;
|
|
5
|
+
suffix?: string;
|
|
6
|
+
type?: "string" | "number" | "int";
|
|
7
|
+
default?: number | string;
|
|
8
|
+
};
|
|
9
|
+
export declare const promptText: (options: Options) => Promise<any>;
|
|
10
|
+
export { Options as PromptTextOptions };
|
|
@@ -0,0 +1,52 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.promptText = void 0;
|
|
16
|
+
const inquirer_1 = __importDefault(require("inquirer"));
|
|
17
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
18
|
+
const promptText = (options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
19
|
+
const { required, message, prefix = "", suffix = "", type, default: defaultValue } = options;
|
|
20
|
+
const { value } = yield inquirer_1.default.prompt({
|
|
21
|
+
name: "value",
|
|
22
|
+
type: "input",
|
|
23
|
+
message,
|
|
24
|
+
default: defaultValue,
|
|
25
|
+
validate(value) {
|
|
26
|
+
if (required) {
|
|
27
|
+
if (typeof value === "undefined" || value === "") {
|
|
28
|
+
return "Mandatory value";
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (type === "int" || type === "number") {
|
|
32
|
+
if (isNaN(parseInt(value)) || parseInt(value).toString() !== value) {
|
|
33
|
+
return "Should be integer";
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return true;
|
|
37
|
+
},
|
|
38
|
+
transformer(value) {
|
|
39
|
+
if (!prefix && !suffix) {
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
if (suffix) {
|
|
43
|
+
setTimeout(() => {
|
|
44
|
+
process.stdout.write(`\x1b[${suffix.length}D`);
|
|
45
|
+
}, 0);
|
|
46
|
+
}
|
|
47
|
+
return `${chalk_1.default.gray(prefix)}${value}${chalk_1.default.gray(suffix)}`;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
return value;
|
|
51
|
+
});
|
|
52
|
+
exports.promptText = promptText;
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wocker/utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"author": "Kris Papercut <krispcut@gmail.com>",
|
|
5
|
+
"description": "Utils for @wocker",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "lib/index.js",
|
|
8
|
+
"types": "lib/index.d.ts",
|
|
9
|
+
"homepage": "https://github.com/kearisp/wocker-utils#readme",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/kearisp/wocker-utils.git"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/kearisp/wocker-utils/issues"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"prepare": "npm run build",
|
|
19
|
+
"watch": "tsc -w",
|
|
20
|
+
"build": "tsc",
|
|
21
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"inquirer": "^7.0.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"jest": "^29.7.0",
|
|
28
|
+
"typescript": "^5.2.2"
|
|
29
|
+
}
|
|
30
|
+
}
|