@clerc/plugin-not-found 0.40.0 → 0.42.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 +25 -0
- package/dist/index.js +74 -2
- package/package.json +15 -16
package/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# @clerc/plugin-not-found
|
|
2
|
+
|
|
3
|
+
[](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
|
|
2
|
-
|
|
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: (context, next) => {
|
|
28
|
+
const commandKeys = Object.keys(cli._commands);
|
|
29
|
+
const hasCommands = commandKeys.length > 0;
|
|
30
|
+
try {
|
|
31
|
+
next();
|
|
32
|
+
} catch (error) {
|
|
33
|
+
if (!(error instanceof NoSuchCommandError || error instanceof NoCommandGivenError)) {
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
if (context.raw._.length === 0 || error 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 = error.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,26 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/plugin-not-found",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.42.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
|
-
"
|
|
8
|
-
"clerc",
|
|
9
|
-
"clerc-plugin",
|
|
8
|
+
"args",
|
|
10
9
|
"arguments",
|
|
11
10
|
"argv",
|
|
12
|
-
"
|
|
11
|
+
"clerc",
|
|
12
|
+
"clerc-plugin",
|
|
13
|
+
"cli",
|
|
13
14
|
"terminal"
|
|
14
15
|
],
|
|
15
|
-
"
|
|
16
|
-
"homepage": "https://github.com/so1ve/clerc/tree/main/packages/plugin-not-found#readme",
|
|
16
|
+
"homepage": "https://github.com/clercjs/clerc/tree/main/packages/plugin-not-found#readme",
|
|
17
17
|
"repository": {
|
|
18
18
|
"type": "git",
|
|
19
|
-
"url": "git+https://github.com/
|
|
19
|
+
"url": "git+https://github.com/clercjs/clerc.git",
|
|
20
20
|
"directory": "/"
|
|
21
21
|
},
|
|
22
22
|
"bugs": {
|
|
23
|
-
"url": "https://github.com/
|
|
23
|
+
"url": "https://github.com/clercjs/clerc/issues"
|
|
24
24
|
},
|
|
25
25
|
"license": "MIT",
|
|
26
26
|
"sideEffects": false,
|
|
@@ -47,17 +47,16 @@
|
|
|
47
47
|
"publishConfig": {
|
|
48
48
|
"access": "public"
|
|
49
49
|
},
|
|
50
|
-
"
|
|
51
|
-
"@clerc/core": "*"
|
|
52
|
-
},
|
|
53
|
-
"devDependencies": {
|
|
50
|
+
"dependencies": {
|
|
54
51
|
"didyoumean2": "^5.0.0",
|
|
55
52
|
"yoctocolors": "^1.0.0",
|
|
56
|
-
"@clerc/
|
|
57
|
-
|
|
53
|
+
"@clerc/utils": "0.42.0"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"@clerc/core": "*"
|
|
58
57
|
},
|
|
59
58
|
"scripts": {
|
|
60
|
-
"build": "pkgroll
|
|
59
|
+
"build": "pkgroll",
|
|
61
60
|
"watch": "pkgroll --watch"
|
|
62
61
|
}
|
|
63
62
|
}
|