Combine telescope and toggleterm to run npm and bash scripts

I’d like to add something to my config that helps me to run scripts:

Ideally I’d want something that discovers the scripts (for example bash scripts in some folders and npm scripts) and lists them in a telescope window.
Then from the telescope window, I’d want to start them with toggleterm.

For now I have some hard code things in toggleterm like:

local npm_run_dev = Terminal:new({ 
  cmd = "npm run dev",
  hidden = true,
  direction = "float",
  on_open = function(term)
    vim.api.nvim_buf_set_keymap(term.bufnr, "t", "q", "<cmd>close<CR>", {noremap = true, silent = true})
  end,
})

function _npm_run_dev_toggle()
  npm_run_dev:toggle()
end

but I want to fill these automatically.

So I think I need some things:

  1. a way to find all bash scripts in some folders
  2. a way to find all npm scripts
  3. populate telescope with this
  4. tell telescope to execute the script with a Terminal:new like command. Ideally the toggle function, so that long running script get reopened instead of started again.

I guess this should be possible, but I have no idea on how to build something like this. Can anyone help or point me in the right direction?

Thansk

Hello, what I would do.

Checkout telescope builtin pickers, you can use the find_files telescope.nvim/telescope.txt at 1c57cc6140644695f0d9bd71b63de45feeca6ae7 · nvim-telescope/telescope.nvim · GitHub There’s an option for search_dirs put the difectoies to find your files there, and by using find_command arguments you can specify ripgrep to filter and only find some file types.

After this you will be able to find your scripts, what you need to do next is add an action to the telescope picker (find_files). Actions are callbacks that have the entry information, here you will have all the information you need to write your logic of launching the script, etc.

Also recommend reading telescope docs, and this guide telescope.nvim/developers.md at master · nvim-telescope/telescope.nvim · GitHub

1 Like