From 0ebe7ea87cc8090f5a6e07e681a442257aab90f5 Mon Sep 17 00:00:00 2001 From: Jinjiang Date: Sat, 12 Jul 2025 19:04:31 +0800 Subject: [PATCH] feat: support zhlint --fix [, ...] and fix path error - Add support for multiple file patterns with --fix flag - Fix RangeError when using absolute paths with gitignore filter - Update CLI help message and documentation - Improve file pattern processing with deduplication - Fix bug in fix logic (origin vs value comparison) --- README.md | 4 ++-- README.zh-CN.md | 4 ++-- bin/index.js | 32 ++++++++++++++++++++++---------- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 4d49c89..b6da911 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,10 @@ pnpm add zhlint -g ```bash # glob files, lint them, and print validation report, # and exit with code `1` if there is any error found. -zhlint +zhlint [, ...] # glob files and fix their all possilbly found errors. -zhlint --fix +zhlint --fix [, ...] # lint the file and output fixed content into another file zhlint --output= diff --git a/README.zh-CN.md b/README.zh-CN.md index edf0c59..fb3e12e 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -24,10 +24,10 @@ pnpm add zhlint -g ```bash # Glob 文件,执行格式化命令,并打印错误报告, # 如果有任何错误被发现,则会以错误码 `1` 退出。 -zhlint +zhlint [, ...] # Glob 文件,并修复所有可能发现的错误。 -zhlint --fix +zhlint --fix [, ...] # 格式化文件并将修复后的内容输出到另一个文件。 zhlint --output= diff --git a/bin/index.js b/bin/index.js index 7312f13..e089009 100755 --- a/bin/index.js +++ b/bin/index.js @@ -1,6 +1,7 @@ #!/usr/bin/env node import fs from 'fs' +import path from 'path' import minimist from 'minimist' import * as glob from 'glob' import gitignore from 'ignore' @@ -11,8 +12,7 @@ This is zhlint! Usage: zhlint [, ...] - zhlint [, ...] --fix - zhlint --fix + zhlint --fix [, ...] zhlint --fix= zhlint --output zhlint --output= @@ -42,9 +42,10 @@ Examples: zhlint foo.md bar.md zhlint foo.md bar.md --fix zhlint --fix foo.md - zhlint --fix=foo.md + zhlint --fix foo.md bar.md zhlint --fix *.md - zhlint --fix=*.md + zhlint --fix *.md *.txt + zhlint --fix=foo.md zhlint foo.md --output dest.md zhlint foo.md --output=dest.md `.trim() @@ -71,8 +72,8 @@ const main = () => { } if (argv._ && argv._.length) { - const [filePattern] = [...argv._] - const configDir = argv.dir + const filePatterns = [...argv._] + const configDir = argv.dir || process.cwd() const configPath = argv.config const fileIgnorePath = argv.ignore || argv['file-ignore'] const caseIgnorePath = argv['case-ignore'] @@ -80,8 +81,19 @@ const main = () => { const fileIgnore = gitignore().add(config.fileIgnores) const fileIgnoreFilter = fileIgnore.createFilter() try { - const files = glob.sync(filePattern) - const resultList = files.filter(fileIgnoreFilter).map((file) => { + // Process all file patterns + const allFiles = new Set() + filePatterns.forEach(pattern => { + const files = glob.sync(pattern) + files.forEach(file => allFiles.add(file)) + }) + + const files = Array.from(allFiles) + const resultList = files.filter(file => { + // Convert absolute path to relative path for gitignore filter + const relativePath = path.relative(configDir, file) + return fileIgnoreFilter(relativePath) + }).map((file) => { console.log(`[start] ${file}`) const origin = fs.readFileSync(file, { encoding: 'utf8' }) const { result, validations } = runWithConfig(origin, config) @@ -104,8 +116,8 @@ const main = () => { ) } } else if (argv.f || argv.fix) { - resultList.forEach(({ file, value, result }) => { - if (value !== result) { + resultList.forEach(({ file, origin, result }) => { + if (origin !== result) { fs.writeFileSync(file, result) console.log(`[fixed] ${file}`) }