@workover/cli 0.14.4 → 0.25.0

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/site-proxy.mjs ADDED
@@ -0,0 +1,77 @@
1
+ // `workover site proxy --write`: put a platform's proxy setup (site-setup.mjs, mountSetup) into the
2
+ // website's own repository, so an agent working there adds it the way it adds any other change.
3
+ //
4
+ // Only where the file can be written without guessing at code it has not seen:
5
+ //
6
+ // - vercel.json and netlify.toml are merged: the two rules go FIRST (both platforms take the first
7
+ // match, and a single-page app's catch-all would otherwise win), and a rule already there for the
8
+ // path is left as it is, so running it twice changes nothing.
9
+ // - The Cloudflare Worker, nginx, Caddy and Apache snippets are files of their own, which the
10
+ // person includes (the lines to do it are returned).
11
+ // - Next.js, CloudFront and Amplify are not written: next.config is code, CloudFront's blocks go
12
+ // inside a resource, and Amplify's rules live in its console. The setup is printed instead.
13
+
14
+ import { readFileSync, writeFileSync } from 'fs';
15
+ import { join } from 'path';
16
+
17
+ const INCLUDE = {
18
+ nginx: (f) => `include ${f}; (inside the server { } block)`,
19
+ caddy: (f) => `import ${f} (inside the site block)`,
20
+ apache: (f) => `Include ${f} (inside the <VirtualHost>)`,
21
+ cloudflare: (f) => `npx wrangler deploy ${f} --name workover-docs --compatibility-date 2026-11-01, then add the two routes to the Worker`,
22
+ };
23
+
24
+ /** Merge two Vercel rewrites into vercel.json's text, first. Returns the new text and what changed. */
25
+ export function mergeVercel(text, recipeCode) {
26
+ const config = text ? JSON.parse(text) : {};
27
+ if (config.routes) throw new Error('vercel.json uses the legacy `routes`, which cannot be combined with rewrites. Add the rewrites by hand.');
28
+ const ours = JSON.parse(recipeCode).rewrites;
29
+ const have = Array.isArray(config.rewrites) ? config.rewrites : [];
30
+ const missing = ours.filter((r) => !have.some((h) => h.source === r.source));
31
+ if (!missing.length) return { text, changed: false };
32
+ config.rewrites = [...missing, ...have];
33
+ return { text: `${JSON.stringify(config, null, 2)}\n`, changed: true };
34
+ }
35
+
36
+ /** Put the two Netlify redirects before the first `[[redirects]]` in netlify.toml, or at its end. */
37
+ export function mergeNetlify(text, recipeCode) {
38
+ // [ \t], not \s: \s also matches newlines, which makes a ^-anchored multiline search slow.
39
+ const from = [...recipeCode.matchAll(/^[ \t]*from = "([^"]+)"/gm)].map((m) => m[1]);
40
+ const has = (f) => new RegExp(`^[ \\t]*from[ \\t]*=[ \\t]*"${f.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&')}"`, 'm').test(text ?? '');
41
+ if (from.every(has)) return { text, changed: false };
42
+ if (from.some(has)) throw new Error(`netlify.toml already has a redirect for one of ${from.join(' and ')}. Replace it with the setup printed above.`);
43
+ const block = `# Workover Help Center (workover site proxy)\n${recipeCode.trimEnd()}\n\n`;
44
+ if (!text) return { text: block, changed: true };
45
+ const at = text.search(/^\[\[redirects\]\]/m);
46
+ if (at >= 0) return { text: `${text.slice(0, at)}${block}${text.slice(at)}`, changed: true };
47
+ // Trailing newlines trimmed by hand: a regex anchored at the end is slow on a long run of them.
48
+ let end = text.length;
49
+ while (end > 0 && text[end - 1] === '\n') end--;
50
+ return { text: `${text.slice(0, end)}\n\n${block}`, changed: true };
51
+ }
52
+
53
+ /**
54
+ * Write `recipe` into `dir`. Returns `{ path, changed, include }`, or `{ path: null }` with the
55
+ * reason when this platform's setup cannot be written as a file.
56
+ */
57
+ export function writeRecipe(recipe, dir) {
58
+ if (!recipe.file)
59
+ return { path: null, reason: `${recipe.label}'s setup cannot be written as a file here. Add it as printed above.` };
60
+ const path = join(dir, recipe.file);
61
+ // Read it directly rather than check first, and create a new file with 'wx', so a file that
62
+ // appears in between is never overwritten.
63
+ let before = null;
64
+ try {
65
+ before = readFileSync(path, 'utf8');
66
+ } catch (err) {
67
+ if (err.code !== 'ENOENT') throw err;
68
+ }
69
+ let next;
70
+ if (recipe.platform === 'vercel') next = mergeVercel(before, recipe.code);
71
+ else if (recipe.platform === 'netlify') next = mergeNetlify(before, recipe.code);
72
+ else if (before !== null && before !== recipe.code)
73
+ throw new Error(`${path} already exists with other contents. Move it, or add the setup printed above by hand.`);
74
+ else next = { text: recipe.code, changed: before === null };
75
+ if (next.changed) writeFileSync(path, next.text, { flag: before === null ? 'wx' : 'w' });
76
+ return { path, changed: next.changed, include: INCLUDE[recipe.platform]?.(recipe.file) ?? null };
77
+ }