@kaynooo/cli-wt 0.1.0 → 0.1.2

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
@@ -1904,9 +1904,19 @@ function slugOf(ref) {
1904
1904
  async function spawnInherit(argv, cwd, env = process.env) {
1905
1905
  return Bun.spawn(argv, { cwd, env, stdio: ["inherit", "inherit", "inherit"] }).exited;
1906
1906
  }
1907
- async function runTemplate(template, worktree) {
1908
- const argv = fillTemplate(template, { project: worktree.project.name, task: worktree.task, dir: worktree.dir, id: worktree.id });
1909
- if (await spawnInherit(argv, worktree.dir) !== 0)
1907
+ async function spawnRunner(role, argv, cwd) {
1908
+ try {
1909
+ return await spawnInherit(argv, cwd);
1910
+ } catch (error) {
1911
+ if (error.code === "ENOENT")
1912
+ throw new WtError(`\`${argv[0]}\` is not on PATH: fix runner.${role} in ${configPath()}`);
1913
+ throw error;
1914
+ }
1915
+ }
1916
+ var argvOf = (template, worktree) => fillTemplate(template, { project: worktree.project.name, task: worktree.task, dir: worktree.dir, id: worktree.id });
1917
+ async function runTemplate(role, template, worktree) {
1918
+ const argv = argvOf(template, worktree);
1919
+ if (await spawnRunner(role, argv, worktree.dir) !== 0)
1910
1920
  throw new WtError(`\`${argv.join(" ")}\` failed`);
1911
1921
  }
1912
1922
  async function runSetup(worktree, env = composeEnv()) {
@@ -1924,7 +1934,7 @@ async function runSetup(worktree, env = composeEnv()) {
1924
1934
  async function stackUp(worktree) {
1925
1935
  const { up } = worktree.project.settings.runner;
1926
1936
  if (up)
1927
- return runTemplate(up, worktree);
1937
+ return runTemplate("up", up, worktree);
1928
1938
  if (await hasDevproxy())
1929
1939
  await devproxyUp();
1930
1940
  const network = worktree.project.settings.proxyNetwork;
@@ -1937,7 +1947,7 @@ async function stackUp(worktree) {
1937
1947
  async function stackDown(worktree) {
1938
1948
  const { down } = worktree.project.settings.runner;
1939
1949
  if (down && existsSync9(worktree.dir))
1940
- return runTemplate(down, worktree);
1950
+ return runTemplate("down", down, worktree);
1941
1951
  const here = existsSync9(worktree.dir);
1942
1952
  const argv = here ? ["docker", "compose", "down", "--remove-orphans"] : ["docker", "compose", "-p", worktree.id, "down", "--remove-orphans"];
1943
1953
  if (await spawnInherit(argv, here ? worktree.dir : homedir3(), composeEnv()) !== 0)
@@ -1947,9 +1957,15 @@ async function afterRemove(worktree) {
1947
1957
  const { removed } = worktree.project.settings.runner;
1948
1958
  if (!removed)
1949
1959
  return;
1950
- const argv = fillTemplate(removed, { project: worktree.project.name, task: worktree.task, dir: worktree.dir, id: worktree.id });
1951
- if (await spawnInherit(argv, worktree.project.root) !== 0)
1952
- console.warn(`wt: \`${argv.join(" ")}\` failed after removing ${worktree.id}`);
1960
+ const argv = argvOf(removed, worktree);
1961
+ try {
1962
+ if (await spawnRunner("removed", argv, worktree.project.root) !== 0)
1963
+ console.warn(`wt: \`${argv.join(" ")}\` failed after removing ${worktree.id}`);
1964
+ } catch (error) {
1965
+ if (!(error instanceof WtError))
1966
+ throw error;
1967
+ console.warn(`wt: ${error.message}, after removing ${worktree.id}`);
1968
+ }
1953
1969
  }
1954
1970
  async function hasContainers(composeName) {
1955
1971
  const found = await $7`docker ps -aq --filter ${`label=com.docker.compose.project=${composeName}`}`.quiet().nothrow();
@@ -2019,23 +2035,27 @@ async function syncTask(task, options) {
2019
2035
  await syncWorktree(worktree);
2020
2036
  console.log(`wt: synced ${worktree.id}`);
2021
2037
  }
2038
+ async function refuseLosingWork(worktree, force) {
2039
+ if (force || !existsSync10(worktree.dir))
2040
+ return;
2041
+ const dirty = await dirtyRepos(worktree);
2042
+ if (dirty.length)
2043
+ throw new WtError(`uncommitted changes in ${dirty.join(", ")}, commit them or pass --force`);
2044
+ const unreachable = await unreachableCommits(worktree);
2045
+ if (unreachable.length)
2046
+ throw new WtError(`commits on no branch in ${unreachable.join(", ")} would be lost, \`git switch -c <branch>\` there or pass --force`);
2047
+ }
2022
2048
  async function assertRemovable(task, options) {
2023
2049
  const worktree = await locateWorktree(process.cwd(), task, options.project);
2024
2050
  if (!existsSync10(worktree.dir) && !Object.values(worktree.files).some((file) => existsSync10(file)) && !await isRegistered(worktree))
2025
2051
  throw new WtError(`no worktree ${worktree.task} of ${worktree.project.name}, nothing removed`);
2052
+ await refuseLosingWork(worktree, options.force === true);
2026
2053
  }
2027
2054
  async function removeTask(task, options) {
2028
2055
  const worktree = await locateWorktree(process.cwd(), task, options.project);
2029
2056
  process.chdir(worktree.project.root);
2030
2057
  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
- }
2058
+ await refuseLosingWork(worktree, options.force === true);
2039
2059
  if (existsSync10(worktree.dir) || await hasContainers(worktree.id))
2040
2060
  await stackDown(worktree);
2041
2061
  await removeVolumes(worktree, anonymous);
@@ -2403,10 +2423,12 @@ class Jobs {
2403
2423
  const job = this.claim(stack, verb, true, service);
2404
2424
  const out = openSync(job.log, "w");
2405
2425
  try {
2406
- Bun.spawn(["setsid", "-f", process.execPath, "--no-env-file", ...argv], { stdio: ["ignore", out, out], env: { ...process.env, [this.env]: stack.slug } });
2426
+ Bun.spawn([process.execPath, "--no-env-file", ...argv], { stdio: ["ignore", out, out], env: { ...process.env, [this.env]: stack.slug }, detached: true }).unref();
2407
2427
  } catch (error) {
2408
2428
  this.finish(stack, job, 1);
2409
2429
  throw error;
2430
+ } finally {
2431
+ closeSync(out);
2410
2432
  }
2411
2433
  return job;
2412
2434
  }
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.2",
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
  }
package/src/stack.ts CHANGED
@@ -23,11 +23,28 @@ async function spawnInherit(argv: string[], cwd: string, env: Record<string, str
23
23
  return Bun.spawn(argv, { cwd, env, stdio: ['inherit', 'inherit', 'inherit'] }).exited
24
24
  }
25
25
 
26
+ type Role = 'up' | 'down' | 'removed'
27
+
28
+ /** Spawns a `runner` command, one whose executable is missing (a shell function, a bin not installed) failing with the config line to fix. */
29
+ async function spawnRunner(role: Role, argv: string[], cwd: string): Promise<number> {
30
+ try {
31
+ return await spawnInherit(argv, cwd)
32
+ }
33
+ catch (error) {
34
+ if ((error as NodeJS.ErrnoException).code === 'ENOENT')
35
+ throw new WtError(`\`${argv[0]}\` is not on PATH: fix runner.${role} in ${configPath()}`)
36
+
37
+ throw error
38
+ }
39
+ }
40
+
41
+ const argvOf = (template: string[], worktree: Worktree) => fillTemplate(template, { project: worktree.project.name, task: worktree.task, dir: worktree.dir, id: worktree.id })
42
+
26
43
  /** Runs a `runner` template, failing with the command named. */
27
- async function runTemplate(template: string[], worktree: Worktree): Promise<void> {
28
- const argv = fillTemplate(template, { project: worktree.project.name, task: worktree.task, dir: worktree.dir, id: worktree.id })
44
+ async function runTemplate(role: Role, template: string[], worktree: Worktree): Promise<void> {
45
+ const argv = argvOf(template, worktree)
29
46
 
30
- if (await spawnInherit(argv, worktree.dir) !== 0)
47
+ if (await spawnRunner(role, argv, worktree.dir) !== 0)
31
48
  throw new WtError(`\`${argv.join(' ')}\` failed`)
32
49
  }
33
50
 
@@ -52,7 +69,7 @@ export async function stackUp(worktree: Worktree): Promise<void> {
52
69
  const { up } = worktree.project.settings.runner
53
70
 
54
71
  if (up)
55
- return runTemplate(up, worktree)
72
+ return runTemplate('up', up, worktree)
56
73
 
57
74
  // The override puts routed services on the proxy's network, which must exist before compose starts them
58
75
  if (await hasDevproxy())
@@ -74,7 +91,7 @@ export async function stackDown(worktree: Worktree): Promise<void> {
74
91
  const { down } = worktree.project.settings.runner
75
92
 
76
93
  if (down && existsSync(worktree.dir))
77
- return runTemplate(down, worktree)
94
+ return runTemplate('down', down, worktree)
78
95
 
79
96
  const here = existsSync(worktree.dir)
80
97
  // Away from the project, whose compose file compose would otherwise read under the worktree's name
@@ -91,10 +108,19 @@ export async function afterRemove(worktree: Worktree): Promise<void> {
91
108
  if (!removed)
92
109
  return
93
110
 
94
- const argv = fillTemplate(removed, { project: worktree.project.name, task: worktree.task, dir: worktree.dir, id: worktree.id })
111
+ const argv = argvOf(removed, worktree)
95
112
 
96
- if (await spawnInherit(argv, worktree.project.root) !== 0)
97
- console.warn(`wt: \`${argv.join(' ')}\` failed after removing ${worktree.id}`)
113
+ try {
114
+ if (await spawnRunner('removed', argv, worktree.project.root) !== 0)
115
+ console.warn(`wt: \`${argv.join(' ')}\` failed after removing ${worktree.id}`)
116
+ }
117
+ catch (error) {
118
+ // The worktree is gone already: what is left to do is said, not failed on
119
+ if (!(error instanceof WtError))
120
+ throw error
121
+
122
+ console.warn(`wt: ${error.message}, after removing ${worktree.id}`)
123
+ }
98
124
  }
99
125
 
100
126
  /** Whether some container, running or not, belongs to that compose project. */