@bright-logic-digital/site-editor 0.0.0-stage → 1.0.0-rc.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 +30 -0
- package/LICENSE +4 -0
- package/README.md +318 -2
- package/bin/cli.js +92 -0
- package/bin/copy-editor.js +211 -0
- package/bin/node-version.js +20 -0
- package/bin/site-config.js +46 -0
- package/bin/site-editor.js +16 -0
- package/editor/admin.css +438 -0
- package/editor/api.js +138 -0
- package/editor/app.js +1178 -0
- package/editor/asset-map.js +38 -0
- package/editor/boot.js +72 -0
- package/editor/copy.js +239 -0
- package/editor/drafts.js +67 -0
- package/editor/format.js +80 -0
- package/editor/forms.js +918 -0
- package/editor/images.js +212 -0
- package/editor/index.html +27 -0
- package/editor/model.js +152 -0
- package/editor/pages.js +297 -0
- package/editor/photos.js +232 -0
- package/editor/publish.js +45 -0
- package/editor/shared/rules.js +118 -0
- package/editor/shared/site.js +128 -0
- package/editor/shared/validate.js +303 -0
- package/editor/signin.js +154 -0
- package/editor/status.js +90 -0
- package/editor/uploads.js +91 -0
- package/guide/check.js +233 -0
- package/hosts/next.js +19 -0
- package/hosts/pages.js +20 -0
- package/hosts/shared.js +42 -0
- package/package.json +91 -4
- package/server/blob-sha.js +10 -0
- package/server/changes.js +24 -0
- package/server/check-content.js +197 -0
- package/server/config.js +97 -0
- package/server/dispatch.js +207 -0
- package/server/github.js +149 -0
- package/server/handlers.js +364 -0
- package/server/index.js +153 -0
- package/server/local-repo.js +171 -0
- package/server/mapped-repo.js +48 -0
- package/server/memory-repo.js +104 -0
- package/server/path-map.js +26 -0
- package/server/signin/access.js +329 -0
- package/server/signin/code-store.sql +60 -0
- package/server/signin/code.js +192 -0
- package/server/signin/codes.js +171 -0
- package/server/signin/refusal.js +76 -0
- package/server/signin/session.js +90 -0
- package/tests/fixtures/image-files.json +14 -0
- package/tests/fixtures/slugs.json +202 -0
- package/tests/fixtures/validation.json +12734 -0
- package/types/bin/cli.d.ts +1 -0
- package/types/bin/copy-editor.d.ts +32 -0
- package/types/bin/node-version.d.ts +8 -0
- package/types/bin/site-config.d.ts +17 -0
- package/types/bin/site-editor.d.ts +2 -0
- package/types/editor/api.d.ts +37 -0
- package/types/editor/copy.d.ts +153 -0
- package/types/editor/shared/rules.d.ts +33 -0
- package/types/editor/shared/site.d.ts +35 -0
- package/types/editor/shared/validate.d.ts +19 -0
- package/types/guide/check.d.ts +31 -0
- package/types/hosts/next.d.ts +12 -0
- package/types/hosts/pages.d.ts +9 -0
- package/types/hosts/shared.d.ts +21 -0
- package/types/server/blob-sha.d.ts +1 -0
- package/types/server/changes.d.ts +2 -0
- package/types/server/check-content.d.ts +16 -0
- package/types/server/config.d.ts +46 -0
- package/types/server/dispatch.d.ts +51 -0
- package/types/server/github.d.ts +27 -0
- package/types/server/handlers.d.ts +116 -0
- package/types/server/index.d.ts +75 -0
- package/types/server/local-repo.d.ts +16 -0
- package/types/server/mapped-repo.d.ts +10 -0
- package/types/server/memory-repo.d.ts +28 -0
- package/types/server/path-map.d.ts +3 -0
- package/types/server/signin/access.d.ts +68 -0
- package/types/server/signin/code.d.ts +44 -0
- package/types/server/signin/codes.d.ts +98 -0
- package/types/server/signin/refusal.d.ts +122 -0
- package/types/server/signin/session.d.ts +29 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// site-editor copy: puts the editor's files, the site's config for the
|
|
3
|
+
// browser (site-config.json) and the site's guide (guide.html) in the folder
|
|
4
|
+
// the site serves at /admin/. The folder is emptied first, so a file the
|
|
5
|
+
// package no longer has does not linger; it is emptied only when it is
|
|
6
|
+
// missing, empty, or an earlier copy (its site-config.json is an object with
|
|
7
|
+
// a version, as copy writes it), so a mistyped --out cannot wipe a project.
|
|
8
|
+
// The page's <title> is then stamped with the site's title.
|
|
9
|
+
// The folder keeps a site-config.json throughout, so a copy that stops part
|
|
10
|
+
// way (a full disk, Ctrl-C) can always be run again.
|
|
11
|
+
import fs from "node:fs";
|
|
12
|
+
import path from "node:path";
|
|
13
|
+
import { fileURLToPath } from "node:url";
|
|
14
|
+
import { SITE_DEFAULTS, browserConfig } from "../editor/shared/site.js";
|
|
15
|
+
import { CommandError, loadSiteConfig, packageVersion } from "./site-config.js";
|
|
16
|
+
|
|
17
|
+
const EDITOR = fileURLToPath(new URL("../editor/", import.meta.url));
|
|
18
|
+
const SITE_CONFIG = "site-config.json";
|
|
19
|
+
/** Where the new site-config.json is written before it is renamed into place. */
|
|
20
|
+
export const TEMP_MARKER = "site-config.json.partial";
|
|
21
|
+
|
|
22
|
+
/** @param {string} file */
|
|
23
|
+
const isFile = (file) => {
|
|
24
|
+
try {
|
|
25
|
+
return fs.statSync(file).isFile();
|
|
26
|
+
} catch {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* True when file is a site-config.json that copy wrote: a JSON object with a
|
|
33
|
+
* version.
|
|
34
|
+
* @param {string} file
|
|
35
|
+
*/
|
|
36
|
+
function isEarlierCopy(file) {
|
|
37
|
+
try {
|
|
38
|
+
const value = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
39
|
+
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
40
|
+
&& typeof value.version === "string" && value.version.trim() !== "";
|
|
41
|
+
} catch {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** A regular file itself, not a link to one. @param {string} file */
|
|
47
|
+
const isPlainFile = (file) => {
|
|
48
|
+
try {
|
|
49
|
+
return fs.lstatSync(file).isFile();
|
|
50
|
+
} catch {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/** @param {string} file */
|
|
56
|
+
const isLink = (file) => {
|
|
57
|
+
try {
|
|
58
|
+
return fs.lstatSync(file).isSymbolicLink();
|
|
59
|
+
} catch {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/** @param {string} p */
|
|
65
|
+
const real = (p) => {
|
|
66
|
+
try {
|
|
67
|
+
return fs.realpathSync(p);
|
|
68
|
+
} catch {
|
|
69
|
+
return path.resolve(p);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* True when p is dir or inside it, links followed.
|
|
75
|
+
* @param {string} p
|
|
76
|
+
* @param {string} dir
|
|
77
|
+
*/
|
|
78
|
+
function isInside(p, dir) {
|
|
79
|
+
const rel = path.relative(real(dir), real(p));
|
|
80
|
+
return rel === "" || (rel.split(path.sep)[0] !== ".." && !path.isAbsolute(rel));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Why copy may not empty out, as a sentence naming it, or null when it may:
|
|
85
|
+
* out does not exist, is an empty folder (or holds only an unfinished
|
|
86
|
+
* temporary marker), or holds the site-config.json of an earlier copy.
|
|
87
|
+
* @param {string} out
|
|
88
|
+
* @returns {string | null}
|
|
89
|
+
*/
|
|
90
|
+
export function destinationProblem(out) {
|
|
91
|
+
const refuse = (/** @type {string} */ why) => `Refusing to copy into ${out}: ${why}`;
|
|
92
|
+
/** @type {fs.Stats} */
|
|
93
|
+
let stat;
|
|
94
|
+
try {
|
|
95
|
+
stat = fs.statSync(out);
|
|
96
|
+
} catch (err) {
|
|
97
|
+
if (/** @type {NodeJS.ErrnoException} */ (err).code !== "ENOENT") {
|
|
98
|
+
return refuse(`it cannot be read (${/** @type {NodeJS.ErrnoException} */ (err).code}).`);
|
|
99
|
+
}
|
|
100
|
+
// Nothing there, unless it is a link to nothing.
|
|
101
|
+
try {
|
|
102
|
+
fs.lstatSync(out);
|
|
103
|
+
} catch {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
return refuse("it is a link to a folder that is not there.");
|
|
107
|
+
}
|
|
108
|
+
if (!stat.isDirectory()) return refuse("it is a file, not a folder.");
|
|
109
|
+
const names = fs.readdirSync(out);
|
|
110
|
+
if (names.length === 0) return null;
|
|
111
|
+
// A first copy into a new folder that stopped while writing its marker
|
|
112
|
+
// leaves only the temporary file: that folder is as good as empty.
|
|
113
|
+
if (names.length === 1 && names[0] === TEMP_MARKER && isPlainFile(path.join(out, TEMP_MARKER))) {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
const marker = path.join(out, SITE_CONFIG);
|
|
117
|
+
// A link would be followed when read and written through when replaced.
|
|
118
|
+
if (isLink(marker)) {
|
|
119
|
+
return refuse(`its ${SITE_CONFIG} is a link, which copy never makes, so it is not an earlier copy ` +
|
|
120
|
+
"of the editor. Give an empty or new folder.");
|
|
121
|
+
}
|
|
122
|
+
if (isFile(marker)) {
|
|
123
|
+
if (isEarlierCopy(marker)) return null;
|
|
124
|
+
return refuse(`its ${SITE_CONFIG} is not one site-editor copy wrote (a JSON object with a version), ` +
|
|
125
|
+
"so it may not be an earlier copy of the editor, and copy empties the folder it copies into. " +
|
|
126
|
+
"Give an empty or new folder.");
|
|
127
|
+
}
|
|
128
|
+
return refuse(`it holds files but no ${SITE_CONFIG}, so it is not an earlier copy of the editor, ` +
|
|
129
|
+
"and copy empties the folder it copies into. Give an empty or new folder.");
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** @param {string} text */
|
|
133
|
+
const escapeHtml = (text) => text.replace(/[&<>"']/g,
|
|
134
|
+
(c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c] ?? c);
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* The editor's page with title put in its one <title> element, escaped, so
|
|
138
|
+
* the tab shows the site's name from the first paint.
|
|
139
|
+
* @param {string} html
|
|
140
|
+
* @param {string} title
|
|
141
|
+
* @returns {string}
|
|
142
|
+
*/
|
|
143
|
+
export function stampTitle(html, title) {
|
|
144
|
+
const found = html.match(/<title>[^]*?<\/title>/g) ?? [];
|
|
145
|
+
if (found.length === 0) {
|
|
146
|
+
throw new CommandError("The package's editor/index.html has no <title> element, so copy cannot " +
|
|
147
|
+
"put the site's title in it. Reinstall @bright-logic-digital/site-editor.");
|
|
148
|
+
}
|
|
149
|
+
if (found.length > 1) {
|
|
150
|
+
throw new CommandError("The package's editor/index.html has more than one <title> element, so copy " +
|
|
151
|
+
"cannot tell which is the page's. Reinstall @bright-logic-digital/site-editor.");
|
|
152
|
+
}
|
|
153
|
+
return html.replace(/** @type {string} */ (found[0]), () => `<title>${escapeHtml(title)}</title>`);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Copy the editor for the site whose config file is configFile into out.
|
|
158
|
+
* Checks everything before it changes anything; throws a CommandError,
|
|
159
|
+
* having changed nothing, when it cannot copy.
|
|
160
|
+
* @param {{ configFile: string, out: string }} options
|
|
161
|
+
* @returns {Promise<{ out: string, version: string }>} out: its absolute path
|
|
162
|
+
*/
|
|
163
|
+
export async function copyEditor({ configFile, out }) {
|
|
164
|
+
const { config, file } = await loadSiteConfig(configFile);
|
|
165
|
+
const target = path.resolve(out);
|
|
166
|
+
const guide = path.resolve(path.dirname(file), config.guide);
|
|
167
|
+
if (!isFile(guide)) {
|
|
168
|
+
throw new CommandError(`The guide named by guide in ${file} is not there: ${guide}`);
|
|
169
|
+
}
|
|
170
|
+
const problem = destinationProblem(target);
|
|
171
|
+
if (problem) throw new CommandError(problem);
|
|
172
|
+
for (const [what, p] of [["config file", file], ["guide", guide]]) {
|
|
173
|
+
if (isInside(p, target)) {
|
|
174
|
+
throw new CommandError(`Refusing to copy into ${target}: the site's ${what} ${p} is inside it, ` +
|
|
175
|
+
`and copy empties that folder. Keep the ${what} somewhere else.`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (isInside(EDITOR, target) || isInside(target, EDITOR)) {
|
|
179
|
+
throw new CommandError(`Refusing to copy into ${target}: it holds the package's own editor files.`);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const version = packageVersion();
|
|
183
|
+
const forBrowser = browserConfig(config);
|
|
184
|
+
const siteConfig = `${JSON.stringify({ ...forBrowser, version }, null, 2)}\n`;
|
|
185
|
+
// Checked before anything is changed: a package page without one title
|
|
186
|
+
// refuses the copy, not a half-made folder.
|
|
187
|
+
const page = stampTitle(fs.readFileSync(path.join(EDITOR, "index.html"), "utf8"),
|
|
188
|
+
/** @type {{ title?: string }} */ (forBrowser).title ?? SITE_DEFAULTS.title);
|
|
189
|
+
fs.mkdirSync(target, { recursive: true });
|
|
190
|
+
try {
|
|
191
|
+
// site-config.json is kept while the folder is emptied and replaced
|
|
192
|
+
// first, whole, by a rename, so once the folder holds anything it holds
|
|
193
|
+
// a site-config.json that parses. A temporary one left by an earlier
|
|
194
|
+
// run is emptied out like anything else.
|
|
195
|
+
for (const name of fs.readdirSync(target)) {
|
|
196
|
+
if (name !== SITE_CONFIG) fs.rmSync(path.join(target, name), { recursive: true, force: true });
|
|
197
|
+
}
|
|
198
|
+
fs.writeFileSync(path.join(target, TEMP_MARKER), siteConfig);
|
|
199
|
+
fs.renameSync(path.join(target, TEMP_MARKER), path.join(target, SITE_CONFIG));
|
|
200
|
+
fs.cpSync(EDITOR, target, { recursive: true });
|
|
201
|
+
fs.copyFileSync(guide, path.join(target, "guide.html"));
|
|
202
|
+
// A plain write is enough: an interruption can leave index.html half
|
|
203
|
+
// written, but the folder's site-config.json already marks it as an
|
|
204
|
+
// earlier copy, so the next copy empties and copies everything again.
|
|
205
|
+
fs.writeFileSync(path.join(target, "index.html"), page);
|
|
206
|
+
} catch (err) {
|
|
207
|
+
throw new CommandError(`The copy into ${target} stopped part way ` +
|
|
208
|
+
`(${/** @type {Error} */ (err).message}). Run site-editor copy again.`);
|
|
209
|
+
}
|
|
210
|
+
return { out: target, version };
|
|
211
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Whether this Node can run the site-editor command. Written in the oldest
|
|
3
|
+
// JavaScript on purpose (no ?., ??, let alone await): site-editor.js loads
|
|
4
|
+
// this file before anything else, so an old Node reaches the sentence below
|
|
5
|
+
// instead of a syntax error.
|
|
6
|
+
var NEEDS = 20;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The sentence to print when version (process.versions.node, such as
|
|
10
|
+
* "24.22.1") is older than the command needs, or null when it is fine. A
|
|
11
|
+
* version that cannot be read counts as too old.
|
|
12
|
+
* @param {string} version
|
|
13
|
+
* @returns {string | null}
|
|
14
|
+
*/
|
|
15
|
+
export function tooOldSentence(version) {
|
|
16
|
+
var match = /^(\d+)\./.exec(String(version));
|
|
17
|
+
if (match && Number(match[1]) >= NEEDS) return null;
|
|
18
|
+
return "site-editor needs Node " + NEEDS + " or newer; this is Node " + version +
|
|
19
|
+
". Install a newer Node and run it again.";
|
|
20
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// What every command starts from: the site's config file, loaded and
|
|
3
|
+
// checked, and the package's own version. A CommandError's message is a
|
|
4
|
+
// sentence for the person running the command, printed as it is.
|
|
5
|
+
import { readFileSync } from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { pathToFileURL } from "node:url";
|
|
8
|
+
import { serverConfigProblems } from "../server/index.js";
|
|
9
|
+
|
|
10
|
+
/** @typedef {import("../server/signin/refusal.js").SiteConfig} SiteConfig */
|
|
11
|
+
|
|
12
|
+
export class CommandError extends Error {}
|
|
13
|
+
|
|
14
|
+
/** The version in the package's own package.json. */
|
|
15
|
+
export function packageVersion() {
|
|
16
|
+
const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
17
|
+
return /** @type {string} */ (pkg.version);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The site's config file (an ES module whose default export is the config),
|
|
22
|
+
* loaded and checked. Throws a CommandError naming the file, and the keys
|
|
23
|
+
* that are wrong, when it cannot be used.
|
|
24
|
+
* @param {string} configFile as given on the command line
|
|
25
|
+
* @returns {Promise<{ config: SiteConfig, file: string }>} file: its absolute path
|
|
26
|
+
*/
|
|
27
|
+
export async function loadSiteConfig(configFile) {
|
|
28
|
+
const file = path.resolve(configFile);
|
|
29
|
+
/** @type {{ default?: unknown }} */
|
|
30
|
+
let mod;
|
|
31
|
+
try {
|
|
32
|
+
mod = await import(pathToFileURL(file).href);
|
|
33
|
+
} catch (err) {
|
|
34
|
+
throw new CommandError(`Could not load the config file ${file}: ${/** @type {Error} */ (err).message}`);
|
|
35
|
+
}
|
|
36
|
+
const config = mod.default;
|
|
37
|
+
if (typeof config !== "object" || config === null || Array.isArray(config)) {
|
|
38
|
+
throw new CommandError(`The config file ${file} must have the config as its default export ` +
|
|
39
|
+
"(export default { ... }); what it exports by default is not an object.");
|
|
40
|
+
}
|
|
41
|
+
const problems = serverConfigProblems(config);
|
|
42
|
+
if (problems.length > 0) {
|
|
43
|
+
throw new CommandError(`The config file ${file} has a problem with: ${problems.join(", ")}.`);
|
|
44
|
+
}
|
|
45
|
+
return { config: /** @type {SiteConfig} */ (config), file };
|
|
46
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// @ts-check
|
|
3
|
+
// The site-editor command's entry. It checks that Node is new enough before
|
|
4
|
+
// anything else, then loads the command (cli.js) with import(). Keep this
|
|
5
|
+
// file's syntax old: no ?., ??, await. A Node too old for the command (below
|
|
6
|
+
// 20) must reach the sentence, not a syntax error (it needs ES modules and
|
|
7
|
+
// import(), so Node 12.20 at the least).
|
|
8
|
+
import { tooOldSentence } from "./node-version.js";
|
|
9
|
+
|
|
10
|
+
var tooOld = tooOldSentence(process.versions.node);
|
|
11
|
+
if (tooOld !== null) {
|
|
12
|
+
console.error(tooOld);
|
|
13
|
+
process.exitCode = 1;
|
|
14
|
+
} else {
|
|
15
|
+
import("./cli.js");
|
|
16
|
+
}
|