Repository navigation
Expand file tree
/
Copy pathinit.ts
More file actions
105 lines (92 loc) · 4.36 KB
/
Copy pathinit.ts
File metadata and controls
105 lines (92 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { Effect, Option } from "effect"
import { Command, Flag } from "effect/cli"
import type { ConfigFile, SettingsPatch } from "#adapters/config.ts"
import { builtIn, ConfigStore, encode, merge, read, withDefaults, withRepo, write } from "#adapters/config.ts"
import { currentRepo, requireAuth } from "#adapters/gh.ts"
import { Paint } from "#adapters/paint.ts"
import { stateDirectory } from "#adapters/store.ts"
import { print } from "#cli/block.ts"
import { asUserError } from "#cli/exit.ts"
import { Effort } from "#terms/review.ts"
const effortFlag = Flag.Literals("effort", Effort.literals).pipe(
Flag.withDescription("How much a review run spends on this repository"),
Flag.optional
)
const baseFlag = Flag.String("base").pipe(
Flag.withDescription("The branch this repository's pull requests target, over the default one"),
Flag.optional
)
/** The settings the flags asked for, and only those. */
const asked = (base: Option.Option<string>, effort: Option.Option<Effort>): SettingsPatch => ({
...(Option.isSome(base) ? { base: base.value } : {}),
...(Option.isSome(effort) ? { review: { effort: effort.value } } : {})
})
/** What a review will open on, as the setup prints it back. */
const opening = (defaults: SettingsPatch): string => {
const review = { ...builtIn.review, ...defaults.review }
return review.command === null
? "my own prompt"
: [review.command, review.effort].filter((part) => part !== null).join(" ")
}
const row = (label: string, value: string): string => `${label.padEnd(12)}${value}`
/**
* Both the machine setup and the repository registration: there is deliberately
* no separate `setup` command.
*
* The first run on a machine checks `gh` and spells the defaults out in the
* configuration file. Run inside a repository, it also registers that
* `owner/repo`, taking the name from `gh` so I never type it. Run again, it
* changes what the flags name, keeps every other setting the file already had,
* and leaves the file untouched where nothing was decided differently.
*
* It asks nothing. Reviews run on Claude Code, and what a run opens on is
* `review.command` and `review.prompt` - a line and a paragraph that belong in
* the file rather than in a terminal prompt.
*
* `--effort` and `--base` are about one repository, so they land on the
* repository this ran in, or in the defaults when it ran outside one.
*/
export const init = Command.make(
"init",
{ effort: effortFlag, base: baseFlag },
Effect.fn("init")(
function* ({ base, effort }) {
yield* requireAuth
const config = yield* ConfigStore
const before = yield* read
const file: ConfigFile = Option.getOrElse(before, (): ConfigFile => ({}))
// A defaults block is what says this machine has been set up. On a first
// run the built-in defaults go under whatever the file already said, so
// spelling them out cannot overwrite a setting I chose by hand.
const firstRun = file.defaults === undefined
const defaults = firstRun ? merge(builtIn, file.defaults ?? {}) : (file.defaults ?? {})
const state = yield* stateDirectory
const repo = yield* currentRepo.pipe(
Effect.asSome,
Effect.catchTag("NoRepository", () => Effect.succeedNone)
)
const overrides = asked(base, effort)
const written = Option.isSome(repo)
? withRepo(withDefaults(file, defaults), repo.value, overrides)
: withDefaults(file, merge(defaults, overrides))
if (encode(written) !== encode(file) || Option.isNone(before)) {
yield* write(written)
}
const paint = yield* Paint
yield* print([
row("review", opening(written.defaults ?? {})),
row("config", paint.dim(config.path)),
row("state", paint.dim(state)),
Option.isNone(repo)
? row("repository", "none here - run dw-mc init inside a repository to register it")
: row(
"repository",
`${repo.value} (${file.repos?.[repo.value] === undefined ? "registered" : "already registered"})`
)
])
},
// The failures worth a sentence become one, so a machine or a file that
// needs fixing says what to fix instead of printing a stack.
Effect.catchTag(["ConfigMalformed", "GhUnauthenticated", "GhUnavailable", "GhUnreadable"], asUserError)
)
).pipe(Command.withDescription("Set this machine up and register the repository I am in"))