@gunshi/plugin-renderer 0.27.0-alpha.5 → 0.27.0-alpha.7
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 +55 -4
- package/lib/index.js +40 -29
- package/package.json +4 -4
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,50 @@
|
|
|
1
1
|
import { Command, CommandContext, DefaultGunshiParams, GunshiParams, PluginWithExtension } from "@gunshi/plugin";
|
|
2
|
-
import { Args } from "args-tokens";
|
|
2
|
+
import { Args, Args as Args$1 } from "args-tokens";
|
|
3
3
|
|
|
4
4
|
//#region rolldown:runtime
|
|
5
|
+
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region ../gunshi/src/types.d.ts
|
|
8
|
+
/**
|
|
9
|
+
* Extend command context type. This type is used to extend the command context with additional properties at {@link CommandContext.extensions}.
|
|
10
|
+
* @since v0.27.0
|
|
11
|
+
*/
|
|
12
|
+
type ExtendContext = Record<string, unknown>;
|
|
13
|
+
/**
|
|
14
|
+
* Gunshi unified parameter type.
|
|
15
|
+
* This type combines both argument definitions and command context extensions.
|
|
16
|
+
* @since v0.27.0
|
|
17
|
+
*/
|
|
18
|
+
interface GunshiParams$1<P extends {
|
|
19
|
+
args?: Args;
|
|
20
|
+
extensions?: ExtendContext;
|
|
21
|
+
} = {
|
|
22
|
+
args: Args;
|
|
23
|
+
extensions: {};
|
|
24
|
+
}> {
|
|
25
|
+
/**
|
|
26
|
+
* Command argument definitions
|
|
27
|
+
*/
|
|
28
|
+
args: P extends {
|
|
29
|
+
args: infer A extends Args;
|
|
30
|
+
} ? A : Args;
|
|
31
|
+
/**
|
|
32
|
+
* Command context extensions
|
|
33
|
+
*/
|
|
34
|
+
extensions: P extends {
|
|
35
|
+
extensions: infer E extends ExtendContext;
|
|
36
|
+
} ? E : {};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Default Gunshi parameters
|
|
40
|
+
* @since v0.27.0
|
|
41
|
+
*/
|
|
42
|
+
type DefaultGunshiParams$1 = GunshiParams$1;
|
|
43
|
+
/**
|
|
44
|
+
* Generic constraint for command-related types.
|
|
45
|
+
* This type constraint allows both GunshiParams and objects with extensions.
|
|
46
|
+
* @since v0.27.0
|
|
47
|
+
*/
|
|
5
48
|
declare namespace constants_d_exports {
|
|
6
49
|
export { ARG_NEGATABLE_PREFIX, ARG_PREFIX, ARG_PREFIX_AND_KEY_SEPARATOR, BUILD_IN_PREFIX_AND_KEY_SEPARATOR, BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX, COMMAND_BUILTIN_RESOURCE_KEYS, COMMON_ARGS, PLUGIN_PREFIX };
|
|
7
50
|
}
|
|
@@ -37,7 +80,7 @@ type RemoveIndexSignature<T> = { [K in keyof T as string extends K ? never : num
|
|
|
37
80
|
* Remove index signature from object or record type.
|
|
38
81
|
*/
|
|
39
82
|
type RemovedIndex<T> = RemoveIndexSignature<{ [K in keyof T]: T[K] }>;
|
|
40
|
-
type KeyOfArgs<A extends Args> = keyof A | { [K in keyof A]: A[K]['type'] extends 'boolean' ? A[K]['negatable'] extends true ? `no-${Extract<K, string>}` : never : never }[keyof A];
|
|
83
|
+
type KeyOfArgs<A extends Args$1> = keyof A | { [K in keyof A]: A[K]['type'] extends 'boolean' ? A[K]['negatable'] extends true ? `no-${Extract<K, string>}` : never : never }[keyof A];
|
|
41
84
|
/**
|
|
42
85
|
* Generate a namespaced key.
|
|
43
86
|
*/
|
|
@@ -63,7 +106,15 @@ type CommandBuiltinKeys = GenerateNamespacedKey<BuiltinResourceKeys> | 'descript
|
|
|
63
106
|
* Command i18n option keys.
|
|
64
107
|
* The command i18n option keys are used by the i18n plugin for translation.
|
|
65
108
|
*/
|
|
66
|
-
type CommandArgKeys<A extends Args> = GenerateNamespacedKey<KeyOfArgs<RemovedIndex<A>>, typeof ARG_PREFIX>;
|
|
109
|
+
type CommandArgKeys<A extends Args$1> = GenerateNamespacedKey<KeyOfArgs<RemovedIndex<A>>, typeof ARG_PREFIX>;
|
|
110
|
+
/**
|
|
111
|
+
* Translation function interface
|
|
112
|
+
*/
|
|
113
|
+
//#endregion
|
|
114
|
+
//#region ../shared/src/localize.d.ts
|
|
115
|
+
interface Localization<T extends string = CommandBuiltinKeys, G extends GunshiParams$1<any> = DefaultGunshiParams$1> {
|
|
116
|
+
<O = CommandArgKeys<G['args']>, K = CommandBuiltinKeys | O | T>(key: K, values?: Record<string, unknown>): Promise<string>;
|
|
117
|
+
}
|
|
67
118
|
//#endregion
|
|
68
119
|
//#region src/types.d.ts
|
|
69
120
|
/**
|
|
@@ -74,7 +125,7 @@ interface UsageRendererCommandContext<G extends GunshiParams<any> = DefaultGunsh
|
|
|
74
125
|
/**
|
|
75
126
|
* Render the text message
|
|
76
127
|
*/
|
|
77
|
-
text: <
|
|
128
|
+
text: Localization<CommandBuiltinKeys, G>;
|
|
78
129
|
/**
|
|
79
130
|
* Load commands
|
|
80
131
|
* @returns A list of commands loaded from the command loader plugin.
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { plugin } from "@gunshi/plugin";
|
|
2
2
|
|
|
3
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
3
|
+
//#region ../../node_modules/.pnpm/args-tokens@0.22.0/node_modules/args-tokens/lib/utils-N7UlhLbz.js
|
|
4
4
|
/**
|
|
5
5
|
* Entry point of utils.
|
|
6
6
|
*
|
|
@@ -28,7 +28,9 @@ async function resolveLazyCommand(cmd, name, needRunResolving = false) {
|
|
|
28
28
|
name: cmd.commandName,
|
|
29
29
|
description: cmd.description,
|
|
30
30
|
args: cmd.args,
|
|
31
|
-
examples: cmd.examples
|
|
31
|
+
examples: cmd.examples,
|
|
32
|
+
internal: cmd.internal,
|
|
33
|
+
entry: cmd.entry
|
|
32
34
|
};
|
|
33
35
|
if ("resource" in cmd && cmd.resource) baseCommand.resource = cmd.resource;
|
|
34
36
|
command = Object.assign(create(), baseCommand);
|
|
@@ -42,6 +44,8 @@ async function resolveLazyCommand(cmd, name, needRunResolving = false) {
|
|
|
42
44
|
command.description = loaded.description;
|
|
43
45
|
command.args = loaded.args;
|
|
44
46
|
command.examples = loaded.examples;
|
|
47
|
+
command.internal = loaded.internal;
|
|
48
|
+
command.entry = loaded.entry;
|
|
45
49
|
if ("resource" in loaded && loaded.resource) command.resource = loaded.resource;
|
|
46
50
|
} else throw new TypeError(`Cannot resolve command: ${cmd.name || name}`);
|
|
47
51
|
}
|
|
@@ -133,6 +137,37 @@ async function resolveExamples$1(ctx, examples) {
|
|
|
133
137
|
function namespacedId(id) {
|
|
134
138
|
return `${PLUGIN_PREFIX}${BUILT_IN_KEY_SEPARATOR}${id}`;
|
|
135
139
|
}
|
|
140
|
+
function makeShortLongOptionPair(schema, name, toKebab) {
|
|
141
|
+
const displayName = toKebab || schema.toKebab ? kebabnize(name) : name;
|
|
142
|
+
let key = `--${displayName}`;
|
|
143
|
+
if (schema.short) key = `-${schema.short}, ${key}`;
|
|
144
|
+
return key;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region ../shared/src/localize.ts
|
|
149
|
+
function localizable(ctx, cmd, translate) {
|
|
150
|
+
async function localize(key, values) {
|
|
151
|
+
if (translate) return translate(key, values);
|
|
152
|
+
else if (key.startsWith(BUILD_IN_PREFIX_AND_KEY_SEPARATOR)) {
|
|
153
|
+
const resKey = key.slice(BUILD_IN_PREFIX_AND_KEY_SEPARATOR.length);
|
|
154
|
+
return en_US_default[resKey] || key;
|
|
155
|
+
} else if (key.startsWith(ARG_PREFIX_AND_KEY_SEPARATOR)) {
|
|
156
|
+
let argKey = key.slice(ARG_PREFIX_AND_KEY_SEPARATOR.length);
|
|
157
|
+
let negatable = false;
|
|
158
|
+
if (argKey.startsWith(ARG_NEGATABLE_PREFIX)) {
|
|
159
|
+
argKey = argKey.slice(ARG_NEGATABLE_PREFIX.length);
|
|
160
|
+
negatable = true;
|
|
161
|
+
}
|
|
162
|
+
const schema = ctx.args[argKey];
|
|
163
|
+
if (!schema) return argKey;
|
|
164
|
+
return negatable && schema.type === "boolean" && schema.negatable ? `${en_US_default["NEGATABLE"]} ${makeShortLongOptionPair(schema, argKey, ctx.toKebab)}` : schema.description || "";
|
|
165
|
+
} else if (key === "description") return "";
|
|
166
|
+
else if (key === "examples") return await resolveExamples$1(ctx, cmd.examples);
|
|
167
|
+
else return key;
|
|
168
|
+
}
|
|
169
|
+
return localize;
|
|
170
|
+
}
|
|
136
171
|
|
|
137
172
|
//#endregion
|
|
138
173
|
//#region src/header.ts
|
|
@@ -329,12 +364,6 @@ function hasAllDefaultOptions(ctx) {
|
|
|
329
364
|
async function generateOptionsSymbols(ctx) {
|
|
330
365
|
return hasOptionalArgs(ctx) ? hasAllDefaultOptions(ctx) ? `[${await ctx.extensions[pluginId].text(resolveBuiltInKey("OPTIONS"))}]` : `<${await ctx.extensions[pluginId].text(resolveBuiltInKey("OPTIONS"))}>` : "";
|
|
331
366
|
}
|
|
332
|
-
function makeShortLongOptionPair(schema, name, toKebab) {
|
|
333
|
-
const displayName = toKebab || schema.toKebab ? kebabnize(name) : name;
|
|
334
|
-
let key = `--${displayName}`;
|
|
335
|
-
if (schema.short) key = `-${schema.short}, ${key}`;
|
|
336
|
-
return key;
|
|
337
|
-
}
|
|
338
367
|
/**
|
|
339
368
|
* Get optional arguments pairs for usage
|
|
340
369
|
* @param ctx A {@link CommandContext | command context}
|
|
@@ -450,29 +479,11 @@ function renderer() {
|
|
|
450
479
|
async function loadCommands() {
|
|
451
480
|
if (cachedCommands) return cachedCommands;
|
|
452
481
|
const subCommands = [...ctx.env.subCommands || []];
|
|
453
|
-
|
|
454
|
-
return cachedCommands;
|
|
455
|
-
}
|
|
456
|
-
async function text(key, values = Object.create(null)) {
|
|
457
|
-
if (i18n) return i18n.translate(key, values);
|
|
458
|
-
else if (key.startsWith(BUILD_IN_PREFIX_AND_KEY_SEPARATOR)) {
|
|
459
|
-
const resKey = key.slice(BUILD_IN_PREFIX_AND_KEY_SEPARATOR.length);
|
|
460
|
-
return en_US_default[resKey] || key;
|
|
461
|
-
} else if (key.startsWith(ARG_PREFIX_AND_KEY_SEPARATOR)) {
|
|
462
|
-
let argKey = key.slice(ARG_PREFIX_AND_KEY_SEPARATOR.length);
|
|
463
|
-
let negatable = false;
|
|
464
|
-
if (argKey.startsWith(ARG_NEGATABLE_PREFIX)) {
|
|
465
|
-
argKey = argKey.slice(ARG_NEGATABLE_PREFIX.length);
|
|
466
|
-
negatable = true;
|
|
467
|
-
}
|
|
468
|
-
const schema = ctx.args[argKey];
|
|
469
|
-
return negatable && schema.type === "boolean" && schema.negatable ? `${en_US_default["NEGATABLE"]} ${makeShortLongOptionPair(schema, argKey, ctx.toKebab)}` : schema.description || "";
|
|
470
|
-
} else if (key === "description") return "";
|
|
471
|
-
else if (key === "examples") return await resolveExamples$1(ctx, cmd.examples);
|
|
472
|
-
else return key;
|
|
482
|
+
const allCommands = await Promise.all(subCommands.map(async ([name, cmd$1]) => await resolveLazyCommand(cmd$1, name)));
|
|
483
|
+
return cachedCommands = allCommands.filter((cmd$1) => !cmd$1.internal).filter(Boolean);
|
|
473
484
|
}
|
|
474
485
|
return {
|
|
475
|
-
text,
|
|
486
|
+
text: localizable(ctx, cmd, i18n?.translate),
|
|
476
487
|
loadCommands
|
|
477
488
|
};
|
|
478
489
|
},
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gunshi/plugin-renderer",
|
|
3
3
|
"description": "usage renderer plugin for gunshi",
|
|
4
|
-
"version": "0.27.0-alpha.
|
|
4
|
+
"version": "0.27.0-alpha.7",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -53,10 +53,10 @@
|
|
|
53
53
|
}
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@gunshi/plugin": "0.27.0-alpha.
|
|
56
|
+
"@gunshi/plugin": "0.27.0-alpha.7"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
|
-
"@gunshi/plugin-i18n": "0.27.0-alpha.
|
|
59
|
+
"@gunshi/plugin-i18n": "0.27.0-alpha.7"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
62
|
"deno": "^2.4.0",
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
"tsdown": "^0.12.9",
|
|
67
67
|
"typedoc": "^0.28.7",
|
|
68
68
|
"typedoc-plugin-markdown": "^4.7.0",
|
|
69
|
-
"@gunshi/shared": "0.27.0-alpha.
|
|
69
|
+
"@gunshi/shared": "0.27.0-alpha.7"
|
|
70
70
|
},
|
|
71
71
|
"scripts": {
|
|
72
72
|
"build": "tsdown",
|