Skip to content

Commit c8678fe

Browse files
authored
Merge pull request #166 from zhlint-project/support-lint-staged
feat: support zhlint --fix <file-pattern>[, ...] and fix path error
2 parents 5046a57 + 0ebe7ea commit c8678fe

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ pnpm add zhlint -g
2424
```bash
2525
# glob files, lint them, and print validation report,
2626
# and exit with code `1` if there is any error found.
27-
zhlint <file-pattern>
27+
zhlint <file-pattern>[, ...]
2828

2929
# glob files and fix their all possilbly found errors.
30-
zhlint <file-pattern> --fix
30+
zhlint --fix <file-pattern>[, ...]
3131

3232
# lint the file and output fixed content into another file
3333
zhlint <input-file-path> --output=<output-file-path>

README.zh-CN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ pnpm add zhlint -g
2424
```bash
2525
# Glob 文件,执行格式化命令,并打印错误报告,
2626
# 如果有任何错误被发现,则会以错误码 `1` 退出。
27-
zhlint <file-pattern>
27+
zhlint <file-pattern>[, ...]
2828

2929
# Glob 文件,并修复所有可能发现的错误。
30-
zhlint <file-pattern> --fix
30+
zhlint --fix <file-pattern>[, ...]
3131

3232
# 格式化文件并将修复后的内容输出到另一个文件。
3333
zhlint <input-file-path> --output=<output-file-path>

bin/index.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env node
22

33
import fs from 'fs'
4+
import path from 'path'
45
import minimist from 'minimist'
56
import * as glob from 'glob'
67
import gitignore from 'ignore'
@@ -11,8 +12,7 @@ This is zhlint!
1112
1213
Usage:
1314
zhlint <file-pattern>[, ...]
14-
zhlint <file-pattern>[, ...] --fix
15-
zhlint --fix <file-pattern>
15+
zhlint --fix <file-pattern>[, ...]
1616
zhlint --fix=<file-pattern>
1717
zhlint <input-file-path> --output <output-file-path>
1818
zhlint <input-file-path> --output=<output-file-path>
@@ -42,9 +42,10 @@ Examples:
4242
zhlint foo.md bar.md
4343
zhlint foo.md bar.md --fix
4444
zhlint --fix foo.md
45-
zhlint --fix=foo.md
45+
zhlint --fix foo.md bar.md
4646
zhlint --fix *.md
47-
zhlint --fix=*.md
47+
zhlint --fix *.md *.txt
48+
zhlint --fix=foo.md
4849
zhlint foo.md --output dest.md
4950
zhlint foo.md --output=dest.md
5051
`.trim()
@@ -71,17 +72,28 @@ const main = () => {
7172
}
7273

7374
if (argv._ && argv._.length) {
74-
const [filePattern] = [...argv._]
75-
const configDir = argv.dir
75+
const filePatterns = [...argv._]
76+
const configDir = argv.dir || process.cwd()
7677
const configPath = argv.config
7778
const fileIgnorePath = argv.ignore || argv['file-ignore']
7879
const caseIgnorePath = argv['case-ignore']
7980
const config = readRc(configDir, configPath, fileIgnorePath, caseIgnorePath)
8081
const fileIgnore = gitignore().add(config.fileIgnores)
8182
const fileIgnoreFilter = fileIgnore.createFilter()
8283
try {
83-
const files = glob.sync(filePattern)
84-
const resultList = files.filter(fileIgnoreFilter).map((file) => {
84+
// Process all file patterns
85+
const allFiles = new Set()
86+
filePatterns.forEach(pattern => {
87+
const files = glob.sync(pattern)
88+
files.forEach(file => allFiles.add(file))
89+
})
90+
91+
const files = Array.from(allFiles)
92+
const resultList = files.filter(file => {
93+
// Convert absolute path to relative path for gitignore filter
94+
const relativePath = path.relative(configDir, file)
95+
return fileIgnoreFilter(relativePath)
96+
}).map((file) => {
8597
console.log(`[start] ${file}`)
8698
const origin = fs.readFileSync(file, { encoding: 'utf8' })
8799
const { result, validations } = runWithConfig(origin, config)
@@ -104,8 +116,8 @@ const main = () => {
104116
)
105117
}
106118
} else if (argv.f || argv.fix) {
107-
resultList.forEach(({ file, value, result }) => {
108-
if (value !== result) {
119+
resultList.forEach(({ file, origin, result }) => {
120+
if (origin !== result) {
109121
fs.writeFileSync(file, result)
110122
console.log(`[fixed] ${file}`)
111123
}

0 commit comments

Comments
 (0)