diff --git a/README.md b/README.md index 0c4bc1e..28cbb8d 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,13 @@ git tidy git work add ``` +- list the current repo worktrees, select one to print its path + +```bash +git work list +cd "$(git work list)" # jump to the selected worktree +``` + - list the current repo worktrees, multi select the ones you want to remove ```bash diff --git a/package.json b/package.json index 6cb73db..22d1a94 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@labset/git-workspaces", - "version": "5.0.0", + "version": "5.1.0", "description": "git workspace cli extensions", "preferGlobal": false, "bin": { diff --git a/src/bin/git-work.ts b/src/bin/git-work.ts index 5855e67..c207d74 100644 --- a/src/bin/git-work.ts +++ b/src/bin/git-work.ts @@ -21,7 +21,8 @@ import { addWorktree, handleCliError, listWorktrees, - selectAndRemoveWorktrees + selectAndRemoveWorktrees, + selectWorktree } from '../lib'; const program = new Command(); @@ -35,6 +36,14 @@ program await addWorktree(name); }); +program + .command('list') + .description('list worktrees and select one to print its path') + .action(async () => { + const selected = await listWorktrees().then(selectWorktree); + if (selected) console.log(selected); + }); + program .command('remove') .description('list worktrees and select the ones to remove') diff --git a/src/lib/worktrees/index.ts b/src/lib/worktrees/index.ts index 84b5119..fac1667 100644 --- a/src/lib/worktrees/index.ts +++ b/src/lib/worktrees/index.ts @@ -16,3 +16,4 @@ export * from './list-worktrees'; export * from './add-worktree'; export * from './select-and-remove-worktrees'; +export * from './select-worktree'; diff --git a/src/lib/worktrees/select-worktree.ts b/src/lib/worktrees/select-worktree.ts new file mode 100644 index 0000000..3ead8ca --- /dev/null +++ b/src/lib/worktrees/select-worktree.ts @@ -0,0 +1,43 @@ +/** + * Copyright 2023 Hasnae Rehioui + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import inquirer from 'inquirer'; + +import { Worktree } from './list-worktrees'; + +export const selectWorktree = async ( + worktrees: Worktree[] +): Promise => { + if (worktrees.length === 0) return undefined; + // Prompt UI goes to stderr so stdout carries only the selected path and + // can be used as `cd "$(git work list)"`. + const answer = await inquirer.createPromptModule({ + output: process.stderr + })([ + { + name: 'selectedPath', + type: 'select', + message: 'choose a worktree', + choices: worktrees.map((worktree) => ({ + name: worktree.branch + ? `${worktree.path} (${worktree.branch})` + : worktree.path, + value: worktree.path + })), + pageSize: 10 + } + ]); + return answer.selectedPath; +};