Skip to content

Commit 1f781c3

Browse files
committed
chore: formatting & readme update
1 parent 602877c commit 1f781c3

12 files changed

Lines changed: 324 additions & 388 deletions

File tree

.editorconfig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
[*]
22
tab_width = 2
3+
indent_size = 2
34
indent_style = space
45
insert_final_newline = true
5-
trim_trailing_whitespace = true
6+
trim_trailing_whitespace = true
7+
8+
[*.md]
9+
trim_trailing_whitespace = false

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
templates/
2+
scaffolds/

.prettierrc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
{
2-
"semi": false,
32
"printWidth": 100,
4-
"singleQuote": false
3+
"semi": false,
4+
"singleQuote": true,
5+
"trailingComma": "all",
6+
"overrides": [
7+
{
8+
"files": "*.md",
9+
"options": {
10+
"printWidth": 100,
11+
"proseWrap": "always"
12+
}
13+
}
14+
]
515
}

README.md

Lines changed: 111 additions & 211 deletions
Large diffs are not rendered by default.

src/color.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
export const ansiStyles = {
2-
reset: "\x1b[0m",
3-
bold: "\x1b[1m",
4-
underline: "\x1b[4m",
5-
black: "\x1b[30m",
2+
reset: '\x1b[0m',
3+
bold: '\x1b[1m',
4+
underline: '\x1b[4m',
5+
black: '\x1b[30m',
66
}
77

88
export const ansiColors = {
9-
red: "\x1b[31m",
10-
green: "\x1b[32m",
11-
yellow: "\x1b[33m", // warning
12-
blue: "\x1b[34m",
13-
magenta: "\x1b[35m", // error
14-
cyan: "\x1b[36m",
15-
white: "\x1b[37m",
16-
gray: "\x1b[90m",
17-
grey: "\x1b[90m",
18-
brightRed: "\x1b[91m",
19-
brightGreen: "\x1b[92m",
20-
brightYellow: "\x1b[93m",
21-
brightBlue: "\x1b[94m",
22-
brightMagenta: "\x1b[95m",
23-
brightCyan: "\x1b[96m",
24-
brightWhite: "\x1b[97m",
9+
red: '\x1b[31m',
10+
green: '\x1b[32m',
11+
yellow: '\x1b[33m', // warning
12+
blue: '\x1b[34m',
13+
magenta: '\x1b[35m', // error
14+
cyan: '\x1b[36m',
15+
white: '\x1b[37m',
16+
gray: '\x1b[90m',
17+
grey: '\x1b[90m',
18+
brightRed: '\x1b[91m',
19+
brightGreen: '\x1b[92m',
20+
brightYellow: '\x1b[93m',
21+
brightBlue: '\x1b[94m',
22+
brightMagenta: '\x1b[95m',
23+
brightCyan: '\x1b[96m',
24+
brightWhite: '\x1b[97m',
2525
}
2626

