couriercrow 0.0.0-stage → 0.1.1
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 +19 -2
- package/bin/couriercrow.js +5 -0
- package/bin/crow.js +14 -0
- package/bundle/migrations/0001_initial.sql +463 -0
- package/bundle/version.json +3 -0
- package/bundle/web/_headers +6 -0
- package/bundle/web/assets/index-Bim8rHvG.css +2 -0
- package/bundle/web/assets/index-YpJn_3e5.js +13 -0
- package/bundle/web/index.html +14 -0
- package/bundle/worker/README.md +1 -0
- package/bundle/worker/worker.js +57841 -0
- package/bundle/wrangler.jsonc +64 -0
- package/package.json +61 -4
- package/src/app-credentials.ts +413 -0
- package/src/bin.ts +578 -0
- package/src/build-config.ts +264 -0
- package/src/build-id.ts +39 -0
- package/src/cloudflare/cloudflare-client.ts +361 -0
- package/src/cloudflare/cloudflare-live-access.ts +454 -0
- package/src/cloudflare/cloudflare-live-account.ts +95 -0
- package/src/cloudflare/cloudflare-live-api.ts +87 -0
- package/src/cloudflare/cloudflare-live-checks.ts +151 -0
- package/src/cloudflare/cloudflare-live-zone.ts +363 -0
- package/src/cloudflare/cloudflare-live.ts +270 -0
- package/src/cloudflare/cloudflare-state.ts +152 -0
- package/src/cloudflare/first-run.ts +322 -0
- package/src/credentials-show.ts +49 -0
- package/src/deploy.ts +807 -0
- package/src/doctor.ts +504 -0
- package/src/environment.ts +141 -0
- package/src/index.ts +4 -0
- package/src/init.ts +332 -0
- package/src/load-config.ts +47 -0
- package/src/mailbox-api.ts +345 -0
- package/src/permissions.ts +133 -0
- package/src/plan.ts +517 -0
- package/src/printable.ts +8 -0
- package/src/product.ts +63 -0
- package/src/purge.ts +93 -0
- package/src/render.ts +406 -0
- package/src/rotate-app-secret.ts +53 -0
- package/src/runtime.ts +33 -0
- package/src/set-secret.ts +63 -0
- package/src/terminal.ts +163 -0
- package/src/wrangler.ts +168 -0
package/src/init.ts
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { writeFile } from 'node:fs/promises'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
|
|
4
|
+
import { validateMailConfig } from '@couriercrow/core'
|
|
5
|
+
import { Effect, Schema } from 'effect'
|
|
6
|
+
|
|
7
|
+
import packageJson from '../package.json'
|
|
8
|
+
import { tokenSteps } from './permissions'
|
|
9
|
+
import { localFolder } from './product'
|
|
10
|
+
import { Terminal } from './terminal'
|
|
11
|
+
|
|
12
|
+
// crow init: the files of a new config repository, written into the current
|
|
13
|
+
// folder. It never overwrites a file.
|
|
14
|
+
|
|
15
|
+
export class InitRefused extends Schema.TaggedError<InitRefused>()('InitRefused', {
|
|
16
|
+
message: Schema.String
|
|
17
|
+
}) {}
|
|
18
|
+
|
|
19
|
+
export interface InitAnswers {
|
|
20
|
+
domain: string
|
|
21
|
+
email: string
|
|
22
|
+
hostname: string
|
|
23
|
+
mailbox: string
|
|
24
|
+
name: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type InitFlags = Partial<InitAnswers>
|
|
28
|
+
|
|
29
|
+
export const initFlagNames = ['domain', 'email', 'hostname', 'mailbox', 'name'] as const
|
|
30
|
+
|
|
31
|
+
export const initFlagExamples: Record<keyof InitAnswers, string> = {
|
|
32
|
+
domain: 'acme.dev',
|
|
33
|
+
email: 'chris@example.com',
|
|
34
|
+
hostname: 'mail.acme.dev',
|
|
35
|
+
mailbox: 'hello',
|
|
36
|
+
name: 'Chris'
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const configExistsMessage = 'mail.config.ts already exists here. Nothing was changed.'
|
|
40
|
+
|
|
41
|
+
export const initNeedsTerminalMessage =
|
|
42
|
+
'Run bunx couriercrow init in a terminal, or pass --hostname, --domain, --mailbox, --name and --email.'
|
|
43
|
+
|
|
44
|
+
// A string in a single-quoted TypeScript literal.
|
|
45
|
+
function quoted(value: string): string {
|
|
46
|
+
return `'${value.replaceAll('\\', '\\\\').replaceAll("'", "\\'")}'`
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// The key of the person under people: the first word of their name in lower
|
|
50
|
+
// case, else the start of their email, else "me".
|
|
51
|
+
function personKey(answers: InitAnswers): string {
|
|
52
|
+
const candidates = [answers.name.split(/\s+/)[0] ?? '', answers.email.split('@')[0] ?? '']
|
|
53
|
+
|
|
54
|
+
for (const candidate of candidates) {
|
|
55
|
+
const key = candidate.toLowerCase().replace(/[^a-z0-9-]/g, '')
|
|
56
|
+
|
|
57
|
+
if (key !== '') {
|
|
58
|
+
return key
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return 'me'
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function configFile(answers: InitAnswers): string {
|
|
66
|
+
const key = personKey(answers)
|
|
67
|
+
|
|
68
|
+
return `import { defineMail } from 'couriercrow'
|
|
69
|
+
|
|
70
|
+
export default defineMail({
|
|
71
|
+
// Where the web app lives. It must be on a domain in your Cloudflare
|
|
72
|
+
// account. Deploy gives the app this hostname and puts Cloudflare Access in
|
|
73
|
+
// front of it.
|
|
74
|
+
app: { hostname: ${quoted(answers.hostname)} },
|
|
75
|
+
|
|
76
|
+
// The domains you receive and send mail for. Each one must be a zone in
|
|
77
|
+
// your Cloudflare account that uses Cloudflare's nameservers.
|
|
78
|
+
domains: {
|
|
79
|
+
${quoted(answers.domain)}: {
|
|
80
|
+
// One entry per address: ${answers.mailbox} is ${answers.mailbox}@${answers.domain}. Each mailbox lists
|
|
81
|
+
// the people who can read it and send from it. Everyone in a mailbox
|
|
82
|
+
// sees the same mail and the same drafts.
|
|
83
|
+
mailboxes: {
|
|
84
|
+
${quoted(answers.mailbox)}: { people: [${quoted(key)}] }
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
// Everyone who can sign in, with the email address they sign in with. The
|
|
90
|
+
// name is the sender name on the mail they send.
|
|
91
|
+
people: {
|
|
92
|
+
${quoted(key)}: { email: ${quoted(answers.email)}, name: ${quoted(answers.name)} }
|
|
93
|
+
}
|
|
94
|
+
})
|
|
95
|
+
`
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const packageFile = `${JSON.stringify(
|
|
99
|
+
{
|
|
100
|
+
private: true,
|
|
101
|
+
type: 'module',
|
|
102
|
+
scripts: { deploy: 'crow deploy', doctor: 'crow doctor', plan: 'crow plan' },
|
|
103
|
+
dependencies: { couriercrow: `^${packageJson.version}` }
|
|
104
|
+
},
|
|
105
|
+
null,
|
|
106
|
+
2
|
|
107
|
+
)}\n`
|
|
108
|
+
|
|
109
|
+
const gitignoreFile = `node_modules
|
|
110
|
+
${localFolder}
|
|
111
|
+
.env
|
|
112
|
+
`
|
|
113
|
+
|
|
114
|
+
const environmentExampleFile = `# Copy this file to .env and fill in both values. The crow command reads them
|
|
115
|
+
# from .env in the folder you run it from. Never commit .env.
|
|
116
|
+
|
|
117
|
+
# The id of your Cloudflare account: the dashboard shows it on the overview
|
|
118
|
+
# page of any domain, under API.
|
|
119
|
+
CLOUDFLARE_ACCOUNT_ID=
|
|
120
|
+
|
|
121
|
+
# An API token with the permissions crow init and crow doctor list.
|
|
122
|
+
CLOUDFLARE_API_TOKEN=
|
|
123
|
+
`
|
|
124
|
+
|
|
125
|
+
// In the order they are asked. The flag of each is its key in InitAnswers.
|
|
126
|
+
const questions: ReadonlyArray<{
|
|
127
|
+
flag: keyof InitAnswers
|
|
128
|
+
label: string
|
|
129
|
+
question: (answers: Partial<InitAnswers>) => string
|
|
130
|
+
}> = [
|
|
131
|
+
{
|
|
132
|
+
flag: 'domain',
|
|
133
|
+
label: 'a domain to receive mail for',
|
|
134
|
+
question: () =>
|
|
135
|
+
'The domain to receive mail for. It must be a zone in your Cloudflare account (for example acme.dev):'
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
flag: 'mailbox',
|
|
139
|
+
label: 'a mailbox',
|
|
140
|
+
question: () => 'The mailbox to create on that domain, the part before the @ (for example hello):'
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
flag: 'hostname',
|
|
144
|
+
label: 'the hostname of the web app',
|
|
145
|
+
question: (answers) =>
|
|
146
|
+
`The hostname of the web app, on a domain in your Cloudflare account [mail.${answers.domain ?? 'acme.dev'}]:`
|
|
147
|
+
},
|
|
148
|
+
{ flag: 'name', label: 'your name', question: () => 'Your name, the sender name on your mail:' },
|
|
149
|
+
{
|
|
150
|
+
flag: 'email',
|
|
151
|
+
label: 'your email address',
|
|
152
|
+
question: () => 'Your email address, to sign in with:'
|
|
153
|
+
}
|
|
154
|
+
]
|
|
155
|
+
|
|
156
|
+
// Domains, hostnames and mailbox names are lower case in the config.
|
|
157
|
+
function normalized(answers: InitAnswers): InitAnswers {
|
|
158
|
+
return {
|
|
159
|
+
...answers,
|
|
160
|
+
domain: answers.domain.toLowerCase(),
|
|
161
|
+
hostname: answers.hostname.toLowerCase(),
|
|
162
|
+
mailbox: answers.mailbox.toLowerCase()
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// The config the answers make. Answers not given yet are filled with the
|
|
167
|
+
// examples, which pass on their own, so a problem is one of the answers'.
|
|
168
|
+
function configOf(given: Partial<InitAnswers>) {
|
|
169
|
+
const domain = given.domain ?? initFlagExamples.domain
|
|
170
|
+
const answers = normalized({
|
|
171
|
+
domain,
|
|
172
|
+
email: given.email ?? initFlagExamples.email,
|
|
173
|
+
hostname: given.hostname ?? `mail.${domain}`,
|
|
174
|
+
mailbox: given.mailbox ?? initFlagExamples.mailbox,
|
|
175
|
+
name: given.name ?? initFlagExamples.name
|
|
176
|
+
})
|
|
177
|
+
const key = personKey(answers)
|
|
178
|
+
|
|
179
|
+
return {
|
|
180
|
+
app: { hostname: answers.hostname },
|
|
181
|
+
domains: { [answers.domain]: { mailboxes: { [answers.mailbox]: { people: [key] } } } },
|
|
182
|
+
people: { [key]: { email: answers.email, name: answers.name } }
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// What the config's own rules say about the answers so far, if anything.
|
|
187
|
+
function answersProblem(given: Partial<InitAnswers>) {
|
|
188
|
+
return validateMailConfig(configOf(given)).pipe(
|
|
189
|
+
Effect.match({ onFailure: (error) => error.message, onSuccess: () => undefined })
|
|
190
|
+
)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function askAnswers(flags: InitFlags) {
|
|
194
|
+
return Effect.gen(function* () {
|
|
195
|
+
const terminal = yield* Terminal
|
|
196
|
+
const complete = initFlagNames.every((name) => flags[name] !== undefined)
|
|
197
|
+
|
|
198
|
+
if (complete) {
|
|
199
|
+
return { ...initFlagExamples, ...flags }
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (!terminal.stdinIsTerminal) {
|
|
203
|
+
return yield* new InitRefused({ message: initNeedsTerminalMessage })
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const answers: Partial<InitAnswers> = { ...flags }
|
|
207
|
+
|
|
208
|
+
for (const { flag, label, question } of questions) {
|
|
209
|
+
if (answers[flag] !== undefined) {
|
|
210
|
+
continue
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Asked again, with the reason, until the config's rules take the
|
|
214
|
+
// answer. An empty answer (or the end of input) stops.
|
|
215
|
+
for (;;) {
|
|
216
|
+
const typed = (yield* terminal.ask(question(answers))).trim()
|
|
217
|
+
const answer = typed === '' && flag === 'hostname' ? `mail.${answers.domain ?? ''}` : typed
|
|
218
|
+
|
|
219
|
+
if (answer === '') {
|
|
220
|
+
return yield* new InitRefused({
|
|
221
|
+
message: `crow init needs ${label}. Nothing was changed. Run bunx couriercrow init again.`
|
|
222
|
+
})
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const problem = yield* answersProblem({ ...answers, [flag]: answer })
|
|
226
|
+
|
|
227
|
+
if (problem === undefined) {
|
|
228
|
+
answers[flag] = answer
|
|
229
|
+
|
|
230
|
+
break
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
yield* terminal.warn(problem)
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return { ...initFlagExamples, ...answers }
|
|
238
|
+
})
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function joined(names: readonly string[]): string {
|
|
242
|
+
return names.length <= 1
|
|
243
|
+
? (names[0] ?? '')
|
|
244
|
+
: `${names.slice(0, -1).join(', ')} and ${names.at(-1) ?? ''}`
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const keptMessages: Record<string, string> = {
|
|
248
|
+
'.env.example': '.env.example exists already and was left as it is.',
|
|
249
|
+
'.gitignore':
|
|
250
|
+
'.gitignore exists already and was left as it is. Make sure it lists node_modules, .courier-crow and .env.',
|
|
251
|
+
'package.json':
|
|
252
|
+
'package.json exists already and was left as it is. Add the command line to it with bun add couriercrow, and the scripts "doctor": "crow doctor", "plan": "crow plan" and "deploy": "crow deploy".'
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export interface InitOptions {
|
|
256
|
+
// The command line runs from a checkout of the repository, not from an
|
|
257
|
+
// installed package, so couriercrow is linked rather than installed.
|
|
258
|
+
fromSource: boolean
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const installStep = ' 1. Install the command line: bun install'
|
|
262
|
+
const linkStep =
|
|
263
|
+
' 1. Link the command line of your Courier Crow checkout: bun link --save couriercrow (run bun link in its packages/cli first)'
|
|
264
|
+
|
|
265
|
+
export function initProject(directory: string, flags: InitFlags, options: InitOptions) {
|
|
266
|
+
return Effect.gen(function* () {
|
|
267
|
+
const terminal = yield* Terminal
|
|
268
|
+
const exists = (name: string) => Effect.promise(() => Bun.file(join(directory, name)).exists())
|
|
269
|
+
|
|
270
|
+
if (yield* exists('mail.config.ts')) {
|
|
271
|
+
return yield* new InitRefused({ message: configExistsMessage })
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const answers = normalized(yield* askAnswers(flags))
|
|
275
|
+
const config = configFile(answers)
|
|
276
|
+
|
|
277
|
+
// The same config the file holds, checked before anything is written.
|
|
278
|
+
yield* validateMailConfig(configOf(answers)).pipe(
|
|
279
|
+
Effect.mapError(
|
|
280
|
+
(error) =>
|
|
281
|
+
new InitRefused({ message: `${error.message}\nNothing was changed. Run bunx couriercrow init again.` })
|
|
282
|
+
)
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
const created: string[] = []
|
|
286
|
+
const kept: string[] = []
|
|
287
|
+
|
|
288
|
+
for (const [name, content] of [
|
|
289
|
+
['package.json', packageFile],
|
|
290
|
+
['mail.config.ts', config],
|
|
291
|
+
['.gitignore', gitignoreFile],
|
|
292
|
+
['.env.example', environmentExampleFile]
|
|
293
|
+
] as const) {
|
|
294
|
+
if (name !== 'mail.config.ts' && (yield* exists(name))) {
|
|
295
|
+
kept.push(name)
|
|
296
|
+
|
|
297
|
+
continue
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// wx: a file that appeared meanwhile is not overwritten either.
|
|
301
|
+
yield* Effect.tryPromise({
|
|
302
|
+
try: () => writeFile(join(directory, name), content, { flag: 'wx' }),
|
|
303
|
+
catch: (reason) =>
|
|
304
|
+
new InitRefused({
|
|
305
|
+
message: `Could not write ${name}: ${reason instanceof Error ? reason.message : String(reason)}. Check the folder and run bunx couriercrow init again.`
|
|
306
|
+
})
|
|
307
|
+
})
|
|
308
|
+
created.push(name)
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
yield* terminal.print(`Created ${joined(created)}.`)
|
|
312
|
+
|
|
313
|
+
for (const name of kept) {
|
|
314
|
+
yield* terminal.print(keptMessages[name] ?? `${name} exists already and was left as it is.`)
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
for (const line of [
|
|
318
|
+
'',
|
|
319
|
+
'Next steps:',
|
|
320
|
+
options.fromSource ? linkStep : installStep,
|
|
321
|
+
...tokenSteps.map((line, index) => (index === 0 ? ` 2. ${line}` : ` ${line}`)),
|
|
322
|
+
' 3. Copy .env.example to .env and fill in CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN.',
|
|
323
|
+
' 4. Check the setup: bun run doctor',
|
|
324
|
+
' 5. See what deploy would change: bun run plan',
|
|
325
|
+
' 6. Deploy: bun run deploy',
|
|
326
|
+
'',
|
|
327
|
+
'The package scripts run the crow command installed in this folder. Elsewhere, type bunx couriercrow <command>.'
|
|
328
|
+
]) {
|
|
329
|
+
yield* terminal.print(line)
|
|
330
|
+
}
|
|
331
|
+
})
|
|
332
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { dirname, resolve } from 'node:path'
|
|
2
|
+
|
|
3
|
+
import { ConfigError, validateMailConfig } from '@couriercrow/core'
|
|
4
|
+
import type { MailConfig } from '@couriercrow/core'
|
|
5
|
+
import { Effect } from 'effect'
|
|
6
|
+
|
|
7
|
+
export const defaultConfigPath = './mail.config.ts'
|
|
8
|
+
|
|
9
|
+
// Bun runs TypeScript directly, so the config is imported as it is.
|
|
10
|
+
export function loadConfig(
|
|
11
|
+
path: string = defaultConfigPath
|
|
12
|
+
): Effect.Effect<MailConfig, ConfigError> {
|
|
13
|
+
return Effect.gen(function* () {
|
|
14
|
+
const file = resolve(path)
|
|
15
|
+
const exists = yield* Effect.promise(() => Bun.file(file).exists())
|
|
16
|
+
|
|
17
|
+
if (!exists) {
|
|
18
|
+
return yield* new ConfigError({
|
|
19
|
+
message: `No mail.config.ts found in ${dirname(file)}. Create one that default-exports defineMail({ ... }).`
|
|
20
|
+
})
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const loaded: unknown = yield* Effect.tryPromise({
|
|
24
|
+
try: () => import(file),
|
|
25
|
+
catch: (cause) =>
|
|
26
|
+
new ConfigError({
|
|
27
|
+
message: `Could not load ${file}: ${
|
|
28
|
+
cause instanceof Error ? cause.message : String(cause)
|
|
29
|
+
}. Fix the error and try again.`
|
|
30
|
+
})
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
const exported =
|
|
34
|
+
typeof loaded === 'object' && loaded !== null && 'default' in loaded
|
|
35
|
+
? loaded.default
|
|
36
|
+
: undefined
|
|
37
|
+
|
|
38
|
+
if (exported === undefined) {
|
|
39
|
+
return yield* new ConfigError({
|
|
40
|
+
message:
|
|
41
|
+
'mail.config.ts has no default export. Export the result of defineMail({ ... }) as default.'
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return yield* validateMailConfig(exported)
|
|
46
|
+
})
|
|
47
|
+
}
|