Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Suggestion] Autocomplete directory path #249

Open
OleksandrKucherenko opened this issue Jun 2, 2023 · 0 comments
Open

[Suggestion] Autocomplete directory path #249

OleksandrKucherenko opened this issue Jun 2, 2023 · 0 comments

Comments

@OleksandrKucherenko
Copy link

import { glob } from 'glob'
import fs from 'node:fs'

const cwd = {
    message: `Current working directory (ex. ~/project, ../project, ./project):`,
    type: `autocomplete`,
    limit: 10,
    choices: [
      /* force prompts to render 10 lines */
      { title: `Current working directory`, value: process.cwd() },
      { title: `Parent directory`, value: `../` },
      { title: `Grand directory`, value: `../../` },
      { title: `Home directory`, value: `~` },
      { title: `Root directory`, value: `/` },
      { title: `reserved-1`, value: `./` },
      { title: `reserved-2`, value: `./` },
      { title: `reserved-3`, value: `./` },
      { title: `reserved-4`, value: `./` },
      { title: `reserved-5`, value: `./` },
    ],
    suggest: async (input, _choices) => {
      const cwd = process.cwd()
      const start = (input.length === 0 ? cwd : input).replace('~', process.env.HOME)
      const files = await glob(`${start}*`, { cwd, maxDepth: 1 })
      const value = files?.[0] ?? start

      const suggestions = files ?? [value]

      return suggestions
        .filter((f) => f !== '.' && f !== '..')
        .filter((f) => {
          try {
            return fs.lstatSync(f).isDirectory()
          } catch (error) {
            return false
          }
        })
        .map((f) => ({ title: f }))
        .slice(0, 10)
    },
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant