@kaynooo/cli-wt 0.1.1 → 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 +24 -8
- package/package.json +1 -1
- package/src/stack.ts +34 -8
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
|
|
1908
|
-
|
|
1909
|
-
|
|
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 =
|
|
1951
|
-
|
|
1952
|
-
|
|
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();
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaynooo/cli-wt",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1.
|
|
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/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 =
|
|
44
|
+
async function runTemplate(role: Role, template: string[], worktree: Worktree): Promise<void> {
|
|
45
|
+
const argv = argvOf(template, worktree)
|
|
29
46
|
|
|
30
|
-
if (await
|
|
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 =
|
|
111
|
+
const argv = argvOf(removed, worktree)
|
|
95
112
|
|
|
96
|
-
|
|
97
|
-
|
|
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. */
|