Skip to content

Commit 5d05ed4

Browse files
authored
feat: add --dryRun option for annotate (#43)
1 parent 579ee6c commit 5d05ed4

File tree

2 files changed

+76
-43
lines changed

2 files changed

+76
-43
lines changed

locales/en.json

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,24 @@
1515
"forced applying of attributes": "forced applying of attributes",
1616
"annotated result detail options": "annotated result detail options",
1717
"the attributes to annotate": "the attributes to annotate",
18+
"target files without annotating": "target files without annotating",
1819
"'--type' is not supported except for 'custom-block'": "'--type' is not supported except for 'custom-block'",
1920
"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",
2021
"Unsupported '{type}' block content type: {actual}": "Unsupported '{type}' block content type: {actual}",
2122
"Detected lang mismatch in block `src` ('{src}') and block content ('{content}')": "Detected lang mismatch in block `src` ('{src}') and block content ('{content}')",
2223
"Detected lang mismatch in `lang` option ('{lang}') and block content ('{content}')": "Detected lang mismatch in `lang` option ('{lang}') and block content ('{content}')",
2324
"Detected lang mismatch in `lang` attr ('{lang}') and block content ('{content}')": "Detected lang mismatch in `lang` attr ('{lang}') and block content ('{content}')",
24-
"{count} annotateable files ": "{count} annotateable files ",
25-
"{count} annotates": "{count} annotates",
26-
"{count} pass": "{count} pass",
27-
"{count} warnings": "{count} warnings",
28-
"{count} forces": "{count} forces",
29-
"{count} ignores": "{count} ignores",
30-
"{count} errors": "{count} errors",
25+
"annotate": "annoatte",
26+
"pass annotate": "pass annotate",
27+
"force annotate": "force annotate",
28+
"ignore annotate": "ignore annotate",
29+
"{count} annotateable files ": "annotateable no files | {count} annotateable file | {count} annotateable files",
30+
"{count} annotated files": "annotated no files | {count} annotated file | {count} annotated files",
31+
"{count} passed files": "passed no files | {count} passed file | {count} passed files",
32+
"{count} warned files": "warned no files | {count} warned file | {count} warned files",
33+
"{count} forced files": "forced no files | {count} forced file | {count} forced files",
34+
"{count} ignored files": "ignored no files | {count} ignored file | {count} ignored files",
35+
"{count} error files": "error no files | {count} error file | {count} error files",
3136
"`lang` attr not found": "`lang` attr not found",
3237
"format for single-file components": "format for single-file components",
3338
"the format type": "the format type",
@@ -37,6 +42,5 @@
3742
"no change": "no change",
3843
"{count} formattable files": "{count} formattable files",
3944
"{count} formatted files": "{count} formatted files",
40-
"{count} no change files": "{count} no change files",
41-
"{count} error files": "{count} error files"
45+
"{count} no change files": "{count} no change files"
4246
}

src/commands/annotate.ts

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type AnnotateOptions = {
2525
force?: boolean
2626
details?: boolean
2727
attrs?: Record<string, any> // eslint-disable-line @typescript-eslint/no-explicit-any
28+
dryRun?: boolean
2829
}
2930

3031
type AnnoateStatus = 'none' | 'fine' | 'warn' | 'error'
@@ -53,26 +54,38 @@ export default function defineCommand() {
5354
})
5455
.option('details', {
5556
type: 'boolean',
56-
alias: 'd',
57+
alias: 'v',
5758
describe: t('annotated result detail options')
5859
})
5960
.option('attrs', {
6061
type: 'array',
6162
alias: 'a',
6263
describe: t('the attributes to annotate')
6364
})
65+
.option('dryRun', {
66+
type: 'boolean',
67+
alias: 'd',
68+
describe: t('target files without annotating')
69+
})
6470
.fail(defineFail(RequireError))
6571
}
6672

