Skip to content

Commit

Permalink
fix(cli): interative mode's path validation should accept glob pattern
Browse files Browse the repository at this point in the history
closes #81
  • Loading branch information
pionxzh committed Dec 30, 2023
1 parent 0b51e23 commit 7af0bab
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,13 @@ async function interactive({
validate(value) {
if (!value) return 'Please enter a file path'

const inputPath = path.resolve(value)
if (!fsa.existsSync(inputPath)) return 'Input does not exist'
if (!fsa.statSync(inputPath).isFile()) return 'Input is not a file'
if (!isPathInside(cwd, inputPath)) return 'Input is outside of the current working directory'
if (fsa.existsSync(value) && fsa.statSync(value).isDirectory()) {
return 'Input is a directory. If you want to include all files in the directory, use a glob pattern (e.g. ./folder/**/*.js)'
}

const resolvedPaths = resolveGlob(value).filter(p => fsa.existsSync(p) && fsa.statSync(p).isFile())
if (resolvedPaths.length === 0) return 'No files matched'
if (resolvedPaths.some(p => !isPathInside(cwd, p))) return 'Input is outside of the current working directory'

return undefined
},
Expand All @@ -222,7 +225,7 @@ async function interactive({
return process.exit(0)
}

inputPaths = resolveGlob(rawInputPath)
inputPaths = resolveGlob(rawInputPath).filter(p => fsa.existsSync(p) && fsa.statSync(p).isFile())
}

let outputPath = outputBase
Expand Down Expand Up @@ -312,9 +315,13 @@ async function interactive({
validate(value) {
if (!value) return 'Please enter a file path'

const resolvedPaths = resolveGlob(value)
if (fsa.existsSync(value) && fsa.statSync(value).isDirectory()) {
return 'Input is a directory. If you want to include all files in the directory, use a glob pattern (e.g. ./folder/**/*.js)'
}

const resolvedPaths = resolveGlob(value).filter(p => fsa.existsSync(p) && fsa.statSync(p).isFile())
if (resolvedPaths.length === 0) return 'No files matched'
if (!isPathInside(cwd, value)) return 'Input is outside of the current working directory'
if (resolvedPaths.some(p => !isPathInside(cwd, p))) return 'Input is outside of the current working directory'

return undefined
},
Expand Down

0 comments on commit 7af0bab

Please sign in to comment.