2727
export type StringStyle = {
@@ -33,13 +33,13 @@ export type StringStyle = {
3333

3434
export function format(string: string, style: StringStyle = {}): string {
3535
const { color, bold, underline, reset } = style
36-
const colorCode = color ? ansiColors[color] : ""
37-
const boldCode = bold ? ansiStyles.bold : ""
38-
const underlineCode = underline ? ansiStyles.underline : ""
39-
const resetCode = reset ? ansiStyles.reset : ""
36+
const colorCode = color ? ansiColors[color] : ''
37+
const boldCode = bold ? ansiStyles.bold : ''
38+
const underlineCode = underline ? ansiStyles.underline : ''
39+
const resetCode = reset ? ansiStyles.reset : ''
4040
return `${colorCode}${boldCode}${underlineCode}${string}${resetCode}`
4141
}
4242

4343
export function stripColors(string: string): string {
44-
return string.replace(/\x1b\[\d+m/g, "")
44+
return string.replace(/\x1b\[\d+m/g, '')
4545
}

src/command.ts

Lines changed: 62 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { z } from "zod"
2-
import { isZodError, ParseError, ValidationError } from "./error"
3-
import { HelpGenerator } from "./help"
1+
import { z } from 'zod'
2+
import { isZodError, ParseError, ValidationError } from './error'
3+
import { HelpGenerator } from './help'
44
import MassargOption, {
55
MassargFlag,
66
OptionConfig,
77
TypedOptionConfig,
88
MassargHelpFlag,
9-
} from "./option"
10-
import { setOrPush } from "./utils"
9+
} from './option'
10+
import { setOrPush } from './utils'
1111

1212
export const CommandConfig = <RunArgs extends z.ZodType>(args: RunArgs) =>
1313
z.object({
@@ -93,9 +93,9 @@ export default class MassargCommand<Args extends ArgsObject = ArgsObject> {
9393
}
9494
}
9595

96-
flag(config: Omit<OptionConfig<boolean>, "parse">): MassargCommand<Args>
96+
flag(config: Omit<OptionConfig<boolean>, 'parse'>): MassargCommand<Args>
9797
flag(config: MassargFlag): MassargCommand<Args>
98-
flag(config: Omit<OptionConfig<boolean>, "parse"> | MassargFlag): MassargCommand<Args> {
98+
flag(config: Omit<OptionConfig<boolean>, 'parse'> | MassargFlag): MassargCommand<Args> {
9999
try {
100100
const flag = config instanceof MassargFlag ? config : new MassargFlag(config)
101101
this.options.push(flag as MassargOption)
@@ -138,39 +138,16 @@ export default class MassargCommand<Args extends ArgsObject = ArgsObject> {
138138
}
139139

140140
parse(argv: string[], args?: Partial<Args>, parent?: MassargCommand<Args>): Promise<void> | void {
141-
this.args ??= {}
142-
this.args = { ...this.args, ...args }
143-
let _argv = [...argv]
144-
while (_argv.length) {
145-
const arg = _argv.shift()!
146-
const found = this.options.some((o) => o._isOption(arg))
147-
if (found) {
148-
_argv = this.parseOption(arg, _argv)
149-
continue
150-
}
151-
152-
const command = this.commands.find((c) => c.name === arg || c.aliases.includes(arg))
153-
if (command) {
154-
return command.parse(_argv, this.args, parent ?? this)
155-
}
156-
const defaultOption = this.options.find((o) => o.isDefault)
157-
if (defaultOption) {
158-
_argv = this.parseOption(`--${defaultOption.name}`, [arg, ..._argv])
159-
continue
160-
}
161-
}
162-
if (this._run) {
163-
this._run({ ...args, ...this.args } as Args, parent ?? this)
164-
}
141+
this.getArgs(argv, args, parent, true)
165142
}
166143

167144
private parseOption(arg: string, argv: string[]) {
168145
const option = this.options.find((o) => o._match(arg))
169146
if (!option) {
170147
throw new ValidationError({
171148
path: [MassargOption.getName(arg)],
172-
code: "unknown_option",
173-
message: "Unknown option",
149+
code: 'unknown_option',
150+
message: 'Unknown option',
174151
})
175152
}
176153
const res = option._parseDetails([arg, ...argv])
@@ -182,9 +159,27 @@ export default class MassargCommand<Args extends ArgsObject = ArgsObject> {
182159
return res.argv
183160
}
184161

185-
getArgs(argv: string[]): Args {
186-
let args: Args = {} as Args
162+
getArgs(
163+
argv: string[],
164+
__args?: Partial<Args>,
165+
parent?: MassargCommand<any>,
166+
parseCommands?: false,
167+
): Promise<void> | void
168+
getArgs(
169+
argv: string[],
170+
__args?: Partial<Args>,
171+
parent?: MassargCommand<any>,
172+
parseCommands?: true,
173+
): Args
174+
getArgs(
175+
argv: string[],
176+
args?: Partial<Args>,
177+
parent?: MassargCommand<any>,
178+
parseCommands = false,
179+
): Args | Promise<void> | void {
180+
let _args: Args = { ...this.args, ...args } as Args
187181
let _argv = [...argv]
182+
const _a = this.args as Record<string, string[]>
188183
while (_argv.length) {
189184
const arg = _argv.shift()!
190185
const found = this.options.some((o) => o._isOption(arg))
@@ -193,25 +188,41 @@ export default class MassargCommand<Args extends ArgsObject = ArgsObject> {
193188
if (!option) {
194189
throw new ValidationError({
195190
path: [MassargOption.getName(arg)],
196-
code: "unknown_option",
197-
message: "Unknown option",
191+
code: 'unknown_option',
192+
message: 'Unknown option',
198193
})
199194
}
200195
const res = option._parseDetails(argv)
201-
args[res.key as keyof Args] = setOrPush<Args[keyof Args]>(
196+
_args[res.key as keyof Args] = setOrPush<Args[keyof Args]>(
202197
res.value,
203-
args[res.key as keyof Args],
198+
_args[res.key as keyof Args],
204199
option.isArray,
205200
)
206201
continue
207202
}
208203

209204
const command = this.commands.find((c) => c.name === arg || c.aliases.includes(arg))
210205
if (command) {
211-
break
206+
if (!parseCommands) {
207+
break
208+
}
209+
return command.parse(_argv, this.args, parent ?? this)
212210
}
211+
const defaultOption = this.options.find((o) => o.isDefault)
212+
if (defaultOption) {
213+
_argv = this.parseOption(`--${defaultOption.name}`, [arg, ..._argv])
214+
continue
215+
}
216+
_a.extra ??= []
217+
_a.extra.push(arg)
218+
}
219+
if (!parseCommands) {
220+
return _args
221+
}
222+
this.args = { ...this.args, ..._args }
223+
if (this._run) {
224+
this._run(this.args, parent ?? this)
213225
}
214-
return args
215226
}
216227

217228
helpString(): string {
@@ -224,11 +235,11 @@ export default class MassargCommand<Args extends ArgsObject = ArgsObject> {
224235
}
225236

226237
export class MassargHelpCommand<T extends ArgsObject = ArgsObject> extends MassargCommand<T> {
227-
constructor(config: Partial<Omit<CommandConfig<T>, "run">> = {}) {
238+
constructor(config: Partial<Omit<CommandConfig<T>, 'run'>> = {}) {
228239
super({
229-
name: "help",
230-
aliases: ["h"],
231-
description: "Print help for this command, or a subcommand if specified",
240+
name: 'help',
241+
aliases: ['h'],
242+
description: 'Print help for this command, or a subcommand if specified',
232243
// argsHint: "[command]",
233244
run: (args, parent) => {
234245
if (args.command) {
@@ -238,9 +249,9 @@ export class MassargHelpCommand<T extends ArgsObject = ArgsObject> extends Massa
238249
return
239250
} else {
240251
throw new ParseError({
241-
path: ["command"],
242-
code: "unknown_command",
243-
message: "Unknown command",
252+
path: ['command'],
253+
code: 'unknown_command',
254+
message: 'Unknown command',
244255
received: args.command,
245256
})
246257
}
@@ -250,9 +261,9 @@ export class MassargHelpCommand<T extends ArgsObject = ArgsObject> extends Massa
250261
...config,
251262
})
252263
this.option({
253-
name: "command",
254-
aliases: ["c"],
255-
description: "Command to print help for",
264+
name: 'command',
265+
aliases: ['c'],
266+
description: 'Command to print help for',
256267
isDefault: true,
257268
})
258269
}

src/error.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import { z } from "zod"
1+
import { z } from 'zod'
22

33
export class ValidationError extends Error {
44
path: string[]
55
code: string
66
message: string
77

88
constructor({ path, code, message }: { path: string[]; code: string; message: string }) {
9-
const msg = `${path.join(".")}: ${message}`
9+
const msg = `${path.join('.')}: ${message}`
1010
super(msg)
1111
this.path = path
1212
this.code = code
1313
this.message = msg
14-
this.name = "ValidationError"
14+
this.name = 'ValidationError'
1515
}
1616
}
1717
export class ParseError extends Error {
@@ -31,15 +31,15 @@ export class ParseError extends Error {
3131
message: string
3232
received?: unknown
3333
}) {
34-
let msg = `${path.join(".")}: ${message}`
34+
let msg = `${path.join('.')}: ${message}`
3535
if (received) {
3636
msg += ` (received: ${received})`
3737
}
3838
super(msg)
3939
this.path = path
4040
this.code = code
4141
this.message = msg
42-
this.name = "ParseError"
42+
this.name = 'ParseError'
4343
this.received = received
4444
}
4545
}

0 commit comments

Comments
 (0)