@unbase-app/cli 0.0.0-stage → 0.3.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/AGENTS.md +38 -0
- package/LICENSE +21 -0
- package/README.md +239 -2
- package/agents/astro.md +179 -0
- package/agents/next.md +129 -0
- package/agents/vite-react.md +126 -0
- package/bin/unbase.mjs +147 -0
- package/lib/analytics.mjs +142 -0
- package/lib/args.mjs +49 -0
- package/lib/build.mjs +71 -0
- package/lib/compare.mjs +103 -0
- package/lib/config-edit.mjs +634 -0
- package/lib/doctor.mjs +37 -0
- package/lib/errors.mjs +15 -0
- package/lib/feature.mjs +23 -0
- package/lib/frameworks/astro.mjs +535 -0
- package/lib/frameworks/index.mjs +57 -0
- package/lib/frameworks/next.mjs +358 -0
- package/lib/frameworks/vite-react.mjs +329 -0
- package/lib/init.mjs +186 -0
- package/lib/next-config.mjs +142 -0
- package/lib/pm.mjs +109 -0
- package/lib/readers.mjs +2138 -0
- package/lib/site.mjs +153 -0
- package/lib/uninstall.mjs +140 -0
- package/lib/versions.mjs +81 -0
- package/lib/vite-config.mjs +296 -0
- package/package.json +44 -4
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// The frameworks the CLI has a package for (docs/plan-packages.md, K5: one
|
|
3
|
+
// package per framework). What differs per framework is in its module; init,
|
|
4
|
+
// doctor and uninstall do the rest the same way for each. A site whose
|
|
5
|
+
// framework has no module here does not open (site.mjs: openSite, code
|
|
6
|
+
// `framework_unsupported`, exit 2).
|
|
7
|
+
//
|
|
8
|
+
// A framework module's default export:
|
|
9
|
+
//
|
|
10
|
+
// id detection's id for it (detect.ts FRAMEWORKS): "astro"
|
|
11
|
+
// name as people say it: "Astro"
|
|
12
|
+
// configFiles its config files, relative to the site, in the order they
|
|
13
|
+
// are looked for: `site.configFile` is the first one there
|
|
14
|
+
// agents the file in this package with the steps init leaves,
|
|
15
|
+
// relative to the package ("agents/astro.md"); AGENTS.md
|
|
16
|
+
// points to it, init names it, doctor's steps cite it
|
|
17
|
+
// comments whether init offers comments on the live site (--comments)
|
|
18
|
+
// analyticsLink
|
|
19
|
+
// whether comment-modus.json links analytics without comment
|
|
20
|
+
// mode (the package reads the file itself): init writes it
|
|
21
|
+
// with --no-comments too, and takes --project then
|
|
22
|
+
// analyticsOption(site)
|
|
23
|
+
// the config's `analytics` option as it is: false only when it
|
|
24
|
+
// says so literally; init leaves the CSP alone then
|
|
25
|
+
// build the production build doctor and uninstall make (build.mjs):
|
|
26
|
+
// { bin, args(outDir), env, output }, `output` the folders a
|
|
27
|
+
// build may write its pages to instead of outDir
|
|
28
|
+
// packages({ comments })
|
|
29
|
+
// what init installs, pinned (versions.mjs), in its order
|
|
30
|
+
// init(site, { comments, project })
|
|
31
|
+
// init's plan for the files besides package.json: an array of
|
|
32
|
+
// { file, action: "created" | "changed", changes, text }, or a
|
|
33
|
+
// CouldNotRun. Writes nothing; init writes them, in order
|
|
34
|
+
// linked(site) the Unbase project the site's code is linked to, or null
|
|
35
|
+
// uninstall(site)
|
|
36
|
+
// uninstall's plan: { packages: names, files: [{ file, action,
|
|
37
|
+
// changes, text }], remove: paths }. Writes nothing
|
|
38
|
+
// doctor(site, { build })
|
|
39
|
+
// the features, each { id, name, state, checks } (feature.mjs)
|
|
40
|
+
// normalise optional: a built page as uninstall compares it, before the
|
|
41
|
+
// shared normalising (compare.mjs), for what differs between
|
|
42
|
+
// two builds of this framework (Next.js: its scripts and hashes)
|
|
43
|
+
//
|
|
44
|
+
// Adding a framework: its module in this folder, one line in FRAMEWORKS, and
|
|
45
|
+
// its agents file (shipped: package.json `files` has agents/). Unbase reads
|
|
46
|
+
// the same list from supabase/functions/_shared/framework-packages.ts, held
|
|
47
|
+
// equal to this one by test/frameworks.test.mjs.
|
|
48
|
+
// ============================================================================
|
|
49
|
+
|
|
50
|
+
import astro from "./astro.mjs";
|
|
51
|
+
import viteReact from "./vite-react.mjs";
|
|
52
|
+
import next from "./next.mjs";
|
|
53
|
+
|
|
54
|
+
export const FRAMEWORKS = [astro, viteReact, next];
|
|
55
|
+
|
|
56
|
+
/** The module for detection's framework id, or null. */
|
|
57
|
+
export const frameworkOf = (id) => FRAMEWORKS.find((f) => f.id === id) ?? null;
|
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// Next.js: what the CLI does that is Next.js's (frameworks/index.mjs has the
|
|
3
|
+
// shape). init adds @unbase-app/next and wraps next.config in withUnbase(…)
|
|
4
|
+
// (next-config.mjs, through the syntax tree), writes comment-modus.json, and
|
|
5
|
+
// on a Next.js before 16.3 imports the comments' loader in
|
|
6
|
+
// instrumentation-client; uninstall takes them out with unbase.pages.json;
|
|
7
|
+
// doctor reads four features with Unbase's own readers (lib/readers.mjs:
|
|
8
|
+
// detect, read_settings on the pages' metadata, list_pages, the kit's core
|
|
9
|
+
// and the CSP reader) on the working tree and one production build (`next
|
|
10
|
+
// build`, into the site's own .next). The steps init leaves: agents/next.md.
|
|
11
|
+
// Analytics comes with the comments' client module, so it needs the same
|
|
12
|
+
// link: on Next.js, --no-comments leaves both out.
|
|
13
|
+
// ============================================================================
|
|
14
|
+
|
|
15
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
16
|
+
import { join } from "node:path";
|
|
17
|
+
import { analyticsFeature } from "../analytics.mjs";
|
|
18
|
+
import { tailOf } from "../build.mjs";
|
|
19
|
+
import { CouldNotRun } from "../errors.mjs";
|
|
20
|
+
import { check, featureState } from "../feature.mjs";
|
|
21
|
+
import { inspectNextConfig, unwrapNextConfig, wrapNextConfig } from "../next-config.mjs";
|
|
22
|
+
import { core, readSettings, registryFiles, registryOf } from "../readers.mjs";
|
|
23
|
+
import { depsOf, installedVersion } from "../site.mjs";
|
|
24
|
+
import { compareVersions, NEXT, SELF } from "../versions.mjs";
|
|
25
|
+
|
|
26
|
+
/** The file with the steps init leaves, in this package. */
|
|
27
|
+
const AGENTS = "agents/next.md";
|
|
28
|
+
/** comment-modus.json: the link to the Unbase project, the same file as on Astro. */
|
|
29
|
+
export const LINK_FILE = "comment-modus.json";
|
|
30
|
+
const STEPS = {
|
|
31
|
+
install: `${AGENTS} step 1: Install`,
|
|
32
|
+
comments: `${AGENTS} step 2: Comments on the site`,
|
|
33
|
+
pageSettings: `${AGENTS} step 3: Page settings`,
|
|
34
|
+
drafts: `${AGENTS} step 4: Draft pages`,
|
|
35
|
+
analytics: `${AGENTS} step 5: Analytics`,
|
|
36
|
+
};
|
|
37
|
+
/** Next.js injects a package's client module from this version on (instrumentationClientInject); before, instrumentation-client imports it. */
|
|
38
|
+
const INJECTS_FROM = "16.3.0";
|
|
39
|
+
/** instrumentation-client exists from this version on. */
|
|
40
|
+
const INSTRUMENTATION_FROM = "15.3.0";
|
|
41
|
+
const LOADER_IMPORT = `import "${NEXT.name}/comments";`;
|
|
42
|
+
/** What the loader leaves in the client's chunks, minified or not. */
|
|
43
|
+
const LOADER_MARK = "[unbase] comments:";
|
|
44
|
+
const DIST = ".next";
|
|
45
|
+
|
|
46
|
+
/** The project comment-modus.json names, or null; undefined when there is no such file. */
|
|
47
|
+
export function readLink(site) {
|
|
48
|
+
const text = site.text(LINK_FILE);
|
|
49
|
+
if (text === null) return undefined;
|
|
50
|
+
try {
|
|
51
|
+
const data = JSON.parse(text);
|
|
52
|
+
return typeof data?.project === "string" && data.project ? data.project : null;
|
|
53
|
+
} catch {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const nextVersion = (site) => installedVersion(site, "next") ?? (String(depsOf(site.pkg).next ?? "").match(/\d+\.\d+\.\d+/)?.[0] ?? null);
|
|
59
|
+
|
|
60
|
+
/** Where instrumentation-client goes: beside the app folder (src/ when the app is in src/app). */
|
|
61
|
+
function instrumentationFile(site) {
|
|
62
|
+
const there = ["src/instrumentation-client.ts", "src/instrumentation-client.js", "instrumentation-client.ts", "instrumentation-client.js"].find((f) => site.exists(f));
|
|
63
|
+
if (there) return there;
|
|
64
|
+
const dir = site.exists("src/app") ? "src/" : "";
|
|
65
|
+
return `${dir}instrumentation-client.${site.exists("tsconfig.json") ? "ts" : "js"}`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const importsLoader = (text) => !!text && text.split("\n").some((l) => l.trim().replace(/;$/, "") === LOADER_IMPORT.replace(/;$/, "") || l.trim().replace(/'/g, '"') === LOADER_IMPORT);
|
|
69
|
+
|
|
70
|
+
// ── init ───────────────────────────────────────────────────────────────────
|
|
71
|
+
|
|
72
|
+
/** init's plan for next.config, comment-modus.json and (before Next.js 16.3) instrumentation-client. */
|
|
73
|
+
function planInit(site, { comments, project }) {
|
|
74
|
+
if (!site.configFile) throw new CouldNotRun("no_config", "No next.config.(ts|mjs|js) in the site's folder.");
|
|
75
|
+
const config = wrapNextConfig(site.text(site.configFile), site.configFile);
|
|
76
|
+
const plan = config.changes.length ? [{ file: site.configFile, action: "changed", changes: config.changes, text: config.code }] : [];
|
|
77
|
+
if (!comments) return plan;
|
|
78
|
+
|
|
79
|
+
const text = site.text(LINK_FILE);
|
|
80
|
+
if (text === null) {
|
|
81
|
+
plan.push({ file: LINK_FILE, action: "created", changes: [project ? `project: "${project}"` : "project: null (Unbase fills it in when the site is connected)"], text: `${JSON.stringify({ project }, null, 2)}\n` });
|
|
82
|
+
} else {
|
|
83
|
+
let data;
|
|
84
|
+
try {
|
|
85
|
+
data = JSON.parse(text);
|
|
86
|
+
} catch {
|
|
87
|
+
throw new CouldNotRun("link_file", `${LINK_FILE} is not valid JSON.`, { file: LINK_FILE });
|
|
88
|
+
}
|
|
89
|
+
if (!data || typeof data !== "object" || Array.isArray(data)) throw new CouldNotRun("link_file", `${LINK_FILE} is not a JSON object.`, { file: LINK_FILE });
|
|
90
|
+
const indent = /^[ \t]+(?=")/m.exec(text)?.[0] ?? " ";
|
|
91
|
+
if (project && data.project !== project) plan.push({ file: LINK_FILE, action: "changed", changes: [`project: ${JSON.stringify(data.project ?? null)} → "${project}"`], text: `${JSON.stringify({ ...data, project }, null, indent)}\n` });
|
|
92
|
+
else if (!("project" in data)) plan.push({ file: LINK_FILE, action: "changed", changes: ["project: null"], text: `${JSON.stringify({ project: null, ...data }, null, indent)}\n` });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Before 16.3 Next.js cannot inject the loader; from 15.3 instrumentation-client imports it.
|
|
96
|
+
const version = nextVersion(site);
|
|
97
|
+
if (version && compareVersions(version, INJECTS_FROM) < 0 && compareVersions(version, INSTRUMENTATION_FROM) >= 0) {
|
|
98
|
+
const file = instrumentationFile(site);
|
|
99
|
+
const now = site.text(file);
|
|
100
|
+
if (now === null) plan.push({ file, action: "created", changes: [LOADER_IMPORT], text: `${LOADER_IMPORT}\n` });
|
|
101
|
+
else if (!importsLoader(now)) plan.push({ file, action: "changed", changes: [LOADER_IMPORT], text: `${LOADER_IMPORT}\n${now}` });
|
|
102
|
+
}
|
|
103
|
+
return plan;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** withUnbase's `analytics` in next.config as it is: false only when it says so literally. */
|
|
107
|
+
function analyticsOption(site) {
|
|
108
|
+
if (!site.configFile) return true;
|
|
109
|
+
try {
|
|
110
|
+
return inspectNextConfig(site.text(site.configFile), site.configFile).analytics !== false;
|
|
111
|
+
} catch {
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ── uninstall ──────────────────────────────────────────────────────────────
|
|
117
|
+
|
|
118
|
+
/** uninstall's plan: next.config unwrapped, the loader's import out of instrumentation-client, the package and its files. */
|
|
119
|
+
function planUninstall(site) {
|
|
120
|
+
const files = [];
|
|
121
|
+
const remove = [LINK_FILE, core.PAGE_FILE];
|
|
122
|
+
if (site.configFile) {
|
|
123
|
+
const config = unwrapNextConfig(site.text(site.configFile), site.configFile);
|
|
124
|
+
if (config.changes.length) files.push({ file: site.configFile, action: "changed", changes: config.changes, text: config.code });
|
|
125
|
+
}
|
|
126
|
+
for (const file of ["src/instrumentation-client.ts", "src/instrumentation-client.js", "instrumentation-client.ts", "instrumentation-client.js"]) {
|
|
127
|
+
const text = site.text(file);
|
|
128
|
+
if (!importsLoader(text)) continue;
|
|
129
|
+
const rest = text
|
|
130
|
+
.split("\n")
|
|
131
|
+
.filter((l) => !importsLoader(l))
|
|
132
|
+
.join("\n");
|
|
133
|
+
if (!rest.trim()) remove.push(file);
|
|
134
|
+
else files.push({ file, action: "changed", changes: [`removed: ${LOADER_IMPORT}`], text: rest });
|
|
135
|
+
}
|
|
136
|
+
return { packages: [NEXT.name, SELF.name], files, remove };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ── doctor ─────────────────────────────────────────────────────────────────
|
|
140
|
+
|
|
141
|
+
/** The client's compiled chunks of the last build, as text. */
|
|
142
|
+
function clientChunks(site) {
|
|
143
|
+
const dir = join(site.root, DIST, "static");
|
|
144
|
+
const out = [];
|
|
145
|
+
const walk = (d) => {
|
|
146
|
+
for (const e of readdirSync(d, { withFileTypes: true })) {
|
|
147
|
+
if (e.isDirectory()) walk(join(d, e.name));
|
|
148
|
+
else if (e.name.endsWith(".js")) out.push(readFileSync(join(d, e.name), "utf8"));
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
if (existsSync(dir)) walk(dir);
|
|
152
|
+
return out;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function envHas(site, names) {
|
|
156
|
+
return names.some((name) => process.env[name] || [".env", ".env.production", ".env.local", ".env.production.local"].some((f) => new RegExp(`^\\s*(export\\s+)?${name}\\s*=\\s*\\S`, "m").test(site.text(f) ?? "")));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const listOf = (list, n = 3) => `${list.slice(0, n).join(", ")}${list.length > n ? ", …" : ""}`;
|
|
160
|
+
|
|
161
|
+
/** doctor's three features on a Next.js site, from the production build doctor made (null: --no-build). */
|
|
162
|
+
async function doctorFeatures(site, { build }) {
|
|
163
|
+
const built = !!build?.ok;
|
|
164
|
+
const deps = depsOf(site.pkg);
|
|
165
|
+
const version = nextVersion(site);
|
|
166
|
+
const features = [];
|
|
167
|
+
const text = (p) => site.text(p);
|
|
168
|
+
|
|
169
|
+
let config = null;
|
|
170
|
+
let configError = null;
|
|
171
|
+
if (site.configFile) {
|
|
172
|
+
try {
|
|
173
|
+
config = inspectNextConfig(site.text(site.configFile), site.configFile);
|
|
174
|
+
} catch (e) {
|
|
175
|
+
if (!(e instanceof CouldNotRun)) throw e;
|
|
176
|
+
configError = e.message;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const listed = !!deps[NEXT.name];
|
|
180
|
+
const have = installedVersion(site, NEXT.name);
|
|
181
|
+
const wrapped = !!config?.wrapped;
|
|
182
|
+
/** The package and its line in next.config: one check, shared by comments and page management. */
|
|
183
|
+
const packageCheck = () => {
|
|
184
|
+
if (!listed)
|
|
185
|
+
return check("package", "missing", have ? `${NEXT.name} is in node_modules (${have}) but not listed in package.json.` : `${NEXT.name} is not a dependency.`, { missing: `${NEXT.name} in dependencies, pinned`, file: "package.json", step: STEPS.install });
|
|
186
|
+
if (configError) return check("config", "missing", configError, { file: site.configFile, step: STEPS.install });
|
|
187
|
+
if (!site.configFile) return check("config", "missing", "No next.config: withUnbase wraps the site's config.", { missing: "next.config.ts with withUnbase(…)", file: "next.config.ts", step: STEPS.install });
|
|
188
|
+
if (!wrapped) return check("config", "missing", `${site.configFile} does not hand Next.js withUnbase(…).`, { missing: "export default withUnbase(nextConfig)", file: site.configFile, step: STEPS.install });
|
|
189
|
+
return check("package", "ok", `${NEXT.name} ${have ?? deps[NEXT.name]} is a dependency, and ${site.configFile} hands Next.js withUnbase(…).`);
|
|
190
|
+
};
|
|
191
|
+
const ranProduction = built && /\[unbase\] production build \(UNBASE_DEPLOY_CONTEXT\)/.test(build.output);
|
|
192
|
+
|
|
193
|
+
// ── Visual editing ────────────────────────────────────────────────────────
|
|
194
|
+
{
|
|
195
|
+
const checks = [check("next", "ok", `Next.js ${version ?? ""}: Unbase edits a text where it is written, found by matching it and proven by a probe that changes it once; nothing installed, no markers.`)];
|
|
196
|
+
if (!build) checks.push(check("build", "skipped", "Not built (--no-build)."));
|
|
197
|
+
else if (built) checks.push(check("build", "ok", `A production build works (${build.html.size} prerendered pages).`));
|
|
198
|
+
else checks.push(check("build", "missing", "The production build fails, so Unbase cannot start or check the site.", { missing: "a site that builds", file: site.configFile, detail: tailOf(build.output) }));
|
|
199
|
+
const names = site.detection.settings.map((s) => s.name);
|
|
200
|
+
if (names.length) checks.push(check("settings", "info", `The code reads ${names.length} setting(s) (${listOf(names, 6)}). If the site needs them to start or build, give them development values in Unbase.`));
|
|
201
|
+
features.push({ id: "visual-editing", name: "Visual editing", state: featureState(checks), checks });
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// ── Comments on the site ──────────────────────────────────────────────────
|
|
205
|
+
{
|
|
206
|
+
const checks = [packageCheck()];
|
|
207
|
+
const project = readLink(site);
|
|
208
|
+
let notInstalled = false;
|
|
209
|
+
if (project === undefined) {
|
|
210
|
+
notInstalled = true;
|
|
211
|
+
checks.push(check("link", "info", `No ${LINK_FILE}: comments on the live site are off. \`unbase init --comments\` adds it. Comments on the canvas in Unbase work without it.`));
|
|
212
|
+
} else {
|
|
213
|
+
if (project) checks.push(check("link", "ok", `Linked to the Unbase project "${project}".`, { file: LINK_FILE }));
|
|
214
|
+
else
|
|
215
|
+
checks.push(
|
|
216
|
+
check("link", "waiting", `Not linked to an Unbase project yet, so comments stay off. Unbase fills in \`project\` in ${LINK_FILE} when the site is connected; or run \`unbase init --project <id or slug>\`.`, { missing: `project in ${LINK_FILE}`, file: LINK_FILE }),
|
|
217
|
+
);
|
|
218
|
+
// The loader on the page: injected by withUnbase, or imported in instrumentation-client.
|
|
219
|
+
const injects = version && compareVersions(version, INJECTS_FROM) >= 0;
|
|
220
|
+
const file = instrumentationFile(site);
|
|
221
|
+
if (injects) checks.push(check("loader", "ok", `Next.js ${version} runs the ?comment loader on every page (withUnbase injects it): nothing to add.`));
|
|
222
|
+
else if (importsLoader(text(file))) checks.push(check("loader", "ok", `${file} imports the ?comment loader.`));
|
|
223
|
+
else if (version && compareVersions(version, INSTRUMENTATION_FROM) >= 0)
|
|
224
|
+
checks.push(check("loader", "missing", `Next.js ${version} cannot inject the loader: ${file} has to import it.`, { missing: LOADER_IMPORT, file, step: STEPS.comments }));
|
|
225
|
+
else checks.push(check("loader", "missing", `Next.js ${version ?? "?"} has no instrumentation-client: the root layout has to render the loader.`, { missing: "the loader in the root layout", file: "src/app/layout.tsx", step: STEPS.comments }));
|
|
226
|
+
if (project && build) {
|
|
227
|
+
if (!built) checks.push(check("built-loader", "skipped", "The build failed."));
|
|
228
|
+
else {
|
|
229
|
+
const chunks = clientChunks(site);
|
|
230
|
+
const inBundle = chunks.some((c) => c.includes(LOADER_MARK));
|
|
231
|
+
const configured = chunks.some((c) => c.includes(`\\"project\\":\\"${project}\\"`) || c.includes(`"project":"${project}"`));
|
|
232
|
+
checks.push(
|
|
233
|
+
inBundle && configured
|
|
234
|
+
? check("built-loader", "ok", "The production build carries the ?comment loader and the project's configuration; a visitor loads nothing unless they open ?comment.")
|
|
235
|
+
: check("built-loader", "missing", inBundle ? "The loader is in the build, but not the project's configuration." : "The ?comment loader is not in the production build.", { missing: "the loader in the build", file: site.configFile, step: STEPS.comments }),
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
if (!envHas(site, ["PUBLIC_SUPABASE_URL", "NEXT_PUBLIC_SUPABASE_URL"]) || !envHas(site, ["PUBLIC_SUPABASE_ANON_KEY", "NEXT_PUBLIC_SUPABASE_ANON_KEY"]))
|
|
240
|
+
checks.push(check("backend", "info", "Comments find Unbase's backend through PUBLIC_SUPABASE_URL and PUBLIC_SUPABASE_ANON_KEY where the site is built; they are not set here, and doctor cannot see your host's (Unbase's Vercel integration sets them)."));
|
|
241
|
+
}
|
|
242
|
+
features.push({ id: "comments", name: "Comments on the site", state: featureState(checks, notInstalled), checks });
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// ── Page management: draft pages and page settings ─────────────────────────
|
|
246
|
+
{
|
|
247
|
+
const checks = [packageCheck()];
|
|
248
|
+
const kit = core.kitInstalled({ framework: "next", text }) && wrapped;
|
|
249
|
+
const files = site.appPaths.filter(registryFiles).map((p) => ({ path: p, text: text(p) ?? "" }));
|
|
250
|
+
const byPath = new Map(files.map((f) => [f.path, f.text]));
|
|
251
|
+
const pageFile = core.parsePageFile(text(core.PAGE_FILE));
|
|
252
|
+
let drafts = 0;
|
|
253
|
+
if (!pageFile.ok) checks.push(check("drafts", "missing", `${core.PAGE_FILE} is invalid: ${pageFile.error}.`, { missing: `a valid ${core.PAGE_FILE}`, file: core.PAGE_FILE, step: STEPS.drafts }));
|
|
254
|
+
else {
|
|
255
|
+
const plan = core.draftPlan({ framework: "next", paths: [...byPath.keys()].sort(), text: (p) => byPath.get(p) ?? null, pageFile });
|
|
256
|
+
drafts = plan.checks.length;
|
|
257
|
+
if (plan.failed.length)
|
|
258
|
+
checks.push(
|
|
259
|
+
check("drafts", "missing", `A production build would stop: ${plan.failed.map((f) => `${f.path} (${f.reason}${f.detail ? `: ${f.detail}` : ""})`).join(", ")}.`, { missing: "drafts the package can keep out", file: core.PAGE_FILE, step: STEPS.drafts }),
|
|
260
|
+
);
|
|
261
|
+
else checks.push(check("drafts", "ok", drafts ? `${drafts} draft(s) in ${core.PAGE_FILE}, each kept out of production builds.` : "No drafts now."));
|
|
262
|
+
}
|
|
263
|
+
if (kit && build) {
|
|
264
|
+
if (!built) checks.push(check("fails-closed", "skipped", "The build failed."));
|
|
265
|
+
else if (!ranProduction) checks.push(check("fails-closed", "missing", "withUnbase did not plan the production build.", { missing: "withUnbase(…) running in next build", file: site.configFile, step: STEPS.install }));
|
|
266
|
+
else if (drafts && !/\[unbase\] kept out:/.test(build.output)) checks.push(check("fails-closed", "missing", "The build did not prove the drafts were kept out.", { missing: "the check after the compile", file: site.configFile, step: STEPS.drafts }));
|
|
267
|
+
else checks.push(check("fails-closed", "ok", drafts ? "withUnbase kept the drafts out of the production build and checked its output." : "withUnbase ran in the production build: a draft it cannot keep out stops the build."));
|
|
268
|
+
}
|
|
269
|
+
const registry = registryOf({ files, framework: "next", appPath: site.appPath, staged: true, truncated: false });
|
|
270
|
+
const pages = registry.pages.filter((p) => p.kind !== "template");
|
|
271
|
+
if (kit) {
|
|
272
|
+
const ok = pages.filter((p) => p.capabilities.draft.ok).length;
|
|
273
|
+
const why = {};
|
|
274
|
+
for (const p of pages) if (!p.capabilities.draft.ok) (why[p.capabilities.draft.reason] ??= []).push(p.path);
|
|
275
|
+
const rest = Object.entries(why).map(([r, list]) => `${r}: ${listOf(list)}`);
|
|
276
|
+
checks.push(check("draftable", "info", `${ok} of ${pages.length} pages can be drafts${rest.length ? ` (not: ${rest.join("; ")})` : ""}. In Unbase, draft pages also need Publishing with a Live branch.`));
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// Page settings: read_settings on the pages' metadata, nothing installed.
|
|
280
|
+
const settings = await readSettings(site.repo, site.appPath, "next");
|
|
281
|
+
if (!settings) checks.push(check("page-settings", "info", "No app router: page settings live in the pages router's <Head>, which Unbase does not set."));
|
|
282
|
+
else {
|
|
283
|
+
const settable = pages.filter((p) => settings.pages[p.path]).length;
|
|
284
|
+
const fresh = pages.filter((p) => settings.pages[p.path] && !Object.values(settings.pages[p.path]).some((f) => f.exists)).length;
|
|
285
|
+
const refused = {};
|
|
286
|
+
for (const [route, r] of Object.entries(settings.refused ?? {})) if (route !== "site" && !settings.pages[route]) (refused[r.reason] ??= []).push(route);
|
|
287
|
+
const not = Object.entries(refused).map(([r, list]) => `${list.length} ${r === "cms" ? "from a CMS (set there)" : r === "computed" ? "built in code" : "where Next.js takes none (a client component, the pages router)"}: ${listOf(list)}`);
|
|
288
|
+
checks.push(
|
|
289
|
+
check(
|
|
290
|
+
"page-settings",
|
|
291
|
+
settable ? "ok" : "info",
|
|
292
|
+
`Page settings in each page's metadata: ${settable} of ${pages.length} pages can be set in Unbase${settable < pages.length && not.length ? `; not ${not.join("; ")}` : ""}.${fresh ? ` ${fresh} without metadata yet get it when their settings are first published.` : ""}`,
|
|
293
|
+
settable < pages.length ? { step: STEPS.pageSettings } : {},
|
|
294
|
+
),
|
|
295
|
+
);
|
|
296
|
+
const site_ = settings.site ? Object.keys(settings.site) : [];
|
|
297
|
+
const layoutWhy = settings.refused?.site;
|
|
298
|
+
checks.push(
|
|
299
|
+
site_.length
|
|
300
|
+
? check("site-settings", "ok", `Site settings in ${Object.values(settings.site)[0].file}: ${site_.join(", ")}.${layoutWhy?.fields ? ` Built in code: ${Object.keys(layoutWhy.fields).join(", ")}.` : ""}`)
|
|
301
|
+
: check("site-settings", "info", `No site settings Unbase can set in the root layout${layoutWhy ? ` (${layoutWhy.reason})` : ""}.`),
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
features.push({ id: "page-management", name: "Page management", state: featureState(checks), checks });
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// ── Analytics: with the comments' client module, so on the same link and loader ──
|
|
308
|
+
{
|
|
309
|
+
const injects = version && compareVersions(version, INJECTS_FROM) >= 0;
|
|
310
|
+
const old = version && compareVersions(version, INSTRUMENTATION_FROM) >= 0 && !injects;
|
|
311
|
+
features.push(
|
|
312
|
+
analyticsFeature(site, {
|
|
313
|
+
umbrella: NEXT,
|
|
314
|
+
version: listed ? (have ?? deps[NEXT.name]) : null,
|
|
315
|
+
configured: wrapped,
|
|
316
|
+
option: wrapped ? config.analytics : null,
|
|
317
|
+
project: readLink(site) || null,
|
|
318
|
+
before: false,
|
|
319
|
+
step: STEPS.analytics,
|
|
320
|
+
install: STEPS.install,
|
|
321
|
+
config: site.configFile ?? "next.config.ts",
|
|
322
|
+
loader: old ? importsLoader(text(instrumentationFile(site))) : null,
|
|
323
|
+
}),
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
return features;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* A built page as a visitor sees it, for comparing two builds: Next.js's scripts (the RSC payload,
|
|
331
|
+
* chunk references, the comments' loader) and its hashed asset names say nothing about what renders.
|
|
332
|
+
*/
|
|
333
|
+
export const normaliseNext = (html) =>
|
|
334
|
+
html
|
|
335
|
+
.replace(/<script\b[^>]*>[\s\S]*?<\/script>/g, "")
|
|
336
|
+
.replace(/<link\b[^>]*\bas="script"[^>]*>/g, "")
|
|
337
|
+
.replace(/\/_next\/static\/[^"'\s)]+/g, "/_next/static/…")
|
|
338
|
+
.replace(/\s(nonce|data-dpl-id)="[^"]*"/g, "");
|
|
339
|
+
|
|
340
|
+
export default {
|
|
341
|
+
id: "next",
|
|
342
|
+
name: "Next.js",
|
|
343
|
+
/** In the order the kit looks for them (kitFiles). */
|
|
344
|
+
configFiles: core.kitFiles("next").filter((f) => f !== "package.json"),
|
|
345
|
+
agents: AGENTS,
|
|
346
|
+
comments: true,
|
|
347
|
+
/** Analytics comes with the comments' client module: comment-modus.json links both, or neither. */
|
|
348
|
+
analyticsLink: false,
|
|
349
|
+
analyticsOption,
|
|
350
|
+
// next build has no output folder of its own: it writes the site's .next, where its pages are read.
|
|
351
|
+
build: { bin: "next", args: () => ["build"], env: { NEXT_TELEMETRY_DISABLED: "1" }, output: [`${DIST}/server/app`, `${DIST}/server/pages`] },
|
|
352
|
+
packages: () => [NEXT, SELF],
|
|
353
|
+
init: planInit,
|
|
354
|
+
linked: (site) => readLink(site) ?? null,
|
|
355
|
+
uninstall: planUninstall,
|
|
356
|
+
doctor: doctorFeatures,
|
|
357
|
+
normalise: normaliseNext,
|
|
358
|
+
};
|