@kaynooo/cli-wt 0.1.0 → 0.1.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/dist/cli.js CHANGED
@@ -2019,23 +2019,27 @@ async function syncTask(task, options) {
2019
2019
  await syncWorktree(worktree);
2020
2020
  console.log(`wt: synced ${worktree.id}`);
2021
2021
  }
2022
+ async function refuseLosingWork(worktree, force) {
2023
+ if (force || !existsSync10(worktree.dir))
2024
+ return;
2025
+ const dirty = await dirtyRepos(worktree);
2026
+ if (dirty.length)
2027
+ throw new WtError(`uncommitted changes in ${dirty.join(", ")}, commit them or pass --force`);
2028
+ const unreachable = await unreachableCommits(worktree);
2029
+ if (unreachable.length)
2030
+ throw new WtError(`commits on no branch in ${unreachable.join(", ")} would be lost, \`git switch -c <branch>\` there or pass --force`);
2031
+ }
2022
2032
  async function assertRemovable(task, options) {
2023
2033
  const worktree = await locateWorktree(process.cwd(), task, options.project);
2024
2034
  if (!existsSync10(worktree.dir) && !Object.values(worktree.files).some((file) => existsSync10(file)) && !await isRegistered(worktree))
2025
2035
  throw new WtError(`no worktree ${worktree.task} of ${worktree.project.name}, nothing removed`);
2036
+ await refuseLosingWork(worktree, options.force === true);
2026
2037
  }
