@clerc/plugin-not-found 1.0.2 → 1.1.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/README.md CHANGED
@@ -2,23 +2,9 @@
2
2
 
3
3
  [![NPM version](https://img.shields.io/npm/v/@clerc/plugin-not-found?color=a1b858&label=)](https://www.npmjs.com/package/@clerc/plugin-not-found)
4
4
 
5
- Clerc plugin to show friendly error message when command not found.
5
+ ## Documenation
6
6
 
7
- ## 📦 Installation
8
-
9
- ```bash
10
- $ npm install @clerc/plugin-not-found
11
- $ yarn add @clerc/plugin-not-found
12
- $ pnpm add @clerc/plugin-not-found
13
- ```
14
-
15
- ## 🚀 Usage
16
-
17
- ```ts
18
- import { notFoundPlugin } from "@clerc/plugin-not-found";
19
-
20
- cli.use(notFoundPlugin());
21
- ```
7
+ Read the [documentation](https://clerc.so1ve.dev/official-plugins/plugin-not-found.html) for more details.
22
8
 
23
9
  ## 📝 License
24
10
 
@@ -0,0 +1,16 @@
1
+ import { Plugin } from "@clerc/core";
2
+
3
+ //#region src/index.d.ts
4
+ interface NotFoundPluginOptions {
5
+ /**
6
+ * Distance threshold for suggesting commands.
7
+ *
8
+ * @default 5
9
+ */
10
+ distanceThreshold?: number;
11
+ }
12
+ declare const notFoundPlugin: ({
13
+ distanceThreshold
14
+ }?: NotFoundPluginOptions) => Plugin;
15
+ //#endregion
16
+ export { NotFoundPluginOptions, notFoundPlugin };
package/dist/index.mjs ADDED
@@ -0,0 +1,154 @@
1
+ import { NoCommandSpecifiedError, NoSuchCommandError, definePlugin } from "@clerc/core";
2
+ import * as tint from "@uttr/tint";
3
+
4
+ //#region rolldown:runtime
5
+ var __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
6
+
7
+ //#endregion
8
+ //#region ../../node_modules/.pnpm/fastest-levenshtein@1.0.16/node_modules/fastest-levenshtein/mod.js
9
+ var require_fastest_levenshtein = /* @__PURE__ */ __commonJSMin(((exports) => {
10
+ exports.__esModule = true;
11
+ exports.distance = exports.closest = void 0;
12
+ var peq = new Uint32Array(65536);
13
+ var myers_32 = function(a, b) {
14
+ var n = a.length;
15
+ var m = b.length;
16
+ var lst = 1 << n - 1;
17
+ var pv = -1;
18
+ var mv = 0;
19
+ var sc = n;
20
+ var i = n;
21
+ while (i--) peq[a.charCodeAt(i)] |= 1 << i;
22
+ for (i = 0; i < m; i++) {
23
+ var eq = peq[b.charCodeAt(i)];
24
+ var xv = eq | mv;
25
+ eq |= (eq & pv) + pv ^ pv;
26
+ mv |= ~(eq | pv);
27
+ pv &= eq;
28
+ if (mv & lst) sc++;
29
+ if (pv & lst) sc--;
30
+ mv = mv << 1 | 1;
31
+ pv = pv << 1 | ~(xv | mv);
32
+ mv &= xv;
33
+ }
34
+ i = n;
35
+ while (i--) peq[a.charCodeAt(i)] = 0;
36
+ return sc;
37
+ };
38
+ var myers_x = function(b, a) {
39
+ var n = a.length;
40
+ var m = b.length;
41
+ var mhc = [];
42
+ var phc = [];
43
+ var hsize = Math.ceil(n / 32);
44
+ var vsize = Math.ceil(m / 32);
45
+ for (var i = 0; i < hsize; i++) {
46
+ phc[i] = -1;
47
+ mhc[i] = 0;
48
+ }
49
+ var j = 0;
50
+ for (; j < vsize - 1; j++) {
51
+ var mv_1 = 0;
52
+ var pv_1 = -1;
53
+ var start_1 = j * 32;
54
+ var vlen_1 = Math.min(32, m) + start_1;
55
+ for (var k = start_1; k < vlen_1; k++) peq[b.charCodeAt(k)] |= 1 << k;
56
+ for (var i = 0; i < n; i++) {
57
+ var eq = peq[a.charCodeAt(i)];
58
+ var pb = phc[i / 32 | 0] >>> i & 1;
59
+ var mb = mhc[i / 32 | 0] >>> i & 1;
60
+ var xv = eq | mv_1;
61
+ var xh = ((eq | mb) & pv_1) + pv_1 ^ pv_1 | eq | mb;
62
+ var ph = mv_1 | ~(xh | pv_1);
63
+ var mh = pv_1 & xh;
64
+ if (ph >>> 31 ^ pb) phc[i / 32 | 0] ^= 1 << i;
65
+ if (mh >>> 31 ^ mb) mhc[i / 32 | 0] ^= 1 << i;
66
+ ph = ph << 1 | pb;
67
+ mh = mh << 1 | mb;
68
+ pv_1 = mh | ~(xv | ph);
69
+ mv_1 = ph & xv;
70
+ }
71
+ for (var k = start_1; k < vlen_1; k++) peq[b.charCodeAt(k)] = 0;
72
+ }
73
+ var mv = 0;
74
+ var pv = -1;
75
+ var start = j * 32;
76
+ var vlen = Math.min(32, m - start) + start;
77
+ for (var k = start; k < vlen; k++) peq[b.charCodeAt(k)] |= 1 << k;
78
+ var score = m;
79
+ for (var i = 0; i < n; i++) {
80
+ var eq = peq[a.charCodeAt(i)];
81
+ var pb = phc[i / 32 | 0] >>> i & 1;
82
+ var mb = mhc[i / 32 | 0] >>> i & 1;
83
+ var xv = eq | mv;
84
+ var xh = ((eq | mb) & pv) + pv ^ pv | eq | mb;
85
+ var ph = mv | ~(xh | pv);
86
+ var mh = pv & xh;
87
+ score += ph >>> m - 1 & 1;
88
+ score -= mh >>> m - 1 & 1;
89
+ if (ph >>> 31 ^ pb) phc[i / 32 | 0] ^= 1 << i;
90
+ if (mh >>> 31 ^ mb) mhc[i / 32 | 0] ^= 1 << i;
91
+ ph = ph << 1 | pb;
92
+ mh = mh << 1 | mb;
93
+ pv = mh | ~(xv | ph);
94
+ mv = ph & xv;
95
+ }
96
+ for (var k = start; k < vlen; k++) peq[b.charCodeAt(k)] = 0;
97
+ return score;
98
+ };
99
+ var distance = function(a, b) {
100
+ if (a.length < b.length) {
101
+ var tmp = b;
102
+ b = a;
103
+ a = tmp;
104
+ }
105
+ if (b.length === 0) return a.length;
106
+ if (a.length <= 32) return myers_32(a, b);
107
+ return myers_x(a, b);
108
+ };
109
+ exports.distance = distance;
110
+ var closest = function(str, arr) {
111
+ var min_distance = Infinity;
112
+ var min_index = 0;
113
+ for (var i = 0; i < arr.length; i++) {
114
+ var dist = distance(str, arr[i]);
115
+ if (dist < min_distance) {
116
+ min_distance = dist;
117
+ min_index = i;
118
+ }
119
+ }
120
+ return arr[min_index];
121
+ };
122
+ exports.closest = closest;
123
+ }));
124
+
125
+ //#endregion
126
+ //#region src/index.ts
127
+ var import_fastest_levenshtein = require_fastest_levenshtein();
128
+ const notFoundPlugin = ({ distanceThreshold = 5 } = {}) => definePlugin({ setup: (cli) => cli.interceptor({
129
+ enforce: "post",
130
+ handler: async (_ctx, next) => {
131
+ const commandKeys = [...cli._commands.keys()];
132
+ const hasCommands = commandKeys.length > 0;
133
+ try {
134
+ await next();
135
+ } catch (e) {
136
+ if (!(e instanceof NoSuchCommandError) && !(e instanceof NoCommandSpecifiedError)) throw e;
137
+ if (e instanceof NoCommandSpecifiedError) {
138
+ let text$1 = "No command specified.";
139
+ if (hasCommands) text$1 += `\nPossible commands: ${commandKeys.join(", ")}.`;
140
+ throw new NoCommandSpecifiedError(text$1);
141
+ }
142
+ const { commandName } = e;
143
+ let closestCommandName = (0, import_fastest_levenshtein.closest)(commandName, commandKeys);
144
+ if ((0, import_fastest_levenshtein.distance)(commandName, closestCommandName) > distanceThreshold) closestCommandName = void 0;
145
+ let text = `Command "${tint.strikethrough(commandName)}" not found.`;
146
+ if (hasCommands && closestCommandName) text += `\nDid you mean "${tint.bold(closestCommandName)}"?`;
147
+ else if (!hasCommands) text += "\nNo commands registered.";
148
+ throw new NoSuchCommandError(commandName, text);
149
+ }
150
+ }
151
+ }) });
152
+
153
+ //#endregion
154
+ export { notFoundPlugin };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/plugin-not-found",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "author": "Ray <i@mk1.io> (https://github.com/so1ve)",
5
5
  "type": "module",
6
6
  "description": "Clerc plugin not found (did you mean)",
@@ -25,19 +25,12 @@
25
25
  "license": "MIT",
26
26
  "sideEffects": false,
27
27
  "exports": {
28
- ".": "./dist/index.js"
29
- },
30
- "main": "./dist/index.js",
31
- "module": "./dist/index.js",
32
- "types": "dist/index.d.ts",
33
- "typesVersions": {
34
- "*": {
35
- "*": [
36
- "./dist/*",
37
- "./dist/index.d.ts"
38
- ]
39
- }
28
+ ".": "./dist/index.mjs",
29
+ "./package.json": "./package.json"
40
30
  },
31
+ "main": "./dist/index.mjs",
32
+ "module": "./dist/index.mjs",
33
+ "types": "./dist/index.d.mts",
41
34
  "files": [
42
35
  "dist"
43
36
  ],
@@ -45,12 +38,11 @@
45
38
  "access": "public"
46
39
  },
47
40
  "dependencies": {
48
- "@uttr/tint": "^0.1.3",
49
- "didyoumean2": "^7.0.4"
41
+ "@uttr/tint": "^0.1.3"
50
42
  },
51
43
  "devDependencies": {
52
- "@clerc/utils": "1.0.2",
53
- "@clerc/core": "1.0.2"
44
+ "fastest-levenshtein": "^1.0.16",
45
+ "@clerc/core": "1.1.0"
54
46
  },
55
47
  "peerDependencies": {
56
48
  "@clerc/core": "*"
package/dist/index.d.ts DELETED
@@ -1,6 +0,0 @@
1
- import { Plugin } from "@clerc/core";
2
-
3
- //#region src/index.d.ts
4
- declare const notFoundPlugin: () => Plugin;
5
- //#endregion
6
- export { notFoundPlugin };
package/dist/index.js DELETED
@@ -1,31 +0,0 @@
1
- import { NoCommandSpecifiedError, NoSuchCommandError, definePlugin } from "@clerc/core";
2
- import * as tint from "@uttr/tint";
3
- import didyoumean from "didyoumean2";
4
-
5
- //#region src/index.ts
6
- const notFoundPlugin = () => definePlugin({ setup: (cli) => cli.interceptor({
7
- enforce: "post",
8
- handler: async (_ctx, next) => {
9
- const commandKeys = [...cli._commands.keys()];
10
- const hasCommands = commandKeys.length > 0;
11
- try {
12
- await next();
13
- } catch (e) {
14
- if (!(e instanceof NoSuchCommandError || e instanceof NoCommandSpecifiedError)) throw e;
15
- if (e instanceof NoCommandSpecifiedError) {
16
- let text$1 = "No command specified.";
17
- if (hasCommands) text$1 += `\nPossible commands: ${commandKeys.join(", ")}.`;
18
- throw new NoCommandSpecifiedError(text$1);
19
- }
20
- const { commandName } = e;
21
- const closestCommandName = didyoumean(commandName, commandKeys);
22
- let text = `Command "${tint.strikethrough(commandName)}" not found.`;
23
- if (hasCommands && closestCommandName) text += `\nDid you mean "${tint.bold(closestCommandName)}"?`;
24
- else if (!hasCommands) text += "\nNo commands registered.";
25
- throw new NoSuchCommandError(commandName, text);
26
- }
27
- }
28
- }) });
29
-
30
- //#endregion
31
- export { notFoundPlugin };