Skip to content

Commit

Permalink
feat: support custom logger
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Jun 20, 2022
1 parent 4e49922 commit 81329ac
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 24 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,18 @@
"preversion": "pnpm typecheck && pnpm build"
},
"dependencies": {
"debug": "^4.3.4",
"kolorist": "^1.5.1",
"minimist": "^1.2.6"
},
"devDependencies": {
"@types/debug": "^4.1.7",
"@types/minimist": "^1.2.2",
"@types/node": "^17.0.43",
"bumpp": "^8.2.1",
"prettier": "^2.7.1",
"typescript": "^4.7.4",
"unbuild": "^0.7.4",
"vite": "^2.9.12",
"vitest": "^0.15.1"
},
"packageManager": "pnpm@7.3.0"
Expand Down
24 changes: 20 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 19 additions & 11 deletions src/breadc.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
import type { AppOption, Logger } from './types';

import minimist from 'minimist';

import { createDefaultLogger } from './logger';

export class Breadc {
private readonly name: string;
private readonly version: string;

private readonly options: BreadcOption[] = [];
private readonly logger: Logger;

private readonly options: Option[] = [];
private readonly commands: Command[] = [];

constructor(name: string, option: { version: string }) {
constructor(name: string, option: AppOption) {
this.name = name;
this.version = option.version;
this.version = option.version ?? 'unknown';
this.logger = option.logger ?? createDefaultLogger(name);
}

option(text: string) {
try {
const option = new BreadcOption(text);
const option = new Option(text);
this.options.push(option);
} catch (error) {
// Handle warning
} catch (error: any) {
this.logger.warn(error.message);
}
return this;
}

command(text: string) {
return new Breadcommand(this, text);
return new Command(this, text);
}

parse(args: string[]) {
Expand All @@ -34,29 +42,29 @@ export class Breadc {
}
}

class Breadcommand {
class Command {
private readonly breadc: Breadc;

constructor(breadc: Breadc, text: string) {
this.breadc = breadc;
}
}

class BreadcOption {
class Option {
private static BooleanRE = /^--[a-zA-Z.]+$/;
private static NameRE = /--([a-zA-Z.]+)/;

readonly name: string;
readonly type: 'string' | 'boolean';

constructor(text: string) {
if (BreadcOption.BooleanRE.test(text)) {
if (Option.BooleanRE.test(text)) {
this.type = 'boolean';
} else {
this.type = 'string';
}

const match = BreadcOption.NameRE.exec(text);
const match = Option.NameRE.exec(text);
if (match) {
this.name = match[1];
} else {
Expand Down
14 changes: 9 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import type { AppOption } from './types';

import kolorist from 'kolorist';
import minimist from 'minimist';
import createDebug from 'debug';

import { Breadc } from './breadc';

interface Option {
version?: string;
export default function breadc(name: string, option: AppOption = {}) {
return new Breadc(name, option);
}

export default function breadc(name: string, option: Option = {}) {
return new Breadc(name, { version: option.version ?? 'unknown' });
}
export { kolorist, minimist, createDebug };
26 changes: 26 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { Logger } from './types';

import createDebug from 'debug';
import { blue, red, yellow } from 'kolorist';

export function createDefaultLogger(name: string): Logger {
const debug = createDebug(name + ':breadc');

return {
println(message) {
console.log(message);
},
info(message, ...args) {
console.log(`${blue('INFO')} ${message}`, ...args);
},
warn(message, ...args) {
console.log(`${yellow('WARN')} ${message}`, ...args);
},
error(message, ...args) {
console.log(`${red('ERROR')} ${message}`, ...args);
},
debug(message, ...args) {
debug(message, ...args);
}
};
}
15 changes: 15 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface AppOption {
version?: string;

help?: string | string[] | (() => string | string[]);

logger?: Logger;
}

export interface Logger {
println: (message: string) => void;
info: (message: string, ...args: any[]) => void;
warn: (message: string, ...args: any[]) => void;
error: (message: string, ...args: any[]) => void;
debug: (message: string, ...args: any[]) => void;
}
21 changes: 18 additions & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest';

import Breadc from '../src';
import { createDefaultLogger } from '../src/logger';

describe('Breadc', () => {
it('should parse', () => {
Expand Down Expand Up @@ -60,9 +61,8 @@ describe('Breadc', () => {
}
`);

expect(
Breadc('cli').option('--root').parse(['folder', 'text', '--root'])
).toMatchInlineSnapshot(`
expect(Breadc('cli').option('--root').parse(['folder', 'text', '--root']))
.toMatchInlineSnapshot(`
{
"_": [
"folder",
Expand All @@ -72,4 +72,19 @@ describe('Breadc', () => {
}
`);
});

it('should not parse wrong option', () => {
const output: string[] = [];
const logger = createDefaultLogger('cli');
logger.warn = (message: string) => {
output.push(message);
};
Breadc('cli', { logger }).option('invalid');

expect(output).toMatchInlineSnapshot(`
[
"Can not extract option name from \\"invalid\\"",
]
`);
});
});

0 comments on commit 81329ac

Please sign in to comment.