@joberthel/worktree 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +315 -2
- package/dist/cli-spec.js +138 -0
- package/dist/cli.js +264 -0
- package/dist/completion.js +178 -0
- package/dist/config/load.js +46 -0
- package/dist/config/schema.js +57 -0
- package/dist/docker/compose.js +253 -0
- package/dist/git/repository.js +196 -0
- package/dist/operations/attached.js +294 -0
- package/dist/operations/commands.js +158 -0
- package/dist/operations/confirmation.js +15 -0
- package/dist/operations/copy-paths.js +109 -0
- package/dist/operations/create.js +255 -0
- package/dist/operations/hooks.js +113 -0
- package/dist/operations/manage.js +446 -0
- package/dist/operations/manage.test/fixture.js +67 -0
- package/dist/operations/process.js +122 -0
- package/dist/operations/progress.js +28 -0
- package/dist/state/atomic-file.js +14 -0
- package/dist/state/store.js +162 -0
- package/dist/ui/confirmation-prompt.js +48 -0
- package/dist/ui/environment-info.js +85 -0
- package/dist/ui/environment-table.js +43 -0
- package/dist/ui/format-status.js +29 -0
- package/dist/ui/hook-log-panel.js +57 -0
- package/dist/ui/json-output.js +41 -0
- package/dist/ui/operation-view.js +33 -0
- package/dist/ui/progress-state.js +111 -0
- package/dist/ui/progress.js +16 -0
- package/dist/ui/show-operation.js +56 -0
- package/dist/ui/use-progress.js +34 -0
- package/package.json +72 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Johannes Berthel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,316 @@
|
|
|
1
|
-
#
|
|
1
|
+
# wt
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`wt` creates and manages isolated development environments from Git worktrees. It pairs each worktree with its own Docker Compose project, copies selected files from the source checkout, and clones eligible named Docker volumes so you can work on multiple branches without sharing mutable app data.
|
|
4
|
+
|
|
5
|
+
## How it works
|
|
6
|
+
|
|
7
|
+
- A worktree is created under the directory configured by `worktrees.directory` (default: `../worktrees`).
|
|
8
|
+
- By default, the worktree name is also used as the new Git branch name. Slash-separated names such as `feature/login` keep that exact branch name and use a flat directory such as `feature--login`.
|
|
9
|
+
- Each worktree gets a separate Compose project name and a separate set of cloned named volumes.
|
|
10
|
+
- `wt` discovers the running Compose project for the current checkout. Start that project with the Git checkout root as the Compose project directory before creating a worktree.
|
|
11
|
+
- State and Compose volume overrides are stored in the repository's shared Git metadata, so they are available from any linked worktree.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
Supported platforms are macOS and Linux. You need Node.js 22 or newer, Git, and Docker Compose 2.24.4 or newer. Install the CLI globally from npm:
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install --global @joberthel/worktree
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
To install from a clone of this repository instead, build and install a packaged copy with pnpm:
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
pnpm install
|
|
25
|
+
pnpm build
|
|
26
|
+
pnpm pack --out /tmp/wt.tgz
|
|
27
|
+
pnpm add --global /tmp/wt.tgz
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Check the installation with:
|
|
31
|
+
|
|
32
|
+
```sh
|
|
33
|
+
wt --help
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Quick start
|
|
37
|
+
|
|
38
|
+
From a project checkout with its Docker Compose environment running:
|
|
39
|
+
|
|
40
|
+
```sh
|
|
41
|
+
wt create feature/login
|
|
42
|
+
wt ls
|
|
43
|
+
cd ../worktrees/feature--login
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The new branch is `feature/login` and the worktree directory is `../worktrees/feature--login`. To use an existing branch instead, provide a separate worktree name and `--branch`:
|
|
47
|
+
|
|
48
|
+
```sh
|
|
49
|
+
wt create login-review --branch feature/login
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
`--branch` accepts a local branch or a remote one. A remote branch such as `origin/feature/login` is checked out as a new local branch `feature/login` that tracks it; if that local branch already exists, it is used as it is. A bare name that is not a local branch is looked up on your remotes and used when exactly one remote has it.
|
|
53
|
+
|
|
54
|
+
A remote branch that has not been fetched yet is fetched automatically. This also works in clones that only track some branches (for example `git clone --single-branch`): the branch is added to the remote's fetch configuration. A shallow clone keeps its existing shallow boundaries when `wt` fetches another branch. Fetching a new branch can bring in more history for that branch.
|
|
55
|
+
|
|
56
|
+
To create a new branch from a ref other than `HEAD`:
|
|
57
|
+
|
|
58
|
+
```sh
|
|
59
|
+
wt create feature/login --from main
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
`--branch` and `--from` cannot be used together.
|
|
63
|
+
|
|
64
|
+
## Commands
|
|
65
|
+
|
|
66
|
+
Run commands from any worktree in the same repository:
|
|
67
|
+
|
|
68
|
+
| Command | What it does |
|
|
69
|
+
| --- | --- |
|
|
70
|
+
| `wt create <name> [--branch <branch> \| --from <ref>]` | Create a worktree, branch, and isolated Compose environment. |
|
|
71
|
+
| `wt ls` | List managed worktrees and their Docker status. |
|
|
72
|
+
| `wt info [name]` | Print the saved environment metadata. |
|
|
73
|
+
| `wt start [name]` | Start its Compose services. |
|
|
74
|
+
| `wt stop [name]` | Stop its Compose services. |
|
|
75
|
+
| `wt restart [name]` | Restart its Compose services. |
|
|
76
|
+
| `wt docker [name] -- <compose args...>` | Run a Docker Compose command in that worktree's project. |
|
|
77
|
+
| `wt reset [name] [--force]` | Stop the environment, recopy configured paths, refresh cloned volumes, and start it again. |
|
|
78
|
+
| `wt rm <name> [--force]` | Remove the managed environment and Git worktree while keeping its branch. |
|
|
79
|
+
| `wt completion <zsh\|bash>` | Print a shell completion script. |
|
|
80
|
+
|
|
81
|
+
Commands with an optional `[name]` default to the managed worktree that contains the current directory.
|
|
82
|
+
|
|
83
|
+
Pass `-w <dir>` (or `--working-dir <dir>`) to run a command as if `wt` was started in that directory, for example for a repository in a subdirectory. It can go before or after the command, but not after `--`:
|
|
84
|
+
|
|
85
|
+
```sh
|
|
86
|
+
wt -w apps/shop create feature/login
|
|
87
|
+
wt ls --working-dir apps/shop
|
|
88
|
+
wt docker -w apps/shop feature/login -- logs -f app
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Shell completion
|
|
92
|
+
|
|
93
|
+
Tab completes commands, options, worktree names (shown with their branch in zsh), branches after `--branch` and `--from`, and directories after `-w`. Load it from your shell's startup file:
|
|
94
|
+
|
|
95
|
+
```sh
|
|
96
|
+
# ~/.zshrc, after compinit
|
|
97
|
+
source <(wt completion zsh)
|
|
98
|
+
|
|
99
|
+
# ~/.bashrc
|
|
100
|
+
eval "$(wt completion bash)"
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
In zsh you can instead save the script as `_wt` in a directory on your `fpath`. In bash, use `eval` as shown: the bash 3.2 that ships with macOS cannot `source` a process substitution. Worktree names are read from `wt`'s state files. Only `wt start`, `wt stop` and `wt restart` also ask Docker, to offer just the worktrees that are stopped or running. If Docker does not answer within 1.5 seconds, they offer every worktree.
|
|
104
|
+
|
|
105
|
+
### JSON output
|
|
106
|
+
|
|
107
|
+
Every command except `wt docker` accepts `--format json` (or `--format=json`) to print its result as JSON on stdout. Use it from scripts and coding agents instead of parsing the tables. Progress still goes to stderr, and errors are printed to stderr as text with a non-zero exit status.
|
|
108
|
+
|
|
109
|
+
```sh
|
|
110
|
+
wt ls --format json
|
|
111
|
+
wt info feature/login --format json | jq -r .path
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
`wt ls` prints an array of environments. `wt info` prints one environment. Each container has its `name`, Compose `service`, Docker `state` (such as `running` or `exited`) and human-readable `status` (such as `Up 2 hours (healthy)`). Values that are absent, such as the branch of a detached checkout, are `null`.
|
|
115
|
+
|
|
116
|
+
```json
|
|
117
|
+
[
|
|
118
|
+
{
|
|
119
|
+
"name": "feature/login",
|
|
120
|
+
"branch": "feature/login",
|
|
121
|
+
"path": "/code/worktrees/feature--login",
|
|
122
|
+
"status": "ready",
|
|
123
|
+
"dockerStatus": "running",
|
|
124
|
+
"projectName": "wt-1a2b3c-feature--login"
|
|
125
|
+
}
|
|
126
|
+
]
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
`create`, `start`, `stop`, `restart`, `reset`, and `rm` print `{ "name", "action", "completed" }`. `create` also includes `path` and `projectName`. A declined `reset` or `rm` prints `"completed": false` and exits with status 0. Without a terminal, `reset` and `rm` need `--force`.
|
|
130
|
+
|
|
131
|
+
For example, inspect or run a command against a worktree's Compose project:
|
|
132
|
+
|
|
133
|
+
```sh
|
|
134
|
+
wt docker feature/login -- ps
|
|
135
|
+
wt docker feature/login -- logs -f app
|
|
136
|
+
|
|
137
|
+
# from inside the feature/login worktree
|
|
138
|
+
wt docker -- exec app sh
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
`reset` and `rm` ask before destructive changes. Pass `--force` to skip the confirmation:
|
|
142
|
+
|
|
143
|
+
```sh
|
|
144
|
+
wt reset feature/login --force
|
|
145
|
+
wt rm feature/login --force
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Long-running commands show the current step and completed steps, including when each configured hook starts and finishes. For example, the progress view identifies `worktree.before_create` and `source.after_create` hooks by their executable path or as an inline script. Confirmation prompts list the directory, Compose project, paths, and volumes that are affected. Cancel is preselected. Use the arrow keys or `y` to select an action, then press Enter. Esc or `n` cancels. When stdout is redirected, type `yes` at the prompt on stderr to proceed.
|
|
149
|
+
|
|
150
|
+
## Configuration
|
|
151
|
+
|
|
152
|
+
Put `.wt.yml` at the root of the source checkout or the main checkout. If the source checkout has no config file, `wt create` uses the main checkout’s file. Reset reloads the same configuration path used at creation. If that file is missing, restore it before retrying. Defaults are an adjacent `../worktrees` directory, empty copy paths, and empty hooks.
|
|
153
|
+
|
|
154
|
+
```yaml
|
|
155
|
+
worktrees:
|
|
156
|
+
directory: ../worktrees
|
|
157
|
+
|
|
158
|
+
copy:
|
|
159
|
+
- .env
|
|
160
|
+
- storage
|
|
161
|
+
|
|
162
|
+
hooks:
|
|
163
|
+
worktree:
|
|
164
|
+
before_create:
|
|
165
|
+
script: |
|
|
166
|
+
set -eu
|
|
167
|
+
echo "Preparing $WORKTREE_NAME at $WORKTREE_PATH"
|
|
168
|
+
# Add any per-worktree setup here.
|
|
169
|
+
after_create:
|
|
170
|
+
script: |
|
|
171
|
+
set -eu
|
|
172
|
+
echo "Started $COMPOSE_PROJECT_NAME"
|
|
173
|
+
source:
|
|
174
|
+
after_create: scripts/register-worktree.sh
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### `worktrees.directory`
|
|
178
|
+
|
|
179
|
+
The directory where worktrees are created. Relative paths are resolved from the main checkout, or from the parent of the Git directory for a bare repository. When an existing linked checkout uses a separate Git directory and Git cannot identify its main checkout, `wt` uses the main checkout recorded in its state or falls back to the current checkout. The default is `../worktrees`.
|
|
180
|
+
|
|
181
|
+
### `copy`
|
|
182
|
+
|
|
183
|
+
Relative files and directories copied from the source checkout into each new worktree. `wt reset` replaces these paths with fresh copies from the source checkout. Paths must stay inside the repository, and overlapping entries are not allowed. Use this for files such as `.env` or seed data; Docker volumes are handled separately.
|
|
184
|
+
|
|
185
|
+
### `hooks`
|
|
186
|
+
|
|
187
|
+
Hooks run while an environment is created by `wt create` and again when it is rebuilt by `wt reset`. They do not run for `wt start`, `wt stop`, or `wt restart`. There are two events:
|
|
188
|
+
|
|
189
|
+
- `before_create`: after the worktree, copied files, and cloned volumes are in place, before Docker Compose starts the services.
|
|
190
|
+
- `after_create`: after Docker Compose has started the services.
|
|
191
|
+
|
|
192
|
+
Hooks are grouped by where they run:
|
|
193
|
+
|
|
194
|
+
- `hooks.worktree`: in the new worktree directory. Use these for per-worktree setup.
|
|
195
|
+
- `hooks.source`: in the source checkout, which is the checkout `wt create` was run from and the same directory as `SOURCE_WORKTREE_PATH`. Use these for work that belongs to the originating repository, such as dumping data or registering the new environment in a shared proxy config.
|
|
196
|
+
|
|
197
|
+
A hook can be an executable path or an inline shell script:
|
|
198
|
+
|
|
199
|
+
```yaml
|
|
200
|
+
hooks:
|
|
201
|
+
worktree:
|
|
202
|
+
before_create: scripts/before-create.sh
|
|
203
|
+
after_create:
|
|
204
|
+
script: |
|
|
205
|
+
./bin/clear-cache
|
|
206
|
+
source:
|
|
207
|
+
after_create:
|
|
208
|
+
script: |
|
|
209
|
+
echo "$WORKTREE_NAME is up at $WORKTREE_PATH" >> .wt-environments.log
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Executable paths are relative to the directory the hook runs in. Inline scripts run with `/bin/sh` and need `set -e` if they should stop at the first failure. Hooks run with stdin closed and no terminal. Redirect background jobs’ output to a file if you need to keep their logs. Hooks execute repository commands during create and reset, so use them with repositories you trust. Source hooks wrap the worktree hooks in this order.
|
|
213
|
+
|
|
214
|
+
1. `source.before_create`
|
|
215
|
+
2. `worktree.before_create`
|
|
216
|
+
3. Docker Compose startup
|
|
217
|
+
4. `worktree.after_create`
|
|
218
|
+
5. `source.after_create`
|
|
219
|
+
|
|
220
|
+
Because `wt reset` runs every hook again, keep them safe to repeat. Executable source hooks are checked before anything is created or reset.
|
|
221
|
+
|
|
222
|
+
All hooks receive these environment variables:
|
|
223
|
+
|
|
224
|
+
- `WORKTREE_NAME`: a Docker-safe slug derived from the worktree name (for example, `feature/login` becomes `feature--login`).
|
|
225
|
+
- `WORKTREE_ORIGINAL_NAME`: the name accepted by `wt`, including any slashes.
|
|
226
|
+
- `WORKTREE_BRANCH`: the checked-out Git branch.
|
|
227
|
+
- `WORKTREE_PATH`: absolute path to the worktree.
|
|
228
|
+
- `WORKTREE_COMPOSE_PROJECT_NAME`: Compose project name of the new environment.
|
|
229
|
+
- `SOURCE_WORKTREE_PATH`: absolute path to the source checkout.
|
|
230
|
+
- `SOURCE_COMPOSE_PROJECT_NAME`: Compose project name of the running source environment.
|
|
231
|
+
- `WORKTREE_COMPOSE_FILE` and `SOURCE_COMPOSE_FILE` give the Compose files for each project.
|
|
232
|
+
- `COMPOSE_PROJECT_NAME` and `COMPOSE_FILE` identify the project in the hook’s checkout. A worktree hook can use plain `docker compose run` or `docker compose exec` and will use the cloned volumes.
|
|
233
|
+
|
|
234
|
+
The slug is useful in hostnames and resource names. Use the original name for commands such as `wt info "$WORKTREE_ORIGINAL_NAME"`.
|
|
235
|
+
|
|
236
|
+
## Worktrees of a nested repository
|
|
237
|
+
|
|
238
|
+
Some repositories only run inside another one. A Shopware plugin, for example, lives in its own Git repository under `custom/plugins/` of a Shopware checkout and needs that checkout's Docker environment. Set `host` in the nested repository's `.wt.yml` to work on it with `wt`:
|
|
239
|
+
|
|
240
|
+
```yaml
|
|
241
|
+
# custom/plugins/SwagExample/.wt.yml
|
|
242
|
+
host:
|
|
243
|
+
path: ../../.. # root of the host checkout, relative to this checkout
|
|
244
|
+
from: trunk # optional host ref to check out; defaults to the host checkout's HEAD
|
|
245
|
+
|
|
246
|
+
copy:
|
|
247
|
+
- .env.local
|
|
248
|
+
|
|
249
|
+
hooks:
|
|
250
|
+
worktree:
|
|
251
|
+
after_create:
|
|
252
|
+
script: |
|
|
253
|
+
set -eu
|
|
254
|
+
docker compose exec -T web bin/console plugin:refresh
|
|
255
|
+
docker compose exec -T web bin/console plugin:install --activate --reinstall SwagExample
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
Run the commands from the nested repository:
|
|
259
|
+
|
|
260
|
+
```sh
|
|
261
|
+
cd custom/plugins/SwagExample
|
|
262
|
+
wt create feature/checkout
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
`wt create` then:
|
|
266
|
+
|
|
267
|
+
1. Creates a worktree of the host repository with a detached HEAD, so the host repository gets no new branch. It uses the host’s `.wt.yml` for the worktree directory, copied files, cloned volumes, and hooks, just like `wt create` in the host checkout. The host worktree is named after the nested checkout’s directory and the worktree name, for example `swagexample/feature/checkout`.
|
|
268
|
+
2. Adds a worktree of the nested repository on the new branch `feature/checkout` at the same relative path inside the host worktree, such as `custom/plugins/SwagExample`. The host’s Compose bind mounts expose it to the containers.
|
|
269
|
+
3. Copies the nested repository’s `copy` paths into it.
|
|
270
|
+
4. Starts the host environment. Hooks run in this order: the host’s before hooks, the nested repository’s before hooks, Docker Compose startup, the host’s after hooks, then the nested repository’s after hooks. The host is fully set up before the nested hooks run.
|
|
271
|
+
|
|
272
|
+
The nested repository’s hooks run in its own checkouts: `hooks.worktree` in the new nested worktree and `hooks.source` in the checkout `wt create` was run from. `COMPOSE_PROJECT_NAME` and `COMPOSE_FILE` address the host environment, so plain `docker compose exec` reaches its containers. `WORKTREE_NAME`, `WORKTREE_BRANCH`, and `WORKTREE_PATH` describe the nested worktree. These variables describe the host worktree:
|
|
273
|
+
|
|
274
|
+
- `HOST_WORKTREE_NAME`: its Docker-safe slug, such as `swagexample--feature--checkout`.
|
|
275
|
+
- `HOST_WORKTREE_ORIGINAL_NAME`: its name.
|
|
276
|
+
- `HOST_WORKTREE_PATH`: its absolute path.
|
|
277
|
+
- `HOST_SOURCE_WORKTREE_PATH`: the host checkout it was created from.
|
|
278
|
+
|
|
279
|
+
The host hooks see the host worktree as usual. Their `WORKTREE_BRANCH` is empty because the host worktree is detached.
|
|
280
|
+
|
|
281
|
+
From the nested repository, `wt ls`, `info`, `start`, `stop`, `restart`, and `docker` work on the host environment. `wt reset` refreshes the host environment and the nested `copy` paths and runs both sets of hooks again. `wt rm` removes the nested worktree first, then the host worktree and its Docker resources. The nested branch is kept. The confirmation shows how many files in the nested worktree have uncommitted changes.
|
|
282
|
+
|
|
283
|
+
The host repository lists the host worktree as detached. It refuses `wt rm` and `wt reset` for that worktree, because they would delete the nested worktree or skip its hooks. Run those commands from the nested repository instead.
|
|
284
|
+
|
|
285
|
+
`host` cannot be combined with `worktrees`, because the host’s configuration sets the worktree directory. The host checkout must not set `host` itself. The host repository should ignore the nested path, as Shopware does for `custom/plugins`. Otherwise the nested worktree shows up as untracked files in the host worktree. `wt create` stops if the host repository tracks files at that path.
|
|
286
|
+
|
|
287
|
+
## Docker volumes
|
|
288
|
+
|
|
289
|
+
`wt` copies non-external named volumes declared in the source Compose configuration, including volumes that currently have no container attached. Reset can read volumes the source project retained after `docker compose down`. If source data is missing, reset stops before tearing down the target or removing its old volumes. External volumes remain shared. Anonymous volumes start empty and are removed when you reset or remove a worktree.
|
|
290
|
+
|
|
291
|
+
The volume copy runs in a digest-pinned Debian image. Set `WT_VOLUME_COPY_IMAGE` to use a different image that contains Bash and GNU tar with sparse file, extended attribute, and ACL support.
|
|
292
|
+
|
|
293
|
+
Stop writes or stop the source services before cloning a database volume if you need a consistent snapshot. Use dynamic host ports and omit fixed `container_name` values when running the source and worktree projects side by side.
|
|
294
|
+
|
|
295
|
+
## Recovering an environment
|
|
296
|
+
|
|
297
|
+
After a partial create or reset, run `wt info <name>` to see the saved error. Fix the issue and rerun `wt reset <name>` to retry setup, or run `wt rm <name>` to remove the checkout and its Docker resources. Removing a source worktree leaves its children running, but they need that source to reset. Reuse the branch from a removed worktree with `wt create <name> --branch <branch>`.
|
|
298
|
+
|
|
299
|
+
If the process is killed before it can release its lock, verify that the process shown in the error has exited before removing the lock file named in the error.
|
|
300
|
+
|
|
301
|
+
## Development checks
|
|
302
|
+
|
|
303
|
+
```sh
|
|
304
|
+
pnpm build
|
|
305
|
+
pnpm test
|
|
306
|
+
pnpm typecheck
|
|
307
|
+
WT_DOCKER_INTEGRATION=1 pnpm test:e2e
|
|
308
|
+
pnpm test:coverage
|
|
309
|
+
pnpm lint
|
|
310
|
+
pnpm audit
|
|
311
|
+
pinact run --check
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
## License
|
|
315
|
+
|
|
316
|
+
[MIT](LICENSE)
|
package/dist/cli-spec.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { stat } from 'node:fs/promises';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import process from 'node:process';
|
|
4
|
+
export const commands = {
|
|
5
|
+
create: {
|
|
6
|
+
usage: '<name> [--branch <branch> | --from <ref>]',
|
|
7
|
+
summary: 'Create an environment',
|
|
8
|
+
options: { branch: 'string', from: 'string' },
|
|
9
|
+
environment: false,
|
|
10
|
+
shared: true,
|
|
11
|
+
},
|
|
12
|
+
ls: {
|
|
13
|
+
usage: '',
|
|
14
|
+
summary: 'List managed environments',
|
|
15
|
+
options: {},
|
|
16
|
+
environment: false,
|
|
17
|
+
shared: true,
|
|
18
|
+
},
|
|
19
|
+
info: {
|
|
20
|
+
usage: '[name]',
|
|
21
|
+
summary: 'Show environment details',
|
|
22
|
+
options: {},
|
|
23
|
+
environment: 'any',
|
|
24
|
+
shared: true,
|
|
25
|
+
},
|
|
26
|
+
start: {
|
|
27
|
+
usage: '[name]',
|
|
28
|
+
summary: 'Start services',
|
|
29
|
+
options: {},
|
|
30
|
+
environment: 'stopped',
|
|
31
|
+
shared: true,
|
|
32
|
+
},
|
|
33
|
+
stop: {
|
|
34
|
+
usage: '[name]',
|
|
35
|
+
summary: 'Stop services',
|
|
36
|
+
options: {},
|
|
37
|
+
environment: 'running',
|
|
38
|
+
shared: true,
|
|
39
|
+
},
|
|
40
|
+
restart: {
|
|
41
|
+
usage: '[name]',
|
|
42
|
+
summary: 'Restart services',
|
|
43
|
+
options: {},
|
|
44
|
+
environment: 'running',
|
|
45
|
+
shared: true,
|
|
46
|
+
},
|
|
47
|
+
docker: {
|
|
48
|
+
usage: '[name] -- <compose arguments...>',
|
|
49
|
+
summary: 'Run a Compose command',
|
|
50
|
+
options: {},
|
|
51
|
+
environment: 'any',
|
|
52
|
+
shared: false,
|
|
53
|
+
},
|
|
54
|
+
reset: {
|
|
55
|
+
usage: '[name] [--force]',
|
|
56
|
+
summary: 'Refresh files and volumes from the source',
|
|
57
|
+
options: { force: 'boolean' },
|
|
58
|
+
environment: 'any',
|
|
59
|
+
shared: true,
|
|
60
|
+
},
|
|
61
|
+
rm: {
|
|
62
|
+
usage: '<name> [--force]',
|
|
63
|
+
summary: 'Remove an environment and keep its branch',
|
|
64
|
+
options: { force: 'boolean' },
|
|
65
|
+
environment: 'any',
|
|
66
|
+
shared: true,
|
|
67
|
+
},
|
|
68
|
+
completion: {
|
|
69
|
+
usage: '<zsh|bash>',
|
|
70
|
+
summary: 'Print a shell completion script',
|
|
71
|
+
options: {},
|
|
72
|
+
environment: false,
|
|
73
|
+
shared: false,
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
export const shells = ['zsh', 'bash'];
|
|
77
|
+
export function isCommand(value) {
|
|
78
|
+
return Object.hasOwn(commands, value);
|
|
79
|
+
}
|
|
80
|
+
/** Options of a command as `parseArgs` expects them, including `--format` where it applies. */
|
|
81
|
+
export function parseOptions(command) {
|
|
82
|
+
const spec = commands[command];
|
|
83
|
+
return Object.fromEntries([
|
|
84
|
+
...Object.entries(spec.options),
|
|
85
|
+
...(spec.shared
|
|
86
|
+
? [
|
|
87
|
+
['format', 'string'],
|
|
88
|
+
]
|
|
89
|
+
: []),
|
|
90
|
+
].map(([name, type]) => [
|
|
91
|
+
name,
|
|
92
|
+
{ type },
|
|
93
|
+
]));
|
|
94
|
+
}
|
|
95
|
+
export class UsageError extends Error {
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Removes `-w`/`--working-dir` from the arguments. It may appear before or
|
|
99
|
+
* after the command, but not after a `--` separator, which belongs to the
|
|
100
|
+
* passed-through Compose arguments.
|
|
101
|
+
*/
|
|
102
|
+
export function extractWorkingDir(argv) {
|
|
103
|
+
const rest = [];
|
|
104
|
+
let workingDir;
|
|
105
|
+
for (let index = 0; index < argv.length; index++) {
|
|
106
|
+
const token = argv[index];
|
|
107
|
+
if (token === '--') {
|
|
108
|
+
rest.push(...argv.slice(index));
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
if (token === '-w' || token === '--working-dir') {
|
|
112
|
+
workingDir = argv[++index];
|
|
113
|
+
if (workingDir === undefined || workingDir === '')
|
|
114
|
+
throw new UsageError(`Option ${token} needs a directory.`);
|
|
115
|
+
}
|
|
116
|
+
else if (token.startsWith('--working-dir=')) {
|
|
117
|
+
workingDir = token.slice('--working-dir='.length);
|
|
118
|
+
}
|
|
119
|
+
else if (token.startsWith('-w') && !token.startsWith('--')) {
|
|
120
|
+
workingDir = token.slice(2);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
rest.push(token);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (workingDir === '')
|
|
127
|
+
throw new UsageError('Option --working-dir needs a directory.');
|
|
128
|
+
return { rest, workingDir };
|
|
129
|
+
}
|
|
130
|
+
export async function workingDirectory(workingDir) {
|
|
131
|
+
if (workingDir === undefined)
|
|
132
|
+
return process.cwd();
|
|
133
|
+
const path = resolve(workingDir);
|
|
134
|
+
const info = await stat(path).catch(() => undefined);
|
|
135
|
+
if (!info?.isDirectory())
|
|
136
|
+
throw new Error(`Working directory ${workingDir} does not exist or is not a directory.`);
|
|
137
|
+
return path;
|
|
138
|
+
}
|