-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
163 lines (154 loc) · 5.25 KB
/
Copy pathmain.ts
File metadata and controls
163 lines (154 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* gwt - Git Worktree Manager
* Copyright (C) 2026 Guillermo G. Almazor <guille@ggalmazor.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { Command } from '@cliffy/command';
import { listCommand } from './src/commands/list.ts';
import { createCommand, createWorktreeNonInteractive } from './src/commands/create.ts';
import { deleteCommand } from './src/commands/delete.ts';
import { configCommand } from './src/commands/config.ts';
import { openCommand } from './src/commands/open.ts';
import { cleanCommand } from './src/commands/clean.ts';
import { upgradeCommand } from './src/commands/upgrade.ts';
import { getConfigPath, loadConfig } from './src/config/manager.ts';
import {
checkForUpdates,
displayUpdateNotification,
needsUpdateCheck,
shouldCheckForUpdates,
touchFile,
} from './src/utils/version-checker.ts';
import { VERSION } from './src/version.ts';
/**
* Check for updates if enabled and needed.
*/
async function checkForUpdatesIfNeeded(): Promise<void> {
try {
const config = await loadConfig();
if (!config || !shouldCheckForUpdates(config)) {
return;
}
const configPath = await getConfigPath();
if (!configPath) {
return;
}
const needsCheck = await needsUpdateCheck(configPath);
if (!needsCheck) {
return;
}
const updateInfo = await checkForUpdates(VERSION);
if (updateInfo) {
displayUpdateNotification(updateInfo);
}
} catch {
// Silently ignore update check errors
}
}
/**
* Touch config file to update its modification time.
*/
async function touchConfigFile(): Promise<void> {
try {
const configPath = await getConfigPath();
if (configPath) {
await touchFile(configPath);
}
} catch {
// Silently ignore errors
}
}
/**
* Run a command with update check before and config file touch after.
*/
async function runCommand(fn: () => Promise<void>): Promise<void> {
await checkForUpdatesIfNeeded();
try {
await fn();
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.error(`Error: ${message}`);
Deno.exit(1);
} finally {
await touchConfigFile();
}
}
const program = new Command()
.name('gwt')
.version(VERSION)
.description('Git Worktree Manager - Manage git worktrees with ease')
.action(function () {
this.showHelp();
})
.command('list', 'List all worktrees')
.alias('ls')
.action(() => runCommand(listCommand))
.command('create', 'Create a new worktree')
.alias('add')
.option('--branch <branch:string>', 'Existing branch to check out')
.option('--new-branch <newBranch:string>', 'Create a new branch')
.option('--base <base:string>', 'Base branch for the new branch')
.option('--path <path:string>', 'Worktree path')
.option('--no-editor', 'Skip editor launch')
.option('--open-editor', 'Open the editor without prompting', { conflicts: ['no-editor'] })
.action((options) =>
runCommand(() => {
const noEditor = options.editor === false;
const openEditor = options.openEditor === true;
if (options.path && (options.branch || options.newBranch)) {
return createWorktreeNonInteractive({
branch: options.branch,
path: options.path,
newBranch: options.newBranch,
base: options.base,
noEditor,
openEditor,
});
}
return createCommand({ noEditor, openEditor });
})
)
.command('delete [target:string]', 'Delete one or more worktrees (multi-select if no target)')
.alias('remove')
.option('--force', 'Skip confirmation prompt')
.action((options, target?: string) =>
runCommand(() => deleteCommand(target, { force: options.force }))
)
.command('open [target:string]', 'Open a worktree in your configured editor')
.option('--no-editor', 'Skip editor launch')
.option('--open-editor', 'Open the editor without prompting', { conflicts: ['no-editor'] })
.action((options, target?: string) =>
runCommand(() =>
openCommand(target, {
noEditor: options.editor === false,
openEditor: options.openEditor === true,
})
)
)
.command('clean', 'Remove orphaned worktree directories')
.option('--all', 'Clean all orphaned directories without prompting')
.action((options) => runCommand(() => cleanCommand({ all: options.all })))
.command('config', configCommand)
.command('upgrade', 'Check for a new version and print upgrade instructions')
.action(async () => {
try {
await upgradeCommand();
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.error(`Error: ${message}`);
Deno.exit(1);
}
});
await program.parse(Deno.args);