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/bin.ts
ADDED
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
import { dirname, resolve } from 'node:path'
|
|
3
|
+
|
|
4
|
+
import { EmailAddress } from '@couriercrow/core'
|
|
5
|
+
import { Effect, Layer, Option, Result, Schema } from 'effect'
|
|
6
|
+
import { FetchHttpClient } from 'effect/http'
|
|
7
|
+
|
|
8
|
+
import packageJson from '../package.json'
|
|
9
|
+
import { appVersionFor, isSourceCheckout, readAppVersion } from './build-config'
|
|
10
|
+
import { CloudflareLive } from './cloudflare/cloudflare-live'
|
|
11
|
+
import { deploy } from './deploy'
|
|
12
|
+
import {
|
|
13
|
+
accountChecks,
|
|
14
|
+
bundleCheck,
|
|
15
|
+
configCheck,
|
|
16
|
+
doctorFailedMessage,
|
|
17
|
+
doctorLines,
|
|
18
|
+
hasFailed,
|
|
19
|
+
localChecks
|
|
20
|
+
} from './doctor'
|
|
21
|
+
import { showCredentials } from './credentials-show'
|
|
22
|
+
import {
|
|
23
|
+
credentialsFromEnvironment,
|
|
24
|
+
readCloudflareSettings,
|
|
25
|
+
readCredentials,
|
|
26
|
+
readCredentialsSync
|
|
27
|
+
} from './environment'
|
|
28
|
+
import { initFlagExamples, initFlagNames, initProject } from './init'
|
|
29
|
+
import type { InitFlags } from './init'
|
|
30
|
+
import { defaultConfigPath, loadConfig } from './load-config'
|
|
31
|
+
import { MailboxApiLive } from './mailbox-api'
|
|
32
|
+
import { buildPlan } from './plan'
|
|
33
|
+
import { printable } from './printable'
|
|
34
|
+
import { purgeWithConfirmation } from './purge'
|
|
35
|
+
import { renderPlan } from './render'
|
|
36
|
+
import { rotateAppSecret } from './rotate-app-secret'
|
|
37
|
+
import { bunVersionProblem } from './runtime'
|
|
38
|
+
import { secretNames, setSecret } from './set-secret'
|
|
39
|
+
import type { SecretName } from './set-secret'
|
|
40
|
+
import { TerminalLive } from './terminal'
|
|
41
|
+
import { WranglerLive } from './wrangler'
|
|
42
|
+
|
|
43
|
+
export const cliVersion = packageJson.version
|
|
44
|
+
|
|
45
|
+
export const usage = `Usage: crow <command> [--config <path>]
|
|
46
|
+
|
|
47
|
+
Commands:
|
|
48
|
+
init Write package.json, mail.config.ts, .gitignore and .env.example here.
|
|
49
|
+
Asks for what it needs, or takes --hostname, --domain, --mailbox,
|
|
50
|
+
--name and --email.
|
|
51
|
+
doctor Check the token, its permissions, the plan, Zero Trust and the
|
|
52
|
+
domains. Nothing is changed.
|
|
53
|
+
plan [--take-over] Show what deploy would change. Nothing is changed.
|
|
54
|
+
deploy [--take-over] [--yes] [--skip-checks]
|
|
55
|
+
Apply the changes. --yes skips the question. --skip-checks
|
|
56
|
+
skips the checks of doctor that deploy runs first.
|
|
57
|
+
purge <address> Delete an archived mailbox and all its mail for good.
|
|
58
|
+
rotate-app-secret <name> Give an app a new secret. The old one stops working at once.
|
|
59
|
+
set-secret RESEND_API_KEY Store the Resend key in the app. Asks for it, or reads it from stdin.
|
|
60
|
+
credentials show Print the deploy credentials once, to store them as CI secrets.
|
|
61
|
+
|
|
62
|
+
--version Print the version of Courier Crow.`
|
|
63
|
+
|
|
64
|
+
export const configNeedsPath =
|
|
65
|
+
'--config needs a path, for example --config ./mail.config.ts'
|
|
66
|
+
|
|
67
|
+
export const purgeNeedsAddress =
|
|
68
|
+
'purge needs the address of an archived mailbox, for example: bunx couriercrow purge hello@acme.dev'
|
|
69
|
+
|
|
70
|
+
export const rotateNeedsName =
|
|
71
|
+
'rotate-app-secret needs the name of an app in mail.config.ts, for example: bunx couriercrow rotate-app-secret shop'
|
|
72
|
+
|
|
73
|
+
export const setSecretNeedsName =
|
|
74
|
+
'set-secret takes the name of the secret, and only RESEND_API_KEY for now: bunx couriercrow set-secret RESEND_API_KEY. It asks for the value, or reads it from stdin.'
|
|
75
|
+
|
|
76
|
+
export type Command =
|
|
77
|
+
| { _tag: 'CredentialsShow'; configPath: string }
|
|
78
|
+
| { _tag: 'Deploy'; configPath: string; skipChecks: boolean; takeOver: boolean; yes: boolean }
|
|
79
|
+
| { _tag: 'Doctor'; configPath: string }
|
|
80
|
+
| { _tag: 'Init'; flags: InitFlags }
|
|
81
|
+
| { _tag: 'Plan'; configPath: string; takeOver: boolean }
|
|
82
|
+
| { _tag: 'Purge'; address: EmailAddress; configPath: string }
|
|
83
|
+
| { _tag: 'RotateAppSecret'; configPath: string; name: string }
|
|
84
|
+
| { _tag: 'SetSecret'; configPath: string; name: SecretName }
|
|
85
|
+
| { _tag: 'Usage'; message: string }
|
|
86
|
+
| { _tag: 'Version' }
|
|
87
|
+
|
|
88
|
+
const commands = ['deploy', 'doctor', 'plan', 'purge', 'rotate-app-secret', 'set-secret'] as const
|
|
89
|
+
|
|
90
|
+
function isSecretName(value: string | undefined): value is SecretName {
|
|
91
|
+
return secretNames.some((name) => name === value)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function isCommand(value: string | undefined): value is (typeof commands)[number] {
|
|
95
|
+
return commands.some((command) => command === value)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const appNamePattern = /^[a-z0-9-]+$/
|
|
99
|
+
const decodeEmailAddress = Schema.decodeUnknownOption(EmailAddress)
|
|
100
|
+
|
|
101
|
+
// The app's files: built by bun run build in the repository, shipped inside
|
|
102
|
+
// an installed package.
|
|
103
|
+
const bundleDirectory = resolve(import.meta.dir, '..', 'bundle')
|
|
104
|
+
|
|
105
|
+
// A defect can carry text from Cloudflare or the API, so it is made
|
|
106
|
+
// printable like every other message.
|
|
107
|
+
export function defectMessage(defect: unknown): string {
|
|
108
|
+
const message = defect instanceof Error ? defect.message : String(defect)
|
|
109
|
+
|
|
110
|
+
return `Something went wrong inside Courier Crow: ${printable(message)}. This is a bug.`
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function isInitFlag(value: string): value is (typeof initFlagNames)[number] {
|
|
114
|
+
return initFlagNames.some((name) => name === value)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// crow init [--hostname <host>] [--domain <domain>] [--mailbox <name>]
|
|
118
|
+
// [--name <name>] [--email <address>]
|
|
119
|
+
function parseInit(rest: readonly string[]): Command {
|
|
120
|
+
const flags: InitFlags = {}
|
|
121
|
+
|
|
122
|
+
for (let index = 0; index < rest.length; index += 2) {
|
|
123
|
+
const name = (rest[index] ?? '').replace(/^--/, '')
|
|
124
|
+
const value = rest[index + 1]
|
|
125
|
+
|
|
126
|
+
if (!(rest[index] ?? '').startsWith('--') || !isInitFlag(name)) {
|
|
127
|
+
return { _tag: 'Usage', message: usage }
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (value === undefined || value.startsWith('--')) {
|
|
131
|
+
return {
|
|
132
|
+
_tag: 'Usage',
|
|
133
|
+
message: `--${name} needs a value, for example --${name} ${initFlagExamples[name]}`
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
flags[name] = value
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return { _tag: 'Init', flags }
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// crow credentials show [--config <path>]
|
|
144
|
+
function parseCredentials(rest: readonly string[]): Command {
|
|
145
|
+
if (rest[0] !== 'show' || rest.length === 2 || rest.length > 3) {
|
|
146
|
+
return rest.length === 2 && rest[1] === '--config'
|
|
147
|
+
? { _tag: 'Usage', message: configNeedsPath }
|
|
148
|
+
: { _tag: 'Usage', message: usage }
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const path = rest[2]
|
|
152
|
+
|
|
153
|
+
if (path === undefined) {
|
|
154
|
+
return { _tag: 'CredentialsShow', configPath: defaultConfigPath }
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (rest[1] !== '--config' || path.startsWith('--')) {
|
|
158
|
+
return { _tag: 'Usage', message: rest[1] === '--config' ? configNeedsPath : usage }
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return { _tag: 'CredentialsShow', configPath: path }
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function parseArguments(argv: readonly string[]): Command {
|
|
165
|
+
const [command, ...rest] = argv
|
|
166
|
+
|
|
167
|
+
if ((command === '--version' || command === '-v') && rest.length === 0) {
|
|
168
|
+
return { _tag: 'Version' }
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (command === 'init') {
|
|
172
|
+
return parseInit(rest)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (command === 'credentials') {
|
|
176
|
+
return parseCredentials(rest)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (!isCommand(command)) {
|
|
180
|
+
return { _tag: 'Usage', message: usage }
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const takesName =
|
|
184
|
+
command === 'purge' || command === 'rotate-app-secret' || command === 'set-secret'
|
|
185
|
+
let configPath = defaultConfigPath
|
|
186
|
+
let skipChecks = false
|
|
187
|
+
let takeOver = false
|
|
188
|
+
let yes = false
|
|
189
|
+
const positional: string[] = []
|
|
190
|
+
|
|
191
|
+
for (let index = 0; index < rest.length; index += 1) {
|
|
192
|
+
const argument = rest[index] ?? ''
|
|
193
|
+
|
|
194
|
+
if (argument === '--take-over' && !takesName && command !== 'doctor') {
|
|
195
|
+
takeOver = true
|
|
196
|
+
} else if (argument === '--yes' && command === 'deploy') {
|
|
197
|
+
yes = true
|
|
198
|
+
} else if (argument === '--skip-checks' && command === 'deploy') {
|
|
199
|
+
skipChecks = true
|
|
200
|
+
} else if (argument === '--config') {
|
|
201
|
+
const value = rest[index + 1]
|
|
202
|
+
|
|
203
|
+
if (value === undefined || value.startsWith('--')) {
|
|
204
|
+
return { _tag: 'Usage', message: configNeedsPath }
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
configPath = value
|
|
208
|
+
index += 1
|
|
209
|
+
} else if (takesName && !argument.startsWith('--')) {
|
|
210
|
+
positional.push(argument)
|
|
211
|
+
} else {
|
|
212
|
+
return { _tag: 'Usage', message: usage }
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (command === 'doctor') {
|
|
217
|
+
return { _tag: 'Doctor', configPath }
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (command === 'plan') {
|
|
221
|
+
return { _tag: 'Plan', configPath, takeOver }
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (command === 'deploy') {
|
|
225
|
+
return { _tag: 'Deploy', configPath, skipChecks, takeOver, yes }
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (command === 'rotate-app-secret') {
|
|
229
|
+
const [name] = positional
|
|
230
|
+
|
|
231
|
+
return positional.length === 1 && name !== undefined && appNamePattern.test(name)
|
|
232
|
+
? { _tag: 'RotateAppSecret', configPath, name }
|
|
233
|
+
: { _tag: 'Usage', message: rotateNeedsName }
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (command === 'set-secret') {
|
|
237
|
+
const [name] = positional
|
|
238
|
+
|
|
239
|
+
return positional.length === 1 && isSecretName(name)
|
|
240
|
+
? { _tag: 'SetSecret', configPath, name }
|
|
241
|
+
: { _tag: 'Usage', message: setSecretNeedsName }
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const address = decodeEmailAddress(positional[0])
|
|
245
|
+
|
|
246
|
+
if (positional.length !== 1 || address._tag === 'None') {
|
|
247
|
+
return { _tag: 'Usage', message: purgeNeedsAddress }
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return { _tag: 'Purge', address: address.value, configPath }
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// What a finished command leaves for the person: text for stdout and
|
|
254
|
+
// warnings for stderr.
|
|
255
|
+
interface Outcome {
|
|
256
|
+
text: string | undefined
|
|
257
|
+
warnings: readonly string[]
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// The API of the app, with the deploy credentials from the two variables or
|
|
261
|
+
// from the file next to the config. The file is read for every request:
|
|
262
|
+
// deploy creates it while it runs.
|
|
263
|
+
function apiLive(directory: string, hostname: string) {
|
|
264
|
+
return MailboxApiLive({
|
|
265
|
+
credentials: () => readCredentialsSync(directory, process.env),
|
|
266
|
+
credentialsFromEnvironment: credentialsFromEnvironment(process.env),
|
|
267
|
+
hostname
|
|
268
|
+
})
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function runPlan(command: Extract<Command, { _tag: 'Plan' }>) {
|
|
272
|
+
return Effect.gen(function* () {
|
|
273
|
+
const config = yield* loadConfig(command.configPath)
|
|
274
|
+
const settings = yield* readCloudflareSettings(process.env)
|
|
275
|
+
const directory = dirname(resolve(command.configPath))
|
|
276
|
+
|
|
277
|
+
// Names a variable that is set without the other.
|
|
278
|
+
yield* readCredentials(directory, process.env)
|
|
279
|
+
|
|
280
|
+
const appVersion = appVersionFor(
|
|
281
|
+
yield* readAppVersion(bundleDirectory, 'plan'),
|
|
282
|
+
config.app
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
const plan = yield* buildPlan(config, {
|
|
286
|
+
appVersion,
|
|
287
|
+
takeOver: command.takeOver
|
|
288
|
+
}).pipe(
|
|
289
|
+
Effect.provide(
|
|
290
|
+
Layer.mergeAll(
|
|
291
|
+
CloudflareLive({ ...settings, hostname: config.app.hostname }),
|
|
292
|
+
apiLive(directory, config.app.hostname)
|
|
293
|
+
).pipe(Layer.provide(FetchHttpClient.layer))
|
|
294
|
+
)
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
const outcome: Outcome = { text: renderPlan(plan, config), warnings: [] }
|
|
298
|
+
|
|
299
|
+
return outcome
|
|
300
|
+
})
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function runDeploy(command: Extract<Command, { _tag: 'Deploy' }>) {
|
|
304
|
+
return Effect.gen(function* () {
|
|
305
|
+
const config = yield* loadConfig(command.configPath)
|
|
306
|
+
const settings = yield* readCloudflareSettings(process.env)
|
|
307
|
+
const directory = dirname(resolve(command.configPath))
|
|
308
|
+
|
|
309
|
+
yield* readCredentials(directory, process.env)
|
|
310
|
+
|
|
311
|
+
const result = yield* deploy(config, {
|
|
312
|
+
accountId: settings.accountId,
|
|
313
|
+
bundleDirectory,
|
|
314
|
+
directory,
|
|
315
|
+
environment: process.env,
|
|
316
|
+
skipChecks: command.skipChecks,
|
|
317
|
+
takeOver: command.takeOver,
|
|
318
|
+
yes: command.yes
|
|
319
|
+
}).pipe(
|
|
320
|
+
Effect.provide(
|
|
321
|
+
Layer.mergeAll(
|
|
322
|
+
Layer.mergeAll(
|
|
323
|
+
CloudflareLive({ ...settings, hostname: config.app.hostname }),
|
|
324
|
+
apiLive(directory, config.app.hostname)
|
|
325
|
+
).pipe(Layer.provide(FetchHttpClient.layer)),
|
|
326
|
+
WranglerLive(settings),
|
|
327
|
+
TerminalLive
|
|
328
|
+
)
|
|
329
|
+
)
|
|
330
|
+
)
|
|
331
|
+
|
|
332
|
+
// Deploy printed its warnings to stderr when they happened.
|
|
333
|
+
void result
|
|
334
|
+
|
|
335
|
+
const outcome: Outcome = { text: undefined, warnings: [] }
|
|
336
|
+
|
|
337
|
+
return outcome
|
|
338
|
+
})
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function runPurge(command: Extract<Command, { _tag: 'Purge' }>) {
|
|
342
|
+
return Effect.gen(function* () {
|
|
343
|
+
const config = yield* loadConfig(command.configPath)
|
|
344
|
+
const directory = dirname(resolve(command.configPath))
|
|
345
|
+
|
|
346
|
+
const result = yield* purgeWithConfirmation(command.address).pipe(
|
|
347
|
+
Effect.provide(
|
|
348
|
+
Layer.mergeAll(
|
|
349
|
+
apiLive(directory, config.app.hostname).pipe(Layer.provide(FetchHttpClient.layer)),
|
|
350
|
+
TerminalLive
|
|
351
|
+
)
|
|
352
|
+
)
|
|
353
|
+
)
|
|
354
|
+
|
|
355
|
+
const outcome: Outcome = {
|
|
356
|
+
text: `Deleted ${command.address} and ${result.deletedMessages} messages.`,
|
|
357
|
+
warnings: []
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return outcome
|
|
361
|
+
})
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function runRotateAppSecret(
|
|
365
|
+
command: Extract<Command, { _tag: 'RotateAppSecret' }>
|
|
366
|
+
) {
|
|
367
|
+
return Effect.gen(function* () {
|
|
368
|
+
const config = yield* loadConfig(command.configPath)
|
|
369
|
+
const settings = yield* readCloudflareSettings(process.env)
|
|
370
|
+
const directory = dirname(resolve(command.configPath))
|
|
371
|
+
|
|
372
|
+
yield* rotateAppSecret(command.name, config.app.hostname).pipe(
|
|
373
|
+
Effect.provide(
|
|
374
|
+
Layer.mergeAll(
|
|
375
|
+
Layer.mergeAll(
|
|
376
|
+
CloudflareLive({ ...settings, hostname: config.app.hostname }),
|
|
377
|
+
apiLive(directory, config.app.hostname)
|
|
378
|
+
).pipe(Layer.provide(FetchHttpClient.layer)),
|
|
379
|
+
TerminalLive
|
|
380
|
+
)
|
|
381
|
+
)
|
|
382
|
+
)
|
|
383
|
+
|
|
384
|
+
// The command printed the new values itself.
|
|
385
|
+
const outcome: Outcome = { text: undefined, warnings: [] }
|
|
386
|
+
|
|
387
|
+
return outcome
|
|
388
|
+
})
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// Every check prints a line, also when an earlier one failed; the Cloudflare
|
|
392
|
+
// checks run once both variables are set. Fails when a line failed.
|
|
393
|
+
function runDoctor(command: Extract<Command, { _tag: 'Doctor' }>) {
|
|
394
|
+
return Effect.gen(function* () {
|
|
395
|
+
const checks = localChecks(Bun.version, process.env)
|
|
396
|
+
const loaded = yield* loadConfig(command.configPath).pipe(Effect.result)
|
|
397
|
+
const version = yield* readAppVersion(bundleDirectory, 'deploy').pipe(Effect.result)
|
|
398
|
+
const config = Result.isSuccess(loaded) ? loaded.success : undefined
|
|
399
|
+
const settings = yield* readCloudflareSettings(process.env).pipe(Effect.result)
|
|
400
|
+
|
|
401
|
+
checks.push(configCheck(loaded), bundleCheck(version))
|
|
402
|
+
|
|
403
|
+
if (Result.isSuccess(settings)) {
|
|
404
|
+
const hostname = config?.app.hostname ?? ''
|
|
405
|
+
const directory = dirname(resolve(command.configPath))
|
|
406
|
+
const credentials = yield* readCredentials(directory, process.env).pipe(Effect.result)
|
|
407
|
+
|
|
408
|
+
if (Result.isFailure(credentials)) {
|
|
409
|
+
checks.push({ mark: 'fail', text: credentials.failure.message })
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
checks.push(
|
|
413
|
+
...(yield* accountChecks(config, {
|
|
414
|
+
appVersion: Result.isSuccess(version)
|
|
415
|
+
? appVersionFor(version.success, config?.app)
|
|
416
|
+
: undefined,
|
|
417
|
+
forDeploy: false,
|
|
418
|
+
hasCredentials: Result.isSuccess(credentials) && Option.isSome(credentials.success)
|
|
419
|
+
}).pipe(
|
|
420
|
+
Effect.provide(
|
|
421
|
+
Layer.mergeAll(
|
|
422
|
+
CloudflareLive({ ...settings.success, hostname }),
|
|
423
|
+
apiLive(directory, hostname)
|
|
424
|
+
).pipe(Layer.provide(FetchHttpClient.layer))
|
|
425
|
+
)
|
|
426
|
+
))
|
|
427
|
+
)
|
|
428
|
+
} else {
|
|
429
|
+
checks.push({
|
|
430
|
+
mark: 'skip',
|
|
431
|
+
text: 'The Cloudflare checks need CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID.'
|
|
432
|
+
})
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
for (const line of doctorLines(checks)) {
|
|
436
|
+
console.log(line)
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
if (hasFailed(checks)) {
|
|
440
|
+
return yield* Effect.fail({ message: `\n${doctorFailedMessage}` })
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
const outcome: Outcome = { text: undefined, warnings: [] }
|
|
444
|
+
|
|
445
|
+
return outcome
|
|
446
|
+
})
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// Writes into the folder the command runs in. Needs no config and no
|
|
450
|
+
// Cloudflare settings.
|
|
451
|
+
function runInit(command: Extract<Command, { _tag: 'Init' }>) {
|
|
452
|
+
return Effect.gen(function* () {
|
|
453
|
+
// In the repository the bundle sits next to the API sources, as
|
|
454
|
+
// checkBundle tells them apart.
|
|
455
|
+
const fromSource = yield* Effect.promise(() => isSourceCheckout(bundleDirectory))
|
|
456
|
+
|
|
457
|
+
yield* initProject(process.cwd(), command.flags, { fromSource }).pipe(
|
|
458
|
+
Effect.provide(TerminalLive)
|
|
459
|
+
)
|
|
460
|
+
|
|
461
|
+
const outcome: Outcome = { text: undefined, warnings: [] }
|
|
462
|
+
|
|
463
|
+
return outcome
|
|
464
|
+
})
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// Needs only the Cloudflare settings: the Worker is found by its name. The
|
|
468
|
+
// value never passes through the Outcome.
|
|
469
|
+
function runSetSecret(command: Extract<Command, { _tag: 'SetSecret' }>) {
|
|
470
|
+
return Effect.gen(function* () {
|
|
471
|
+
const config = yield* loadConfig(command.configPath)
|
|
472
|
+
const settings = yield* readCloudflareSettings(process.env)
|
|
473
|
+
|
|
474
|
+
yield* setSecret(command.name).pipe(
|
|
475
|
+
Effect.provide(
|
|
476
|
+
Layer.mergeAll(
|
|
477
|
+
CloudflareLive({ ...settings, hostname: config.app.hostname }).pipe(
|
|
478
|
+
Layer.provide(FetchHttpClient.layer)
|
|
479
|
+
),
|
|
480
|
+
TerminalLive,
|
|
481
|
+
WranglerLive(settings)
|
|
482
|
+
)
|
|
483
|
+
)
|
|
484
|
+
)
|
|
485
|
+
|
|
486
|
+
const outcome: Outcome = { text: undefined, warnings: [] }
|
|
487
|
+
|
|
488
|
+
return outcome
|
|
489
|
+
})
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function runCommand(
|
|
493
|
+
command: Exclude<Command, { _tag: 'Usage' } | { _tag: 'Version' }>
|
|
494
|
+
): Effect.Effect<Outcome, { message: string }> {
|
|
495
|
+
switch (command._tag) {
|
|
496
|
+
case 'CredentialsShow':
|
|
497
|
+
return showCredentials(dirname(resolve(command.configPath))).pipe(
|
|
498
|
+
Effect.provide(TerminalLive),
|
|
499
|
+
Effect.as({ text: undefined, warnings: [] })
|
|
500
|
+
)
|
|
501
|
+
case 'Deploy':
|
|
502
|
+
return runDeploy(command)
|
|
503
|
+
case 'Doctor':
|
|
504
|
+
return runDoctor(command)
|
|
505
|
+
case 'Init':
|
|
506
|
+
return runInit(command)
|
|
507
|
+
case 'Plan':
|
|
508
|
+
return runPlan(command)
|
|
509
|
+
case 'Purge':
|
|
510
|
+
return runPurge(command)
|
|
511
|
+
case 'RotateAppSecret':
|
|
512
|
+
return runRotateAppSecret(command)
|
|
513
|
+
case 'SetSecret':
|
|
514
|
+
return runSetSecret(command)
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// bin/crow.js calls this once it knows it runs on Bun.
|
|
519
|
+
export async function main(): Promise<void> {
|
|
520
|
+
const tooOld = bunVersionProblem(Bun.version)
|
|
521
|
+
|
|
522
|
+
if (tooOld !== undefined) {
|
|
523
|
+
console.error(tooOld)
|
|
524
|
+
process.exit(1)
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
const command = parseArguments(process.argv.slice(2))
|
|
528
|
+
|
|
529
|
+
if (command._tag === 'Usage') {
|
|
530
|
+
console.error(command.message)
|
|
531
|
+
process.exit(1)
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
if (command._tag === 'Version') {
|
|
535
|
+
console.log(cliVersion)
|
|
536
|
+
|
|
537
|
+
return
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
try {
|
|
541
|
+
const running = runCommand(command)
|
|
542
|
+
|
|
543
|
+
const result = await Effect.runPromise(
|
|
544
|
+
running.pipe(
|
|
545
|
+
Effect.match({
|
|
546
|
+
onFailure: (error) => ({ _tag: 'Failed' as const, error }),
|
|
547
|
+
onSuccess: (outcome) => ({ _tag: 'Done' as const, outcome })
|
|
548
|
+
})
|
|
549
|
+
)
|
|
550
|
+
)
|
|
551
|
+
|
|
552
|
+
if (result._tag === 'Failed') {
|
|
553
|
+
console.error(result.error.message)
|
|
554
|
+
process.exit(1)
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
for (const warning of result.outcome.warnings) {
|
|
558
|
+
console.error(warning)
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
if (result.outcome.text !== undefined) {
|
|
562
|
+
console.log(result.outcome.text)
|
|
563
|
+
}
|
|
564
|
+
} catch (defect) {
|
|
565
|
+
// Only a bug gets here: every expected failure is an Effect error above.
|
|
566
|
+
console.error(defectMessage(defect))
|
|
567
|
+
|
|
568
|
+
if (process.env['COURIER_CROW_DEBUG'] === '1' && defect instanceof Error) {
|
|
569
|
+
console.error(defect.stack)
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
process.exit(1)
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
if (import.meta.main) {
|
|
577
|
+
await main()
|
|
578
|
+
}
|