@gunshi/plugin 0.27.0-alpha.8 → 0.27.0-beta.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/README.md +1014 -9
  2. package/lib/index.d.ts +807 -150
  3. package/lib/index.js +64 -24
  4. package/package.json +6 -6
package/lib/index.js CHANGED
@@ -1,5 +1,8 @@
1
1
  //#region ../gunshi/src/constants.ts
2
2
  const ANONYMOUS_COMMAND_NAME = "(anonymous)";
3
+ /**
4
+ * A no-operation function.
5
+ */
3
6
  const NOOP = () => {};
4
7
  const CLI_OPTIONS_DEFAULT = {
5
8
  name: void 0,
@@ -15,20 +18,45 @@ const CLI_OPTIONS_DEFAULT = {
15
18
  renderHeader: void 0,
16
19
  renderUsage: void 0,
17
20
  renderValidationErrors: void 0,
18
- plugins: void 0
21
+ plugins: void 0,
22
+ fallbackToEntry: false
19
23
  };
20
24
 
21
25
  //#endregion
22
26
  //#region ../gunshi/src/utils.ts
27
+ /**
28
+ * Check if the given command is a {@link LazyCommand}.
29
+ *
30
+ * @param cmd - A command to check
31
+ * @returns `true` if the command is a {@link LazyCommand}, otherwise `false
32
+ */
23
33
  function isLazyCommand(cmd) {
24
34
  return typeof cmd === "function" && "commandName" in cmd && !!cmd.commandName;
25
35
  }
36
+ /**
37
+ * Create an object with the specified prototype. A shorthand for `Object.create`.
38
+ *
39
+ * @param obj - An object to use as the prototype for the new object. If `null`, it will create an object with no prototype.
40
+ * @returns A new object with the specified prototype
41
+ */
26
42
  function create(obj = null) {
27
43
  return Object.create(obj);
28
44
  }
45
+ /**
46
+ * Log a message to the console.
47
+ *
48
+ * @param args - Arguments to log
49
+ */
29
50
  function log(...args) {
30
51
  console.log(...args);
31
52
  }
53
+ /**
54
+ * Deep freeze an object, making it immutable.
55
+ *
56
+ * @param obj - The object to freeze
57
+ * @param ignores - Properties to ignore during freezing
58
+ * @returns A frozen object
59
+ */
32
60
  function deepFreeze(obj, ignores = []) {
33
61
  if (obj === null || typeof obj !== "object") return obj;
34
62
  for (const key of Object.keys(obj)) {
@@ -42,11 +70,12 @@ function deepFreeze(obj, ignores = []) {
42
70
  //#endregion
43
71
  //#region ../gunshi/src/context.ts
44
72
  /**
45
- * Create a {@link CommandContext | command context}
46
- * @param param A {@link CommandContextParams | parameters} to create a {@link CommandContext | command context}
47
- * @returns A {@link CommandContext | command context}, which is readonly
73
+ * Create a command context.
74
+ *
75
+ * @param param - A {@link CommandContextParams | parameters} to create a command context.
76
+ * @returns A {@link CommandContext | command context}, which is readonly.
48
77
  */
49
- async function createCommandContext({ args, explicit, values, positionals, rest, argv, tokens, command, extensions = {}, cliOptions, callMode = "entry", omitted = false, validationError }) {
78
+ async function createCommandContext({ args = {}, explicit = {}, values = {}, positionals = [], rest = [], argv = [], tokens = [], command = {}, extensions = {}, cliOptions = {}, callMode = "entry", omitted = false, validationError = void 0 }) {
50
79
  /**
51
80
  * normailize the options schema and values, to avoid prototype pollution
52
81
  */
@@ -59,6 +88,15 @@ async function createCommandContext({ args, explicit, values, positionals, rest,
59
88
  */
60
89
  const env = Object.assign(create(), CLI_OPTIONS_DEFAULT, cliOptions);
61
90
  /**
91
+ * apply Command definition's rendering option with highest priority
92
+ */
93
+ if (command.rendering) {
94
+ const { header, usage, validationErrors } = command.rendering;
95
+ if (header !== void 0) env.renderHeader = header;
96
+ if (usage !== void 0) env.renderUsage = usage;
97
+ if (validationErrors !== void 0) env.renderValidationErrors = validationErrors;
98
+ }
99
+ /**
62
100
  * create the command context
63
101
  */
64
102
  const core = Object.assign(create(), {
@@ -94,8 +132,7 @@ async function createCommandContext({ args, explicit, values, positionals, rest,
94
132
  if (extension.onFactory) await extension.onFactory(core, command);
95
133
  }
96
134
  }
97
- const ctx = deepFreeze(core, ["extensions"]);
98
- return ctx;
135
+ return deepFreeze(core, ["extensions"]);
99
136
  }
100
137
  function getCommandName(cmd) {
101
138
  if (isLazyCommand(cmd)) return cmd.commandName || cmd.name || ANONYMOUS_COMMAND_NAME;
@@ -105,35 +142,40 @@ function getCommandName(cmd) {
105
142
 
106
143
  //#endregion
107
144
  //#region ../gunshi/src/plugin/core.ts
145
+ const NOOP_EXTENSION = () => {
146
+ return Object.create(null);
147
+ };
108
148
  /**
109
149
  * Define a plugin
110
- * @param options - {@link PluginOptions | plugin options}
111
- * @returns A defined plugin.
150
+ *
151
+ * @param options - {@linkcode PluginOptions | plugin options}
152
+ * @returns A defined plugin
153
+ *
112
154
  * @since v0.27.0
113
155
  */
114
- function plugin(options) {
115
- const { id, name, setup, extension, onExtension, dependencies } = options;
156
+ function plugin(options = {}) {
157
+ const { id, name, setup, onExtension, dependencies } = options;
158
+ const extension = options.extension || NOOP_EXTENSION;
116
159
  const pluginFn = async (ctx) => {
117
160
  if (setup) await setup(ctx);
118
161
  };
162
+ const props = {
163
+ writable: false,
164
+ enumerable: true,
165
+ configurable: true
166
+ };
119
167
  return Object.defineProperties(pluginFn, {
120
168
  id: {
121
169
  value: id,
122
- writable: false,
123
- enumerable: true,
124
- configurable: true
170
+ ...props
125
171
  },
126
172
  ...name && { name: {
127
173
  value: name,
128
- writable: false,
129
- enumerable: true,
130
- configurable: true
174
+ ...props
131
175
  } },
132
176
  ...dependencies && { dependencies: {
133
177
  value: dependencies,
134
- writable: false,
135
- enumerable: true,
136
- configurable: true
178
+ ...props
137
179
  } },
138
180
  ...extension && { extension: {
139
181
  value: {
@@ -141,12 +183,10 @@ function plugin(options) {
141
183
  factory: extension,
142
184
  onFactory: onExtension
143
185
  },
144
- writable: false,
145
- enumerable: true,
146
- configurable: true
186
+ ...props
147
187
  } }
148
188
  });
149
189
  }
150
190
 
151
191
  //#endregion
152
- export { CLI_OPTIONS_DEFAULT, createCommandContext, plugin };
192
+ export { ANONYMOUS_COMMAND_NAME, CLI_OPTIONS_DEFAULT, createCommandContext, plugin };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@gunshi/plugin",
3
- "description": "utilities for gunshi plugin",
4
- "version": "0.27.0-alpha.8",
3
+ "description": "plugin development kit for gunshi",
4
+ "version": "0.27.0-beta.0",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
7
7
  "email": "kawakazu80@gmail.com"
@@ -51,12 +51,12 @@
51
51
  }
52
52
  },
53
53
  "devDependencies": {
54
- "deno": "^2.4.0",
54
+ "deno": "^2.5.4",
55
55
  "jsr": "^0.13.5",
56
56
  "jsr-exports-lint": "^0.4.1",
57
- "publint": "^0.3.12",
58
- "tsdown": "^0.12.9",
59
- "gunshi": "0.27.0-alpha.8"
57
+ "publint": "^0.3.14",
58
+ "tsdown": "^0.15.6",
59
+ "gunshi": "0.27.0-beta.0"
60
60
  },
61
61
  "scripts": {
62
62
  "build": "tsdown",