Skip to content

Commit

Permalink
feat: compatible with cac
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Jun 20, 2022
1 parent e0e0b40 commit abc1a7b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 19 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ cli.run(process.argv.slice(2))

If you are using IDEs that support TypeScript (like [Visual Studio Code](https://code.visualstudio.com/)), move your cursor to the parameter `option` in this `dev` command, and then you will find the `option` is automatically typed with `{ host: string, port: string }` or `Record<'host' | 'port', string>`.

## Inspiration

+ [cac](https://github.com/cacjs/cac): Simple yet powerful framework for building command-line apps.
+ [TypeScript: Documentation - Template Literal Types](https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html)

## License

MIT License © 2021 [XLor](https://github.com/yjl9903)
36 changes: 19 additions & 17 deletions examples/vite.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import Breadc from '../src';

const vite = Breadc('vite', { version: '1.0.0' })
.option('-c, --config <file>', { description: `[string] use specified config file` })
.option('--base <path>', { description: `[string] public base path (default: /)` })
.option('-l, --logLevel <level>', { description: `[string] info | warn | error | silent` })
.option('--clearScreen', { description: `[boolean] allow/disable clear screen when logging` })
.option('-d, --debug [feat]', { description: `[string | boolean] show debug logs` })
.option('-f, --filter <filter>', { description: `[string] filter debug logs` })
.option('-m, --mode <mode>', { description: `[string] set env mode` });
.option('-c, --config <file>', `[string] use specified config file`)
.option('--base <path>', `[string] public base path (default: /)`)
.option('-l, --logLevel <level>', `[string] info | warn | error | silent`)
.option('--clearScreen', `[boolean] allow/disable clear screen when logging`)
.option('-d, --debug [feat]', `[string | boolean] show debug logs`)
.option('-f, --filter <filter>', `[string] filter debug logs`)
.option('-m, --mode <mode>', `[string] set env mode`);

vite
.command('dev [root]', { description: 'start dev server' })
.option('--host [host]', { description: `[string] specify hostname` })
.option('--port <port>', { description: `[number] specify port` })
.option('--https', { description: `[boolean] use TLS + HTTP/2` })
.option('--open [path]', { description: `[boolean | string] open browser on startup` })
.option('--cors', { description: `[boolean] enable CORS` })
.option('--strictPort', { description: `[boolean] exit if specified port is already in use` })
.option('--force', {
description: `[boolean] force the optimizer to ignore the cache and re-bundle`
})
.action(async (root, option) => {});
.option('--host [host]', `[string] specify hostname`)
.option('--port <port>', `[number] specify port`)
.option('--https', `[boolean] use TLS + HTTP/2`)
.option('--open [path]', `[boolean | string] open browser on startup`)
.option('--cors', `[boolean] enable CORS`)
.option('--strictPort', `[boolean] exit if specified port is already in use`)
.option('--force', `[boolean] force the optimizer to ignore the cache and re-bundle`)
.action(async (root, option) => {
console.log(`Root: ${root}`);
console.log(`Option:`);
console.log(option);
});

vite.run(process.argv.slice(2)).catch((err) => vite.logger.error(err.message));
18 changes: 17 additions & 1 deletion src/breadc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,24 @@ export class Breadc<GlobalOption extends string | never = never> {

option<F extends string>(
format: F,
config: OptionConfig = {}
description: string,
config?: Omit<OptionConfig, 'description'>
): Breadc<GlobalOption | ExtractOption<F>>;

option<F extends string>(
format: F,
config: OptionConfig
): Breadc<GlobalOption | ExtractOption<F>>;

option<F extends string>(
format: F,
configOrDescription: OptionConfig | string,
otherConfig: Omit<OptionConfig, 'description'> = {}
): Breadc<GlobalOption | ExtractOption<F>> {
const config: OptionConfig = otherConfig;
if (typeof configOrDescription === 'string') {
config.description = configOrDescription;
}
try {
const option = new Option(format, config);
this.options.push(option);
Expand Down
18 changes: 17 additions & 1 deletion src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,24 @@ export class Command<

option<OF extends string>(
format: OF,
config: OptionConfig = {}
description: string,
config?: Omit<OptionConfig, 'description'>
): Command<F, GlobalOption, CommandOption | ExtractOption<OF>>;

option<OF extends string>(
format: OF,
config: OptionConfig
): Command<F, GlobalOption, CommandOption | ExtractOption<OF>>;

option<OF extends string>(
format: OF,
configOrDescription: OptionConfig | string,
otherConfig: Omit<OptionConfig, 'description'> = {}
): Command<F, GlobalOption, CommandOption | ExtractOption<OF>> {
const config: OptionConfig = otherConfig;
if (typeof configOrDescription === 'string') {
config.description = configOrDescription;
}
try {
const option = new Option(format, config);
this.options.push(option);
Expand Down

0 comments on commit abc1a7b

Please sign in to comment.