@able-autonomy/able 0.0.9 → 0.0.10

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/able.ts CHANGED
@@ -106,6 +106,18 @@ if (command && modules[command]) {
106
106
  await import(modules[command])
107
107
  process.exit(0)
108
108
  }
109
+ // An unknown command falls through to a just recipe, then a package.json script, with one dim line saying so.
110
+ async function fallback(name: string) {
111
+ const summary = Bun.which('just') ? Bun.spawnSync(['just', '--summary'], { stderr: 'ignore' }).stdout.toString().split(/\s+/) : []
112
+ if (summary.some((r) => r === name || r.startsWith(`${name}::`))) return ['just', name, ...args]
113
+ const pkg = Bun.file('package.json')
114
+ if ((await pkg.exists()) && (await pkg.json()).scripts?.[name]) return ['bun', 'run', name, ...args]
115
+ }
116
+ const delegate = command && command !== 'gh' ? await fallback(command) : undefined
117
+ if (delegate) {
118
+ console.error(`\x1b[2mable: no '${command}' command, running ${delegate.join(' ')}\x1b[0m`)
119
+ process.exit(Bun.spawnSync(delegate, { stdio: ['inherit', 'inherit', 'inherit'] }).exitCode ?? 1)
120
+ }
109
121
  if (command !== 'gh') {
110
122
  console.error(
111
123
  "usage: able gh <gh arguments> run gh as Able's GitHub App\n able sync [--dry-run] file follow-ups and write ended weeks' records\n able config [--json] print the host's able.config.ts\n able commit \"<message>\" [<path>…] a pathspec commit after the touched folders' checks\n able push pushes this branch with its tags\n able ws <new|ls|current|refresh|merge|rm|start|stop|status|take> workspaces",
package/config.ts CHANGED
@@ -28,8 +28,10 @@ export interface WorkspacesConfig {
28
28
  onRemove?: Command[]
29
29
  services?: Record<string, Service>
30
30
  /** Shared resources with one holder at a time on a machine (the tunnel, the devices): `able ws take` runs `take` and
31
- * records the holder. An `auto` one is taken by `able ws start` and goes back to its earlier holder at `able ws stop`. */
32
- resources?: Record<string, { take: Command; auto?: boolean }>
31
+ * records the holder. An `auto` one is taken by `able ws start` and goes back to its earlier holder at `able ws stop`.
32
+ * `holder`, when set, is asked instead of that record by `able ws ls`: run in the main checkout, it prints the holding
33
+ * checkouts' slugs or paths, one per line, so a resource the host moves with its own tools still shows. */
34
+ resources?: Record<string, { take: Command; auto?: boolean; holder?: Command }>
33
35
  }
34
36
 
35
37
  export interface AbleConfig {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@able-autonomy/able",
3
- "version": "0.0.9",
3
+ "version": "0.0.10",
4
4
  "description": "Able's command line and the config types a host's able.config.ts uses",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -1,9 +1,15 @@
1
1
  // able ws ls: the main checkout and every workspace, read live: offset, services, tree, shared resources held.
2
+ // A service counts as running when Able started it or its `ready` check answers; a resource's holders come from its
3
+ // `holder` command when the host gives one, otherwise from Able's record.
2
4
  // With --json, only the workspaces (slug, branch, path, portOffset, createdAt), for a host's own tools (84).
3
5
 
4
6
  import { existsSync } from 'node:fs'
7
+ import { resolve } from 'node:path'
8
+ import { loadConfig } from '../../config.ts'
5
9
  import { dirtyCount, mainRoot, unpushedCount } from '../git.ts'
6
- import { bold, dim, yellow } from '../ui.ts'
10
+ import { shellFor, type Place } from '../run.ts'
11
+ import { bold, dim, red, yellow } from '../ui.ts'
12
+ import { answers } from './services.ts'
7
13
  import { checkouts, holderOf, readState, running, workspaces } from './state.ts'
8
14
 
9
15
  const main = mainRoot()
@@ -13,19 +19,52 @@ if (process.argv.includes('--json')) {
13
19
  process.exit(0)
14
20
  }
15
21
  const rows = await checkouts(main, state)
22
+ const { services = {}, resources = {} } = (await loadConfig(main)).workspaces ?? {}
23
+ const home = rows[0]
24
+
25
+ const asked = (c: NonNullable<(typeof resources)[string]['holder']>, at: Place) => {
26
+ const { run, cwd } = typeof c === 'string' ? { run: c } : c
27
+ const sh = shellFor(at, run, cwd)
28
+ const r = Bun.spawnSync(sh.argv, { cwd: sh.cwd, env: sh.env, stdout: 'pipe', stderr: 'ignore' })
29
+ return r.exitCode === 0
30
+ ? r.stdout
31
+ .toString()
32
+ .split('\n')
33
+ .map((l) => l.trim())
34
+ .filter(Boolean)
35
+ : []
36
+ }
37
+ const holders = Object.fromEntries(
38
+ Object.entries(resources).map(([name, r]) => {
39
+ if (!r.holder || !home) return [name, rows.filter((w) => holderOf(state, name) === w.slug).map((w) => w.slug)]
40
+ const lines = asked(r.holder, home)
41
+ return [name, rows.filter((w) => lines.some((l) => l === w.slug || resolve(home.path, l) === resolve(w.path))).map((w) => w.slug)]
42
+ }),
43
+ )
44
+
45
+ const upCount = async (w: Place) => {
46
+ const started = await running(w.path)
47
+ const live = await Promise.all(Object.entries(services).map(async ([name, s]) => Boolean(started[name]) || (await answers(s, w))))
48
+ return live.filter(Boolean).length
49
+ }
50
+ const ups = await Promise.all(rows.map(upCount))
16
51
 
17
52
  console.log(`\n${bold(' CHECKOUT OFFSET SERVICES TREE')}`)
18
- for (const w of rows) {
19
- const up = Object.keys(await running(w.path)).length
53
+ rows.forEach((w, i) => {
54
+ const up = ups[i] ?? 0
20
55
  let tree = yellow('directory missing')
21
56
  if (existsSync(w.path)) {
22
57
  const dirty = dirtyCount(w.path)
23
58
  const ahead = unpushedCount(w.path)
24
59
  tree = [dirty ? yellow(`${dirty} dirty`) : dim('clean'), ahead ? yellow(`${ahead} unpushed`) : dim('0 unpushed')].join(dim(' · '))
25
60
  }
26
- const holds = Object.keys(state.holders ?? {}).filter((name) => holderOf(state, name) === w.slug)
27
- const services = up ? `${up} running`.padEnd(10) : dim('stopped'.padEnd(10))
28
- console.log(` ${w.slug.padEnd(16)} ${String(w.portOffset).padEnd(7)} ${services} ${tree}${holds.length ? bold(` holds ${holds.join(' + ')}`) : ''}`)
29
- }
61
+ const holds = Object.entries(holders)
62
+ .filter(([, slugs]) => slugs.includes(w.slug))
63
+ .map(([name, slugs]) => (slugs.length > 1 ? red(name) : name))
64
+ const column = up ? `${up} running`.padEnd(10) : dim('stopped'.padEnd(10))
65
+ console.log(` ${w.slug.padEnd(16)} ${String(w.portOffset).padEnd(7)} ${column} ${tree}${holds.length ? bold(` holds ${holds.join(' + ')}`) : ''}`)
66
+ })
67
+ for (const [name, slugs] of Object.entries(holders))
68
+ if (slugs.length > 1) console.log(red(`\n ${name} is held by ${slugs.join(' and ')} at once: able ws take ${name} in the one that should hold it`))
30
69
  if (rows.length === 1) console.log(dim('\n no workspaces: able ws new <slug>'))
31
70
  console.log()
@@ -43,11 +43,14 @@ const portOpen = (port: number) =>
43
43
  socket.on('error', () => done(false))
44
44
  })
45
45
 
46
+ /** Whether a service's `ready` check answers right now; a service without one is unknown. */
47
+ export const answers = async (s: Service, at: Place) =>
48
+ s.ready ? ('port' in s.ready ? portOpen(s.ready.port + at.portOffset) : runCommand({ run: s.ready.cmd, cwd: s.cwd }, at, { quiet: true })) : false
49
+
46
50
  async function ready(s: Service, at: Place, timeoutMs = 120_000): Promise<boolean> {
47
- const check = s.ready
48
- if (!check) return true
51
+ if (!s.ready) return true
49
52
  for (const until = Date.now() + timeoutMs; Date.now() < until; await Bun.sleep(250)) {
50
- if ('port' in check ? await portOpen(check.port + at.portOffset) : runCommand({ run: check.cmd, cwd: s.cwd }, at, { quiet: true })) return true
53
+ if (await answers(s, at)) return true
51
54
  }
52
55
  return false
53
56
  }