Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --dryRun option for annotate #43

Merged
merged 1 commit into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,24 @@
"forced applying of attributes": "forced applying of attributes",
"annotated result detail options": "annotated result detail options",
"the attributes to annotate": "the attributes to annotate",
"target files without annotating": "target files without annotating",
"'--type' is not supported except for 'custom-block'": "'--type' is not supported except for 'custom-block'",
"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",
"Unsupported '{type}' block content type: {actual}": "Unsupported '{type}' block content type: {actual}",
"Detected lang mismatch in block `src` ('{src}') and block content ('{content}')": "Detected lang mismatch in block `src` ('{src}') and block content ('{content}')",
"Detected lang mismatch in `lang` option ('{lang}') and block content ('{content}')": "Detected lang mismatch in `lang` option ('{lang}') and block content ('{content}')",
"Detected lang mismatch in `lang` attr ('{lang}') and block content ('{content}')": "Detected lang mismatch in `lang` attr ('{lang}') and block content ('{content}')",
"{count} annotateable files ": "{count} annotateable files ",
"{count} annotates": "{count} annotates",
"{count} pass": "{count} pass",
"{count} warnings": "{count} warnings",
"{count} forces": "{count} forces",
"{count} ignores": "{count} ignores",
"{count} errors": "{count} errors",
"annotate": "annoatte",
"pass annotate": "pass annotate",
"force annotate": "force annotate",
"ignore annotate": "ignore annotate",
"{count} annotateable files ": "annotateable no files | {count} annotateable file | {count} annotateable files",
"{count} annotated files": "annotated no files | {count} annotated file | {count} annotated files",
"{count} passed files": "passed no files | {count} passed file | {count} passed files",
"{count} warned files": "warned no files | {count} warned file | {count} warned files",
"{count} forced files": "forced no files | {count} forced file | {count} forced files",
"{count} ignored files": "ignored no files | {count} ignored file | {count} ignored files",
"{count} error files": "error no files | {count} error file | {count} error files",
"`lang` attr not found": "`lang` attr not found",
"format for single-file components": "format for single-file components",
"the format type": "the format type",
Expand All @@ -37,6 +42,5 @@
"no change": "no change",
"{count} formattable files": "{count} formattable files",
"{count} formatted files": "{count} formatted files",
"{count} no change files": "{count} no change files",
"{count} error files": "{count} error files"
"{count} no change files": "{count} no change files"
}
97 changes: 63 additions & 34 deletions src/commands/annotate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type AnnotateOptions = {
force?: boolean
details?: boolean
attrs?: Record<string, any> // eslint-disable-line @typescript-eslint/no-explicit-any
dryRun?: boolean
}

type AnnoateStatus = 'none' | 'fine' | 'warn' | 'error'
Expand Down Expand Up @@ -53,26 +54,38 @@ export default function defineCommand() {
})
.option('details', {
type: 'boolean',
alias: 'd',
alias: 'v',
describe: t('annotated result detail options')
})
.option('attrs', {
type: 'array',
alias: 'a',
describe: t('the attributes to annotate')
})
.option('dryRun', {
type: 'boolean',
alias: 'd',
describe: t('target files without annotating')
})
.fail(defineFail(RequireError))
}

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

const { source, type, force, details, attrs } = args as AnnotateOptions
debug('annotate args:', source, type, force, details, attrs)
const { source, type, force, details, attrs, dryRun } =
args as AnnotateOptions
debug('annotate args:', source, type, force, details, attrs, dryRun)

checkType(args.type)
checkSource(args._.length, source)

if (dryRun) {
console.log()
console.log(chalk.bold.yellowBright(`${t('dry run mode')}:`))
console.log()
}

let counter = 0
let passCounter = 0
let fineCounter = 0
Expand All @@ -81,6 +94,34 @@ export default function defineCommand() {
let ignoreCounter = 0
let errorCounter = 0

function printStats() {
console.log('')
console.log(
chalk.bold.white(t('{count} annotateable files ', { count: counter }))
)
console.log(
chalk.bold.green(t('{count} annotated files', { count: fineCounter }))
)
if (details) {
console.log(
chalk.white(t('{count} passed files', { count: passCounter }))
)
}
console.log(
chalk.yellow(t('{count} warned files', { count: warnCounter }))
)
if (details) {
console.log(
chalk.yellow(t('{count} forced files', { count: forceCounter }))
)
console.log(
chalk.yellow(t('{count} ignored files', { count: ignoreCounter }))
)
}
console.log(chalk.red(t('{count} error files', { count: errorCounter })))
console.log('')
}

let status: AnnoateStatus = 'fine'
const onWarn = warnHnadler(() => (status = 'warn'))

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

if (status === 'fine') {
console.log(chalk.bold.green(`annotate: ${file}`))
await fs.writeFile(file, annotated, 'utf8')
fineCounter++
console.log(chalk.green(`${file}: ${t('annotate')}`))
if (!dryRun) {
await fs.writeFile(file, annotated, 'utf8')
}
} else if (status === 'none') {
console.log(chalk.white(`pass annotate: ${file}`))
passCounter++
console.log(chalk.white(`${file}: ${t('pass annotate')}`))
} else if (status === 'warn') {
warnCounter++
if (force) {
console.log(chalk.bold.yellow(`force annotate: ${file}`))
await fs.writeFile(file, annotated, 'utf8')
forceCounter++
console.log(chalk.yellow(`${file}: ${t('force annotate')}`))
if (!dryRun) {
await fs.writeFile(file, annotated, 'utf8')
}
} else {
console.log(chalk.yellow(`ignore annotate: ${file}`))
ignoreCounter++
console.log(chalk.yellow(`${file}: ${t('ignore annotate')}`))
}
warnCounter++
}
} catch (e: unknown) {
status = 'error'
errorCounter++
if (isSFCParserError(e)) {
console.error(chalk.bold.red(`${e.message} at ${e.filepath}`))
e.erorrs.forEach(err =>
console.error(chalk.bold.red(` ${err.message}`))
)
e.erorrs.forEach(err => console.error(chalk.red(` ${err.message}`)))
} else if (e instanceof SFCAnnotateError) {
console.error(chalk.bold.red(e.message))
console.error(
chalk.red(`${e.filepath}: ${t(e.message as any)}`) // eslint-disable-line @typescript-eslint/no-explicit-any
)
} else {
console.error(chalk.red((e as Error).message))
}
if (!dryRun) {
throw e
}
}
counter++
}

console.log('')
console.log(
`${chalk.bold.white(
t('{count} annotateable files ', { count: counter })
)}\n${chalk.bold.green(
t('{count} annotates', { count: fineCounter })
)}\n${
details
? `${chalk.white(t('{count} pass', { count: passCounter }))}\n`
: ''
}${chalk.bold.yellow(t('{count} warnings', { count: warnCounter }))}\n${
details
? `${chalk.yellow(t('{count} forces', { count: forceCounter }))}\n`
: ''
}${
details
? `${chalk.yellow(t('{count} ignores', { count: ignoreCounter }))}\n`
: ''
}${chalk.bold.red(t('{count} errors', { count: errorCounter }))}`
)
printStats()
}

return {
Expand Down