@wocker/utils 2.0.5 → 2.0.6-beta.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.
@@ -0,0 +1,9 @@
1
+ export declare class Volume {
2
+ source: string;
3
+ destination: string;
4
+ options?: string | undefined;
5
+ static readonly REGEX: RegExp;
6
+ constructor(source: string, destination: string, options?: string | undefined);
7
+ toString(): string;
8
+ static parse(volume: string): Volume;
9
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Volume = void 0;
4
+ class Volume {
5
+ constructor(source, destination, options) {
6
+ this.source = source;
7
+ this.destination = destination;
8
+ this.options = options;
9
+ }
10
+ toString() {
11
+ return `${this.source}:${this.destination}`;
12
+ }
13
+ static parse(volume) {
14
+ const [, source, destination, options] = Volume.REGEX.exec(volume) || [];
15
+ return new Volume(source, destination, options);
16
+ }
17
+ }
18
+ exports.Volume = Volume;
19
+ Volume.REGEX = /^([^:]+):([^:]+)(?::([^:]+))?$/;
@@ -1,3 +1,4 @@
1
1
  export * from "./promptConfirm";
2
2
  export * from "./promptInput";
3
+ export * from "./promptPath";
3
4
  export * from "./promptSelect";
@@ -16,4 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./promptConfirm"), exports);
18
18
  __exportStar(require("./promptInput"), exports);
19
+ __exportStar(require("./promptPath"), exports);
19
20
  __exportStar(require("./promptSelect"), exports);
@@ -13,6 +13,7 @@ exports.promptConfirm = void 0;
13
13
  const core_1 = require("@inquirer/core");
14
14
  const validatePrompt_1 = require("../validation/validatePrompt");
15
15
  const prepareMessage_1 = require("../tools/prepareMessage");
16
+ const prepareHelp_1 = require("../tools/prepareHelp");
16
17
  exports.promptConfirm = (0, core_1.createPrompt)((config, done) => {
17
18
  const { message = "Confirm", default: defaultValue = false } = config;
18
19
  const [status, setStatus] = (0, core_1.useState)("idle"), [inputValue, setInputValue] = (0, core_1.useState)(""), [error, setError] = (0, core_1.useState)(""), icon = (0, core_1.usePrefix)({ status });
@@ -30,7 +31,8 @@ exports.promptConfirm = (0, core_1.createPrompt)((config, done) => {
30
31
  }, [inputValue, defaultValue]);
31
32
  const theme = (0, core_1.makeTheme)({
32
33
  style: {
33
- message: prepareMessage_1.prepareMessage
34
+ message: prepareMessage_1.prepareMessage,
35
+ defaultAnswer: prepareHelp_1.prepareHelp
34
36
  }
35
37
  }, config.theme);
36
38
  (0, core_1.useKeypress)((key, readline) => __awaiter(void 0, void 0, void 0, function* () {
@@ -14,12 +14,14 @@ const core_1 = require("@inquirer/core");
14
14
  const validatePrompt_1 = require("../validation/validatePrompt");
15
15
  const prepareMessage_1 = require("../tools/prepareMessage");
16
16
  const prepareValue_1 = require("../tools/prepareValue");
17
+ const prepareHelp_1 = require("../tools/prepareHelp");
17
18
  exports.promptInput = (0, core_1.createPrompt)((config, done) => {
18
19
  const { message = "Input", type = "text", step = 1, prefix = "", suffix = "", default: defaultValue } = config;
19
20
  const theme = (0, core_1.makeTheme)({
20
21
  style: {
21
22
  message: prepareMessage_1.prepareMessage,
22
- value: prepareValue_1.prepareValue
23
+ value: prepareValue_1.prepareValue,
24
+ help: prepareHelp_1.prepareHelp
23
25
  }
24
26
  }, config.theme);
25
27
  const [inputValue = "", setInputValue] = (0, core_1.useState)(typeof defaultValue === "number" ? defaultValue.toString() : defaultValue), [status, setStatus] = (0, core_1.useState)("idle"), [error, setError] = (0, core_1.useState)("");
@@ -0,0 +1,8 @@
1
+ import { PromptConfig } from "../types/PromptConfig";
2
+ type Config = PromptConfig<string, {
3
+ type?: "file" | "directory" | "any";
4
+ basePath?: string;
5
+ showHidden?: boolean;
6
+ }>;
7
+ export declare const promptPath: import("@inquirer/type").Prompt<string, Config>;
8
+ export {};
@@ -0,0 +1,177 @@
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.promptPath = void 0;
16
+ const core_1 = require("@inquirer/core");
17
+ const figures_1 = __importDefault(require("@inquirer/figures"));
18
+ const fs_1 = __importDefault(require("fs"));
19
+ const path_1 = __importDefault(require("path"));
20
+ const validatePrompt_1 = require("../validation/validatePrompt");
21
+ const prepareMessage_1 = require("../tools/prepareMessage");
22
+ const prepareValue_1 = require("../tools/prepareValue");
23
+ const prepareHelp_1 = require("../tools/prepareHelp");
24
+ exports.promptPath = (0, core_1.createPrompt)((config, done) => {
25
+ const { message = "Enter path", type = "any", basePath = process.cwd(), showHidden = false, default: defaultValue = "" } = config;
26
+ const theme = (0, core_1.makeTheme)({
27
+ style: {
28
+ message: prepareMessage_1.prepareMessage,
29
+ value: prepareValue_1.prepareValue,
30
+ help: prepareHelp_1.prepareHelp
31
+ }
32
+ }, config.theme);
33
+ const [inputValue, setInputValue] = (0, core_1.useState)(defaultValue), [status, setStatus] = (0, core_1.useState)("idle"), [error, setError] = (0, core_1.useState)(""), [selectedSuggestion, setSelectedSuggestion] = (0, core_1.useState)(0);
34
+ const [base, currentDir] = (0, core_1.useMemo)(() => {
35
+ const fullPath = inputValue
36
+ ? (path_1.default.isAbsolute(inputValue) ? inputValue : path_1.default.join(basePath, inputValue))
37
+ : basePath;
38
+ try {
39
+ const stat = fs_1.default.statSync(fullPath);
40
+ if (stat.isDirectory()) {
41
+ return ["", fullPath];
42
+ }
43
+ }
44
+ catch (err) { }
45
+ return inputValue
46
+ ? [path_1.default.basename(fullPath), path_1.default.dirname(fullPath)]
47
+ : ["", fullPath];
48
+ }, [inputValue]);
49
+ const suggestions = (0, core_1.useMemo)(() => {
50
+ try {
51
+ if (!fs_1.default.existsSync(currentDir)) {
52
+ return [];
53
+ }
54
+ const files = fs_1.default.readdirSync(currentDir);
55
+ return files
56
+ .filter(file => {
57
+ if (!showHidden && file.startsWith("."))
58
+ return false;
59
+ if (!file.toLowerCase().startsWith(base.toLowerCase()))
60
+ return false;
61
+ const filePath = path_1.default.join(currentDir, file);
62
+ const stats = fs_1.default.statSync(filePath);
63
+ if (type === "file")
64
+ return stats.isFile();
65
+ if (type === "directory")
66
+ return stats.isDirectory();
67
+ return true;
68
+ })
69
+ .sort((a, b) => {
70
+ const aPath = path_1.default.join(currentDir, a);
71
+ const bPath = path_1.default.join(currentDir, b);
72
+ const aIsDir = fs_1.default.statSync(aPath).isDirectory();
73
+ const bIsDir = fs_1.default.statSync(bPath).isDirectory();
74
+ if (aIsDir && !bIsDir)
75
+ return -1;
76
+ if (!aIsDir && bIsDir)
77
+ return 1;
78
+ return a.localeCompare(b);
79
+ });
80
+ }
81
+ catch (err) {
82
+ return [];
83
+ }
84
+ }, [inputValue]);
85
+ const icon = (0, core_1.usePrefix)({ theme, status });
86
+ const page = (0, core_1.usePagination)({
87
+ items: suggestions,
88
+ active: selectedSuggestion,
89
+ pageSize: 6,
90
+ renderItem: ({ item, isActive }) => {
91
+ const status = isActive
92
+ ? "selected"
93
+ : "idle";
94
+ return [
95
+ isActive
96
+ ? theme.style.value(figures_1.default.pointer, "active")
97
+ : " ",
98
+ " ",
99
+ theme.style.value(item, status)
100
+ ].join("");
101
+ }
102
+ });
103
+ const handleUp = () => {
104
+ setSelectedSuggestion((suggestions.length + selectedSuggestion - 1) % suggestions.length);
105
+ };
106
+ const handleDown = () => {
107
+ if (suggestions.length === 0) {
108
+ return;
109
+ }
110
+ setSelectedSuggestion((selectedSuggestion + 1) % suggestions.length);
111
+ };
112
+ const handleChange = (readline) => {
113
+ setInputValue(readline.line);
114
+ setSelectedSuggestion(0);
115
+ setStatus("idle");
116
+ setError("");
117
+ };
118
+ const handleTab = (readline) => {
119
+ if (suggestions.length === 0)
120
+ return;
121
+ const suggestion = suggestions[selectedSuggestion];
122
+ const newPath = path_1.default.join(currentDir, suggestion);
123
+ const relativePath = path_1.default.isAbsolute(inputValue)
124
+ ? newPath
125
+ : path_1.default.relative(basePath, newPath);
126
+ const stats = fs_1.default.statSync(newPath);
127
+ const finalPath = stats.isDirectory()
128
+ ? relativePath + path_1.default.sep
129
+ : relativePath;
130
+ readline.clearLine(-1);
131
+ readline.write(finalPath);
132
+ setInputValue(finalPath);
133
+ setSelectedSuggestion(0);
134
+ };
135
+ const handleSubmit = (readline) => __awaiter(void 0, void 0, void 0, function* () {
136
+ setStatus("loading");
137
+ const error = yield (0, validatePrompt_1.validatePrompt)(inputValue, config);
138
+ if (error) {
139
+ readline.write(inputValue);
140
+ setStatus("error");
141
+ setError(error.message);
142
+ return;
143
+ }
144
+ setStatus("done");
145
+ setError("");
146
+ done(inputValue);
147
+ });
148
+ (0, core_1.useEffect)(() => {
149
+ setSelectedSuggestion(0);
150
+ }, [inputValue]);
151
+ (0, core_1.useKeypress)((key, readline) => __awaiter(void 0, void 0, void 0, function* () {
152
+ if ((0, core_1.isEnterKey)(key)) {
153
+ return yield handleSubmit(readline);
154
+ }
155
+ if ((0, core_1.isTabKey)(key)) {
156
+ return handleTab(readline);
157
+ }
158
+ if ((0, core_1.isUpKey)(key)) {
159
+ return handleUp();
160
+ }
161
+ if ((0, core_1.isDownKey)(key)) {
162
+ return handleDown();
163
+ }
164
+ handleChange(readline);
165
+ }));
166
+ return [
167
+ [
168
+ `${icon} `,
169
+ theme.style.message(message, status),
170
+ theme.style.value(inputValue, status)
171
+ ].join(""),
172
+ [
173
+ error ? `${theme.style.error(error)}` : "",
174
+ status !== "done" && page ? `\n${page}` : ""
175
+ ].join("")
176
+ ];
177
+ });
@@ -21,12 +21,14 @@ const tools_1 = require("../tools");
21
21
  const validatePrompt_1 = require("../validation/validatePrompt");
22
22
  const prepareMessage_1 = require("../tools/prepareMessage");
23
23
  const prepareValue_1 = require("../tools/prepareValue");
24
+ const prepareHelp_1 = require("../tools/prepareHelp");
24
25
  const selectView = (config, done) => {
25
26
  const { message = "Select", multiple, pageSize = 5, options: rawOptions = [] } = config;
26
27
  const theme = (0, core_1.makeTheme)({
27
28
  style: {
28
29
  message: prepareMessage_1.prepareMessage,
29
- value: prepareValue_1.prepareValue
30
+ value: prepareValue_1.prepareValue,
31
+ help: prepareHelp_1.prepareHelp
30
32
  }
31
33
  }, config.theme);
32
34
  const options = (0, core_1.useMemo)(() => {
@@ -0,0 +1 @@
1
+ export declare const prepareHelp: (message: string) => string;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.prepareHelp = void 0;
7
+ const yoctocolors_cjs_1 = __importDefault(require("yoctocolors-cjs"));
8
+ const prepareHelp = (message) => {
9
+ return yoctocolors_cjs_1.default.gray(message);
10
+ };
11
+ exports.prepareHelp = prepareHelp;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wocker/utils",
3
3
  "type": "commonjs",
4
- "version": "2.0.5",
4
+ "version": "2.0.6-beta.0",
5
5
  "author": "Kris Papercut <krispcut@gmail.com>",
6
6
  "description": "Utils for @wocker",
7
7
  "license": "MIT",