Skip to content
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <file-pattern>
zhlint <file-pattern>[, ...]

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

# lint the file and output fixed content into another file
zhlint <input-file-path> --output=<output-file-path>
Expand Down
4 changes: 2 additions & 2 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ pnpm add zhlint -g
```bash
# Glob 文件,执行格式化命令,并打印错误报告,
# 如果有任何错误被发现,则会以错误码 `1` 退出。
zhlint <file-pattern>
zhlint <file-pattern>[, ...]

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

# 格式化文件并将修复后的内容输出到另一个文件。
zhlint <input-file-path> --output=<output-file-path>
Expand Down
32 changes: 22 additions & 10 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -11,8 +12,7 @@ This is zhlint!

Usage:
zhlint <file-pattern>[, ...]
zhlint <file-pattern>[, ...] --fix
zhlint --fix <file-pattern>
zhlint --fix <file-pattern>[, ...]
zhlint --fix=<file-pattern>
zhlint <input-file-path> --output <output-file-path>
zhlint <input-file-path> --output=<output-file-path>
Expand Down Expand Up @@ -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()
Expand All @@ -71,17 +72,28 @@ 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']
const config = readRc(configDir, configPath, fileIgnorePath, caseIgnorePath)
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)
Expand All @@ -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}`)
}
Expand Down