Skip to content

Commit 881a95a

Browse files
authored
improvement: accept files in annotate command (#39)
1 parent a48926e commit 881a95a

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

locales/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"annotated result detail options": "annotated result detail options",
1616
"the attributes to annotate": "the attributes to annotate",
1717
"'--type' is not supported except for 'custom-block'": "'--type' is not supported except for 'custom-block'",
18+
"if you don't specify some files at the end of the command, the '—-source' option is required": "if you don't specify some files at the end of the command, the '—-source' option is required",
1819
"there is a parse error in {filename}": "there is a parse error in {filename}",
1920
"Unsupported '{type}' block content type: {actual}": "Unsupported '{type}' block content type: {actual}",
2021
"Detected lang mismatch in block `src` ('{src}') and block content ('{content}')": "Detected lang mismatch in block `src` ('{src}') and block content ('{content}')",

src/commands/annotate.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
isSFCParserError,
1111
SFCAnnotateError
1212
} from '../api/annotate'
13+
import { defineFail, RequireError } from './fail'
1314

1415
import type { Arguments, Argv } from 'yargs'
1516
import type { SFCBlock } from '@vue/compiler-sfc'
@@ -19,7 +20,7 @@ const debug = createDebug('@intlify/cli:annotate')
1920
export type AnnotateMode = 'custom-block'
2021

2122
type AnnotateOptions = {
22-
source: string
23+
source?: string
2324
type?: string
2425
force?: boolean
2526
details?: boolean
@@ -38,8 +39,7 @@ export default function defineCommand() {
3839
.option('source', {
3940
type: 'string',
4041
alias: 's',
41-
describe: t('the source path'),
42-
demandOption: true
42+
describe: t('the source path')
4343
})
4444
.option('type', {
4545
type: 'string',
@@ -61,6 +61,7 @@ export default function defineCommand() {
6161
alias: 'a',
6262
describe: t('the attributes to annotate')
6363
})
64+
.fail(defineFail(RequireError))
6465
}
6566

6667
const handler = async (args: Arguments<AnnotateOptions>): Promise<void> => {
@@ -73,12 +74,15 @@ export default function defineCommand() {
7374
debug('annotate args:', source, type, force, details, attrs)
7475

7576
if ((type as AnnotateMode) !== 'custom-block') {
76-
console.log(
77-
chalk.bold.yellow(
78-
t(`'--type' is not supported except for 'custom-block'`)
79-
)
77+
throw new RequireError(
78+
`'--type' is not supported except for 'custom-block'`
79+
)
80+
}
81+
82+
if (source == null && args._.length === 1) {
83+
throw new RequireError(
84+
`if you don't specify some files at the end of the command, the '—-source' option is required`
8085
)
81-
return
8286
}
8387

8488
let counter = 0
@@ -92,7 +96,11 @@ export default function defineCommand() {
9296
let status: AnnoateStatus = 'fine'
9397
const onWarn = warnHnadler(() => (status = 'warn'))
9498

95-
const files = await globAsync(source)
99+
const files =
100+
source != null
101+
? await globAsync(source)
102+
: [...args._].map(a => a.toString()).splice(1)
103+
96104
for (const file of files) {
97105
const parsed = path.parse(file)
98106
if (parsed.ext !== '.vue') {

src/commands/fail.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import chalk from 'chalk'
2+
3+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4+
type Constructor<T> = { new (...args: any[]): T }
5+
6+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7+
function typeGuard<T>(o: any, className: Constructor<T>): o is T {
8+
return o instanceof className
9+
}
10+
11+
export class RequireError extends Error {}
12+
13+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
14+
export function defineFail(userError: any) {
15+
return (msg: string, err: Error) => {
16+
if (msg) {
17+
// TODO: should refactor console message
18+
console.error(msg)
19+
console.warn(chalk.bold.red(msg))
20+
process.exit(1)
21+
} else {
22+
if (typeGuard(err, userError)) {
23+
console.warn(chalk.bold.yellow(err.message))
24+
process.exit(0)
25+
} else {
26+
// preserve statck! see the https://github.com/yargs/yargs/blob/master/docs/api.md#failfn
27+
throw err
28+
}
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)