yargs-file-commands 1.1.0 → 1.1.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.
Files changed (2) hide show
  1. package/README.md +73 -2
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -123,6 +123,75 @@ export const command = defineCommand({
123
123
  });
124
124
  ```
125
125
 
126
+ ### 4. Shared Options
127
+
128
+ To share options between commands while maintaining type safety, you can use either helper functions (recommended for correct type inference) or shared option objects.
129
+
130
+ **Approach 1: Helper Functions (Recommended)**
131
+
132
+ This approach uses function composition to chain option definitions, allowing TypeScript to correctly infer the resulting types.
133
+
134
+ ```ts
135
+ // shared.ts
136
+ import type { Argv } from 'yargs';
137
+
138
+ export const withPagination = <T>(yargs: Argv<T>) => {
139
+ return yargs
140
+ .option('page', {
141
+ type: 'number',
142
+ default: 1,
143
+ describe: 'Page number'
144
+ })
145
+ .option('limit', {
146
+ type: 'number',
147
+ default: 10,
148
+ describe: 'Items per page'
149
+ });
150
+ };
151
+
152
+ // commands/users.ts
153
+ import { defineCommand } from 'yargs-file-commands';
154
+ import { withPagination } from '../shared.js';
155
+
156
+ export const command = defineCommand({
157
+ command: 'list',
158
+ builder: (yargs) => withPagination(yargs),
159
+ handler: async (argv) => {
160
+ // argv.page and argv.limit are correctly typed as number
161
+ console.log(`Page: ${argv.page}, Limit: ${argv.limit}`);
162
+ }
163
+ });
164
+ ```
165
+
166
+ **Approach 2: Shared Objects**
167
+
168
+ You can also define a common options object and spread it into your command definitions.
169
+
170
+ ```ts
171
+ // shared.ts
172
+ export const commonOptions = {
173
+ verbose: {
174
+ alias: 'v',
175
+ type: 'boolean',
176
+ describe: 'Run with verbose logging',
177
+ default: false,
178
+ } as const
179
+ };
180
+
181
+ // commands/users.ts
182
+ import { defineCommand } from 'yargs-file-commands';
183
+ import { commonOptions } from '../shared.js';
184
+
185
+ export const command = defineCommand({
186
+ command: 'list',
187
+ builder: (yargs) => yargs.options(commonOptions),
188
+ handler: async (argv) => {
189
+ // argv.verbose is correctly typed
190
+ if (argv.verbose) console.log('Verbose mode');
191
+ }
192
+ });
193
+ ```
194
+
126
195
  ## Options
127
196
 
128
197
  The `fileCommands` method takes the following options:
@@ -159,7 +228,7 @@ pnpm install
159
228
  pnpm run build
160
229
 
161
230
  # biome
162
- pnpm run chec
231
+ pnpm biome check --write
163
232
 
164
233
  # tests
165
234
  pnpm vitest
@@ -169,9 +238,11 @@ pnpm clean
169
238
 
170
239
  # run example cli
171
240
  npx example-cli
241
+
242
+ # publish new release
243
+ pnpm make-release
172
244
  ```
173
245
 
174
- Underneath the hood, we are using [NX](https://nx.dev) to manage the monorepo and shared scripts.
175
246
 
176
247
  [npm]: https://img.shields.io/npm/v/yargs-file-commands
177
248
  [npm-url]: https://www.npmjs.com/package/yargs-file-commands
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "yargs-file-commands",
3
3
  "description": "A yargs helper function that lets you define your commands structure via directory and file naming conventions.",
4
- "version": "1.1.0",
4
+ "version": "1.1.1",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",