1
1
#!/usr/bin/env node
2
2
3
3
import fs from 'fs'
4
+ import path from 'path'
4
5
import minimist from 'minimist'
5
6
import * as glob from 'glob'
6
7
import gitignore from 'ignore'
@@ -11,8 +12,7 @@ This is zhlint!
11
12
12
13
Usage:
13
14
zhlint <file-pattern>[, ...]
14
- zhlint <file-pattern>[, ...] --fix
15
- zhlint --fix <file-pattern>
15
+ zhlint --fix <file-pattern>[, ...]
16
16
zhlint --fix=<file-pattern>
17
17
zhlint <input-file-path> --output <output-file-path>
18
18
zhlint <input-file-path> --output=<output-file-path>
@@ -42,9 +42,10 @@ Examples:
42
42
zhlint foo.md bar.md
43
43
zhlint foo.md bar.md --fix
44
44
zhlint --fix foo.md
45
- zhlint --fix= foo.md
45
+ zhlint --fix foo.md bar .md
46
46
zhlint --fix *.md
47
- zhlint --fix=*.md
47
+ zhlint --fix *.md *.txt
48
+ zhlint --fix=foo.md
48
49
zhlint foo.md --output dest.md
49
50
zhlint foo.md --output=dest.md
50
51
` . trim ( )
@@ -71,17 +72,28 @@ const main = () => {
71
72
}
72
73
73
74
if ( argv . _ && argv . _ . length ) {
74
- const [ filePattern ] = [ ...argv . _ ]
75
- const configDir = argv . dir
75
+ const filePatterns = [ ...argv . _ ]
76
+ const configDir = argv . dir || process . cwd ( )
76
77
const configPath = argv . config
77
78
const fileIgnorePath = argv . ignore || argv [ 'file-ignore' ]
78
79
const caseIgnorePath = argv [ 'case-ignore' ]
79
80
const config = readRc ( configDir , configPath , fileIgnorePath , caseIgnorePath )
80
81
const fileIgnore = gitignore ( ) . add ( config . fileIgnores )
81
82
const fileIgnoreFilter = fileIgnore . createFilter ( )
82
83
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 ) => {
85
97
console . log ( `[start] ${ file } ` )
86
98
const origin = fs . readFileSync ( file , { encoding : 'utf8' } )
87
99
const { result, validations } = runWithConfig ( origin , config )
@@ -104,8 +116,8 @@ const main = () => {
104
116
)
105
117
}
106
118
} 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 ) {
109
121
fs . writeFileSync ( file , result )
110
122
console . log ( `[fixed] ${ file } ` )
111
123
}
0 commit comments