|
1
|
#!/usr/bin/env node |
|
2
|
// Loaded before anything reads the environment, so a local `.env` can hold the |
|
3
|
// Cloudinary credentials. It never overwrites a variable that is already set, |
|
4
|
// which leaves CI — where they arrive as real environment variables — alone. |
|
5
|
import "dotenv/config" |
|
6
|
|
|
7
|
import { spawn } from "node:child_process" |
|
8
|
import { mkdir, writeFile } from "node:fs/promises" |
|
9
|
import { dirname, resolve } from "node:path" |
|
10
|
|
|
11
|
import { Command, InvalidArgumentError } from "commander" |
|
12
|
|
|
13
|
import { |
|
14
|
bannerOf, |
|
15
|
BANNER_TYPES, |
|
16
|
DEFAULT_BANNER_TYPE, |
|
17
|
type BannerInput, |
|
18
|
type BannerType, |
|
19
|
} from "./banners/index.js" |
|
20
|
import { uploadBanner } from "./cloudinary.js" |
|
21
|
import { writePreview } from "./preview.js" |
|
22
|
import { withRandomSuffix } from "./public-id.js" |
|
23
|
import { renderPng, renderSvg } from "./render.js" |
|
24
|
|
|
25
|
const DEFAULT_OUT_DIR = "out" |
|
26
|
|
|
27
|
/** |
|
28
|
* Opens a path in the OS's default application, detached so the CLI can exit |
|
29
|
* without waiting on the browser. |
|
30
|
* |
|
31
|
* Failing to open is not worth failing the command over — the page has already |
|
32
|
* been written by this point — so errors are reported and swallowed. |
|
33
|
*/ |
|
34
|
function openInBrowser(target: string): void { |
|
35
|
const windows = process.platform === "win32" |
|
36
|
const command = windows |
|
37
|
? "start" |
|
38
|
: process.platform === "darwin" |
|
39
|
? "open" |
|
40
|
: "xdg-open" |
|
41
|
|
|
42
|
// `start` is a shell builtin, and takes a window title before the path. |
|
43
|
const args = windows ? ["", target] : [target] |
|
44
|
|
|
45
|
const child = spawn(command, args, { |
|
46
|
stdio: "ignore", |
|
47
|
detached: true, |
|
48
|
shell: windows, |
|
49
|
}) |
|
50
|
|
|
51
|
child.on("error", (error: Error) => { |
|
52
|
process.stderr.write(`Could not open ${target}: ${error.message}\n`) |
|
53
|
}) |
|
54
|
|
|
55
|
child.unref() |
|
56
|
} |
|
57
|
|
|
58
|
function parseType(value: string): BannerType { |
|
59
|
if (!(BANNER_TYPES as string[]).includes(value)) { |
|
60
|
throw new InvalidArgumentError(`expected one of ${BANNER_TYPES.join(", ")}`) |
|
61
|
} |
|
62
|
|
|
63
|
return value as BannerType |
|
64
|
} |
|
65
|
|
|
66
|
type ContentOptions = { |
|
67
|
type: BannerType |
|
68
|
version?: string |
|
69
|
date?: string |
|
70
|
} |
|
71
|
|
|
72
|
/** |
|
73
|
* Builds a banner input from the flags that describe its content. |
|
74
|
* |
|
75
|
* This is the one place that knows which flags belong to which type; everything |
|
76
|
* downstream goes through the type's definition. |
|
77
|
*/ |
|
78
|
function inputFrom(options: ContentOptions): BannerInput { |
|
79
|
switch (options.type) { |
|
80
|
case "release": |
|
81
|
if (!options.version) { |
|
82
|
throw new Error("--version is required for the release banner") |
|
83
|
} |
|
84
|
|
|
85
|
return { type: "release", version: options.version } |
|
86
|
|
|
87
|
case "cloud-changelog": |
|
88
|
if (!options.date) { |
|
89
|
throw new Error("--date is required for the cloud-changelog banner") |
|
90
|
} |
|
91
|
|
|
92
|
return { type: "cloud-changelog", date: options.date } |
|
93
|
} |
|
94
|
} |
|
95
|
|
|
96
|
/** Builds an input from a bare positional value, for the preview command. */ |
|
97
|
function inputFromValue(type: BannerType, value: string): BannerInput { |
|
98
|
switch (type) { |
|
99
|
case "release": |
|
100
|
return { type, version: value } |
|
101
|
case "cloud-changelog": |
|
102
|
return { type, date: value } |
|
103
|
} |
|
104
|
} |
|
105
|
|
|
106
|
type SpecOptions = { |
|
107
|
type: BannerType |
|
108
|
width?: string |
|
109
|
fontSize?: string |
|
110
|
gap?: string |
|
111
|
} |
|
112
|
|
|
113
|
/** |
|
114
|
* Turns the design flags into spec overrides. |
|
115
|
* |
|
116
|
* The values are checked against the spec the type actually declares, so a flag |
|
117
|
* that means nothing for a type is reported instead of silently ignored. |
|
118
|
*/ |
|
119
|
function specFrom(options: SpecOptions): BannerInput["spec"] { |
|
120
|
const { spec: defaults } = bannerOf(options.type) |
|
121
|
const overrides: Record<string, unknown> = {} |
|
122
|
const content: Record<string, unknown> = {} |
|
123
|
|
|
124
|
if (options.width) { |
|
125
|
const width = Number(options.width) |
|
126
|
|
|
127
|
// Hold the reference aspect ratio so the spec's proportions survive. |
|
128
|
overrides.width = width |
|
129
|
overrides.height = Math.round((width * defaults.height) / defaults.width) |
|
130
|
} |
|
131
|
|
|
132
|
if (options.fontSize) { |
|
133
|
content.fontSize = Number(options.fontSize) |
|
134
|
} |
|
135
|
|
|
136
|
if (options.gap) { |
|
137
|
content.gap = Number(options.gap) |
|
138
|
} |
|
139
|
|
|
140
|
if (Object.keys(content).length) { |
|
141
|
if (!defaults.content) { |
|
142
|
throw new Error( |
|
143
|
`the ${options.type} banner has no content section to override` |
|
144
|
) |
|
145
|
} |
|
146
|
|
|
147
|
overrides.content = content |
|
148
|
} |
|
149
|
|
|
150
|
return overrides as BannerInput["spec"] |
|
151
|
} |
|
152
|
|
|
153
|
const program = new Command() |
|
154
|
|
|
155
|
program |
|
156
|
.name("release-banner") |
|
157
|
.description("Generates the banner images used in Medusa release notes") |
|
158
|
|
|
159
|
/** Flags shared by every command that renders a banner from content. */ |
|
160
|
function withContentOptions(command: Command): Command { |
|
161
|
return command |
|
162
|
.option( |
|
163
|
"--type <type>", |
|
164
|
`banner type: ${BANNER_TYPES.join(" | ")}`, |
|
165
|
parseType, |
|
166
|
DEFAULT_BANNER_TYPE |
|
167
|
) |
|
168
|
.option( |
|
169
|
"-v, --version <version>", |
|
170
|
`release: version to render, with or without the leading "v"` |
|
171
|
) |
|
172
|
.option( |
|
173
|
"-d, --date <date>", |
|
174
|
"cloud-changelog: date shown after the wordmark, rendered as given" |
|
175
|
) |
|
176
|
} |
|
177
|
|
|
178
|
withContentOptions( |
|
179
|
program.command("render").description("Render a single banner") |
|
180
|
) |
|
181
|
.option("-o, --out <path>", "output file; defaults to out/<label>.png") |
|
182
|
.option( |
|
183
|
"-f, --format <format>", |
|
184
|
`"png" or "svg"; inferred from --out when it ends in .svg`, |
|
185
|
"png" |
|
186
|
) |
|
187
|
.option("-w, --width <px>", "canvas width; the height scales with it") |
|
188
|
.option("--font-size <px>", "override the main text size") |
|
189
|
.option("--gap <px>", "override the space between the logo and the text") |
|
190
|
.action(async (options) => { |
|
191
|
const definition = bannerOf(options.type) |
|
192
|
const input = definition.normalize(inputFrom(options)) as BannerInput |
|
193
|
const out = resolve( |
|
194
|
options.out ?? `${DEFAULT_OUT_DIR}/${definition.label(input)}.png` |
|
195
|
) |
|
196
|
const format = out.endsWith(".svg") ? "svg" : options.format |
|
197
|
|
|
198
|
const withSpec = { ...input, spec: specFrom(options) } as BannerInput |
|
199
|
|
|
200
|
const output = |
|
201
|
format === "svg" |
|
202
|
? Buffer.from(await renderSvg(withSpec)) |
|
203
|
: await renderPng(withSpec) |
|
204
|
|
|
205
|
await mkdir(dirname(out), { recursive: true }) |
|
206
|
await writeFile(out, output) |
|
207
|
|
|
208
|
process.stdout.write(`${out}\n`) |
|
209
|
}) |
|
210
|
|
|
211
|
program |
|
212
|
.command("preview") |
|
213
|
.description("Render a spread of banners and a page to review them on") |
|
214
|
.argument( |
|
215
|
"[values...]", |
|
216
|
"versions or titles to render; defaults to a set spread" |
|
217
|
) |
|
218
|
.option( |
|
219
|
"--type <type>", |
|
220
|
`banner type: ${BANNER_TYPES.join(" | ")}`, |
|
221
|
parseType, |
|
222
|
DEFAULT_BANNER_TYPE |
|
223
|
) |
|
224
|
.option("-o, --out-dir <path>", "output directory", DEFAULT_OUT_DIR) |
|
225
|
.option("--no-open", "write the page without opening it") |
|
226
|
.action(async (values: string[], options) => { |
|
227
|
const out = await writePreview({ |
|
228
|
type: options.type, |
|
229
|
outDir: resolve(options.outDir), |
|
230
|
inputs: values.map((value) => inputFromValue(options.type, value)), |
|
231
|
onRender: (file) => process.stdout.write(`rendered ${file}\n`), |
|
232
|
}) |
|
233
|
|
|
234
|
process.stdout.write(`\n${out}\n`) |
|
235
|
|
|
236
|
// There is no browser to open the page in on CI, and nobody to look at it. |
|
237
|
if (options.open && !process.env.CI) { |
|
238
|
openInBrowser(out) |
|
239
|
} |
|
240
|
}) |
|
241
|
|
|
242
|
withContentOptions( |
|
243
|
program |
|
244
|
.command("upload") |
|
245
|
.description("Render a banner and upload it to Cloudinary") |
|
246
|
) |
|
247
|
.option( |
|
248
|
"--folder <folder>", |
|
249
|
"Cloudinary folder to upload into; defaults to the type's own folder" |
|
250
|
) |
|
251
|
.option( |
|
252
|
"--dry-run", |
|
253
|
"render and report the target public ID without uploading" |
|
254
|
) |
|
255
|
.action(async (options) => { |
|
256
|
const definition = bannerOf(options.type) |
|
257
|
const input = definition.normalize(inputFrom(options)) as BannerInput |
|
258
|
const folder = options.folder ?? definition.folder |
|
259
|
const publicId = withRandomSuffix(definition.publicId(input)) |
|
260
|
const png = await renderPng(input) |
|
261
|
|
|
262
|
if (options.dryRun) { |
|
263
|
// The suffix is drawn fresh every run, so this reports the shape of the |
|
264
|
// target rather than the ID a real upload would end up at. |
|
265
|
process.stdout.write( |
|
266
|
`would upload ${png.length} bytes to ${folder}/${publicId}\n` |
|
267
|
) |
|
268
|
return |
|
269
|
} |
|
270
|
|
|
271
|
const { url } = await uploadBanner({ png, folder, publicId }) |
|
272
|
|
|
273
|
// The URL is the point of this command — keep it the only thing on stdout so |
|
274
|
// a caller can read it straight off. |
|
275
|
process.stdout.write(`${url}\n`) |
|
276
|
}) |
|
277
|
|
|
278
|
try { |
|
279
|
await program.parseAsync() |
|
280
|
} catch (error) { |
|
281
|
// A missing credential or an upload rejection is an ordinary outcome to report, |
|
282
|
// not a crash to dump a stack trace for. |
|
283
|
const message = error instanceof Error ? error.message : String(error) |
|
284
|
|
|
285
|
process.stderr.write(`${message}\n`) |
|
286
|
process.exitCode = 1 |
|
287
|
} |