(opts: PathOptions)
| 59 | * ``` |
| 60 | */ |
| 61 | export const path = (opts: PathOptions) => { |
| 62 | const validate = opts.validate; |
| 63 | |
| 64 | return autocomplete({ |
| 65 | ...opts, |
| 66 | initialUserInput: opts.initialValue ?? opts.root ?? process.cwd(), |
| 67 | maxItems: 5, |
| 68 | validate(value) { |
| 69 | if (Array.isArray(value)) { |
| 70 | // Shouldn't ever happen since we don't enable `multiple: true` |
| 71 | return undefined; |
| 72 | } |
| 73 | if (!value) { |
| 74 | return 'Please select a path'; |
| 75 | } |
| 76 | if (validate) { |
| 77 | return runValidation(validate, value); |
| 78 | } |
| 79 | return undefined; |
| 80 | }, |
| 81 | options() { |
| 82 | const userInput = this.userInput; |
| 83 | if (userInput === '') { |
| 84 | return []; |
| 85 | } |
| 86 | |
| 87 | try { |
| 88 | let searchPath: string; |
| 89 | |
| 90 | if (!existsSync(userInput)) { |
| 91 | searchPath = dirname(userInput); |
| 92 | } else { |
| 93 | const stat = lstatSync(userInput); |
| 94 | if (stat.isDirectory() && (!opts.directory || userInput.endsWith('/'))) { |
| 95 | searchPath = userInput; |
| 96 | } else { |
| 97 | searchPath = dirname(userInput); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // Strip trailing slash so startsWith matches the directory itself among its siblings |
| 102 | const prefix = |
| 103 | userInput.length > 1 && userInput.endsWith('/') ? userInput.slice(0, -1) : userInput; |
| 104 | |
| 105 | const items = readdirSync(searchPath) |
| 106 | .map((item) => { |
| 107 | const path = join(searchPath, item); |
| 108 | const stats = lstatSync(path); |
| 109 | return { |
| 110 | name: item, |
| 111 | path, |
| 112 | isDirectory: stats.isDirectory(), |
| 113 | }; |
| 114 | }) |
| 115 | .filter( |
| 116 | ({ path, isDirectory }) => path.startsWith(prefix) && (isDirectory || !opts.directory) |
| 117 | ); |
| 118 |
nothing calls this directly
no test coverage detected