Skip to content

Commit

Permalink
feat: print version
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Feb 3, 2023
1 parent db018bd commit e240fed
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 37 deletions.
7 changes: 5 additions & 2 deletions packages/breadc/src/breadc.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import type { Breadc, AppOption, Command, Option } from './types';

import { makeCommand } from './command';
import { makeCommand, makeHelpCommand, makeVersionCommand } from './command';
import { makeTreeNode, parse } from './parser';
import { initContextOptions, makeOption } from './option';

export function breadc(name: string, config: AppOption = {}) {
let defaultCommand: Command | undefined = undefined;
const allCommands: Command[] = [];
const globalOptions: Option[] = [];
const globalOptions: Option[] = [
makeVersionCommand(name, config),
makeHelpCommand(name, config)
];

const root = makeTreeNode({
init(context) {
Expand Down
71 changes: 60 additions & 11 deletions packages/breadc/src/command.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AppOption } from './types/breadc';
import type { Command, Argument, Option } from './types';

import { ParseError } from './error';
Expand Down Expand Up @@ -149,25 +150,73 @@ export function makeCommand<F extends string = string>(
return command;
}

export function makeHelpCommand(): { option: Option; node: TreeNode } {
export function makeVersionCommand(name: string, config: AppOption): Option {
const command: Command = {
callback() {
const text = `${name}/${config.version ? config.version : 'unknown'}`;
console.log(text);
return text;
},
description: 'Print version',
_arguments: [],
_options: [],
option() {
return command;
},
action() {}
};

const node = makeTreeNode({
command,
next() {
return false;
}
});

const option: Option = {
format: '-h, --help',
name: 'help',
short: 'h',
format: '-v, --version',
name: 'version',
short: 'v',
type: 'boolean',
initial: false,
description: 'Print help'
initial: undefined,
description: 'Print version',
action() {
return node;
}
};

return option;
}

export function makeHelpCommand(name: string, config: AppOption): Option {
const command: Command = {
callback() {},
description: '',
_arguments: [],
_options: [],
option() {
return command;
},
action() {}
};

const node = makeTreeNode({
next() {
return false;
},
finish(context) {}
}
});

return {
option,
node
const option: Option = {
format: '-h, --help',
name: 'help',
short: 'h',
type: 'boolean',
initial: undefined,
description: 'Print help',
action() {
return node;
}
};

return option;
}
4 changes: 3 additions & 1 deletion packages/breadc/src/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export const initContextOptions = (options: Option[], context: Context) => {
context.options.set('no-' + option.name, option);
}

context.result.options[camelCase(option.name)] = option.initial;
if (option.initial !== undefined) {
context.result.options[camelCase(option.name)] = option.initial;
}
}
};
4 changes: 3 additions & 1 deletion packages/breadc/src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export function parseOption(
const option = context.options.get(key)!;
const name = camelCase(option.name);

if (option.type === 'boolean') {
if (option.action) {
return option.action(cursor, token, context);
} else if (option.type === 'boolean') {
context.result.options[name] = !key.startsWith('no-') ? true : false;
} else if (option.type === 'string') {
if (rawV !== undefined) {
Expand Down
12 changes: 10 additions & 2 deletions packages/breadc/src/types/breadc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ParseResult } from '../parser';
import type { ParseResult, TreeNode, Context, Token } from '../parser';

import type { Letter } from './utils';

Expand Down Expand Up @@ -63,8 +63,16 @@ export interface Option<
name: string;
short?: string;
type: T extends string ? 'string' : T extends boolean ? 'boolean' : never;
initial: T extends string ? string : T extends boolean ? boolean : never;
// Set initial option value, undefined means not init this option
initial?: T extends string ? string : T extends boolean ? boolean : never;
description: string;

// Replace the default option parser behavior
action?: (
cursor: TreeNode,
token: Token,
context: Context
) => TreeNode | false;
}

export interface OptionOption<T extends string | boolean> {
Expand Down
33 changes: 13 additions & 20 deletions packages/breadc/test/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@ import { describe, expect, it } from 'vitest';
import breadc from '../src';

describe('Version Command', () => {
it('should print version', () => {
const cli = breadc('cli', { version: '0.0.0' });
it('should print unknown version', async () => {
const cli = breadc('cli');
expect(await cli.run(['-v'])).toMatchInlineSnapshot('"cli/unknown"');
expect(await cli.run(['--version'])).toMatchInlineSnapshot('"cli/unknown"');
});

it('should print version', async () => {
const cli = breadc('cli', {
version: '1.0.0'
});

expect(await cli.run(['-v'])).toMatchInlineSnapshot('"cli/1.0.0"');
expect(await cli.run(['--version'])).toMatchInlineSnapshot('"cli/1.0.0"');
});
});

Expand Down Expand Up @@ -52,24 +63,6 @@ describe('Version Command', () => {
// });
// });

// describe('Version command', () => {
// it('should print version', async () => {
// const output: string[] = [];
// const cli = Breadc('cli', {
// version: '1.0.0',
// logger: (message: string) => {
// output.push(message);
// }
// });

// await cli.run(['-v']);
// await cli.run(['--version']);

// expect(output[0]).toMatchInlineSnapshot('"cli/1.0.0"');
// expect(output[1]).toMatchInlineSnapshot('"cli/1.0.0"');
// });
// });

// describe('Help command', () => {
// it('should print simple help', async () => {
// const output: string[] = [];
Expand Down

0 comments on commit e240fed

Please sign in to comment.