create-spillway 0.0.0-stage → 0.1.10
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 +40 -2
- package/dist/cli.d.ts +23 -0
- package/dist/cli.js +133 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +116 -0
- package/dist/index.js +291 -0
- package/dist/index.js.map +1 -0
- package/package.json +69 -3
- package/templates/astro/.env.example +27 -0
- package/templates/astro/README.md +57 -0
- package/templates/astro/_gitignore +11 -0
- package/templates/astro/astro.config.mjs +21 -0
- package/templates/astro/package.json +34 -0
- package/templates/astro/scripts/brand.mjs +40 -0
- package/templates/astro/src/env.d.ts +2 -0
- package/templates/astro/src/layouts/Layout.astro +57 -0
- package/templates/astro/src/lib/cache.ts +52 -0
- package/templates/astro/src/lib/spillway.ts +106 -0
- package/templates/astro/src/middleware.ts +18 -0
- package/templates/astro/src/pages/[...path].astro +26 -0
- package/templates/astro/src/pages/api/preview/exit.ts +4 -0
- package/templates/astro/src/pages/api/preview/index.ts +15 -0
- package/templates/astro/src/pages/api/revalidate.ts +34 -0
- package/templates/astro/src/pages/robots.txt.ts +6 -0
- package/templates/astro/src/pages/sitemap.xml.ts +19 -0
- package/templates/astro/src/styles/site.css +83 -0
- package/templates/astro/src/styles/spillway-brand.css +1 -0
- package/templates/astro/src/styles/spillway-brand.json +3 -0
- package/templates/astro/tsconfig.json +6 -0
- package/templates/next/.env.example +24 -0
- package/templates/next/README.md +48 -0
- package/templates/next/_gitignore +12 -0
- package/templates/next/app/[[...slug]]/page.tsx +56 -0
- package/templates/next/app/api/preview/exit/route.ts +6 -0
- package/templates/next/app/api/preview/route.ts +16 -0
- package/templates/next/app/api/revalidate/route.ts +41 -0
- package/templates/next/app/globals.css +83 -0
- package/templates/next/app/layout.tsx +71 -0
- package/templates/next/app/not-found.tsx +10 -0
- package/templates/next/app/robots.ts +10 -0
- package/templates/next/app/sitemap.ts +12 -0
- package/templates/next/app/spillway-brand.css +1 -0
- package/templates/next/app/spillway-brand.json +3 -0
- package/templates/next/components/Nav.tsx +30 -0
- package/templates/next/lib/spillway.ts +71 -0
- package/templates/next/next.config.ts +14 -0
- package/templates/next/package.json +34 -0
- package/templates/next/scripts/brand.mjs +40 -0
- package/templates/next/tsconfig.json +21 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 KWALL LLC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,41 @@
|
|
|
1
|
-
#
|
|
1
|
+
# create-spillway
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A front end of your own on a [Spillway CMS](https://spillwaycms.com) site, in one command. MIT licensed.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm create spillway@latest my-site
|
|
7
|
+
# or
|
|
8
|
+
pnpm create spillway my-site
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
It asks for the framework, your Spillway site's address and, optionally, an API token for draft previews, then writes a starter that renders the site's published pages with Spillway's own block library and keeps itself up to date:
|
|
12
|
+
|
|
13
|
+
- **Next.js** (App Router): every published path prerendered and revalidated by tag when Spillway's webhook arrives, draft mode previews.
|
|
14
|
+
- **Astro** (server output on the Node adapter): every page rendered on request, kept in an in-memory cache the webhook purges, cookie previews.
|
|
15
|
+
|
|
16
|
+
Both have one catch-all route for every published path and a 404 for the rest, Spillway's redirects, the SEO data (title, description, canonical, hreflang alternates, Open Graph, JSON-LD), the main and footer menus, the site's brand through [`@spillway-cms/brand`](https://github.com/kwall-llc/spillway-cms/tree/main/packages/brand), previews from release share links or drafts, a revalidation route that verifies the webhook signature, `sitemap.xml` and `robots.txt`. Each starter's README lists its files and the setup steps.
|
|
17
|
+
|
|
18
|
+
## Options
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm create spillway@latest my-site -- --template astro --url https://cms.example.org --yes
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
| Option | What it does |
|
|
25
|
+
| --- | --- |
|
|
26
|
+
| `[dir]` | The folder to create; it must be new or empty (a `.git` folder is fine) |
|
|
27
|
+
| `-t`, `--template <next\|astro>` | The starter |
|
|
28
|
+
| `-u`, `--url <url>` | The Spillway site's address |
|
|
29
|
+
| `--token <spw_...>` | An API token with `content.view_drafts`, for draft previews |
|
|
30
|
+
| `--no-brand` | Do not fetch the brand stylesheet |
|
|
31
|
+
| `-y`, `--yes` | Do not ask; take the defaults (Next.js, `my-spillway-site`) for what the flags leave out |
|
|
32
|
+
|
|
33
|
+
Without a terminal (in CI) it never asks. The address, the token and two generated secrets (for the webhook rule and the preview route) go in the starter's env file (`.env.local` for Next.js, `.env` for Astro), which its `.gitignore` keeps out of version control. The only network request is the brand stylesheet, fetched when `--url` is given; when the site cannot be reached the starter keeps a placeholder and its first build fetches the brand.
|
|
34
|
+
|
|
35
|
+
## After it runs
|
|
36
|
+
|
|
37
|
+
1. `cd my-site`, install, and run the dev server.
|
|
38
|
+
2. In Spillway, under **Site settings → Notifications**, add **Webhook** rules for published, unpublished, deleted, menu changed, redirects changed, site settings changed and brand published, pointing at `<your front end>/api/revalidate` with the `SPILLWAY_WEBHOOK_SECRET` from the env file.
|
|
39
|
+
3. Deploy it with the same variables and `SITE_URL` set to its public address.
|
|
40
|
+
|
|
41
|
+
See [the headless docs](https://github.com/kwall-llc/spillway-cms/blob/main/docs/adapters.md#start-a-front-end-create-spillway) for the API, the adapters and how revalidation works.
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { type FetchBrandOptions, type Flags, type TemplateName } from './index.js';
|
|
3
|
+
/** Asks one question and answers the trimmed reply (or the default for an empty reply). */
|
|
4
|
+
export type Ask = (question: string, fallback?: string) => Promise<string>;
|
|
5
|
+
export interface Answers {
|
|
6
|
+
dir: string;
|
|
7
|
+
template: TemplateName;
|
|
8
|
+
url?: string;
|
|
9
|
+
token?: string;
|
|
10
|
+
}
|
|
11
|
+
/** Fills in what the flags leave out: by asking when `ask` is given, else with the defaults. */
|
|
12
|
+
export declare function resolveAnswers(flags: Flags, ask?: Ask): Promise<Answers>;
|
|
13
|
+
export interface MainIo {
|
|
14
|
+
/** Whether to ask questions (a terminal on both ends); false in CI and pipes. */
|
|
15
|
+
interactive?: boolean;
|
|
16
|
+
ask?: Ask;
|
|
17
|
+
log?: (line: string) => void;
|
|
18
|
+
error?: (line: string) => void;
|
|
19
|
+
cwd?: string;
|
|
20
|
+
brandFetch?: FetchBrandOptions['fetch'];
|
|
21
|
+
}
|
|
22
|
+
/** Runs the command; resolves to the exit code. */
|
|
23
|
+
export declare function main(argv: string[], io?: MainIo): Promise<number>;
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* The `create-spillway` command (`npm create spillway@latest`, `pnpm create spillway`). Asks for what the flags leave out
|
|
4
|
+
* when it runs in a terminal; with `--yes`, or without a terminal, takes the defaults instead. See `index.ts`.
|
|
5
|
+
*/
|
|
6
|
+
import { createInterface } from 'node:readline/promises';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { DEFAULT_DIR, TEMPLATES, TEMPLATE_NAMES, USAGE, UsageError, fetchBrandFiles, isDirEmpty, nextSteps, normalizeUrl, ownVersion, packageManager, parseFlags, parseTemplate, scaffold, } from './index.js';
|
|
9
|
+
/** Fills in what the flags leave out: by asking when `ask` is given, else with the defaults. */
|
|
10
|
+
export async function resolveAnswers(flags, ask) {
|
|
11
|
+
let dir = flags.dir;
|
|
12
|
+
let template = flags.template;
|
|
13
|
+
let url = flags.url;
|
|
14
|
+
let token = flags.token;
|
|
15
|
+
if (ask) {
|
|
16
|
+
while (!dir) {
|
|
17
|
+
const reply = await ask('Folder for the new front end', DEFAULT_DIR);
|
|
18
|
+
if (reply && isDirEmpty(reply))
|
|
19
|
+
dir = reply;
|
|
20
|
+
else if (reply)
|
|
21
|
+
console.log(` ${reply} is not empty; choose another folder.`);
|
|
22
|
+
}
|
|
23
|
+
while (!template) {
|
|
24
|
+
const menu = TEMPLATE_NAMES.map((n, i) => `${i + 1}) ${TEMPLATES[n].label}: ${TEMPLATES[n].description}`).join('\n ');
|
|
25
|
+
const reply = await ask(`Framework:\n ${menu}\nChoose`, '1');
|
|
26
|
+
const byNumber = TEMPLATE_NAMES[Number(reply) - 1];
|
|
27
|
+
try {
|
|
28
|
+
template = byNumber ?? parseTemplate(reply);
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
console.log(` ${err instanceof Error ? err.message : String(err)}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
while (url === undefined) {
|
|
35
|
+
const reply = await ask('Spillway site address (empty to fill in .env later)', '');
|
|
36
|
+
if (!reply)
|
|
37
|
+
break;
|
|
38
|
+
try {
|
|
39
|
+
url = normalizeUrl(reply);
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
console.log(` ${err instanceof Error ? err.message : String(err)}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (token === undefined) {
|
|
46
|
+
const reply = await ask('API token for draft previews (optional, spw_...)', '');
|
|
47
|
+
if (reply)
|
|
48
|
+
token = reply;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return { dir: dir ?? DEFAULT_DIR, template: template ?? 'next', ...(url ? { url } : {}), ...(token ? { token } : {}) };
|
|
52
|
+
}
|
|
53
|
+
/** Runs the command; resolves to the exit code. */
|
|
54
|
+
export async function main(argv, io = {}) {
|
|
55
|
+
const log = io.log ?? ((line) => console.log(line));
|
|
56
|
+
const error = io.error ?? ((line) => console.error(line));
|
|
57
|
+
let flags;
|
|
58
|
+
try {
|
|
59
|
+
flags = parseFlags(argv);
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
error(`create-spillway: ${err instanceof Error ? err.message : String(err)}\n\n${USAGE}`);
|
|
63
|
+
return 1;
|
|
64
|
+
}
|
|
65
|
+
if (flags.help) {
|
|
66
|
+
log(USAGE);
|
|
67
|
+
return 0;
|
|
68
|
+
}
|
|
69
|
+
if (flags.version) {
|
|
70
|
+
log(ownVersion());
|
|
71
|
+
return 0;
|
|
72
|
+
}
|
|
73
|
+
const interactive = !flags.yes && (io.interactive ?? Boolean(process.stdin.isTTY && process.stdout.isTTY));
|
|
74
|
+
let rl;
|
|
75
|
+
let ask = io.ask;
|
|
76
|
+
if (interactive && !ask) {
|
|
77
|
+
rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
78
|
+
const iface = rl;
|
|
79
|
+
ask = async (question, fallback) => {
|
|
80
|
+
const reply = (await iface.question(`${question}${fallback ? ` (${fallback})` : ''}: `)).trim();
|
|
81
|
+
return reply || fallback || '';
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
let answers;
|
|
85
|
+
try {
|
|
86
|
+
answers = await resolveAnswers(flags, interactive ? ask : undefined);
|
|
87
|
+
}
|
|
88
|
+
finally {
|
|
89
|
+
rl?.close();
|
|
90
|
+
}
|
|
91
|
+
const cwd = io.cwd ?? process.cwd();
|
|
92
|
+
const target = path.resolve(cwd, answers.dir);
|
|
93
|
+
if (!isDirEmpty(target)) {
|
|
94
|
+
error(`create-spillway: ${answers.dir} is not empty; choose a new folder or empty this one`);
|
|
95
|
+
return 1;
|
|
96
|
+
}
|
|
97
|
+
let brand = null;
|
|
98
|
+
if (answers.url && flags.brand) {
|
|
99
|
+
const got = await fetchBrandFiles(answers.url, io.brandFetch ? { fetch: io.brandFetch } : {});
|
|
100
|
+
if ('error' in got)
|
|
101
|
+
log(`Could not fetch the brand from ${answers.url} (${got.error}); the first build will.`);
|
|
102
|
+
else {
|
|
103
|
+
brand = got;
|
|
104
|
+
log(`Fetched the brand (version ${got.version}).`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
let result;
|
|
108
|
+
try {
|
|
109
|
+
result = scaffold({ targetDir: target, template: answers.template, url: answers.url, token: answers.token, brand });
|
|
110
|
+
}
|
|
111
|
+
catch (err) {
|
|
112
|
+
if (err instanceof UsageError) {
|
|
113
|
+
error(`create-spillway: ${err.message}`);
|
|
114
|
+
return 1;
|
|
115
|
+
}
|
|
116
|
+
throw err;
|
|
117
|
+
}
|
|
118
|
+
log(`\nCreated the ${TEMPLATES[answers.template].label} starter in ${target} (${result.files.length} files).`);
|
|
119
|
+
if (!answers.url)
|
|
120
|
+
log(`Set SPILLWAY_URL in ${result.envFile} to your Spillway site's address.`);
|
|
121
|
+
log('\nNext steps:');
|
|
122
|
+
for (const line of nextSteps(result, answers.template, packageManager(), cwd))
|
|
123
|
+
log(line ? ` ${line}` : '');
|
|
124
|
+
return 0;
|
|
125
|
+
}
|
|
126
|
+
const invoked = process.argv[1] && /create-spillway$|cli\.js$/.test(process.argv[1]);
|
|
127
|
+
if (invoked) {
|
|
128
|
+
main(process.argv.slice(2)).then((code) => process.exit(code), (err) => {
|
|
129
|
+
console.error(`create-spillway: ${err instanceof Error ? err.message : String(err)}`);
|
|
130
|
+
process.exit(1);
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AACA;;;GAGG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EACL,WAAW,EACX,SAAS,EACT,cAAc,EACd,KAAK,EACL,UAAU,EACV,eAAe,EACf,UAAU,EACV,SAAS,EACT,YAAY,EACZ,UAAU,EACV,cAAc,EACd,UAAU,EACV,aAAa,EACb,QAAQ,GAKT,MAAM,YAAY,CAAC;AAYpB,gGAAgG;AAChG,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,KAAY,EAAE,GAAS;IAC1D,IAAI,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC;IACpB,IAAI,QAAQ,GAAG,KAAK,CAAC,QAAQ,CAAC;IAC9B,IAAI,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC;IACpB,IAAI,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC;IACxB,IAAI,GAAG,EAAE,CAAC;QACR,OAAO,CAAC,GAAG,EAAE,CAAC;YACZ,MAAM,KAAK,GAAG,MAAM,GAAG,CAAC,8BAA8B,EAAE,WAAW,CAAC,CAAC;YACrE,IAAI,KAAK,IAAI,UAAU,CAAC,KAAK,CAAC;gBAAE,GAAG,GAAG,KAAK,CAAC;iBACvC,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'next' | 'astro';
|
|
2
|
+
export interface TemplateInfo {
|
|
3
|
+
name: TemplateName;
|
|
4
|
+
label: string;
|
|
5
|
+
description: string;
|
|
6
|
+
/** The env file the framework reads in development (and the starter's scripts load). */
|
|
7
|
+
envFile: string;
|
|
8
|
+
/** The brand stylesheet `@spillway-cms/brand` writes, and the JSON file that records its version. */
|
|
9
|
+
brandCss: string;
|
|
10
|
+
brandJson: string;
|
|
11
|
+
/** The dev server's address, for SITE_URL. */
|
|
12
|
+
devUrl: string;
|
|
13
|
+
}
|
|
14
|
+
export declare const TEMPLATES: Record<TemplateName, TemplateInfo>;
|
|
15
|
+
export declare const TEMPLATE_NAMES: TemplateName[];
|
|
16
|
+
export declare const DEFAULT_DIR = "my-spillway-site";
|
|
17
|
+
export declare class UsageError extends Error {
|
|
18
|
+
constructor(message: string);
|
|
19
|
+
}
|
|
20
|
+
export interface Flags {
|
|
21
|
+
/** The target folder (the first positional argument). */
|
|
22
|
+
dir?: string;
|
|
23
|
+
template?: TemplateName;
|
|
24
|
+
url?: string;
|
|
25
|
+
token?: string;
|
|
26
|
+
/** Take the defaults for everything not given on the command line; never prompt. */
|
|
27
|
+
yes: boolean;
|
|
28
|
+
/** `--no-brand`: do not fetch the brand stylesheet even when `--url` is given. */
|
|
29
|
+
brand: boolean;
|
|
30
|
+
help: boolean;
|
|
31
|
+
version: boolean;
|
|
32
|
+
}
|
|
33
|
+
export declare const USAGE = "create-spillway: a front end of your own on Spillway CMS\n\nUsage:\n npm create spillway@latest [dir] -- [options]\n pnpm create spillway [dir] [options]\n\nOptions:\n -t, --template <next|astro> The starter: Next.js (App Router) or Astro (server output)\n -u, --url <url> The Spillway site's address, e.g. https://cms.example.org\n --token <spw_...> An API token with content.view_drafts, for draft previews (optional)\n --no-brand Do not fetch the brand stylesheet from --url\n -y, --yes Do not ask; take the defaults (template next, folder my-spillway-site)\n -h, --help Show this help\n -v, --version Show the version\n\nThe folder must be new or empty. The address and token go in the starter's env file, never in a file\nthat is committed. Only the brand stylesheet is fetched while scaffolding, and only when --url is given.";
|
|
34
|
+
/** Parses the command line. Throws `UsageError` on an unknown option, a missing value or an unknown template. */
|
|
35
|
+
export declare function parseFlags(argv: string[]): Flags;
|
|
36
|
+
/** `next`, `nextjs`, `Next.js` → `next`; `astro` → `astro`. */
|
|
37
|
+
export declare function parseTemplate(raw: string): TemplateName;
|
|
38
|
+
/**
|
|
39
|
+
* An http(s) address without a trailing slash; a bare host gets https (http for localhost, where Spillway runs in
|
|
40
|
+
* development). Throws `UsageError` for anything else.
|
|
41
|
+
*/
|
|
42
|
+
export declare function normalizeUrl(raw: string): string;
|
|
43
|
+
/** A valid npm package name from a folder name (`My Site!` → `my-site`). */
|
|
44
|
+
export declare function packageNameFrom(dir: string): string;
|
|
45
|
+
/** True when the folder does not exist, is empty, or holds only a `.git` folder. */
|
|
46
|
+
export declare function isDirEmpty(dir: string): boolean;
|
|
47
|
+
/** Where the templates live: next to `dist/` in the published package, next to `src/` in the repository. */
|
|
48
|
+
export declare function templatesRoot(): string;
|
|
49
|
+
/** Every file of a template, relative to its folder, in a stable order. */
|
|
50
|
+
export declare function templateFiles(template: TemplateName, root?: string): string[];
|
|
51
|
+
/** npm leaves `.gitignore` out of published packages, so templates carry it as `_gitignore`. */
|
|
52
|
+
export declare function outputName(file: string): string;
|
|
53
|
+
/** This package's version; the starter depends on the `@spillway-cms/*` packages at the same one. */
|
|
54
|
+
export declare function ownVersion(): string;
|
|
55
|
+
/** A random secret for the webhook rule and the preview route. */
|
|
56
|
+
export declare function randomSecret(): string;
|
|
57
|
+
export interface EnvValues {
|
|
58
|
+
url?: string;
|
|
59
|
+
token?: string;
|
|
60
|
+
webhookSecret: string;
|
|
61
|
+
previewSecret: string;
|
|
62
|
+
siteUrl: string;
|
|
63
|
+
}
|
|
64
|
+
/** The env file the starter reads: the Spillway address and token, the two secrets and the front end's own address. */
|
|
65
|
+
export declare function envFileContents(v: EnvValues): string;
|
|
66
|
+
/** The brand stylesheet as `@spillway-cms/brand` writes it, and its version. */
|
|
67
|
+
export interface BrandFiles {
|
|
68
|
+
css: string;
|
|
69
|
+
version: string;
|
|
70
|
+
}
|
|
71
|
+
export interface FetchBrandOptions {
|
|
72
|
+
fetch?: (input: string, init?: RequestInit) => Promise<Response>;
|
|
73
|
+
timeoutMs?: number;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The site's brand stylesheet (`GET /api/public/brand`), exactly what the starter's `brand` script writes. Never throws:
|
|
77
|
+
* an unreachable site, an older Spillway without the endpoint or a timeout gives `{ error }` and the starter keeps its
|
|
78
|
+
* placeholder until the first build fetches it.
|
|
79
|
+
*/
|
|
80
|
+
export declare function fetchBrandFiles(url: string, opts?: FetchBrandOptions): Promise<BrandFiles | {
|
|
81
|
+
error: string;
|
|
82
|
+
}>;
|
|
83
|
+
export interface ScaffoldOptions {
|
|
84
|
+
targetDir: string;
|
|
85
|
+
template: TemplateName;
|
|
86
|
+
url?: string;
|
|
87
|
+
token?: string;
|
|
88
|
+
/** The `@spillway-cms/*` version to depend on; this package's own by default. */
|
|
89
|
+
version?: string;
|
|
90
|
+
/** The brand stylesheet to write over the template's placeholder. */
|
|
91
|
+
brand?: BrandFiles | null;
|
|
92
|
+
/** Fixed secrets (tests); random ones by default. */
|
|
93
|
+
secrets?: {
|
|
94
|
+
webhook: string;
|
|
95
|
+
preview: string;
|
|
96
|
+
};
|
|
97
|
+
/** Where the templates are; this package's `templates/` by default. */
|
|
98
|
+
templatesRoot?: string;
|
|
99
|
+
}
|
|
100
|
+
export interface ScaffoldResult {
|
|
101
|
+
dir: string;
|
|
102
|
+
packageName: string;
|
|
103
|
+
/** Every file written, relative to `dir`. */
|
|
104
|
+
files: string[];
|
|
105
|
+
envFile: string;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Writes the starter. Refuses (with `UsageError`) a target that exists and is not empty, so it never overwrites anything.
|
|
109
|
+
*/
|
|
110
|
+
export declare function scaffold(opts: ScaffoldOptions): ScaffoldResult;
|
|
111
|
+
/** The template's package.json with the project's name and the `@spillway-cms/*` packages at `^version`. */
|
|
112
|
+
export declare function packageJsonFor(source: string, name: string, version: string): string;
|
|
113
|
+
/** The package manager that ran the command (from `npm_config_user_agent`), for the next steps. */
|
|
114
|
+
export declare function packageManager(userAgent?: string): 'npm' | 'pnpm' | 'yarn' | 'bun';
|
|
115
|
+
/** What to do after scaffolding, as lines to print. */
|
|
116
|
+
export declare function nextSteps(result: ScaffoldResult, template: TemplateName, pm: ReturnType<typeof packageManager>, cwd?: string): string[];
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* create-spillway: scaffolds a front end of its own on a Spillway CMS site (docs/adapters.md#start-a-front-end-create-spillway).
|
|
3
|
+
*
|
|
4
|
+
* `npm create spillway@latest my-site` asks for the framework, the Spillway site's address and an optional API token, then
|
|
5
|
+
* copies a starter from `templates/<name>` into the new folder: package.json named after the folder with the
|
|
6
|
+
* `@spillway-cms/*` packages at this version, a `.gitignore`, an env file with the address, the token and fresh webhook
|
|
7
|
+
* and preview secrets, and (when the address answers) the site's brand stylesheet. Nothing else touches the network.
|
|
8
|
+
*
|
|
9
|
+
* Everything the command does is exported here so it can be tested without a terminal; `cli.ts` is the thin wrapper.
|
|
10
|
+
*/
|
|
11
|
+
import crypto from 'node:crypto';
|
|
12
|
+
import fs from 'node:fs';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
import { fileURLToPath } from 'node:url';
|
|
15
|
+
import { parseArgs } from 'node:util';
|
|
16
|
+
import { brandStylesheet, fetchBrand } from '@spillway-cms/brand';
|
|
17
|
+
export const TEMPLATES = {
|
|
18
|
+
next: {
|
|
19
|
+
name: 'next',
|
|
20
|
+
label: 'Next.js',
|
|
21
|
+
description: 'the App Router, server components, on-demand revalidation by tag, draft mode previews',
|
|
22
|
+
envFile: '.env.local',
|
|
23
|
+
brandCss: 'app/spillway-brand.css',
|
|
24
|
+
brandJson: 'app/spillway-brand.json',
|
|
25
|
+
devUrl: 'http://localhost:3001',
|
|
26
|
+
},
|
|
27
|
+
astro: {
|
|
28
|
+
name: 'astro',
|
|
29
|
+
label: 'Astro',
|
|
30
|
+
description: 'server output on the Node adapter, an in-memory cache purged by the webhook, cookie previews',
|
|
31
|
+
envFile: '.env',
|
|
32
|
+
brandCss: 'src/styles/spillway-brand.css',
|
|
33
|
+
brandJson: 'src/styles/spillway-brand.json',
|
|
34
|
+
devUrl: 'http://localhost:4321',
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
export const TEMPLATE_NAMES = Object.keys(TEMPLATES);
|
|
38
|
+
export const DEFAULT_DIR = 'my-spillway-site';
|
|
39
|
+
export class UsageError extends Error {
|
|
40
|
+
constructor(message) {
|
|
41
|
+
super(message);
|
|
42
|
+
this.name = 'UsageError';
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export const USAGE = `create-spillway: a front end of your own on Spillway CMS
|
|
46
|
+
|
|
47
|
+
Usage:
|
|
48
|
+
npm create spillway@latest [dir] -- [options]
|
|
49
|
+
pnpm create spillway [dir] [options]
|
|
50
|
+
|
|
51
|
+
Options:
|
|
52
|
+
-t, --template <next|astro> The starter: Next.js (App Router) or Astro (server output)
|
|
53
|
+
-u, --url <url> The Spillway site's address, e.g. https://cms.example.org
|
|
54
|
+
--token <spw_...> An API token with content.view_drafts, for draft previews (optional)
|
|
55
|
+
--no-brand Do not fetch the brand stylesheet from --url
|
|
56
|
+
-y, --yes Do not ask; take the defaults (template next, folder ${DEFAULT_DIR})
|
|
57
|
+
-h, --help Show this help
|
|
58
|
+
-v, --version Show the version
|
|
59
|
+
|
|
60
|
+
The folder must be new or empty. The address and token go in the starter's env file, never in a file
|
|
61
|
+
that is committed. Only the brand stylesheet is fetched while scaffolding, and only when --url is given.`;
|
|
62
|
+
/** Parses the command line. Throws `UsageError` on an unknown option, a missing value or an unknown template. */
|
|
63
|
+
export function parseFlags(argv) {
|
|
64
|
+
let parsed;
|
|
65
|
+
try {
|
|
66
|
+
parsed = parseArgs({
|
|
67
|
+
args: argv,
|
|
68
|
+
allowPositionals: true,
|
|
69
|
+
allowNegative: true,
|
|
70
|
+
strict: true,
|
|
71
|
+
options: {
|
|
72
|
+
template: { type: 'string', short: 't' },
|
|
73
|
+
url: { type: 'string', short: 'u' },
|
|
74
|
+
token: { type: 'string' },
|
|
75
|
+
brand: { type: 'boolean', default: true },
|
|
76
|
+
yes: { type: 'boolean', short: 'y', default: false },
|
|
77
|
+
help: { type: 'boolean', short: 'h', default: false },
|
|
78
|
+
version: { type: 'boolean', short: 'v', default: false },
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
catch (err) {
|
|
83
|
+
throw new UsageError(err instanceof Error ? err.message : String(err));
|
|
84
|
+
}
|
|
85
|
+
const { values, positionals } = parsed;
|
|
86
|
+
if (positionals.length > 1)
|
|
87
|
+
throw new UsageError(`Give one folder, not ${positionals.length}: ${positionals.join(' ')}`);
|
|
88
|
+
const flags = { yes: values.yes === true, brand: values.brand !== false, help: values.help === true, version: values.version === true };
|
|
89
|
+
if (positionals[0])
|
|
90
|
+
flags.dir = positionals[0];
|
|
91
|
+
if (values.template !== undefined)
|
|
92
|
+
flags.template = parseTemplate(values.template);
|
|
93
|
+
if (values.url !== undefined)
|
|
94
|
+
flags.url = normalizeUrl(values.url);
|
|
95
|
+
if (values.token !== undefined && values.token !== '')
|
|
96
|
+
flags.token = values.token;
|
|
97
|
+
return flags;
|
|
98
|
+
}
|
|
99
|
+
/** `next`, `nextjs`, `Next.js` → `next`; `astro` → `astro`. */
|
|
100
|
+
export function parseTemplate(raw) {
|
|
101
|
+
const key = raw.trim().toLowerCase().replace(/[.\s-]/g, '');
|
|
102
|
+
if (key === 'next' || key === 'nextjs')
|
|
103
|
+
return 'next';
|
|
104
|
+
if (key === 'astro')
|
|
105
|
+
return 'astro';
|
|
106
|
+
throw new UsageError(`Unknown template "${raw}": choose ${TEMPLATE_NAMES.join(' or ')}`);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* An http(s) address without a trailing slash; a bare host gets https (http for localhost, where Spillway runs in
|
|
110
|
+
* development). Throws `UsageError` for anything else.
|
|
111
|
+
*/
|
|
112
|
+
export function normalizeUrl(raw) {
|
|
113
|
+
let value = raw.trim();
|
|
114
|
+
if (!value)
|
|
115
|
+
throw new UsageError('The Spillway address is empty');
|
|
116
|
+
if (!/^[a-z][a-z0-9+.-]*:\/\//i.test(value))
|
|
117
|
+
value = `${/^(localhost|127\.0\.0\.1|\[::1\])(:|\/|$)/i.test(value) ? 'http' : 'https'}://${value}`;
|
|
118
|
+
let url;
|
|
119
|
+
try {
|
|
120
|
+
url = new URL(value);
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
throw new UsageError(`"${raw}" is not an address; give the Spillway site's URL, e.g. https://cms.example.org`);
|
|
124
|
+
}
|
|
125
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:')
|
|
126
|
+
throw new UsageError(`"${raw}" must start with http:// or https://`);
|
|
127
|
+
if (url.username || url.password)
|
|
128
|
+
throw new UsageError('Leave the user name and password out of the address');
|
|
129
|
+
url.search = '';
|
|
130
|
+
url.hash = '';
|
|
131
|
+
return url.toString().replace(/\/+$/, '');
|
|
132
|
+
}
|
|
133
|
+
/** A valid npm package name from a folder name (`My Site!` → `my-site`). */
|
|
134
|
+
export function packageNameFrom(dir) {
|
|
135
|
+
const base = path.basename(path.resolve(dir));
|
|
136
|
+
const name = base
|
|
137
|
+
.toLowerCase()
|
|
138
|
+
.replace(/[^a-z0-9._~-]+/g, '-')
|
|
139
|
+
.replace(/^[._-]+/, '')
|
|
140
|
+
.replace(/-+$/, '')
|
|
141
|
+
.slice(0, 214);
|
|
142
|
+
return name || 'spillway-site';
|
|
143
|
+
}
|
|
144
|
+
/** True when the folder does not exist, is empty, or holds only a `.git` folder. */
|
|
145
|
+
export function isDirEmpty(dir) {
|
|
146
|
+
if (!fs.existsSync(dir))
|
|
147
|
+
return true;
|
|
148
|
+
if (!fs.statSync(dir).isDirectory())
|
|
149
|
+
return false;
|
|
150
|
+
return fs.readdirSync(dir).every((entry) => entry === '.git');
|
|
151
|
+
}
|
|
152
|
+
/** Where the templates live: next to `dist/` in the published package, next to `src/` in the repository. */
|
|
153
|
+
export function templatesRoot() {
|
|
154
|
+
return path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', 'templates');
|
|
155
|
+
}
|
|
156
|
+
/** Every file of a template, relative to its folder, in a stable order. */
|
|
157
|
+
export function templateFiles(template, root = templatesRoot()) {
|
|
158
|
+
const base = path.join(root, template);
|
|
159
|
+
const out = [];
|
|
160
|
+
const walk = (dir) => {
|
|
161
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
162
|
+
if (entry.name === 'node_modules' || entry.name === '.next' || entry.name === '.astro' || entry.name === 'dist')
|
|
163
|
+
continue;
|
|
164
|
+
const full = path.join(dir, entry.name);
|
|
165
|
+
if (entry.isDirectory())
|
|
166
|
+
walk(full);
|
|
167
|
+
else
|
|
168
|
+
out.push(path.relative(base, full).split(path.sep).join('/'));
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
walk(base);
|
|
172
|
+
return out;
|
|
173
|
+
}
|
|
174
|
+
/** npm leaves `.gitignore` out of published packages, so templates carry it as `_gitignore`. */
|
|
175
|
+
export function outputName(file) {
|
|
176
|
+
return file.split('/').map((part) => (part === '_gitignore' ? '.gitignore' : part)).join('/');
|
|
177
|
+
}
|
|
178
|
+
/** This package's version; the starter depends on the `@spillway-cms/*` packages at the same one. */
|
|
179
|
+
export function ownVersion() {
|
|
180
|
+
const pkg = JSON.parse(fs.readFileSync(path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', 'package.json'), 'utf8'));
|
|
181
|
+
return pkg.version;
|
|
182
|
+
}
|
|
183
|
+
/** A random secret for the webhook rule and the preview route. */
|
|
184
|
+
export function randomSecret() {
|
|
185
|
+
return crypto.randomBytes(24).toString('base64url');
|
|
186
|
+
}
|
|
187
|
+
/** The env file the starter reads: the Spillway address and token, the two secrets and the front end's own address. */
|
|
188
|
+
export function envFileContents(v) {
|
|
189
|
+
return [
|
|
190
|
+
'# Written by create-spillway. It holds secrets: keep it out of version control (.gitignore lists it).',
|
|
191
|
+
'# .env.example explains each value.',
|
|
192
|
+
`SPILLWAY_URL=${v.url ?? 'http://localhost:3000'}`,
|
|
193
|
+
`SPILLWAY_TOKEN=${v.token ?? ''}`,
|
|
194
|
+
'SPILLWAY_SITE=',
|
|
195
|
+
`SPILLWAY_WEBHOOK_SECRET=${v.webhookSecret}`,
|
|
196
|
+
`SPILLWAY_PREVIEW_SECRET=${v.previewSecret}`,
|
|
197
|
+
`SITE_URL=${v.siteUrl}`,
|
|
198
|
+
'',
|
|
199
|
+
].join('\n');
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* The site's brand stylesheet (`GET /api/public/brand`), exactly what the starter's `brand` script writes. Never throws:
|
|
203
|
+
* an unreachable site, an older Spillway without the endpoint or a timeout gives `{ error }` and the starter keeps its
|
|
204
|
+
* placeholder until the first build fetches it.
|
|
205
|
+
*/
|
|
206
|
+
export async function fetchBrandFiles(url, opts = {}) {
|
|
207
|
+
try {
|
|
208
|
+
const brand = await fetchBrand(url, { ...(opts.fetch ? { fetch: opts.fetch } : {}), requestInit: { signal: AbortSignal.timeout(opts.timeoutMs ?? 10_000) } });
|
|
209
|
+
return { css: brandStylesheet(brand), version: brand.version };
|
|
210
|
+
}
|
|
211
|
+
catch (err) {
|
|
212
|
+
return { error: err instanceof Error ? err.message : String(err) };
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Writes the starter. Refuses (with `UsageError`) a target that exists and is not empty, so it never overwrites anything.
|
|
217
|
+
*/
|
|
218
|
+
export function scaffold(opts) {
|
|
219
|
+
const info = TEMPLATES[opts.template];
|
|
220
|
+
if (!info)
|
|
221
|
+
throw new UsageError(`Unknown template "${String(opts.template)}"`);
|
|
222
|
+
const dir = path.resolve(opts.targetDir);
|
|
223
|
+
if (!isDirEmpty(dir))
|
|
224
|
+
throw new UsageError(`${opts.targetDir} is not empty; choose a new folder or empty this one`);
|
|
225
|
+
const root = opts.templatesRoot ?? templatesRoot();
|
|
226
|
+
const version = opts.version ?? ownVersion();
|
|
227
|
+
const packageName = packageNameFrom(dir);
|
|
228
|
+
const files = [];
|
|
229
|
+
const write = (rel, body) => {
|
|
230
|
+
const full = path.join(dir, rel);
|
|
231
|
+
fs.mkdirSync(path.dirname(full), { recursive: true });
|
|
232
|
+
fs.writeFileSync(full, body);
|
|
233
|
+
files.push(rel);
|
|
234
|
+
};
|
|
235
|
+
for (const file of templateFiles(opts.template, root)) {
|
|
236
|
+
const src = path.join(root, opts.template, file);
|
|
237
|
+
const rel = outputName(file);
|
|
238
|
+
if (rel === 'package.json') {
|
|
239
|
+
write(rel, packageJsonFor(fs.readFileSync(src, 'utf8'), packageName, version));
|
|
240
|
+
}
|
|
241
|
+
else if (rel === 'README.md') {
|
|
242
|
+
write(rel, fs.readFileSync(src, 'utf8').replaceAll('__PROJECT_NAME__', packageName));
|
|
243
|
+
}
|
|
244
|
+
else if (opts.brand && rel === info.brandCss) {
|
|
245
|
+
write(rel, opts.brand.css);
|
|
246
|
+
}
|
|
247
|
+
else if (opts.brand && rel === info.brandJson) {
|
|
248
|
+
write(rel, JSON.stringify({ version: opts.brand.version }, null, 2) + '\n');
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
write(rel, fs.readFileSync(src));
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
const secrets = opts.secrets ?? { webhook: randomSecret(), preview: randomSecret() };
|
|
255
|
+
write(info.envFile, envFileContents({ url: opts.url, token: opts.token, webhookSecret: secrets.webhook, previewSecret: secrets.preview, siteUrl: info.devUrl }));
|
|
256
|
+
return { dir, packageName, files: files.sort(), envFile: info.envFile };
|
|
257
|
+
}
|
|
258
|
+
/** The template's package.json with the project's name and the `@spillway-cms/*` packages at `^version`. */
|
|
259
|
+
export function packageJsonFor(source, name, version) {
|
|
260
|
+
const pkg = JSON.parse(source);
|
|
261
|
+
pkg.name = name;
|
|
262
|
+
for (const deps of [pkg.dependencies, pkg.devDependencies]) {
|
|
263
|
+
if (!deps)
|
|
264
|
+
continue;
|
|
265
|
+
for (const dep of Object.keys(deps))
|
|
266
|
+
if (dep.startsWith('@spillway-cms/'))
|
|
267
|
+
deps[dep] = `^${version}`;
|
|
268
|
+
}
|
|
269
|
+
return JSON.stringify(pkg, null, 2) + '\n';
|
|
270
|
+
}
|
|
271
|
+
/** The package manager that ran the command (from `npm_config_user_agent`), for the next steps. */
|
|
272
|
+
export function packageManager(userAgent = process.env.npm_config_user_agent ?? '') {
|
|
273
|
+
const name = userAgent.split('/')[0];
|
|
274
|
+
return name === 'pnpm' || name === 'yarn' || name === 'bun' ? name : 'npm';
|
|
275
|
+
}
|
|
276
|
+
/** What to do after scaffolding, as lines to print. */
|
|
277
|
+
export function nextSteps(result, template, pm, cwd = process.cwd()) {
|
|
278
|
+
const rel = path.relative(cwd, result.dir) || '.';
|
|
279
|
+
const run = pm === 'npm' ? 'npm run' : pm;
|
|
280
|
+
const info = TEMPLATES[template];
|
|
281
|
+
return [
|
|
282
|
+
`cd ${rel.includes(' ') ? JSON.stringify(rel) : rel}`,
|
|
283
|
+
`${pm} install`,
|
|
284
|
+
`${run} dev # ${info.devUrl}`,
|
|
285
|
+
'',
|
|
286
|
+
'Then in Spillway, under Site settings → Notifications, add Webhook rules for published, unpublished, deleted,',
|
|
287
|
+
`menu changed, redirects changed, site settings changed and brand published, pointing at <your front end>/api/revalidate`,
|
|
288
|
+
`with the secret SPILLWAY_WEBHOOK_SECRET from ${result.envFile}. README.md has the rest (previews, the brand, deploying).`,
|
|
289
|
+
];
|
|
290
|
+
}
|
|
291
|
+
//# sourceMappingURL=index.js.map
|