@contentworker/create-site 0.0.0-stage → 0.1.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/CHANGELOG.md +12 -0
- package/LICENSE +202 -0
- package/README.md +35 -2
- package/dist/cli.js +40 -0
- package/dist/create.js +185 -0
- package/dist/templates.js +104 -0
- package/package.json +44 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
`@contentworker/create-site` follows semantic versioning. Release it like the runtime: bump
|
|
4
|
+
`version`, add its section here, merge, then push the tag `create-site@v<version>`.
|
|
5
|
+
|
|
6
|
+
## 0.1.0
|
|
7
|
+
|
|
8
|
+
- `npm create @contentworker/site [dir]`: writes a site on `@contentworker/frontend`
|
|
9
|
+
(`package.json`, `wrangler.jsonc`, the `site.css` theme hook, `.dev.vars`, `.gitignore`,
|
|
10
|
+
README), installs it, and with `--cma-key` creates the content model and starter pages, plus
|
|
11
|
+
the purge webhook when `--origin` is given. Every prompt has a flag; off a terminal it never
|
|
12
|
+
prompts.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,36 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @contentworker/create-site
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A new ContentWorker website on [@contentworker/frontend](https://www.npmjs.com/package/@contentworker/frontend),
|
|
4
|
+
ready to run:
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
npm create @contentworker/site my-site
|
|
8
|
+
cd my-site && npm run dev
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
It asks for your Delivery URL (`https://cdn.<your domain>/<space>/<env>`) and a Content Delivery
|
|
12
|
+
API key, writes the site (`package.json`, `wrangler.jsonc`, a `public/styles/site.css` theme hook,
|
|
13
|
+
`.dev.vars` with the key, `.gitignore`, README), and installs it. Give it a Management key and it
|
|
14
|
+
also creates the content model and starter pages in the space; give it the site's public origin
|
|
15
|
+
too and it registers the purge webhook.
|
|
16
|
+
|
|
17
|
+
Unattended (CI, agents), every prompt is a flag and it never prompts off a terminal:
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
npm create @contentworker/site my-site -- --yes \
|
|
21
|
+
--delivery https://cdn.example.com/my-space/main --key "$CDA_KEY" \
|
|
22
|
+
--cma-key "$CMA_KEY" --origin https://www.example.com
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
| Option | Meaning |
|
|
26
|
+
| -------------- | ---------------------------------------------------------- |
|
|
27
|
+
| `--delivery` | Delivery URL, `https://cdn.<your domain>/<space>/<env>` |
|
|
28
|
+
| `--key` | Content Delivery API key, written to `.dev.vars` |
|
|
29
|
+
| `--locale` | Default locale, `en-US` unless set |
|
|
30
|
+
| `--origin` | The site's public origin (`SITE_ORIGIN`) |
|
|
31
|
+
| `--cma-key` | Management key: create the content model and starter pages |
|
|
32
|
+
| `--no-install` | Write the files only |
|
|
33
|
+
| `--yes` | Take defaults, never prompt |
|
|
34
|
+
|
|
35
|
+
`CONTENTWORKER`, `CONTENTWORKER_KEY` and `CONTENTWORKER_CMA_KEY` in the environment fill the
|
|
36
|
+
matching prompts. Guide: https://contentworker.site/integrate
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/** `npm create @contentworker/site`: the terminal around src/create.ts. */
|
|
3
|
+
import { spawn } from 'node:child_process';
|
|
4
|
+
import { createInterface } from 'node:readline/promises';
|
|
5
|
+
import { create, UsageError } from './create.js';
|
|
6
|
+
const rl = process.stdin.isTTY
|
|
7
|
+
? createInterface({ input: process.stdin, output: process.stdout })
|
|
8
|
+
: null;
|
|
9
|
+
const io = {
|
|
10
|
+
cwd: process.cwd(),
|
|
11
|
+
env: process.env,
|
|
12
|
+
interactive: rl !== null,
|
|
13
|
+
async ask(question, fallback) {
|
|
14
|
+
if (!rl)
|
|
15
|
+
return fallback ?? '';
|
|
16
|
+
const answer = (await rl.question(`${question}${fallback ? ` (${fallback})` : ''}: `)).trim();
|
|
17
|
+
return answer || fallback || '';
|
|
18
|
+
},
|
|
19
|
+
log: (line) => console.log(line),
|
|
20
|
+
run: (cmd, args, { cwd, env }) => new Promise((done) => {
|
|
21
|
+
const child = spawn(cmd, args, {
|
|
22
|
+
cwd,
|
|
23
|
+
env: { ...process.env, ...env },
|
|
24
|
+
stdio: 'inherit',
|
|
25
|
+
shell: process.platform === 'win32',
|
|
26
|
+
});
|
|
27
|
+
child.on('close', (code) => done(code ?? 1));
|
|
28
|
+
child.on('error', () => done(1));
|
|
29
|
+
}),
|
|
30
|
+
};
|
|
31
|
+
try {
|
|
32
|
+
process.exitCode = await create(process.argv.slice(2), io);
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
console.error(error instanceof UsageError ? error.message : error);
|
|
36
|
+
process.exitCode = error instanceof UsageError ? 2 : 1;
|
|
37
|
+
}
|
|
38
|
+
finally {
|
|
39
|
+
rl?.close();
|
|
40
|
+
}
|
package/dist/create.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `npm create @contentworker/site [dir]`: a new ContentWorker site on @contentworker/frontend.
|
|
3
|
+
*
|
|
4
|
+
* Writes the two files a site is (package.json, wrangler.jsonc), its theme hook, .gitignore,
|
|
5
|
+
* README and a .dev.vars holding the Delivery key, then installs, and, given a Management key,
|
|
6
|
+
* creates the content model and starter pages in the space. Every prompt has a flag, so CI and
|
|
7
|
+
* agents run it unattended (`--yes`); off a terminal it never prompts.
|
|
8
|
+
*/
|
|
9
|
+
import { randomBytes } from 'node:crypto';
|
|
10
|
+
import { existsSync, mkdirSync, readdirSync, writeFileSync } from 'node:fs';
|
|
11
|
+
import { basename, join, resolve } from 'node:path';
|
|
12
|
+
import { files } from './templates.js';
|
|
13
|
+
export class UsageError extends Error {
|
|
14
|
+
}
|
|
15
|
+
const USAGE = `Usage: npm create @contentworker/site [dir] -- [options]
|
|
16
|
+
|
|
17
|
+
--delivery <url> Delivery URL, https://cdn.<your domain>/<space>/<env>
|
|
18
|
+
--key <key> a Content Delivery API key for the space (written to .dev.vars)
|
|
19
|
+
--locale <code> default en-US
|
|
20
|
+
--origin <url> the site's public origin, e.g. https://www.example.com
|
|
21
|
+
--cma-key <key> a Management key: also create the content model and starter pages
|
|
22
|
+
--no-install skip npm install (and seeding)
|
|
23
|
+
--yes take defaults, never prompt
|
|
24
|
+
--help`;
|
|
25
|
+
const FLAGS = new Set(['delivery', 'key', 'locale', 'origin', 'cma-key']);
|
|
26
|
+
export function parseArgs(argv) {
|
|
27
|
+
const flags = new Map();
|
|
28
|
+
let dir;
|
|
29
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
30
|
+
const arg = argv[i];
|
|
31
|
+
// `npm create x -- --flag` drops the separator; npx and other package managers pass it on.
|
|
32
|
+
if (arg === '--')
|
|
33
|
+
continue;
|
|
34
|
+
if (!arg.startsWith('--')) {
|
|
35
|
+
if (dir)
|
|
36
|
+
throw new UsageError(`Unexpected argument: ${arg}`);
|
|
37
|
+
dir = arg;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
const [name, inline] = arg.slice(2).split(/=(.*)/s, 2);
|
|
41
|
+
if (name === 'yes' || name === 'no-install' || name === 'help') {
|
|
42
|
+
flags.set(name, true);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (!FLAGS.has(name))
|
|
46
|
+
throw new UsageError(`Unknown option --${name}\n\n${USAGE}`);
|
|
47
|
+
const value = inline ?? argv[++i];
|
|
48
|
+
if (value === undefined || value.startsWith('--'))
|
|
49
|
+
throw new UsageError(`--${name} needs a value`);
|
|
50
|
+
flags.set(name, value);
|
|
51
|
+
}
|
|
52
|
+
return dir === undefined ? { flags } : { dir, flags };
|
|
53
|
+
}
|
|
54
|
+
/** The Delivery URL, checked: https (http only for localhost), and a <space>/<env> path. */
|
|
55
|
+
export function checkDelivery(value) {
|
|
56
|
+
let url;
|
|
57
|
+
try {
|
|
58
|
+
url = new URL(value);
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
throw new UsageError(`--delivery must be a URL like https://cdn.example.com/<space>/<env>`);
|
|
62
|
+
}
|
|
63
|
+
const local = url.hostname === 'localhost' || url.hostname === '127.0.0.1';
|
|
64
|
+
if (url.protocol !== 'https:' && !(local && url.protocol === 'http:'))
|
|
65
|
+
throw new UsageError('--delivery must use https');
|
|
66
|
+
const parts = url.pathname
|
|
67
|
+
.replace(/^\/(delivery\/)?/, '')
|
|
68
|
+
.replace(/\/$/, '')
|
|
69
|
+
.split('/');
|
|
70
|
+
if (parts.length !== 2 || !parts.every((p) => /^[A-Za-z0-9][A-Za-z0-9_-]*$/.test(p)))
|
|
71
|
+
throw new UsageError('--delivery path must be /<space>/<env>');
|
|
72
|
+
return `${url.origin}/${parts.join('/')}`;
|
|
73
|
+
}
|
|
74
|
+
/** The Management API origin for a Delivery URL: cdn.<domain> → api.<domain>. */
|
|
75
|
+
export function apiOrigin(delivery) {
|
|
76
|
+
const url = new URL(delivery);
|
|
77
|
+
return url.origin.replace('//cdn.', '//api.');
|
|
78
|
+
}
|
|
79
|
+
async function gather(argv, io) {
|
|
80
|
+
const { dir, flags } = parseArgs(argv);
|
|
81
|
+
if (flags.has('help')) {
|
|
82
|
+
io.log(USAGE);
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
const quiet = flags.has('yes') || !io.interactive;
|
|
86
|
+
const get = async (name, question, fallback) => {
|
|
87
|
+
const given = flags.get(name);
|
|
88
|
+
if (typeof given === 'string')
|
|
89
|
+
return given;
|
|
90
|
+
if (quiet)
|
|
91
|
+
return fallback ?? '';
|
|
92
|
+
return io.ask(question, fallback);
|
|
93
|
+
};
|
|
94
|
+
const target = dir ?? (quiet ? 'my-site' : await io.ask('Directory', 'my-site'));
|
|
95
|
+
const delivery = await get('delivery', 'Delivery URL (https://cdn.<your domain>/<space>/<env>)', io.env.CONTENTWORKER);
|
|
96
|
+
if (!delivery)
|
|
97
|
+
throw new UsageError(`--delivery is required\n\n${USAGE}`);
|
|
98
|
+
const key = await get('key', 'Content Delivery API key (Settings → API keys; blank to add later)', io.env.CONTENTWORKER_KEY);
|
|
99
|
+
const locale = await get('locale', 'Default locale', 'en-US');
|
|
100
|
+
const origin = await get('origin', "The site's public origin (blank for none yet)");
|
|
101
|
+
const cmaKey = await get('cma-key', 'Management key to create the content model and starter pages (blank to skip)', io.env.CONTENTWORKER_CMA_KEY);
|
|
102
|
+
const opts = {
|
|
103
|
+
dir: target,
|
|
104
|
+
delivery: checkDelivery(delivery),
|
|
105
|
+
locale: locale || 'en-US',
|
|
106
|
+
install: !flags.has('no-install'),
|
|
107
|
+
};
|
|
108
|
+
if (key)
|
|
109
|
+
opts.key = key;
|
|
110
|
+
if (origin)
|
|
111
|
+
opts.origin = new URL(origin).origin;
|
|
112
|
+
if (cmaKey)
|
|
113
|
+
opts.cmaKey = cmaKey;
|
|
114
|
+
return opts;
|
|
115
|
+
}
|
|
116
|
+
/** Worker names: lowercase letters, digits and dashes, at most 63 characters. */
|
|
117
|
+
export function workerName(dir) {
|
|
118
|
+
const name = basename(resolve(dir))
|
|
119
|
+
.toLowerCase()
|
|
120
|
+
.replace(/[^a-z0-9-]+/g, '-')
|
|
121
|
+
.replace(/^-+|-+$/g, '')
|
|
122
|
+
.slice(0, 63);
|
|
123
|
+
return name || 'my-site';
|
|
124
|
+
}
|
|
125
|
+
export async function create(argv, io) {
|
|
126
|
+
const opts = await gather(argv, io);
|
|
127
|
+
if (!opts)
|
|
128
|
+
return 0;
|
|
129
|
+
const root = resolve(io.cwd, opts.dir);
|
|
130
|
+
if (existsSync(root) && readdirSync(root).length > 0)
|
|
131
|
+
throw new UsageError(`${opts.dir} already exists and isn't empty`);
|
|
132
|
+
// With a public origin and a Management key, seeding also registers the purge webhook; its
|
|
133
|
+
// secret is generated here and kept in .dev.vars for `wrangler secret put`.
|
|
134
|
+
const webhookSecret = opts.install && opts.cmaKey && opts.origin ? randomBytes(32).toString('hex') : undefined;
|
|
135
|
+
const written = files({
|
|
136
|
+
name: workerName(root),
|
|
137
|
+
delivery: opts.delivery,
|
|
138
|
+
locale: opts.locale,
|
|
139
|
+
...(opts.origin ? { origin: opts.origin } : {}),
|
|
140
|
+
...(opts.key ? { key: opts.key } : {}),
|
|
141
|
+
...(webhookSecret ? { webhookSecret } : {}),
|
|
142
|
+
});
|
|
143
|
+
for (const [path, { body, mode }] of Object.entries(written)) {
|
|
144
|
+
const file = join(root, path);
|
|
145
|
+
mkdirSync(join(file, '..'), { recursive: true });
|
|
146
|
+
writeFileSync(file, body, mode === undefined ? {} : { mode });
|
|
147
|
+
io.log(` wrote ${path}`);
|
|
148
|
+
}
|
|
149
|
+
if (!opts.install) {
|
|
150
|
+
io.log(`\nCreated ${opts.dir}. Next: cd ${opts.dir} && npm install && npm run dev`);
|
|
151
|
+
return 0;
|
|
152
|
+
}
|
|
153
|
+
if ((await io.run('npm', ['install', '--no-audit', '--no-fund'], { cwd: root })) !== 0) {
|
|
154
|
+
io.log('\nnpm install failed; fix it, then run npm install again.');
|
|
155
|
+
return 1;
|
|
156
|
+
}
|
|
157
|
+
if (opts.cmaKey) {
|
|
158
|
+
io.log('\nCreating the content model and starter pages…');
|
|
159
|
+
const webhook = webhookSecret
|
|
160
|
+
? ['--webhook', `${opts.origin}/__cw/webhook`, '--webhook-secret', webhookSecret]
|
|
161
|
+
: [];
|
|
162
|
+
const code = await io.run('npx', ['contentworker-site', 'seed', ...webhook], {
|
|
163
|
+
cwd: root,
|
|
164
|
+
env: {
|
|
165
|
+
CONTENTWORKER: opts.delivery,
|
|
166
|
+
CONTENTWORKER_API: apiOrigin(opts.delivery),
|
|
167
|
+
CONTENTWORKER_CMA_KEY: opts.cmaKey,
|
|
168
|
+
CONTENTWORKER_LOCALE: opts.locale,
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
if (code !== 0) {
|
|
172
|
+
io.log('Seeding failed; run `npm run seed -- --dry-run` in the site to see why.');
|
|
173
|
+
return 1;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
io.log(`\nCreated ${opts.dir}.`);
|
|
177
|
+
io.log(` cd ${opts.dir}`);
|
|
178
|
+
if (!opts.key)
|
|
179
|
+
io.log(' add CONTENTWORKER_KEY=<a Content Delivery API key> to .dev.vars');
|
|
180
|
+
io.log(' npm run dev the site on http://localhost:8787');
|
|
181
|
+
io.log(' npx wrangler secret put CONTENTWORKER_KEY before the first deploy');
|
|
182
|
+
if (webhookSecret)
|
|
183
|
+
io.log(' npx wrangler secret put CONTENTWORKER_WEBHOOK_SECRET (the value in .dev.vars), for purge on publish');
|
|
184
|
+
return 0;
|
|
185
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The files a new site starts with. A site is a package.json and a wrangler.jsonc; the rest is
|
|
3
|
+
* its theme hook, its secrets for local development and a README. Kept in step with the showcase
|
|
4
|
+
* site's own config (the repo root) by test/create.test.ts.
|
|
5
|
+
*/
|
|
6
|
+
/** The @contentworker/frontend range new sites depend on; a test holds it to the package. */
|
|
7
|
+
export const FRONTEND_RANGE = '^0.2.0';
|
|
8
|
+
/** wrangler for new sites: the version the runtime is tested with. */
|
|
9
|
+
export const WRANGLER_RANGE = '^4.156.1';
|
|
10
|
+
export const COMPATIBILITY_DATE = '2026-10-15';
|
|
11
|
+
export function files(site) {
|
|
12
|
+
const vars = {
|
|
13
|
+
CONTENTWORKER: site.delivery,
|
|
14
|
+
CONTENTWORKER_LOCALE: site.locale,
|
|
15
|
+
...(site.origin ? { SITE_ORIGIN: site.origin } : {}),
|
|
16
|
+
BUILD_ID: 'dev',
|
|
17
|
+
};
|
|
18
|
+
const devVars = [
|
|
19
|
+
'# Local secrets for `npm run dev`. Never commit this file; deploy with `wrangler secret put`.',
|
|
20
|
+
`CONTENTWORKER_KEY=${site.key ?? ''}`,
|
|
21
|
+
...(site.webhookSecret ? [`CONTENTWORKER_WEBHOOK_SECRET=${site.webhookSecret}`] : []),
|
|
22
|
+
'',
|
|
23
|
+
].join('\n');
|
|
24
|
+
return {
|
|
25
|
+
'package.json': {
|
|
26
|
+
body: `${JSON.stringify({
|
|
27
|
+
name: site.name,
|
|
28
|
+
private: true,
|
|
29
|
+
type: 'module',
|
|
30
|
+
scripts: {
|
|
31
|
+
dev: 'wrangler dev',
|
|
32
|
+
deploy: 'wrangler deploy',
|
|
33
|
+
seed: 'contentworker-site seed',
|
|
34
|
+
'new-block': 'contentworker-site new-block',
|
|
35
|
+
},
|
|
36
|
+
dependencies: { '@contentworker/frontend': FRONTEND_RANGE },
|
|
37
|
+
devDependencies: { wrangler: WRANGLER_RANGE },
|
|
38
|
+
}, null, 2)}\n`,
|
|
39
|
+
},
|
|
40
|
+
'wrangler.jsonc': {
|
|
41
|
+
body: `{
|
|
42
|
+
"$schema": "node_modules/wrangler/config-schema.json",
|
|
43
|
+
"name": ${JSON.stringify(site.name)},
|
|
44
|
+
// The whole Worker comes from @contentworker/frontend. For your own blocks, point main at
|
|
45
|
+
// src/worker.ts and call defineSite (npx contentworker-site new-block <name> does it for you).
|
|
46
|
+
"main": "node_modules/@contentworker/frontend/src/entry.ts",
|
|
47
|
+
"compatibility_date": ${JSON.stringify(COMPATIBILITY_DATE)},
|
|
48
|
+
// Workers Caching: pages are rendered by the cached Site entrypoint; the gateway is not cached.
|
|
49
|
+
"cache": { "enabled": true },
|
|
50
|
+
"exports": {
|
|
51
|
+
"default": { "type": "worker", "cache": { "enabled": false } },
|
|
52
|
+
"Site": { "type": "worker", "cache": { "enabled": true } }
|
|
53
|
+
},
|
|
54
|
+
// PurgeQueue batches purges on publish. Migration tags only ever append.
|
|
55
|
+
"migrations": [{ "tag": "v1", "new_sqlite_classes": ["PurgeQueue"] }],
|
|
56
|
+
"assets": {
|
|
57
|
+
"directory": "public",
|
|
58
|
+
"binding": "ASSETS",
|
|
59
|
+
"not_found_handling": "none",
|
|
60
|
+
"html_handling": "none"
|
|
61
|
+
},
|
|
62
|
+
"observability": { "enabled": true },
|
|
63
|
+
"vars": ${JSON.stringify(vars, null, 2).replace(/\n/g, '\n ')}
|
|
64
|
+
// Secrets: CONTENTWORKER_KEY (a Content Delivery API key) and, for purge on publish,
|
|
65
|
+
// CONTENTWORKER_WEBHOOK_SECRET. Locally they come from .dev.vars.
|
|
66
|
+
// Your domain, once it's on Cloudflare (www redirects to SITE_ORIGIN by itself):
|
|
67
|
+
// "routes": [{ "pattern": "example.com", "custom_domain": true }]
|
|
68
|
+
}
|
|
69
|
+
`,
|
|
70
|
+
},
|
|
71
|
+
'public/styles/site.css': {
|
|
72
|
+
body: `/*
|
|
73
|
+
* This site's theme: the runtime appends it to its critical CSS, after every runtime rule, so
|
|
74
|
+
* overriding the tokens is enough. Keep it small; it counts toward the 8 KB critical CSS budget.
|
|
75
|
+
*
|
|
76
|
+
* :root { --link-color: #0b6e4f; --content-width: 1080px; }
|
|
77
|
+
* @media (prefers-color-scheme: dark) { :root { --link-color: #6fe3b9; } }
|
|
78
|
+
*/
|
|
79
|
+
`,
|
|
80
|
+
},
|
|
81
|
+
'.dev.vars': { body: devVars, mode: 0o600 },
|
|
82
|
+
'.gitignore': { body: 'node_modules/\n.wrangler/\n.dev.vars\n' },
|
|
83
|
+
'README.md': {
|
|
84
|
+
body: `# ${site.name}
|
|
85
|
+
|
|
86
|
+
A ContentWorker site on [@contentworker/frontend](https://www.npmjs.com/package/@contentworker/frontend).
|
|
87
|
+
|
|
88
|
+
\`\`\`sh
|
|
89
|
+
npm run dev # http://localhost:8787
|
|
90
|
+
npx wrangler secret put CONTENTWORKER_KEY
|
|
91
|
+
npm run deploy
|
|
92
|
+
\`\`\`
|
|
93
|
+
|
|
94
|
+
- **Theme:** \`public/styles/site.css\` overrides the CSS tokens.
|
|
95
|
+
- **Blocks:** \`npm run new-block -- <name> [--js]\`.
|
|
96
|
+
- **Content model and starter pages:** \`CONTENTWORKER_API=… CONTENTWORKER_CMA_KEY=… npm run seed -- --dry-run\`.
|
|
97
|
+
- **Purge on publish:** a ContentWorker webhook to \`https://<your site>/__cw/webhook\`, with its
|
|
98
|
+
secret set as \`CONTENTWORKER_WEBHOOK_SECRET\`.
|
|
99
|
+
|
|
100
|
+
Guide: https://contentworker.site/integrate
|
|
101
|
+
`,
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentworker/create-site",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "npm create @contentworker/site: a new ContentWorker site on @contentworker/frontend, ready to run, optionally with its content model and starter pages.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"contentworker",
|
|
7
|
+
"create",
|
|
8
|
+
"starter",
|
|
9
|
+
"cloudflare",
|
|
10
|
+
"workers",
|
|
11
|
+
"cms"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://contentworker.site/integrate",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/contentworker/site",
|
|
17
|
+
"directory": "packages/create-site"
|
|
18
|
+
},
|
|
19
|
+
"license": "Apache-2.0",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"bin": {
|
|
22
|
+
"create-site": "dist/cli.js"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"README.md",
|
|
27
|
+
"CHANGELOG.md"
|
|
28
|
+
],
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=24"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"check-types": "tsc -p tsconfig.json",
|
|
34
|
+
"test": "node --test \"test/**/*.test.ts\"",
|
|
35
|
+
"build": "tsc -p tsconfig.build.json",
|
|
36
|
+
"prepack": "npm run build"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/node": "26.6.6",
|
|
40
|
+
"typescript": "7.0.2"
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public",
|
|
44
|
+
"provenance": true
|
|
45
|
+
}
|
|
46
|
+
}
|