6773
const handler = async (args: Arguments<AnnotateOptions>): Promise<void> => {
6874
args.type = args.type || 'custom-block'
6975

70-
const { source, type, force, details, attrs } = args as AnnotateOptions
71-
debug('annotate args:', source, type, force, details, attrs)
76+
const { source, type, force, details, attrs, dryRun } =
77+
args as AnnotateOptions
78+
debug('annotate args:', source, type, force, details, attrs, dryRun)
7279

7380
checkType(args.type)
7481
checkSource(args._.length, source)
7582

83+
if (dryRun) {
84+
console.log()
85+
console.log(chalk.bold.yellowBright(`${t('dry run mode')}:`))
86+
console.log()
87+
}
88+
7689
let counter = 0
7790
let passCounter = 0
7891
let fineCounter = 0
@@ -81,6 +94,34 @@ export default function defineCommand() {
8194
let ignoreCounter = 0
8295
let errorCounter = 0
8396

97+
function printStats() {
98+
console.log('')
99+
console.log(
100+
chalk.bold.white(t('{count} annotateable files ', { count: counter }))
101+
)
102+
console.log(
103+
chalk.bold.green(t('{count} annotated files', { count: fineCounter }))
104+
)
105+
if (details) {
106+
console.log(
107+
chalk.white(t('{count} passed files', { count: passCounter }))
108+
)
109+
}
110+
console.log(
111+
chalk.yellow(t('{count} warned files', { count: warnCounter }))
112+
)
113+
if (details) {
114+
console.log(
115+
chalk.yellow(t('{count} forced files', { count: forceCounter }))
116+
)
117+
console.log(
118+
chalk.yellow(t('{count} ignored files', { count: ignoreCounter }))
119+
)
120+
}
121+
console.log(chalk.red(t('{count} error files', { count: errorCounter })))
122+
console.log('')
123+
}
124+
84125
let status: AnnoateStatus = 'fine'
85126
const onWarn = warnHnadler(() => (status = 'warn'))
86127

@@ -103,60 +144,48 @@ export default function defineCommand() {
103144
}
104145

105146
if (status === 'fine') {
106-
console.log(chalk.bold.green(`annotate: ${file}`))
107-
await fs.writeFile(file, annotated, 'utf8')
108147
fineCounter++
148+
console.log(chalk.green(`${file}: ${t('annotate')}`))
149+
if (!dryRun) {
150+
await fs.writeFile(file, annotated, 'utf8')
151+
}
109152
} else if (status === 'none') {
110-
console.log(chalk.white(`pass annotate: ${file}`))
111153
passCounter++
154+
console.log(chalk.white(`${file}: ${t('pass annotate')}`))
112155
} else if (status === 'warn') {
156+
warnCounter++
113157
if (force) {
114-
console.log(chalk.bold.yellow(`force annotate: ${file}`))
115-
await fs.writeFile(file, annotated, 'utf8')
116158
forceCounter++
159+
console.log(chalk.yellow(`${file}: ${t('force annotate')}`))
160+
if (!dryRun) {
161+
await fs.writeFile(file, annotated, 'utf8')
162+
}
117163
} else {
118-
console.log(chalk.yellow(`ignore annotate: ${file}`))
119164
ignoreCounter++
165+
console.log(chalk.yellow(`${file}: ${t('ignore annotate')}`))
120166
}
121-
warnCounter++
122167
}
123168
} catch (e: unknown) {
124169
status = 'error'
125170
errorCounter++
126171
if (isSFCParserError(e)) {
127172
console.error(chalk.bold.red(`${e.message} at ${e.filepath}`))
128-
e.erorrs.forEach(err =>
129-
console.error(chalk.bold.red(` ${err.message}`))
130-
)
173+
e.erorrs.forEach(err => console.error(chalk.red(` ${err.message}`)))
131174
} else if (e instanceof SFCAnnotateError) {
132-
console.error(chalk.bold.red(e.message))
175+
console.error(
176+
chalk.red(`${e.filepath}: ${t(e.message as any)}`) // eslint-disable-line @typescript-eslint/no-explicit-any
177+
)
133178
} else {
179+
console.error(chalk.red((e as Error).message))
180+
}
181+
if (!dryRun) {
134182
throw e
135183
}
136184
}
137185
counter++
138186
}
139187

140-
console.log('')
141-
console.log(
142-
`${chalk.bold.white(
143-
t('{count} annotateable files ', { count: counter })
144-
)}\n${chalk.bold.green(
145-
t('{count} annotates', { count: fineCounter })
146-
)}\n${
147-
details
148-
? `${chalk.white(t('{count} pass', { count: passCounter }))}\n`
149-
: ''
150-
}${chalk.bold.yellow(t('{count} warnings', { count: warnCounter }))}\n${
151-
details
152-
? `${chalk.yellow(t('{count} forces', { count: forceCounter }))}\n`
153-
: ''
154-
}${
155-
details
156-
? `${chalk.yellow(t('{count} ignores', { count: ignoreCounter }))}\n`
157-
: ''
158-
}${chalk.bold.red(t('{count} errors', { count: errorCounter }))}`
159-
)
188+
printStats()
160189
}
161190

162191
return {

0 commit comments

Comments
 (0)