@clerc/plugin-not-found 0.39.0 → 0.41.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.
Files changed (4) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +25 -0
  3. package/dist/index.js +74 -2
  4. package/package.json +12 -13
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2022 Ray <https://github.com/so1ve>
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.
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Ray <https://github.com/so1ve>
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
@@ -0,0 +1,25 @@
1
+ # @clerc/plugin-not-found
2
+
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
+
5
+ Clerc plugin to show friendly error message when command not found.
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
+ ```
22
+
23
+ ## 📝 License
24
+
25
+ [MIT](../../LICENSE). Made with ❤️ by [Ray](https://github.com/so1ve)
package/dist/index.js CHANGED
@@ -1,2 +1,74 @@
1
- import{definePlugin as Y,NoSuchCommandError as P,NoCommandGivenError as w}from"@clerc/core";import G from"node:tty";const z={en:{"utils.and":"%s and %s"},"zh-CN":{"utils.and":"%s \u548C %s"}},q=(e,{add:o,t:r})=>(o(z),e.length<=1?e[0]:r("utils.and",e.slice(0,-1).join(", "),e[e.length-1])),E=new Uint32Array(65536),B=(e,o)=>{const r=e.length,h=o.length,x=1<<r-1;let a=-1,l=0,m=r,s=r;for(;s--;)E[e.charCodeAt(s)]|=1<<s;for(s=0;s<h;s++){let f=E[o.charCodeAt(s)];const S=f|l;f|=(f&a)+a^a,l|=~(f|a),a&=f,l&x&&m++,a&x&&m--,l=l<<1|1,a=a<<1|~(S|l),l&=S}for(s=r;s--;)E[e.charCodeAt(s)]=0;return m},Z=(e,o)=>{const r=o.length,h=e.length,x=[],a=[],l=Math.ceil(r/32),m=Math.ceil(h/32);for(let t=0;t<l;t++)a[t]=-1,x[t]=0;let s=0;for(;s<m-1;s++){let t=0,c=-1;const i=s*32,y=Math.min(32,h)+i;for(let d=i;d<y;d++)E[e.charCodeAt(d)]|=1<<d;for(let d=0;d<r;d++){const I=E[o.charCodeAt(d)],A=a[d/32|0]>>>d&1,T=x[d/32|0]>>>d&1,O=I|t,M=((I|T)&c)+c^c|I|T;let g=t|~(M|c),L=c&M;g>>>31^A&&(a[d/32|0]^=1<<d),L>>>31^T&&(x[d/32|0]^=1<<d),g=g<<1|A,L=L<<1|T,c=L|~(O|g),t=g&O}for(let d=i;d<y;d++)E[e.charCodeAt(d)]=0}let f=0,S=-1;const p=s*32,u=Math.min(32,h-p)+p;for(let t=p;t<u;t++)E[e.charCodeAt(t)]|=1<<t;let n=h;for(let t=0;t<r;t++){const c=E[o.charCodeAt(t)],i=a[t/32|0]>>>t&1,y=x[t/32|0]>>>t&1,d=c|f,I=((c|y)&S)+S^S|c|y;let A=f|~(I|S),T=S&I;n+=A>>>h-1&1,n-=T>>>h-1&1,A>>>31^i&&(a[t/32|0]^=1<<t),T>>>31^y&&(x[t/32|0]^=1<<t),A=A<<1|i,T=T<<1|y,S=T|~(d|A),f=A&d}for(let t=p;t<u;t++)E[e.charCodeAt(t)]=0;return n},N=(e,o)=>{if(e.length<o.length){const r=o;o=e,e=r}return o.length===0?e.length:e.length<=32?B(e,o):Z(e,o)};var _=typeof globalThis!="undefined"?globalThis:typeof window!="undefined"?window:typeof global!="undefined"?global:typeof self!="undefined"?self:{};function J(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var K=1/0,W="[object Symbol]",Q=/[\xc0-\xd6\xd8-\xf6\xf8-\xff\u0100-\u017f]/g,V="\\u0300-\\u036f\\ufe20-\\ufe23",X="\\u20d0-\\u20f0",$="["+V+X+"]",ee=RegExp($,"g"),te={\u00C0:"A",\u00C1:"A",\u00C2:"A",\u00C3:"A",\u00C4:"A",\u00C5:"A",\u00E0:"a",\u00E1:"a",\u00E2:"a",\u00E3:"a",\u00E4:"a",\u00E5:"a",\u00C7:"C",\u00E7:"c",\u00D0:"D",\u00F0:"d",\u00C8:"E",\u00C9:"E",\u00CA:"E",\u00CB:"E",\u00E8:"e",\u00E9:"e",\u00EA:"e",\u00EB:"e",\u00CC:"I",\u00CD:"I",\u00CE:"I",\u00CF:"I",\u00EC:"i",\u00ED:"i",\u00EE:"i",\u00EF:"i",\u00D1:"N",\u00F1:"n",\u00D2:"O",\u00D3:"O",\u00D4:"O",\u00D5:"O",\u00D6:"O",\u00D8:"O",\u00F2:"o",\u00F3:"o",\u00F4:"o",\u00F5:"o",\u00F6:"o",\u00F8:"o",\u00D9:"U",\u00DA:"U",\u00DB:"U",\u00DC:"U",\u00F9:"u",\u00FA:"u",\u00FB:"u",\u00FC:"u",\u00DD:"Y",\u00FD:"y",\u00FF:"y",\u00C6:"Ae",\u00E6:"ae",\u00DE:"Th",\u00FE:"th",\u00DF:"ss",\u0100:"A",\u0102:"A",\u0104:"A",\u0101:"a",\u0103:"a",\u0105:"a",\u0106:"C",\u0108:"C",\u010A:"C",\u010C:"C",\u0107:"c",\u0109:"c",\u010B:"c",\u010D:"c",\u010E:"D",\u0110:"D",\u010F:"d",\u0111:"d",\u0112:"E",\u0114:"E",\u0116:"E",\u0118:"E",\u011A:"E",\u0113:"e",\u0115:"e",\u0117:"e",\u0119:"e",\u011B:"e",\u011C:"G",\u011E:"G",\u0120:"G",\u0122:"G",\u011D:"g",\u011F:"g",\u0121:"g",\u0123:"g",\u0124:"H",\u0126:"H",\u0125:"h",\u0127:"h",\u0128:"I",\u012A:"I",\u012C:"I",\u012E:"I",\u0130:"I",\u0129:"i",\u012B:"i",\u012D:"i",\u012F:"i",\u0131:"i",\u0134:"J",\u0135:"j",\u0136:"K",\u0137:"k",\u0138:"k",\u0139:"L",\u013B:"L",\u013D:"L",\u013F:"L",\u0141:"L",\u013A:"l",\u013C:"l",\u013E:"l",\u0140:"l",\u0142:"l",\u0143:"N",\u0145:"N",\u0147:"N",\u014A:"N",\u0144:"n",\u0146:"n",\u0148:"n",\u014B:"n",\u014C:"O",\u014E:"O",\u0150:"O",\u014D:"o",\u014F:"o",\u0151:"o",\u0154:"R",\u0156:"R",\u0158:"R",\u0155:"r",\u0157:"r",\u0159:"r",\u015A:"S",\u015C:"S",\u015E:"S",\u0160:"S",\u015B:"s",\u015D:"s",\u015F:"s",\u0161:"s",\u0162:"T",\u0164:"T",\u0166:"T",\u0163:"t",\u0165:"t",\u0167:"t",\u0168:"U",\u016A:"U",\u016C:"U",\u016E:"U",\u0170:"U",\u0172:"U",\u0169:"u",\u016B:"u",\u016D:"u",\u016F:"u",\u0171:"u",\u0173:"u",\u0174:"W",\u0175:"w",\u0176:"Y",\u0177:"y",\u0178:"Y",\u0179:"Z",\u017B:"Z",\u017D:"Z",\u017A:"z",\u017C:"z",\u017E:"z",\u0132:"IJ",\u0133:"ij",\u0152:"Oe",\u0153:"oe",\u0149:"'n",\u017F:"ss"},oe=typeof _=="object"&&_&&_.Object===Object&&_,ue=typeof self=="object"&&self&&self.Object===Object&&self,ne=oe||ue||Function("return this")();function re(e){return function(o){return e==null?void 0:e[o]}}var se=re(te),ce=Object.prototype,ae=ce.toString,v=ne.Symbol,D=v?v.prototype:void 0,R=D?D.toString:void 0;function de(e){if(typeof e=="string")return e;if(fe(e))return R?R.call(e):"";var o=e+"";return o=="0"&&1/e==-K?"-0":o}function le(e){return!!e&&typeof e=="object"}function fe(e){return typeof e=="symbol"||le(e)&&ae.call(e)==W}function ie(e){return e==null?"":de(e)}function he(e){return e=ie(e),e&&e.replace(Q,se).replace(ee,"")}var me=he,xe=J(me);let C,b;(function(e){e.ALL_CLOSEST_MATCHES="all-closest-matches",e.ALL_MATCHES="all-matches",e.ALL_SORTED_MATCHES="all-sorted-matches",e.FIRST_CLOSEST_MATCH="first-closest-match",e.FIRST_MATCH="first-match"})(C||(C={})),function(e){e.EDIT_DISTANCE="edit-distance",e.SIMILARITY="similarity"}(b||(b={}));const k=new Error("unknown returnType"),F=new Error("unknown thresholdType"),j=(e,o)=>{let r=e;return o.trimSpaces&&(r=r.trim().replace(/\s+/g," ")),o.deburr&&(r=xe(r)),o.caseSensitive||(r=r.toLowerCase()),r},H=(e,o)=>{const{matchPath:r}=o,h=((x,a)=>{const l=a.length>0?a.reduce((m,s)=>m==null?void 0:m[s],x):x;return typeof l!="string"?"":l})(e,r);return j(h,o)};function Se(e,o,r){const h=(u=>{const n={caseSensitive:!1,deburr:!0,matchPath:[],returnType:C.FIRST_CLOSEST_MATCH,thresholdType:b.SIMILARITY,trimSpaces:!0,...u};switch(n.thresholdType){case b.EDIT_DISTANCE:return{threshold:20,...n};case b.SIMILARITY:return{threshold:.4,...n};default:throw F}})(r),{returnType:x,threshold:a,thresholdType:l}=h,m=j(e,h);let s,f;switch(l){case b.EDIT_DISTANCE:s=u=>u<=a,f=u=>N(m,H(u,h));break;case b.SIMILARITY:s=u=>u>=a,f=u=>((n,t)=>{if(!n||!t)return 0;if(n===t)return 1;const c=N(n,t),i=Math.max(n.length,t.length);return(i-c)/i})(m,H(u,h));break;default:throw F}const S=[],p=o.length;switch(x){case C.ALL_CLOSEST_MATCHES:case C.FIRST_CLOSEST_MATCH:{const u=[];let n;switch(l){case b.EDIT_DISTANCE:n=1/0;for(let c=0;c<p;c+=1){const i=f(o[c]);n>i&&(n=i),u.push(i)}break;case b.SIMILARITY:n=0;for(let c=0;c<p;c+=1){const i=f(o[c]);n<i&&(n=i),u.push(i)}break;default:throw F}const t=u.length;for(let c=0;c<t;c+=1){const i=u[c];s(i)&&i===n&&S.push(c)}break}case C.ALL_MATCHES:for(let u=0;u<p;u+=1)s(f(o[u]))&&S.push(u);break;case C.ALL_SORTED_MATCHES:{const u=[];for(let n=0;n<p;n+=1){const t=f(o[n]);s(t)&&u.push({score:t,index:n})}switch(l){case b.EDIT_DISTANCE:u.sort((n,t)=>n.score-t.score);break;case b.SIMILARITY:u.sort((n,t)=>t.score-n.score);break;default:throw F}for(const n of u)S.push(n.index);break}case C.FIRST_MATCH:for(let u=0;u<p;u+=1)if(s(f(o[u]))){S.push(u);break}break;default:throw k}return((u,n,t)=>{switch(t){case C.ALL_CLOSEST_MATCHES:case C.ALL_MATCHES:case C.ALL_SORTED_MATCHES:return n.map(c=>u[c]);case C.FIRST_CLOSEST_MATCH:case C.FIRST_MATCH:return n.length?u[n[0]]:null;default:throw k}})(o,S,x)}const Ce=G.WriteStream.prototype.hasColors(),U=(e,o)=>Ce?r=>"\x1B["+e+"m"+r+"\x1B["+o+"m":r=>r,be=U(1,22),pe=U(9,29),Te={en:{"notFound.possibleCommands":"Possible commands: %s.","notFound.commandNotFound":'Command "%s" not found.',"notFound.didyoumean":'Did you mean "%s"?',"notFound.commandNotRegisteredNote":"NOTE: You haven't register any command yet."},"zh-CN":{"notFound.possibleCommands":"\u53EF\u80FD\u7684\u547D\u4EE4: %s\u3002","notFound.commandNotFound":'\u672A\u627E\u5230\u547D\u4EE4 "%s"\u3002',"notFound.didyoumean":'\u4F60\u662F\u4E0D\u662F\u6307 "%s"\uFF1F',"notFound.commandNotRegisteredNote":"\u63D0\u793A: \u4F60\u8FD8\u6CA1\u6709\u6CE8\u518C\u4EFB\u4F55\u547D\u4EE4\u3002"}},Ae=()=>Y({setup:e=>{const{t:o,add:r}=e.i18n;return r(Te),e.inspector({enforce:"pre",fn:(h,x)=>{const a=Object.keys(e._commands),l=!!a.length;try{x()}catch(m){if(!(m instanceof P||m instanceof w))throw m;if(h.raw._.length===0||m instanceof w){console.error(o("core.noCommandGiven")),l&&console.error(o("notFound.possibleCommands",q(a,e.i18n)));return}const s=m.commandName,f=Se(s,a);console.error(o("notFound.commandNotFound",pe(s))),l&&f?console.error(o("notFound.didyoumean",be(f))):l||console.error(o("notFound.commandNotRegisteredNote")),process.stderr.write(`
2
- `),process.exit(2)}}})}});export{Ae as notFoundPlugin};
1
+ import { definePlugin, NoSuchCommandError, NoCommandGivenError } from '@clerc/core';
2
+ import { semanticArray } from '@clerc/utils';
3
+ import didyoumean from 'didyoumean2';
4
+ import * as yc from 'yoctocolors';
5
+
6
+ const locales = {
7
+ "en": {
8
+ "notFound.possibleCommands": "Possible commands: %s.",
9
+ "notFound.commandNotFound": 'Command "%s" not found.',
10
+ "notFound.didyoumean": 'Did you mean "%s"?',
11
+ "notFound.commandNotRegisteredNote": "NOTE: You haven't register any command yet."
12
+ },
13
+ "zh-CN": {
14
+ "notFound.possibleCommands": "\u53EF\u80FD\u7684\u547D\u4EE4: %s\u3002",
15
+ "notFound.commandNotFound": '\u672A\u627E\u5230\u547D\u4EE4 "%s"\u3002',
16
+ "notFound.didyoumean": '\u4F60\u662F\u4E0D\u662F\u6307 "%s"\uFF1F',
17
+ "notFound.commandNotRegisteredNote": "\u63D0\u793A: \u4F60\u8FD8\u6CA1\u6709\u6CE8\u518C\u4EFB\u4F55\u547D\u4EE4\u3002"
18
+ }
19
+ };
20
+
21
+ const notFoundPlugin = () => definePlugin({
22
+ setup: (cli) => {
23
+ const { t, add } = cli.i18n;
24
+ add(locales);
25
+ return cli.inspector({
26
+ enforce: "pre",
27
+ fn: (ctx, next) => {
28
+ const commandKeys = Object.keys(cli._commands);
29
+ const hasCommands = commandKeys.length > 0;
30
+ try {
31
+ next();
32
+ } catch (e) {
33
+ if (!(e instanceof NoSuchCommandError || e instanceof NoCommandGivenError)) {
34
+ throw e;
35
+ }
36
+ if (ctx.raw._.length === 0 || e instanceof NoCommandGivenError) {
37
+ console.error(t("core.noCommandGiven"));
38
+ if (hasCommands) {
39
+ console.error(
40
+ t(
41
+ "notFound.possibleCommands",
42
+ semanticArray(commandKeys, cli.i18n)
43
+ )
44
+ );
45
+ }
46
+ return;
47
+ }
48
+ const calledCommandName = e.commandName;
49
+ const closestCommandName = didyoumean(
50
+ calledCommandName,
51
+ commandKeys
52
+ );
53
+ console.error(
54
+ t(
55
+ "notFound.commandNotFound",
56
+ yc.strikethrough(calledCommandName)
57
+ )
58
+ );
59
+ if (hasCommands && closestCommandName) {
60
+ console.error(
61
+ t("notFound.didyoumean", yc.bold(closestCommandName))
62
+ );
63
+ } else if (!hasCommands) {
64
+ console.error(t("notFound.commandNotRegisteredNote"));
65
+ }
66
+ process.stderr.write("\n");
67
+ process.exit(2);
68
+ }
69
+ }
70
+ });
71
+ }
72
+ });
73
+
74
+ export { notFoundPlugin };
package/package.json CHANGED
@@ -1,18 +1,18 @@
1
1
  {
2
2
  "name": "@clerc/plugin-not-found",
3
- "version": "0.39.0",
3
+ "version": "0.41.0",
4
4
  "author": "Ray <i@mk1.io> (https://github.com/so1ve)",
5
+ "type": "module",
5
6
  "description": "Clerc plugin not found (did you mean)",
6
7
  "keywords": [
7
- "cli",
8
- "clerc",
9
- "clerc-plugin",
8
+ "args",
10
9
  "arguments",
11
10
  "argv",
12
- "args",
11
+ "clerc",
12
+ "clerc-plugin",
13
+ "cli",
13
14
  "terminal"
14
15
  ],
15
- "type": "module",
16
16
  "homepage": "https://github.com/so1ve/clerc/tree/main/packages/plugin-not-found#readme",
17
17
  "repository": {
18
18
  "type": "git",
@@ -47,17 +47,16 @@
47
47
  "publishConfig": {
48
48
  "access": "public"
49
49
  },
50
- "peerDependencies": {
51
- "@clerc/core": "*"
52
- },
53
- "devDependencies": {
50
+ "dependencies": {
54
51
  "didyoumean2": "^5.0.0",
55
52
  "yoctocolors": "^1.0.0",
56
- "@clerc/core": "0.39.0",
57
- "@clerc/utils": "0.39.0"
53
+ "@clerc/utils": "0.41.0"
54
+ },
55
+ "peerDependencies": {
56
+ "@clerc/core": "*"
58
57
  },
59
58
  "scripts": {
60
- "build": "pkgroll --minify",
59
+ "build": "pkgroll",
61
60
  "watch": "pkgroll --watch"
62
61
  }
63
62
  }