@wocker/utils 2.0.5 → 2.0.6-beta.1

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,3 +1,4 @@
1
+ export * from "./makes";
1
2
  export * from "./prompts";
2
3
  export * from "./tools";
3
4
  export * from "./validators";
package/lib/index.js CHANGED
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./makes"), exports);
17
18
  __exportStar(require("./prompts"), exports);
18
19
  __exportStar(require("./tools"), exports);
19
20
  __exportStar(require("./validators"), exports);
@@ -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,22 @@
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}` + (this.options ? `:${this.options}` : "");
12
+ }
13
+ static parse(volume) {
14
+ if (!Volume.REGEX.test(volume)) {
15
+ throw new Error(`Invalid volume format for volume "${volume}"`);
16
+ }
17
+ const [, source = "/", destination = "/", options] = Volume.REGEX.exec(volume) || [];
18
+ return new Volume(source, destination, options);
19
+ }
20
+ }
21
+ exports.Volume = Volume;
22
+ Volume.REGEX = /^([^:]+):([^:]+)(?::([^:]+))?$/;
@@ -0,0 +1 @@
1
+ export * from "./Volume";
@@ -0,0 +1,17 @@
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("./Volume"), exports);
@@ -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,9 @@
1
+ import { Stats } from "fs";
2
+ import { PromptConfig } from "../types/PromptConfig";
3
+ type Config = PromptConfig<string, {
4
+ basePath?: string;
5
+ filter?: "file" | "directory" | ((path: string, stat: Stats) => boolean);
6
+ showHidden?: boolean;
7
+ }>;
8
+ export declare const promptPath: import("@inquirer/type").Prompt<string, Config>;
9
+ export {};
@@ -0,0 +1,178 @@
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", basePath = process.cwd(), showHidden = false, filter, 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
+ return fs_1.default.readdirSync(currentDir)
55
+ .filter((file) => {
56
+ if (!showHidden && file.startsWith("."))
57
+ return false;
58
+ if (!file.toLowerCase().startsWith(base.toLowerCase()))
59
+ return false;
60
+ const filePath = path_1.default.join(currentDir, file);
61
+ const stats = fs_1.default.statSync(filePath);
62
+ if (typeof filter === "function") {
63
+ return filter(filePath, stats);
64
+ }
65
+ else if (filter === "file") {
66
+ return stats.isFile() || stats.isDirectory();
67
+ }
68
+ else if (filter === "directory") {
69
+ return stats.isDirectory();
70
+ }
71
+ return true;
72
+ })
73
+ .sort((a, b) => {
74
+ const aPath = path_1.default.join(currentDir, a), bPath = path_1.default.join(currentDir, b), aIsDir = fs_1.default.statSync(aPath).isDirectory(), bIsDir = fs_1.default.statSync(bPath).isDirectory();
75
+ if (aIsDir && !bIsDir)
76
+ return -1;
77
+ if (!aIsDir && bIsDir)
78
+ return 1;
79
+ return a.localeCompare(b);
80
+ });
81
+ }
82
+ catch (err) {
83
+ return [];
84
+ }
85
+ }, [inputValue]);
86
+ const icon = (0, core_1.usePrefix)({ theme, status });
87
+ const page = (0, core_1.usePagination)({
88
+ items: suggestions,
89
+ active: selectedSuggestion,
90
+ pageSize: 6,
91
+ renderItem: ({ item, isActive }) => {
92
+ const status = isActive
93
+ ? "selected"
94
+ : "idle";
95
+ return [
96
+ isActive
97
+ ? theme.style.value(figures_1.default.pointer, "active")
98
+ : " ",
99
+ " ",
100
+ theme.style.value(item, status)
101
+ ].join("");
102
+ }
103
+ });
104
+ const handleUp = () => {
105
+ setSelectedSuggestion((suggestions.length + selectedSuggestion - 1) % suggestions.length);
106
+ };
107
+ const handleDown = () => {
108
+ if (suggestions.length === 0) {
109
+ return;
110
+ }
111
+ setSelectedSuggestion((selectedSuggestion + 1) % suggestions.length);
112
+ };
113
+ const handleChange = (readline) => {
114
+ setInputValue(readline.line);
115
+ setSelectedSuggestion(0);
116
+ setStatus("idle");
117
+ setError("");
118
+ };
119
+ const handleTab = (readline) => {
120
+ if (suggestions.length === 0)
121
+ return;
122
+ const suggestion = suggestions[selectedSuggestion];
123
+ const newPath = path_1.default.join(currentDir, suggestion);
124
+ const relativePath = path_1.default.isAbsolute(inputValue)
125
+ ? newPath
126
+ : path_1.default.relative(basePath, newPath);
127
+ const stats = fs_1.default.statSync(newPath);
128
+ const finalPath = stats.isDirectory()
129
+ ? relativePath + path_1.default.sep
130
+ : relativePath;
131
+ readline.clearLine(-1);
132
+ readline.write(finalPath);
133
+ setInputValue(finalPath);
134
+ setSelectedSuggestion(0);
135
+ };
136
+ const handleSubmit = (readline) => __awaiter(void 0, void 0, void 0, function* () {
137
+ setStatus("loading");
138
+ const error = yield (0, validatePrompt_1.validatePrompt)(inputValue, config);
139
+ if (error) {
140
+ readline.write(inputValue);
141
+ setStatus("error");
142
+ setError(error.message);
143
+ return;
144
+ }
145
+ setStatus("done");
146
+ setError("");
147
+ done(inputValue);
148
+ });
149
+ (0, core_1.useEffect)(() => {
150
+ setSelectedSuggestion(0);
151
+ }, [inputValue]);
152
+ (0, core_1.useKeypress)((key, readline) => __awaiter(void 0, void 0, void 0, function* () {
153
+ if ((0, core_1.isEnterKey)(key)) {
154
+ return yield handleSubmit(readline);
155
+ }
156
+ if ((0, core_1.isTabKey)(key)) {
157
+ return handleTab(readline);
158
+ }
159
+ if ((0, core_1.isUpKey)(key)) {
160
+ return handleUp();
161
+ }
162
+ if ((0, core_1.isDownKey)(key)) {
163
+ return handleDown();
164
+ }
165
+ handleChange(readline);
166
+ }));
167
+ return [
168
+ [
169
+ `${icon} `,
170
+ theme.style.message(message, status),
171
+ theme.style.value(inputValue, status)
172
+ ].join(""),
173
+ [
174
+ error ? `${theme.style.error(error)}` : "",
175
+ status !== "done" && page ? `\n${page}` : ""
176
+ ].join("")
177
+ ];
178
+ });
@@ -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;
@@ -1,6 +1,3 @@
1
- export type Volume = {
2
- source: string;
3
- destination: string;
4
- options?: string;
5
- };
6
- export declare const volumeFormat: (volume: Volume) => string;
1
+ import { VolumeData } from "../types/VolumeData";
2
+ /** @deprecated */
3
+ export declare const volumeFormat: (volume: VolumeData) => string;
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.volumeFormat = void 0;
4
+ const Volume_1 = require("../makes/Volume");
5
+ /** @deprecated */
4
6
  const volumeFormat = (volume) => {
5
- const { source = "/", destination = "/", options } = volume;
6
- return `${source}:${destination}` + (options ? `:${options}` : "");
7
+ return (new Volume_1.Volume(volume.source || "/", volume.destination || "/", volume.options)).toString();
7
8
  };
8
9
  exports.volumeFormat = volumeFormat;
@@ -1,2 +1,3 @@
1
- import { Volume } from "./volumeFormat";
2
- export declare const volumeParse: (volume: string) => Volume;
1
+ import { VolumeData } from "../types/VolumeData";
2
+ /** @deprecated */
3
+ export declare const volumeParse: (volume: string) => VolumeData;
@@ -1,13 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.volumeParse = void 0;
4
+ const Volume_1 = require("../makes/Volume");
5
+ /** @deprecated */
4
6
  const volumeParse = (volume) => {
5
- const regVolume = /^([^:]+):([^:]+)(?::([^:]+))?$/;
6
- const [, source, destination, options] = regVolume.exec(volume) || [];
7
- return {
8
- source,
9
- destination,
10
- options
11
- };
7
+ try {
8
+ const v = Volume_1.Volume.parse(volume);
9
+ return {
10
+ source: v.source,
11
+ destination: v.destination,
12
+ options: v.options
13
+ };
14
+ }
15
+ catch (_a) {
16
+ return {};
17
+ }
12
18
  };
13
19
  exports.volumeParse = volumeParse;
@@ -0,0 +1,5 @@
1
+ export type VolumeData = {
2
+ source: string;
3
+ destination: string;
4
+ options?: string | undefined;
5
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
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.1",
5
5
  "author": "Kris Papercut <krispcut@gmail.com>",
6
6
  "description": "Utils for @wocker",
7
7
  "license": "MIT",