@kaynooo/cli-wt 0.0.0-stage → 0.1.0
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 +93 -2
- package/dist/cli.js +2762 -0
- package/package.json +37 -4
- package/src/cli.ts +319 -0
- package/src/commands.ts +160 -0
- package/src/compose.ts +212 -0
- package/src/config.ts +83 -0
- package/src/env_file.ts +7 -0
- package/src/errors.ts +36 -0
- package/src/files.ts +333 -0
- package/src/git.ts +102 -0
- package/src/index.ts +16 -0
- package/src/init.ts +149 -0
- package/src/jobs.ts +421 -0
- package/src/list.ts +98 -0
- package/src/port.ts +11 -0
- package/src/project.ts +196 -0
- package/src/proxy.ts +51 -0
- package/src/queue.ts +69 -0
- package/src/routes.ts +42 -0
- package/src/run.ts +251 -0
- package/src/shell.ts +28 -0
- package/src/stack.ts +105 -0
- package/src/sync.ts +28 -0
package/src/git.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { Worktree } from './project'
|
|
2
|
+
import { existsSync, mkdirSync, readdirSync, rmdirSync } from 'node:fs'
|
|
3
|
+
import { basename, dirname, join } from 'node:path'
|
|
4
|
+
import { $ } from 'bun'
|
|
5
|
+
import { WtError } from './project'
|
|
6
|
+
|
|
7
|
+
export async function git(cwd: string, ...args: string[]): Promise<{ ok: boolean, out: string }> {
|
|
8
|
+
const result = await $`git -C ${cwd} ${args}`.quiet().nothrow()
|
|
9
|
+
|
|
10
|
+
return { ok: result.exitCode === 0, out: `${result.stdout.toString()}${result.stderr.toString()}`.trim() }
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async function branchExists(repo: string, branch: string): Promise<boolean> {
|
|
14
|
+
if ((await git(repo, 'show-ref', '--quiet', '--verify', `refs/heads/${branch}`)).ok)
|
|
15
|
+
return true
|
|
16
|
+
|
|
17
|
+
return (await git(repo, 'for-each-ref', '--format=%(refname)', `refs/remotes/*/${branch}`)).out !== ''
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// A branch neither local nor remote is created from the repo's current HEAD; a remote one is checked out tracking it.
|
|
21
|
+
async function onBranch(repo: string, dir: string, branch: string): Promise<{ ok: boolean, out: string }> {
|
|
22
|
+
return git(repo, 'worktree', 'add', ...await branchExists(repo, branch) ? [dir, branch] : ['-b', branch, dir])
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Every repo inside the root checks out the branch; the root does too when it is the only repo, and is detached otherwise, its nested repos holding the work.
|
|
26
|
+
export async function addWorktrees(worktree: Worktree, branch: string): Promise<void> {
|
|
27
|
+
const { project, dir } = worktree
|
|
28
|
+
|
|
29
|
+
if (existsSync(dir))
|
|
30
|
+
throw new WtError(`${dir} already exists: \`wt sync ${worktree.task} -p ${project.name}\` finishes it, \`wt rm ${worktree.task} -p ${project.name}\` removes it`)
|
|
31
|
+
|
|
32
|
+
mkdirSync(dirname(dir), { recursive: true })
|
|
33
|
+
|
|
34
|
+
const root = project.repos.length ? await git(project.root, 'worktree', 'add', '--detach', dir) : await onBranch(project.root, dir, branch)
|
|
35
|
+
|
|
36
|
+
if (!root.ok)
|
|
37
|
+
throw new WtError(`git worktree add failed for the root:\n${root.out}`)
|
|
38
|
+
|
|
39
|
+
for (const repo of project.repos) {
|
|
40
|
+
const added = await onBranch(join(project.root, repo), join(dir, repo), branch)
|
|
41
|
+
|
|
42
|
+
if (!added.ok) {
|
|
43
|
+
await removeWorktrees(worktree)
|
|
44
|
+
|
|
45
|
+
throw new WtError(`git worktree add failed for ${repo}, rolled back:\n${added.out}`)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function removeWorktrees(worktree: Worktree): Promise<void> {
|
|
51
|
+
const { project, dir } = worktree
|
|
52
|
+
|
|
53
|
+
await Promise.all(project.repos.map(async repo => git(join(project.root, repo), 'worktree', 'remove', '--force', join(dir, repo))))
|
|
54
|
+
await git(project.root, 'worktree', 'remove', '--force', dir)
|
|
55
|
+
|
|
56
|
+
// Data cloned for the stack belongs to container users, so whatever git could not delete goes through a container.
|
|
57
|
+
if (existsSync(dir))
|
|
58
|
+
await $`docker run --rm -v ${`${dirname(dir)}:/parent`} alpine rm -rf ${`/parent/${basename(dir)}`}`.quiet()
|
|
59
|
+
|
|
60
|
+
await Promise.all(['.', ...project.repos].map(async repo => git(join(project.root, repo), 'worktree', 'prune')))
|
|
61
|
+
|
|
62
|
+
if (existsSync(dirname(dir)) && !readdirSync(dirname(dir)).length)
|
|
63
|
+
rmdirSync(dirname(dir))
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export async function dirtyRepos(worktree: Worktree): Promise<string[]> {
|
|
67
|
+
const repos = ['.', ...worktree.project.repos]
|
|
68
|
+
const statuses = await Promise.all(repos.map(async repo => git(join(worktree.dir, repo), 'status', '--porcelain')))
|
|
69
|
+
|
|
70
|
+
return repos.filter((_, index) => statuses[index]!.ok && statuses[index]!.out !== '').map(repo => repo === '.' ? 'root' : repo)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// How many commits each repo's HEAD has that no branch, remote or tag holds, which removing the checkout would lose; the root is named "root".
|
|
74
|
+
async function unreachableByRepo(dir: string, repos: string[]): Promise<{ repo: string, count: number }[]> {
|
|
75
|
+
const all = ['.', ...repos]
|
|
76
|
+
const counts = await Promise.all(all.map(async repo => git(join(dir, repo), 'rev-list', '--count', 'HEAD', '--not', '--branches', '--remotes', '--tags')))
|
|
77
|
+
|
|
78
|
+
return all.map((repo, index) => ({ repo: repo === '.' ? 'root' : repo, count: Number(counts[index]!.ok ? counts[index]!.out : 0) })).filter(({ count }) => count > 0)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** The repos whose HEAD has commits no branch, remote or tag holds, which removing the worktree would lose, with how many. */
|
|
82
|
+
export async function unreachableCommits(worktree: Worktree): Promise<string[]> {
|
|
83
|
+
return (await unreachableByRepo(worktree.dir, worktree.project.repos)).map(({ repo, count }) => `${repo} (${count} commit${count === 1 ? '' : 's'})`)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** How many commits on no branch a checkout holds, over the root and its repos. */
|
|
87
|
+
export async function unreachableCount(dir: string, repos: string[]): Promise<number> {
|
|
88
|
+
return (await unreachableByRepo(dir, repos)).reduce((total, { count }) => total + count, 0)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Whether git still lists the worktree, its folder gone or not. */
|
|
92
|
+
export async function isRegistered(worktree: Worktree): Promise<boolean> {
|
|
93
|
+
const listed = await git(worktree.project.root, 'worktree', 'list', '--porcelain')
|
|
94
|
+
|
|
95
|
+
return listed.out.split('\n').includes(`worktree ${worktree.dir}`)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export async function currentBranch(dir: string): Promise<string> {
|
|
99
|
+
const result = await git(dir, 'rev-parse', '--abbrev-ref', 'HEAD')
|
|
100
|
+
|
|
101
|
+
return result.ok ? result.out : '?'
|
|
102
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** What other tools build on wt with: athome reads the manifests and worktrees, shares the job locks and runs a worktree's setup. */
|
|
2
|
+
|
|
3
|
+
export type { ComposeConfig } from './compose'
|
|
4
|
+
export { COMPOSE_FILES, composeEnv } from './compose'
|
|
5
|
+
export type { Resolved, WtConfig } from './config'
|
|
6
|
+
export { expandHome, wtConfig } from './config'
|
|
7
|
+
export { alive, bootTime, failKnown, KnownError } from './errors'
|
|
8
|
+
export type { Job, JobStack, JobState, JobWords } from './jobs'
|
|
9
|
+
export { HELD_ENV, Jobs, logTail } from './jobs'
|
|
10
|
+
export type { Project, ProjectConfig, Route, RunConfig, Worktree } from './project'
|
|
11
|
+
export { findProject, listTasks, loadProjects, locate, locateWorktree, worktreeHost, worktreeOf, WtError } from './project'
|
|
12
|
+
export type { ProxyHost } from './proxy'
|
|
13
|
+
export { devproxyUp, proxyHostList } from './proxy'
|
|
14
|
+
export { inLine, queueDir } from './queue'
|
|
15
|
+
export { urlsOf } from './routes'
|
|
16
|
+
export { runSetup, slugOf } from './stack'
|
package/src/init.ts
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import type { ComposeConfig } from './compose'
|
|
2
|
+
import type { Project, Route } from './project'
|
|
3
|
+
import { existsSync, mkdirSync, readdirSync, statSync, writeFileSync } from 'node:fs'
|
|
4
|
+
import { homedir } from 'node:os'
|
|
5
|
+
import { basename, join, relative } from 'node:path'
|
|
6
|
+
import { composeConfig } from './compose'
|
|
7
|
+
import { wtConfig } from './config'
|
|
8
|
+
import { volumeExists } from './files'
|
|
9
|
+
import { git } from './git'
|
|
10
|
+
import { stringToPort } from './port'
|
|
11
|
+
import { WtError } from './project'
|
|
12
|
+
import { proxyHostList } from './proxy'
|
|
13
|
+
|
|
14
|
+
type Draft = Omit<Project, 'link' | 'env' | 'setup' | 'settings'> & Required<Pick<Project, 'copy' | 'clone' | 'skip'>>
|
|
15
|
+
|
|
16
|
+
const CERT_PATH = /cert/i
|
|
17
|
+
// devproxy's upstreams: `http://<container>:<port>` or `host:<port>`, and `<container>:<port>` for a passthrough.
|
|
18
|
+
const HTTP_TARGET = /^(?:https?:\/\/)?([\w.-]+):(\d+)$/
|
|
19
|
+
const PASSTHROUGH_TARGET = /^([\w.-]+):(\d+)$/
|
|
20
|
+
|
|
21
|
+
// check-ignore prints the paths it matches and exits 1 when none does, so only its output is trusted.
|
|
22
|
+
async function ignoredPaths(repo: string, paths: string[]): Promise<string[]> {
|
|
23
|
+
if (!paths.length)
|
|
24
|
+
return []
|
|
25
|
+
|
|
26
|
+
const matched = new Set((await git(repo, 'check-ignore', ...paths)).out.split('\n'))
|
|
27
|
+
|
|
28
|
+
return paths.filter(path => matched.has(path))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Host paths the stack mounts, whether as a bind or as a named volume bound to a device.
|
|
32
|
+
function mountedPaths(config: ComposeConfig): string[] {
|
|
33
|
+
const binds = Object.values(config.services).flatMap(service => (service.volumes ?? []).filter(v => v.type === 'bind').map(v => v.source!))
|
|
34
|
+
const devices = Object.values(config.volumes ?? {}).flatMap(volume => volume.driver_opts?.device ?? [])
|
|
35
|
+
|
|
36
|
+
return [...new Set([...binds, ...devices])]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Routes are read back from the hosts the shared proxy serves main on: those dialling one of its containers, and its dev server port on the host.
|
|
40
|
+
async function findRoutes(config: ComposeConfig, name: string, network: string): Promise<Route[]> {
|
|
41
|
+
const serviceOf = (target: string) => Object.entries(config.services).find(([service, settings]) =>
|
|
42
|
+
settings.container_name === target
|
|
43
|
+
|| `${config.name}-${service}-1` === target
|
|
44
|
+
|| settings.networks?.[network]?.aliases?.includes(target))?.[0]
|
|
45
|
+
const routes: Route[] = []
|
|
46
|
+
|
|
47
|
+
for (const { host, to, passthrough } of await proxyHostList()) {
|
|
48
|
+
const [, target = '', port = ''] = (passthrough ? PASSTHROUGH_TARGET : HTTP_TARGET).exec(to) ?? []
|
|
49
|
+
const service = serviceOf(target)
|
|
50
|
+
|
|
51
|
+
if (target === 'host' && Number(port) === stringToPort(name))
|
|
52
|
+
routes.push({ host, to: 'client' })
|
|
53
|
+
else if (service !== undefined)
|
|
54
|
+
routes.push({ host, to: `${service}:${Number(port)}`, ...passthrough ? { passthrough } : {} })
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return routes
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Ignored env files and JWT keys of the root and of each repo, in that order.
|
|
61
|
+
async function secretsToCopy(root: string, repos: string[]): Promise<string[]> {
|
|
62
|
+
const perBase = await Promise.all(['.', ...repos].map(async (base) => {
|
|
63
|
+
const envFiles = readdirSync(join(root, base)).filter(entry => (entry.startsWith('.env') || entry === '.nuxtrc') && statSync(join(root, base, entry)).isFile())
|
|
64
|
+
const jwt = base !== '.' && existsSync(join(root, base, 'config', 'jwt')) ? [join(base, 'config', 'jwt')] : []
|
|
65
|
+
|
|
66
|
+
return [...(await ignoredPaths(join(root, base), envFiles)).map(entry => join(base, entry)), ...jwt]
|
|
67
|
+
}))
|
|
68
|
+
|
|
69
|
+
return perBase.flat()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// `name` names the manifest and is hashed into the dev server port, the checkout's folder name by default.
|
|
73
|
+
async function draftFor(dir: string, given?: string): Promise<Draft> {
|
|
74
|
+
const top = await git(dir, 'rev-parse', '--show-toplevel')
|
|
75
|
+
|
|
76
|
+
if (!top.ok)
|
|
77
|
+
throw new WtError(`${dir} is not inside a git repository`)
|
|
78
|
+
|
|
79
|
+
const root = top.out
|
|
80
|
+
const config = await composeConfig(root)
|
|
81
|
+
const name = given ?? basename(root)
|
|
82
|
+
const mounted = mountedPaths(config).filter(path => path.startsWith(`${root}/`)).map(path => relative(root, path))
|
|
83
|
+
const repos = readdirSync(root).filter(entry => existsSync(join(root, entry, '.git')) && mounted.some(path => path === entry || path.startsWith(`${entry}/`)))
|
|
84
|
+
const inRepo = (path: string) => repos.some(repo => path === repo || path.startsWith(`${repo}/`))
|
|
85
|
+
const volumes = Object.entries(config.volumes ?? {}).filter(([, volume]) => (volume.driver_opts?.device ?? '') === '')
|
|
86
|
+
|
|
87
|
+
const [secrets, ignoredMounts, volumesExist] = await Promise.all([
|
|
88
|
+
secretsToCopy(root, repos),
|
|
89
|
+
ignoredPaths(root, mounted.filter(path => !inRepo(path) && existsSync(join(root, path)))),
|
|
90
|
+
Promise.all(volumes.map(async ([key, volume]) => volumeExists(volume.name ?? `${config.name}_${key}`))),
|
|
91
|
+
])
|
|
92
|
+
const routes = await findRoutes(config, name, (await wtConfig()).proxyNetwork)
|
|
93
|
+
const certs = routes.some(route => route.passthrough) ? mounted.find(path => CERT_PATH.test(path)) : undefined
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
name,
|
|
97
|
+
root: root.replace(homedir(), '~'),
|
|
98
|
+
repos,
|
|
99
|
+
copy: [...secrets, ...ignoredMounts.filter(path => CERT_PATH.test(path))],
|
|
100
|
+
clone: [...ignoredMounts.filter(path => !CERT_PATH.test(path)), ...volumes.filter((_, index) => volumesExist[index]).map(([key]) => key)],
|
|
101
|
+
skip: Object.entries(config.services).filter(([, service]) => service.network_mode === 'host').map(([service]) => service),
|
|
102
|
+
...certs !== undefined && { certs },
|
|
103
|
+
routes,
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const list = (values: string[]) => `[${values.map(value => `'${value}'`).join(', ')}]`
|
|
108
|
+
|
|
109
|
+
function render(draft: Draft): string {
|
|
110
|
+
const routes = draft.routes.map(route => ` { host: '${route.host}', to: '${route.to}'${route.passthrough ? ', passthrough: true' : ''} },`)
|
|
111
|
+
|
|
112
|
+
return [
|
|
113
|
+
`import type { ProjectConfig } from '@kaynooo/cli-wt'`,
|
|
114
|
+
'',
|
|
115
|
+
'// Drafted by `wt init`: check every list against the stack before the first `wt add`.',
|
|
116
|
+
'export default {',
|
|
117
|
+
` root: '${draft.root}',`,
|
|
118
|
+
` repos: ${list(draft.repos)},`,
|
|
119
|
+
` copy: ${list(draft.copy)},`,
|
|
120
|
+
` clone: ${list(draft.clone)},`,
|
|
121
|
+
...draft.skip.length ? [` skip: ${list(draft.skip)},`] : [],
|
|
122
|
+
...draft.certs !== undefined ? [` certs: '${draft.certs}',`] : [],
|
|
123
|
+
' setup: [],',
|
|
124
|
+
' routes: [',
|
|
125
|
+
...routes,
|
|
126
|
+
' ],',
|
|
127
|
+
'} satisfies ProjectConfig',
|
|
128
|
+
'',
|
|
129
|
+
].join('\n')
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export async function initProject(dir: string, options: { print?: boolean, name?: string }): Promise<void> {
|
|
133
|
+
const draft = await draftFor(dir, options.name)
|
|
134
|
+
const content = render(draft)
|
|
135
|
+
|
|
136
|
+
if (options.print)
|
|
137
|
+
return console.log(content)
|
|
138
|
+
|
|
139
|
+
const { manifests } = await wtConfig()
|
|
140
|
+
const file = join(manifests, `${draft.name}.ts`)
|
|
141
|
+
|
|
142
|
+
mkdirSync(manifests, { recursive: true })
|
|
143
|
+
|
|
144
|
+
if (existsSync(file))
|
|
145
|
+
throw new WtError(`${file} already exists, compare with \`wt init --print\``)
|
|
146
|
+
|
|
147
|
+
writeFileSync(file, content)
|
|
148
|
+
console.log(`wt: drafted ${file}, review it before \`wt add\``)
|
|
149
|
+
}
|
package/src/jobs.ts
ADDED
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One action at a time per stack: each holds the stack's lock while it runs and leaves a job file saying what ran and how it ended.
|
|
3
|
+
* `--bg` runs the action detached from any terminal, its output in the stack's log, a notification telling how it went.
|
|
4
|
+
* Tools pointing at the same folder share the locks: athome's up and wt's rm of one worktree never run together.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { closeSync, fstatSync, mkdirSync, openSync, readdirSync, readFileSync, readSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs'
|
|
8
|
+
import { join } from 'node:path'
|
|
9
|
+
import { $ } from 'bun'
|
|
10
|
+
import { alive, bootTime, failKnown, KnownError } from './errors'
|
|
11
|
+
|
|
12
|
+
export interface Job {
|
|
13
|
+
ref: string
|
|
14
|
+
verb: string
|
|
15
|
+
/** The one service a restart, stop, start or rebuild acts on. */
|
|
16
|
+
service?: string
|
|
17
|
+
/** The process running it, null for the moment a detached one takes to start. */
|
|
18
|
+
pid: number | null
|
|
19
|
+
startedAt: number
|
|
20
|
+
endedAt?: number
|
|
21
|
+
/** Its exit code once ended, null when it was killed before it could say. */
|
|
22
|
+
code?: number | null
|
|
23
|
+
/** Where a detached job's output goes, empty for one run in a terminal. */
|
|
24
|
+
log: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface JobStack {
|
|
28
|
+
ref: string
|
|
29
|
+
slug: string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface JobState {
|
|
33
|
+
/** The job running now. */
|
|
34
|
+
running?: Job
|
|
35
|
+
/** The last one that ended, or was killed before it could. */
|
|
36
|
+
last?: Job
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The locks the jobs of this process tree hold, one `<pid> <lock path>` per line, so a command a job runs (wt rm running `athome down`) works inside it instead of being refused. */
|
|
40
|
+
export const HELD_ENV = 'WT_JOBS_HELD'
|
|
41
|
+
/** How long a detached job may take to write its pid before its lock counts as abandoned. */
|
|
42
|
+
const STARTING_MS = 30_000
|
|
43
|
+
/** How long a takeover of an abandoned lock may last before another claimer takes it over in turn. */
|
|
44
|
+
const TAKEOVER_MS = 5_000
|
|
45
|
+
const TAIL_BYTES = 4096
|
|
46
|
+
/** What a refusal says of wt's own actions; another tool sharing the folder adds its verbs through `words`. */
|
|
47
|
+
const DOING: Record<string, string> = { up: 'coming up', down: 'stopping', add: 'being made', sync: 'syncing', rm: 'being removed' }
|
|
48
|
+
/** The actions a notification reports the success of, as a title and a body. */
|
|
49
|
+
const DONE: Record<string, [string, string]> = { up: ['is up', 'Its stack came up'], add: ['is ready', 'The worktree is made, its stack still down'] }
|
|
50
|
+
|
|
51
|
+
export interface JobWords {
|
|
52
|
+
doing?: Record<string, string>
|
|
53
|
+
done?: Record<string, [string, string]>
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Written beside the file and renamed over it, so a mod reading it meanwhile never sees half of it. */
|
|
57
|
+
function writeJob(file: string, job: Job): void {
|
|
58
|
+
const partial = `${file}.${process.pid}.tmp`
|
|
59
|
+
|
|
60
|
+
writeFileSync(partial, JSON.stringify(job))
|
|
61
|
+
renameSync(partial, file)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function readJob(file: string): Job | undefined {
|
|
65
|
+
try {
|
|
66
|
+
return JSON.parse(readFileSync(file, 'utf8')) as Job
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return undefined
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** What a job is doing, its service named: "restarting php"; a verb with no words is named as it is: "busy with panes". */
|
|
74
|
+
function doing(table: Record<string, string>, job: Pick<Job, 'verb' | 'service'> | undefined): string {
|
|
75
|
+
const words = job === undefined ? 'busy' : table[job.verb] ?? `busy with ${job.verb}`
|
|
76
|
+
|
|
77
|
+
return job?.service !== undefined ? `${words} ${job.service}` : words
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Whether the lock is held: by a live pid written since boot, or still empty while its detached job starts. */
|
|
81
|
+
function held(lock: string, now = Date.now()): boolean {
|
|
82
|
+
try {
|
|
83
|
+
const pid = Number(readFileSync(lock, 'utf8').trim())
|
|
84
|
+
const writtenAt = statSync(lock, { throwIfNoEntry: false })?.mtimeMs ?? 0
|
|
85
|
+
|
|
86
|
+
if (writtenAt < bootTime())
|
|
87
|
+
return false
|
|
88
|
+
|
|
89
|
+
return pid ? alive(pid) : now - writtenAt < STARTING_MS
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return false
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Removes an abandoned lock under a takeover file of its own, checking it is still abandoned once that is held: two claimers seeing the same dead lock would otherwise both remove it, the second one the lock the first had just made.
|
|
98
|
+
* False when another claimer is taking it over right now.
|
|
99
|
+
*/
|
|
100
|
+
function takeOver(lock: string): boolean {
|
|
101
|
+
const guard = `${lock}.takeover`
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
writeFileSync(guard, '', { flag: 'wx' })
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
if ((error as NodeJS.ErrnoException).code !== 'EEXIST')
|
|
108
|
+
throw error
|
|
109
|
+
|
|
110
|
+
if (Date.now() - (statSync(guard, { throwIfNoEntry: false })?.mtimeMs ?? 0) < TAKEOVER_MS)
|
|
111
|
+
return false
|
|
112
|
+
|
|
113
|
+
rmSync(guard, { force: true })
|
|
114
|
+
|
|
115
|
+
return takeOver(lock)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
try {
|
|
119
|
+
if (!held(lock))
|
|
120
|
+
rmSync(lock, { force: true })
|
|
121
|
+
|
|
122
|
+
return true
|
|
123
|
+
}
|
|
124
|
+
finally {
|
|
125
|
+
rmSync(guard, { force: true })
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const heldHere = () => (process.env[HELD_ENV] ?? '').split('\n').filter(Boolean)
|
|
130
|
+
const heldEntry = (lock: string) => `${process.pid} ${lock}`
|
|
131
|
+
|
|
132
|
+
/** Whether an entry of HELD_ENV still names the lock's holder: a lock taken over since (its holder gone, its pid reused) holds another pid. */
|
|
133
|
+
function stillHeldBy(entry: string): boolean {
|
|
134
|
+
const space = entry.indexOf(' ')
|
|
135
|
+
const lock = entry.slice(space + 1)
|
|
136
|
+
|
|
137
|
+
try {
|
|
138
|
+
return readFileSync(lock, 'utf8').trim() === entry.slice(0, space) && held(lock)
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
return false
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** The last lines of a log, read from its end: a long build's log is not read whole, and one gone reads empty. */
|
|
146
|
+
export function logTail(file: string, lines = 5): string {
|
|
147
|
+
try {
|
|
148
|
+
const fd = openSync(file, 'r')
|
|
149
|
+
|
|
150
|
+
try {
|
|
151
|
+
const size = fstatSync(fd).size
|
|
152
|
+
const buffer = Buffer.alloc(Math.min(size, TAIL_BYTES))
|
|
153
|
+
|
|
154
|
+
readSync(fd, buffer, 0, buffer.length, size - buffer.length)
|
|
155
|
+
|
|
156
|
+
return buffer.toString('utf8').trim().split('\n').slice(-lines).join('\n')
|
|
157
|
+
}
|
|
158
|
+
finally {
|
|
159
|
+
closeSync(fd)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
return ''
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async function notify(app: string, title: string, body: string, critical = false): Promise<void> {
|
|
168
|
+
if (Bun.which('notify-send') !== null)
|
|
169
|
+
await $`notify-send -a ${app} ${critical ? ['-u', 'critical'] : []} ${title} ${body}`.quiet().nothrow()
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** The jobs of one folder, notified under `app`, whose `<APP>_JOB` env var marks the detached process of a job. */
|
|
173
|
+
export class Jobs {
|
|
174
|
+
readonly dir: string
|
|
175
|
+
readonly app: string
|
|
176
|
+
readonly env: string
|
|
177
|
+
|
|
178
|
+
readonly doing: Record<string, string>
|
|
179
|
+
readonly done: Record<string, [string, string]>
|
|
180
|
+
|
|
181
|
+
constructor({ dir, app, words = {} }: { dir: string, app: string, words?: JobWords }) {
|
|
182
|
+
this.dir = dir
|
|
183
|
+
this.app = app
|
|
184
|
+
this.env = `${app.toUpperCase()}_JOB`
|
|
185
|
+
this.doing = { ...DOING, ...words.doing }
|
|
186
|
+
this.done = { ...DONE, ...words.done }
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
paths(slug: string) {
|
|
190
|
+
return { job: join(this.dir, `${slug}.json`), lock: join(this.dir, `${slug}.lock`), log: join(this.dir, `${slug}.log`) }
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Whether a job of this process tree already holds the stack's lock. */
|
|
194
|
+
holds(stack: Pick<JobStack, 'slug'>): boolean {
|
|
195
|
+
const { lock } = this.paths(stack.slug)
|
|
196
|
+
|
|
197
|
+
return heldHere().some(entry => entry.endsWith(` ${lock}`) && stillHeldBy(entry))
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Takes the stack's lock for `verb`, refusing while another action holds it; a lock its process left behind is taken over. */
|
|
201
|
+
claim(stack: JobStack, verb: string, detached: boolean, service?: string): Job {
|
|
202
|
+
const { job: jobFile, lock, log } = this.paths(stack.slug)
|
|
203
|
+
|
|
204
|
+
mkdirSync(this.dir, { recursive: true })
|
|
205
|
+
|
|
206
|
+
for (let attempt = 0; ; attempt++) {
|
|
207
|
+
try {
|
|
208
|
+
writeFileSync(lock, detached ? '' : String(process.pid), { flag: 'wx' })
|
|
209
|
+
|
|
210
|
+
break
|
|
211
|
+
}
|
|
212
|
+
catch (error) {
|
|
213
|
+
if ((error as NodeJS.ErrnoException).code !== 'EEXIST')
|
|
214
|
+
throw error
|
|
215
|
+
|
|
216
|
+
if (attempt > 0 || held(lock) || !takeOver(lock)) {
|
|
217
|
+
const running = readJob(jobFile)
|
|
218
|
+
|
|
219
|
+
throw new KnownError(`${stack.ref} is already ${doing(this.doing, running)}${running !== undefined && running.log !== '' ? `, see ${running.log}` : ''}`)
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const job: Job = { ref: stack.ref, verb, ...service !== undefined && { service }, pid: detached ? null : process.pid, startedAt: Date.now(), log: detached ? log : '' }
|
|
225
|
+
|
|
226
|
+
writeJob(jobFile, job)
|
|
227
|
+
|
|
228
|
+
return job
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** The detached process takes over the job its parent claimed, writing its own pid into the lock. */
|
|
232
|
+
adopt(stack: Pick<JobStack, 'slug'>): Job {
|
|
233
|
+
const { job: jobFile, lock } = this.paths(stack.slug)
|
|
234
|
+
const job = readJob(jobFile)
|
|
235
|
+
|
|
236
|
+
if (!job)
|
|
237
|
+
throw new KnownError(`no job claimed for ${stack.slug}`)
|
|
238
|
+
|
|
239
|
+
job.pid = process.pid
|
|
240
|
+
writeFileSync(lock, String(process.pid))
|
|
241
|
+
writeJob(jobFile, job)
|
|
242
|
+
|
|
243
|
+
return job
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
finish(stack: Pick<JobStack, 'slug'>, job: Job, code: number): void {
|
|
247
|
+
const { job: jobFile, lock } = this.paths(stack.slug)
|
|
248
|
+
|
|
249
|
+
writeJob(jobFile, { ...job, endedAt: Date.now(), code })
|
|
250
|
+
rmSync(lock, { force: true })
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** What every stack's job file says, by slug. */
|
|
254
|
+
states(): Map<string, JobState> {
|
|
255
|
+
const states = new Map<string, JobState>()
|
|
256
|
+
let files: string[]
|
|
257
|
+
|
|
258
|
+
try {
|
|
259
|
+
files = readdirSync(this.dir).filter(file => file.endsWith('.json'))
|
|
260
|
+
}
|
|
261
|
+
catch {
|
|
262
|
+
return states
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
for (const file of files) {
|
|
266
|
+
const slug = file.slice(0, -'.json'.length)
|
|
267
|
+
const job = readJob(join(this.dir, file))
|
|
268
|
+
|
|
269
|
+
if (!job)
|
|
270
|
+
continue
|
|
271
|
+
|
|
272
|
+
if (job.endedAt !== undefined)
|
|
273
|
+
states.set(slug, { last: job })
|
|
274
|
+
else if (held(this.paths(slug).lock))
|
|
275
|
+
states.set(slug, { running: job })
|
|
276
|
+
// Killed before it could say: it ended at the latest when its job file was last written, so a mod waiting on it sees it end
|
|
277
|
+
else
|
|
278
|
+
states.set(slug, { last: { ...job, code: null, endedAt: statSync(join(this.dir, file), { throwIfNoEntry: false })?.mtimeMs ?? job.startedAt } })
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
return states
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** Drops what a stack's jobs left, for a worktree removed for good: one made again under its name starts with no history. */
|
|
285
|
+
forget(stack: Pick<JobStack, 'slug'>): void {
|
|
286
|
+
for (const file of Object.values(this.paths(stack.slug)))
|
|
287
|
+
rmSync(file, { force: true })
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Runs `work` under the stack's lock and records how it ended; in the detached process `detach` started, it takes over the claimed job and notifies at the end.
|
|
292
|
+
* Run by a job of this process tree holding the same lock, it runs inside that job, which records the end.
|
|
293
|
+
* A known failure prints its message alone, anything else its stack trace; either way the job ends with code 1.
|
|
294
|
+
*/
|
|
295
|
+
async run(stack: JobStack, verb: string, work: (detached: boolean) => Promise<void>, service?: string): Promise<number> {
|
|
296
|
+
if (this.holds(stack))
|
|
297
|
+
return attempt(async () => work(false))
|
|
298
|
+
|
|
299
|
+
const detached = process.env[this.env] === stack.slug
|
|
300
|
+
const job = detached ? this.adopt(stack) : this.claim(stack, verb, false, service)
|
|
301
|
+
const before = process.env[HELD_ENV]
|
|
302
|
+
|
|
303
|
+
// The commands the job runs are not the job, but they work under its lock
|
|
304
|
+
delete process.env[this.env]
|
|
305
|
+
process.env[HELD_ENV] = [...heldHere(), heldEntry(this.paths(stack.slug).lock)].join('\n')
|
|
306
|
+
|
|
307
|
+
let code = 1
|
|
308
|
+
|
|
309
|
+
try {
|
|
310
|
+
code = await attempt(async () => work(detached))
|
|
311
|
+
}
|
|
312
|
+
finally {
|
|
313
|
+
if (before === undefined)
|
|
314
|
+
delete process.env[HELD_ENV]
|
|
315
|
+
else
|
|
316
|
+
process.env[HELD_ENV] = before
|
|
317
|
+
|
|
318
|
+
this.finish(stack, job, code)
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
if (detached)
|
|
322
|
+
await this.notifyEnd(stack, job, code)
|
|
323
|
+
|
|
324
|
+
return code
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* A detached process failing before its job started (its worktree gone, a ref it cannot read) ends the job its parent claimed, and says so, since no terminal shows it.
|
|
329
|
+
* Once `run` runs, it has taken the job over and this does nothing.
|
|
330
|
+
*/
|
|
331
|
+
async failDetached(message: string): Promise<void> {
|
|
332
|
+
const slug = process.env[this.env]
|
|
333
|
+
|
|
334
|
+
if (slug === undefined || slug === '')
|
|
335
|
+
return
|
|
336
|
+
|
|
337
|
+
delete process.env[this.env]
|
|
338
|
+
|
|
339
|
+
const job = readJob(this.paths(slug).job)
|
|
340
|
+
|
|
341
|
+
if (!job || job.endedAt !== undefined)
|
|
342
|
+
return
|
|
343
|
+
|
|
344
|
+
this.finish({ slug }, job, 1)
|
|
345
|
+
await notify(this.app, `${this.what(job)} failed`, message, true)
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Runs a command: a known failure (a stack the config does not know, an action refused) ends it with its message instead of a stack trace.
|
|
350
|
+
* `bg` names a `--bg` action, whose refusal is notified as well; a detached run failing before its job started ends that job.
|
|
351
|
+
*/
|
|
352
|
+
async guarded(action: () => Promise<number | void>, bg?: string): Promise<number> {
|
|
353
|
+
try {
|
|
354
|
+
return await action() ?? 0
|
|
355
|
+
}
|
|
356
|
+
catch (error) {
|
|
357
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
358
|
+
|
|
359
|
+
await this.failDetached(message)
|
|
360
|
+
|
|
361
|
+
if (bg !== undefined)
|
|
362
|
+
await this.notifyRefused(bg, message)
|
|
363
|
+
|
|
364
|
+
return failKnown(error)
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** A `--bg` command refused before any job started (the stack busy, a ref it does not know): told by notification, as its caller is often a popup that closed meanwhile. */
|
|
369
|
+
async notifyRefused(what: string, message: string): Promise<void> {
|
|
370
|
+
await notify(this.app, `${what} not started`, message, true)
|
|
371
|
+
}
|
|
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. */
|
|
374
|
+
detach(stack: JobStack, verb: string, argv: string[], service?: string): Job {
|
|
375
|
+
const job = this.claim(stack, verb, true, service)
|
|
376
|
+
const out = openSync(job.log, 'w')
|
|
377
|
+
|
|
378
|
+
try {
|
|
379
|
+
Bun.spawn(['setsid', '-f', process.execPath, '--no-env-file', ...argv], { stdio: ['ignore', out, out], env: { ...process.env, [this.env]: stack.slug } })
|
|
380
|
+
}
|
|
381
|
+
catch (error) {
|
|
382
|
+
this.finish(stack, job, 1)
|
|
383
|
+
|
|
384
|
+
throw error
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
return job
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/** The command a job ran, as a notification title names it: "athome restart booki php". */
|
|
391
|
+
what(job: Pick<Job, 'verb' | 'ref' | 'service'>): string {
|
|
392
|
+
return [this.app, job.verb, job.ref, ...job.service !== undefined ? [job.service] : []].join(' ')
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/** How a detached job ended, for the user who may have moved on: a failure always, an up, restart of the whole stack or new worktree once done. */
|
|
396
|
+
async notifyEnd(stack: Pick<JobStack, 'ref'>, job: Job, code: number): Promise<void> {
|
|
397
|
+
const done = job.service !== undefined ? undefined : this.done[job.verb]
|
|
398
|
+
|
|
399
|
+
if (code === 0) {
|
|
400
|
+
if (done)
|
|
401
|
+
await notify(this.app, `${stack.ref} ${done[0]}`, done[1])
|
|
402
|
+
|
|
403
|
+
return
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
await notify(this.app, `${this.what(job)} failed`, logTail(job.log) || `see ${job.log}`, true)
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
async function attempt(work: () => Promise<void>): Promise<number> {
|
|
411
|
+
try {
|
|
412
|
+
await work()
|
|
413
|
+
|
|
414
|
+
return 0
|
|
415
|
+
}
|
|
416
|
+
catch (error) {
|
|
417
|
+
console.error(error instanceof KnownError ? `error: ${error.message}` : error)
|
|
418
|
+
|
|
419
|
+
return 1
|
|
420
|
+
}
|
|
421
|
+
}
|