@gunshi/plugin-global 0.37.3 → 1.0.0-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/README.md CHANGED
@@ -78,6 +78,31 @@ When these options are used:
78
78
  - **With `--help`**: The command execution is bypassed, and the usage information is displayed instead
79
79
  - **With `--version`**: The command execution is bypassed, and only the version number is printed
80
80
 
81
+ ### Commands That Take the Name
82
+
83
+ `help` and `version` are not reserved. A command may declare an argument of its own under either name, and gunshi merges the arguments of a command over the global options, so the command's argument wins:
84
+
85
+ ```js
86
+ import { define } from 'gunshi'
87
+
88
+ const release = define({
89
+ name: 'release',
90
+ args: {
91
+ version: { type: 'string', description: 'Version to release' }
92
+ },
93
+ run: ctx => console.log(`releasing ${ctx.values.version}`)
94
+ })
95
+ ```
96
+
97
+ `my-cli release --version 1.2.3` runs the command with `1.2.3`, rather than printing the version of the CLI. Two things follow from the command owning the name:
98
+
99
+ - The option no longer does what this plugin does automatically. `my-cli release --version` does not print the version of the CLI, and `--help` on a command that takes the name no longer shows its usage by itself — the command receives the value and decides, and it can still call `showUsage()` or `showVersion()` from `ctx.extensions['g:global']`
100
+ - The short name goes with it. The global schema is replaced whole, so `-v` (or `-h`) is no longer defined unless the command declares a `short` of its own
101
+
102
+ Other commands of the same CLI are unaffected, and so is the entry command unless it declares the name itself.
103
+
104
+ One case is left to this plugin on purpose: a schema that is identical to the one this plugin registers, field for field, cannot be told apart from it, so `version: { type: 'boolean', short: 'v', description: 'Display this version' }` still prints the version of the CLI. Any difference at all — a description of your own, an extra property, another type — makes the argument the command's.
105
+
81
106
  ## 🧩 Context Extensions
82
107
 
83
108
  When using the global options plugin, your command context is extended via `ctx.extensions['g:global']`.
package/lib/index.d.ts CHANGED
@@ -1,11 +1,5 @@
1
1
  import { Awaitable, PluginWithExtension } from "@gunshi/plugin";
2
-
3
2
  //#region src/extension.d.ts
4
- /**
5
- * @author kazuya kawaguchi (a.k.a. kazupon)
6
- * @license MIT
7
- */
8
-
9
3
  /**
10
4
  * Extended command context which provides utilities via global options plugin.
11
5
  * These utilities are available via `CommandContext.extensions['g:global']`.
@@ -46,33 +40,12 @@ interface GlobalExtension {
46
40
  declare const BUILT_IN_PREFIX = "_";
47
41
  declare const PLUGIN_PREFIX = "g";
48
42
  declare const BUILT_IN_KEY_SEPARATOR = ":";
49
- //#endregion
50
- //#region ../plugin-i18n/src/types.d.ts
51
-
52
- //#endregion
53
- //#region ../plugin-i18n/src/helpers.d.ts
54
-
55
- //#endregion
56
- //#region ../plugin-i18n/src/translation.d.ts
57
-
58
- //#endregion
59
- //#region ../gunshi/src/index.d.ts
60
-
61
43
  //#endregion
62
44
  //#region ../shared/src/types.d.ts
63
45
  /**
64
46
  * Generate a namespaced key.
65
47
  */
66
48
  type GenerateNamespacedKey<Key extends string, Prefixed extends string = typeof BUILT_IN_PREFIX> = `${Prefixed}${typeof BUILT_IN_KEY_SEPARATOR}${Key}`;
67
- //#endregion
68
- //#region ../shared/src/localization.d.ts
69
-
70
- //#endregion
71
- //#region ../shared/src/utils.d.ts
72
-
73
- //#endregion
74
- //#region ../shared/src/index.d.ts
75
-
76
49
  //#endregion
77
50
  //#region src/types.d.ts
78
51
  /**
package/lib/index.js CHANGED
@@ -67,16 +67,21 @@ const pluginId = namespacedId("global");
67
67
  * @returns A command decorator that adds global options handling
68
68
  */
69
69
  const decorator = (baseRunner) => async (ctx) => {
70
- const { values, validationError, extensions: { [pluginId]: { showVersion, showHeader, showUsage, showValidationErrors } } } = ctx;
70
+ /**
71
+ * NOTE(kazupon): the core puts the globals still in effect on `env.globalOptions`. A command that
72
+ * declares the same name — even with an identical schema — is not in that map, so `--version` /
73
+ * `--help` belong to the command.
74
+ */
75
+ const { values, validationError, env: { globalOptions }, extensions: { [pluginId]: { showVersion, showHeader, showUsage, showValidationErrors } } } = ctx;
71
76
  if (hasPriorityValidationError(validationError)) {
72
77
  await showValidationErrors(validationError);
73
78
  throw validationError;
74
79
  }
75
- if (values.version) return showVersion();
80
+ if (values.version && globalOptions?.has("version")) return showVersion();
76
81
  const buf = [];
77
82
  const header = await showHeader();
78
83
  if (header) buf.push(header);
79
- if (values.help) {
84
+ if (values.help && globalOptions?.has("help")) {
80
85
  const usage = await showUsage();
81
86
  if (usage) {
82
87
  buf.push(usage);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@gunshi/plugin-global",
3
3
  "description": "global options plugin for gunshi",
4
- "version": "0.37.3",
4
+ "version": "1.0.0-beta.1",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
7
7
  "email": "kawakazu80@gmail.com"
@@ -51,15 +51,15 @@
51
51
  }
52
52
  },
53
53
  "dependencies": {
54
- "@gunshi/plugin": "0.37.3"
54
+ "@gunshi/plugin": "1.0.0-beta.1"
55
55
  },
56
56
  "devDependencies": {
57
+ "@gunshi/shared": "1.0.0-beta.1",
57
58
  "deno": "^2.7.14",
58
59
  "jsr": "^0.14.3",
59
60
  "jsr-exports-lint": "^0.4.2",
60
61
  "publint": "^0.3.20",
61
- "tsdown": "0.21.0",
62
- "@gunshi/shared": "0.37.3"
62
+ "tsdown": "0.22.14"
63
63
  },
64
64
  "scripts": {
65
65
  "build": "tsdown",