2027
2038
  async function removeTask(task, options) {
2028
2039
  const worktree = await locateWorktree(process.cwd(), task, options.project);
2029
2040
  process.chdir(worktree.project.root);
2030
2041
  const anonymous = await anonymousVolumes(worktree);
2031
- if (existsSync10(worktree.dir)) {
2032
- const dirty = options.force ? [] : await dirtyRepos(worktree);
2033
- if (dirty.length)
2034
- throw new WtError(`uncommitted changes in ${dirty.join(", ")}, commit them or pass --force`);
2035
- const unreachable = options.force ? [] : await unreachableCommits(worktree);
2036
- if (unreachable.length)
2037
- throw new WtError(`commits on no branch in ${unreachable.join(", ")} would be lost, \`git switch -c <branch>\` there or pass --force`);
2038
- }
2042
+ await refuseLosingWork(worktree, options.force === true);
2039
2043
  if (existsSync10(worktree.dir) || await hasContainers(worktree.id))
2040
2044
  await stackDown(worktree);
2041
2045
  await removeVolumes(worktree, anonymous);
@@ -2403,10 +2407,12 @@ class Jobs {
2403
2407
  const job = this.claim(stack, verb, true, service);
2404
2408
  const out = openSync(job.log, "w");
2405
2409
  try {
2406
- Bun.spawn(["setsid", "-f", process.execPath, "--no-env-file", ...argv], { stdio: ["ignore", out, out], env: { ...process.env, [this.env]: stack.slug } });
2410
+ Bun.spawn([process.execPath, "--no-env-file", ...argv], { stdio: ["ignore", out, out], env: { ...process.env, [this.env]: stack.slug }, detached: true }).unref();
2407
2411
  } catch (error) {
2408
2412
  this.finish(stack, job, 1);
2409
2413
  throw error;
2414
+ } finally {
2415
+ closeSync(out);
2410
2416
  }
2411
2417
  return job;
2412
2418
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kaynooo/cli-wt",
3
3
  "type": "module",
4
- "version": "0.1.0",
4
+ "version": "0.1.1",
5
5
  "description": "wt: git worktrees of a compose project, each with its own stack, data and <task>.<host> subdomains beside main.",
6
6
  "author": "Kaynooo <contact@kaynooo.fr>",
7
7
  "license": "MIT",
package/src/commands.ts CHANGED
@@ -80,12 +80,30 @@ export async function syncTask(task: string | undefined, options: GlobalOptions)
80
80
  console.log(`wt: synced ${worktree.id}`)
81
81
  }
82
82
 
83
- /** Refuses a removal with nothing to remove, before it claims a job that would leave a record of a worktree never there. */
84
- export async function assertRemovable(task: string, options: GlobalOptions): Promise<void> {
83
+ /** Refuses a removal that would lose uncommitted changes or commits on no branch, unless forced. */
84
+ async function refuseLosingWork(worktree: Worktree, force: boolean): Promise<void> {
85
+ if (force || !existsSync(worktree.dir))
86
+ return
87
+
88
+ const dirty = await dirtyRepos(worktree)
89
+
90
+ if (dirty.length)
91
+ throw new WtError(`uncommitted changes in ${dirty.join(', ')}, commit them or pass --force`)
92
+
93
+ const unreachable = await unreachableCommits(worktree)
94
+
95
+ if (unreachable.length)
96
+ throw new WtError(`commits on no branch in ${unreachable.join(', ')} would be lost, \`git switch -c <branch>\` there or pass --force`)
97
+ }
98
+
99
+ /** Refuses a removal with nothing to remove, or work it would lose, before it claims a job: a job left recording a worktree never there, or a `--bg` one failing out of sight. */
100
+ export async function assertRemovable(task: string, options: GlobalOptions & { force?: boolean }): Promise<void> {
85
101
  const worktree = await locateWorktree(process.cwd(), task, options.project)
86
102
 
87
103
  if (!existsSync(worktree.dir) && !Object.values(worktree.files).some(file => existsSync(file)) && !await isRegistered(worktree))
88
104
  throw new WtError(`no worktree ${worktree.task} of ${worktree.project.name}, nothing removed`)
105
+
106
+ await refuseLosingWork(worktree, options.force === true)
89
107
  }
90
108
 
91
109
  export async function removeTask(task: string, options: GlobalOptions & { force?: boolean }): Promise<void> {
@@ -96,17 +114,8 @@ export async function removeTask(task: string, options: GlobalOptions & { force?
96
114
 
97
115
  const anonymous = await anonymousVolumes(worktree)
98
116
 
99
- if (existsSync(worktree.dir)) {
100
- const dirty = options.force ? [] : await dirtyRepos(worktree)
101
-
102
- if (dirty.length)
103
- throw new WtError(`uncommitted changes in ${dirty.join(', ')}, commit them or pass --force`)
104
-
105
- const unreachable = options.force ? [] : await unreachableCommits(worktree)
106
-
107
- if (unreachable.length)
108
- throw new WtError(`commits on no branch in ${unreachable.join(', ')} would be lost, \`git switch -c <branch>\` there or pass --force`)
109
- }
117
+ // Again inside the job: the tree may have changed since the check before it
118
+ await refuseLosingWork(worktree, options.force === true)
110
119
 
111
120
  // With the folder gone, its containers may still hold its volumes
112
121
  if (existsSync(worktree.dir) || await hasContainers(worktree.id))
package/src/jobs.ts CHANGED
@@ -370,19 +370,23 @@ export class Jobs {
370
370
  await notify(this.app, `${what} not started`, message, true)
371
371
  }
372
372
 
373
- /** Runs `argv` with bun away from any terminal: setsid outlives the caller (a Herdr popup that closes), the output goes to the stack's log. */
373
+ /** Runs `argv` with bun away from any terminal, in a session of its own that outlives the caller (a Herdr popup that closes), the output in the stack's log. */
374
374
  detach(stack: JobStack, verb: string, argv: string[], service?: string): Job {
375
375
  const job = this.claim(stack, verb, true, service)
376
376
  const out = openSync(job.log, 'w')
377
377
 
378
378
  try {
379
- Bun.spawn(['setsid', '-f', process.execPath, '--no-env-file', ...argv], { stdio: ['ignore', out, out], env: { ...process.env, [this.env]: stack.slug } })
379
+ // Not setsid -f, which busybox and macOS lack
380
+ Bun.spawn([process.execPath, '--no-env-file', ...argv], { stdio: ['ignore', out, out], env: { ...process.env, [this.env]: stack.slug }, detached: true }).unref()
380
381
  }
381
382
  catch (error) {
382
383
  this.finish(stack, job, 1)
383
384
 
384
385
  throw error
385
386
  }
387
+ finally {
388
+ closeSync(out)
389
+ }
386
390
 
387
391
  return job
388
392
  }