@guanghechen/commander 1.0.11 → 1.0.12
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/CHANGELOG.md +9 -0
- package/README.md +223 -5
- package/package.json +8 -8
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,15 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## <small>1.0.12 (2025-08-29)</small>
|
|
7
|
+
|
|
8
|
+
* improve: refine READMEs ([c41c1f2](https://github.com/guanghechen/node-scaffolds/commit/c41c1f2))
|
|
9
|
+
* :wrench: chore: upgrade dependencies & update the node requirement from ([8ddfde1](https://github.com/guanghechen/node-scaffolds/commit/8ddfde1))
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
6
15
|
## <small>1.0.11 (2025-07-04)</small>
|
|
7
16
|
|
|
8
17
|
* :bookmark: release ([2313a96](https://github.com/guanghechen/node-scaffolds/commit/2313a96))
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<header>
|
|
2
2
|
<h1 align="center">
|
|
3
|
-
<a href="https://github.com/guanghechen/node-scaffolds/tree/@guanghechen/commander@1.0.
|
|
3
|
+
<a href="https://github.com/guanghechen/node-scaffolds/tree/@guanghechen/commander@1.0.12/packages/commander#readme">@guanghechen/commander</a>
|
|
4
4
|
</h1>
|
|
5
5
|
<div align="center">
|
|
6
6
|
<a href="https://www.npmjs.com/package/@guanghechen/commander">
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
</a>
|
|
36
36
|
<a href="https://github.com/facebook/jest">
|
|
37
37
|
<img
|
|
38
|
-
alt="
|
|
38
|
+
alt="ESLint Version"
|
|
39
39
|
src="https://img.shields.io/npm/dependency-version/@guanghechen/commander/peer/jest"
|
|
40
40
|
/>
|
|
41
41
|
</a>
|
|
@@ -55,8 +55,7 @@
|
|
|
55
55
|
</header>
|
|
56
56
|
<br/>
|
|
57
57
|
|
|
58
|
-
|
|
59
|
-
Utility functions for creating command programs tools based on [commander.js][].
|
|
58
|
+
Utility functions for creating command program tools based on [commander.js][].
|
|
60
59
|
|
|
61
60
|
## Install
|
|
62
61
|
|
|
@@ -72,12 +71,231 @@ Utility functions for creating command programs tools based on [commander.js][].
|
|
|
72
71
|
yarn add --dev @guanghechen/commander
|
|
73
72
|
```
|
|
74
73
|
|
|
74
|
+
## Usage
|
|
75
|
+
|
|
76
|
+
This package extends [commander.js][] with enhanced functionality for building CLI applications with configuration management and sub-command support.
|
|
77
|
+
|
|
78
|
+
### Enhanced Command Class
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { Command, type ICommandActionCallback } from '@guanghechen/commander'
|
|
82
|
+
|
|
83
|
+
interface IMyOptions extends ICommandConfigurationOptions {
|
|
84
|
+
readonly input: string
|
|
85
|
+
readonly output: string
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const command = new Command()
|
|
89
|
+
.name('my-tool')
|
|
90
|
+
.description('My awesome CLI tool')
|
|
91
|
+
.action<IMyOptions>((args, options, extra, self) => {
|
|
92
|
+
console.log('Arguments:', args)
|
|
93
|
+
console.log('Options:', options)
|
|
94
|
+
console.log('Extra args:', extra)
|
|
95
|
+
})
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Creating Top-Level Commands
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import { createTopCommand } from '@guanghechen/commander'
|
|
102
|
+
|
|
103
|
+
const program = createTopCommand('my-cli', '1.0.0')
|
|
104
|
+
// The command comes pre-configured with:
|
|
105
|
+
// - Configuration file options (--config-path, --parastic-config-path, etc.)
|
|
106
|
+
// - Logging options (--log-level, --log-encoding, etc.)
|
|
107
|
+
// - Workspace option (--workspace)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Main Command Utilities
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
import {
|
|
114
|
+
createMainCommandMounter,
|
|
115
|
+
createMainCommandExecutor,
|
|
116
|
+
type IMainCommandProcessor,
|
|
117
|
+
type IMainCommandCreator
|
|
118
|
+
} from '@guanghechen/commander'
|
|
119
|
+
|
|
120
|
+
// Define your main command processor
|
|
121
|
+
const processor: IMainCommandProcessor<IMyOptions> = async (options) => {
|
|
122
|
+
console.log('Processing with options:', options)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Create a command creator
|
|
126
|
+
const creator: IMainCommandCreator<IMyOptions> = (handle) => {
|
|
127
|
+
return new Command()
|
|
128
|
+
.name('build')
|
|
129
|
+
.description('Build the project')
|
|
130
|
+
.action(handle ? (opts) => handle(opts) : () => {})
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Create and use a mounter for adding to parent command
|
|
134
|
+
const mounter = createMainCommandMounter(creator, processor)
|
|
135
|
+
mounter(program)
|
|
136
|
+
|
|
137
|
+
// Or create an executor for direct execution
|
|
138
|
+
const executor = createMainCommandExecutor(creator, processor)
|
|
139
|
+
await executor(process.argv)
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Sub-Command Architecture
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
import { SubCommand, type ISubCommandOptions, type ISubCommandProcessor } from '@guanghechen/commander'
|
|
146
|
+
|
|
147
|
+
interface IBuildOptions extends ISubCommandOptions {
|
|
148
|
+
readonly target: string
|
|
149
|
+
readonly production: boolean
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
class BuildCommand extends SubCommand<IBuildOptions> {
|
|
153
|
+
public readonly subCommandName = 'build'
|
|
154
|
+
public readonly aliases = ['b']
|
|
155
|
+
|
|
156
|
+
public command(processor: ISubCommandProcessor<IBuildOptions>): Command {
|
|
157
|
+
return new Command()
|
|
158
|
+
.name(this.subCommandName)
|
|
159
|
+
.aliases(this.aliases)
|
|
160
|
+
.description('Build the project')
|
|
161
|
+
.option('--target <target>', 'Build target', 'development')
|
|
162
|
+
.option('--production', 'Production build', false)
|
|
163
|
+
.action(async (args, options, extra) => {
|
|
164
|
+
await processor.process(args, options)
|
|
165
|
+
})
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
public async resolveProcessor(args: string[], options: IBuildOptions): Promise<ISubCommandProcessor<IBuildOptions>> {
|
|
169
|
+
return {
|
|
170
|
+
process: async (args, options) => {
|
|
171
|
+
console.log(`Building for ${options.target}`)
|
|
172
|
+
if (options.production) {
|
|
173
|
+
console.log('Production mode enabled')
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Usage
|
|
181
|
+
const buildCmd = new BuildCommand()
|
|
182
|
+
buildCmd.mount(program, { isDefault: false })
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### Utility Functions
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
import { hasGitInstalled, installDependencies } from '@guanghechen/commander'
|
|
189
|
+
|
|
190
|
+
// Check if Git is available
|
|
191
|
+
if (hasGitInstalled()) {
|
|
192
|
+
console.log('Git is available')
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Install dependencies with user selection
|
|
196
|
+
await installDependencies({
|
|
197
|
+
cwd: process.cwd(),
|
|
198
|
+
plopBypass: [], // Or ['yarn'] to skip user prompt
|
|
199
|
+
reporter: myReporter
|
|
200
|
+
})
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## API Reference
|
|
204
|
+
|
|
205
|
+
| Name | Signature | Description |
|
|
206
|
+
|------|-----------|-------------|
|
|
207
|
+
| `Command` | Class | Enhanced version with improved TypeScript support |
|
|
208
|
+
| `SubCommand` | `abstract class SubCommand<O>` | Abstract base class for implementing sub-commands with configuration support |
|
|
209
|
+
| `createTopCommand` | `(commandName: string, version: string) => Command` | Creates a top-level command with pre-configured common options |
|
|
210
|
+
| `createMainCommandMounter` | `<O>(creator, handle) => IMainCommandMounter` | Creates a function for mounting main commands to parent commands |
|
|
211
|
+
| `createMainCommandExecutor` | `<O>(creator, handle) => IMainCommandExecutor` | Creates a function for executing main commands directly |
|
|
212
|
+
| `hasGitInstalled` | `() => boolean` | Checks if Git is installed and available in PATH |
|
|
213
|
+
| `installDependencies` | `(params) => Promise<void>` | Prompts user to install dependencies using npm or yarn |
|
|
214
|
+
|
|
215
|
+
### Detailed Interfaces
|
|
216
|
+
|
|
217
|
+
#### `Command` Class Methods
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
class Command {
|
|
221
|
+
action<T>(fn: ICommandActionCallback<T>): this // Register action callback with enhanced type safety
|
|
222
|
+
opts<T>(): T // Get options with proper type inference
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
#### `SubCommand<O>` Abstract Class
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
abstract class SubCommand<O extends ISubCommandOptions> {
|
|
230
|
+
// Abstract properties
|
|
231
|
+
abstract readonly subCommandName: string // The name of the sub-command
|
|
232
|
+
abstract readonly aliases: string[] // Command aliases
|
|
233
|
+
|
|
234
|
+
// Abstract methods
|
|
235
|
+
abstract command(processor: ISubCommandProcessor<O>): Command
|
|
236
|
+
abstract resolveProcessor(args: string[], options: O): Promise<ISubCommandProcessor<O>>
|
|
237
|
+
|
|
238
|
+
// Instance methods
|
|
239
|
+
mount(parentCommand: Command, opts?: { isDefault?: boolean }): void
|
|
240
|
+
execute(parentCommand: Command, rawArgs: string[]): Promise<void>
|
|
241
|
+
process(args: string[], options: O): Promise<void>
|
|
242
|
+
}
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
#### `createTopCommand` Pre-configured Options
|
|
246
|
+
|
|
247
|
+
The command comes with these built-in options:
|
|
248
|
+
|
|
249
|
+
```typescript
|
|
250
|
+
--config-path, -c // Configuration file paths
|
|
251
|
+
--parastic-config-path // Parasitic configuration file path
|
|
252
|
+
--parastic-config-entry // Entry key in parasitic config
|
|
253
|
+
--workspace // Working directory
|
|
254
|
+
--log-level // Logging level
|
|
255
|
+
--log-encoding // Log file encoding
|
|
256
|
+
--log-filepath // Log file path
|
|
257
|
+
--log-name // Logger name
|
|
258
|
+
--log-mode // Log format mode
|
|
259
|
+
--log-flight // Logger flight options
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
#### `installDependencies` Parameters
|
|
263
|
+
|
|
264
|
+
```typescript
|
|
265
|
+
interface IInstallDependenciesParams {
|
|
266
|
+
cwd: string // Working directory
|
|
267
|
+
plopBypass: string[] // Pre-selected choices to bypass prompts
|
|
268
|
+
reporter?: IReporter // Optional logger
|
|
269
|
+
}
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
#### Type Definitions
|
|
273
|
+
|
|
274
|
+
```typescript
|
|
275
|
+
// Action callback function signature
|
|
276
|
+
type ICommandActionCallback<T> = (
|
|
277
|
+
args: string[],
|
|
278
|
+
options: T,
|
|
279
|
+
extra: string[],
|
|
280
|
+
self: Command,
|
|
281
|
+
) => void | Promise<void> | never
|
|
282
|
+
|
|
283
|
+
// Sub-command processor interface
|
|
284
|
+
interface ISubCommandProcessor<O> {
|
|
285
|
+
process(args: string[], options: O): Promise<void>
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// Base interface for sub-command options
|
|
289
|
+
interface ISubCommandOptions extends ICommandConfigurationOptions {
|
|
290
|
+
// Inherits all configuration options
|
|
291
|
+
}
|
|
292
|
+
```
|
|
75
293
|
|
|
76
294
|
## Related
|
|
77
295
|
|
|
78
296
|
* [commander.js][]
|
|
79
297
|
|
|
80
298
|
|
|
81
|
-
[homepage]: https://github.com/guanghechen/node-scaffolds/tree/@guanghechen/commander@1.0.
|
|
299
|
+
[homepage]: https://github.com/guanghechen/node-scaffolds/tree/@guanghechen/commander@1.0.12/packages/commander#readme
|
|
82
300
|
[commander.js]: https://github.com/tj/commander.js/
|
|
83
301
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@guanghechen/commander",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.12",
|
|
4
4
|
"description": "A wrapper of commander.js with some utilities",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "guanghechen",
|
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
},
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
|
-
"url": "https://github.com/guanghechen/node-scaffolds/tree/@guanghechen/commander@1.0.
|
|
11
|
+
"url": "https://github.com/guanghechen/node-scaffolds/tree/@guanghechen/commander@1.0.11",
|
|
12
12
|
"directory": "packages/commander"
|
|
13
13
|
},
|
|
14
|
-
"homepage": "https://github.com/guanghechen/node-scaffolds/tree/@guanghechen/commander@1.0.
|
|
14
|
+
"homepage": "https://github.com/guanghechen/node-scaffolds/tree/@guanghechen/commander@1.0.11/packages/commander#readme",
|
|
15
15
|
"keywords": [
|
|
16
16
|
"commander"
|
|
17
17
|
],
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"types": "./lib/types/index.d.ts",
|
|
31
31
|
"license": "MIT",
|
|
32
32
|
"engines": {
|
|
33
|
-
"node": ">=
|
|
33
|
+
"node": ">= 20.0.0"
|
|
34
34
|
},
|
|
35
35
|
"files": [
|
|
36
36
|
"lib/",
|
|
@@ -41,11 +41,11 @@
|
|
|
41
41
|
"README.md"
|
|
42
42
|
],
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@guanghechen/cli": "^1.0.
|
|
45
|
-
"@guanghechen/exec": "^1.0.
|
|
44
|
+
"@guanghechen/cli": "^1.0.8",
|
|
45
|
+
"@guanghechen/exec": "^1.0.9",
|
|
46
46
|
"@guanghechen/reporter.types": "^1.0.5",
|
|
47
|
-
"@inquirer/select": "4.2
|
|
47
|
+
"@inquirer/select": "4.3.2",
|
|
48
48
|
"commander": "12.1.0"
|
|
49
49
|
},
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "26026650aac329e6342b68d80111d82fd0213a20"
|
|
51
51
|
}
|