11#!/usr/bin/env node
22
33import fs from 'fs'
4+ import path from 'path'
45import minimist from 'minimist'
56import * as glob from 'glob'
67import gitignore from 'ignore'
@@ -11,8 +12,7 @@ This is zhlint!
1112
1213Usage:
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