@unbase-app/cli 0.3.0 → 0.4.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 +26 -2
- package/README.md +64 -6
- package/bin/unbase.mjs +53 -3
- package/lib/args.mjs +19 -7
- package/lib/page.mjs +193 -0
- package/lib/readers.mjs +684 -23
- package/lib/uninstall.mjs +15 -1
- package/lib/versions.mjs +5 -5
- package/package.json +2 -2
package/AGENTS.md
CHANGED
|
@@ -32,7 +32,31 @@ These are conventions, not an Unbase runtime: everything the steps ask for is pl
|
|
|
32
32
|
framework that a developer would write, it renders the same with or without Unbase, and you can
|
|
33
33
|
follow it while a site is first written.
|
|
34
34
|
|
|
35
|
+
## Renaming, duplicating and deleting pages
|
|
36
|
+
|
|
37
|
+
Don't move, copy or delete a page by hand: use the CLI, which does it by Unbase's own rule and so
|
|
38
|
+
makes exactly the changes Unbase's editor would (the page's files, the links to it, its route
|
|
39
|
+
file, `unbase.pages.json`).
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
npx @unbase-app/cli page list # what each page can do here, and why not
|
|
43
|
+
npx @unbase-app/cli page rename /funding /financiering [--name "Financiering"] [--not-live]
|
|
44
|
+
npx @unbase-app/cli page duplicate /pricing [/pricing-2027] [--name "Pricing 2027"]
|
|
45
|
+
npx @unbase-app/cli page delete /old-offer [--redirect /pricing | --not-found] [--not-live]
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
- Run it with `--dry-run --json` first; it writes the files in the working tree and commits nothing.
|
|
49
|
+
- A page counts as live unless you pass `--not-live` (only for a page that was never on the live
|
|
50
|
+
site). A live page renamed or deleted leaves a redirect in `unbase.pages.json`; never write
|
|
51
|
+
that redirect into the framework's config yourself, and never remove one.
|
|
52
|
+
- A refusal exits 1 with its reason and what it means. `no_redirects`: the site's Unbase package
|
|
53
|
+
is too old to apply redirects; run `npx @unbase-app/cli init` to update it, never `--not-live`
|
|
54
|
+
for a page that was live. `content_bound`, `unparsed`: leave it to a person or Unbase's Operator.
|
|
55
|
+
- `unlinked` lists files that still name a deleted page's address (a link built at runtime, or one
|
|
56
|
+
the rule leaves alone): change those by hand where the site's text allows it.
|
|
57
|
+
|
|
35
58
|
## Leaving
|
|
36
59
|
|
|
37
|
-
`npx @unbase-app/cli uninstall` takes out what init added, after the drafts are published or deleted
|
|
38
|
-
|
|
60
|
+
`npx @unbase-app/cli uninstall` takes out what init added, after the drafts are published or deleted
|
|
61
|
+
and the redirects in `unbase.pages.json` are in the site's own config, and compares every built
|
|
62
|
+
page before and after.
|
package/README.md
CHANGED
|
@@ -6,6 +6,7 @@ Adds [Unbase](https://unbase.app) to an Astro, Vite + React or Next.js site, che
|
|
|
6
6
|
npx @unbase-app/cli init # the framework's package, its line in its config, comment-modus.json
|
|
7
7
|
npx @unbase-app/cli doctor # per feature: on, or what is missing, where, and how to fix it
|
|
8
8
|
npx @unbase-app/cli uninstall # all of it out again; the built pages compared before and after
|
|
9
|
+
npx @unbase-app/cli page … # rename, duplicate or delete a page, as Unbase's editor does
|
|
9
10
|
```
|
|
10
11
|
|
|
11
12
|
Always with the package's name: the unscoped `unbase` on npm is another product's package, so a
|
|
@@ -56,7 +57,7 @@ versions:
|
|
|
56
57
|
|
|
57
58
|
| Package | Version | |
|
|
58
59
|
|---|---|---|
|
|
59
|
-
| `@unbase-app/astro` | 0.
|
|
60
|
+
| `@unbase-app/astro` | 0.3.0 | Unbase in an Astro site, one integration: comment mode (`@jens4m/comment-modus` 1.9.0: comments on the live site; it writes the content schemas Unbase reads), the draft-pages kit (`@unbase-app/pages` 0.4.0: draft pages kept out of production builds, and the redirects a moved page leaves applied) and the analytics beacon (`@unbase-app/analytics` 0.1.0) |
|
|
60
61
|
| `@unbase-app/cli` | this one | devDependency: `AGENTS.md` and `doctor`, pinned |
|
|
61
62
|
|
|
62
63
|
In `astro.config`, found and changed through its syntax tree, never by text substitution:
|
|
@@ -83,7 +84,7 @@ second run changes nothing. It touches no content.
|
|
|
83
84
|
|
|
84
85
|
### Vite + React
|
|
85
86
|
|
|
86
|
-
One package, `@unbase-app/vite` (a devDependency, beside `vite`), at 0.
|
|
87
|
+
One package, `@unbase-app/vite` (a devDependency, beside `vite`), at 0.3.0: the draft-pages kit, the analytics beacon and
|
|
87
88
|
comment mode's loader in one Vite plugin. In `vite.config`, through its syntax tree (Lovable's
|
|
88
89
|
`defineConfig(({ mode }) => ({ … }))` and `plugins: [ … ].filter(Boolean)` included):
|
|
89
90
|
|
|
@@ -101,7 +102,7 @@ Unbase reads and writes as it is; it has no page settings, so nothing is written
|
|
|
101
102
|
|
|
102
103
|
### Next.js
|
|
103
104
|
|
|
104
|
-
On a Next.js site `init` adds `@unbase-app/next` (0.
|
|
105
|
+
On a Next.js site `init` adds `@unbase-app/next` (0.3.0) and wraps the config `next.config` hands Next.js in
|
|
105
106
|
`withUnbase(…)` (through its syntax tree; a CommonJS config gets the `require`), writes
|
|
106
107
|
`comment-modus.json`, and on a Next.js from 15.3 to 16.2 imports the comments' loader in
|
|
107
108
|
`instrumentation-client.ts` (from 16.3 `withUnbase` injects it). `doctor` builds with `next build`
|
|
@@ -196,7 +197,9 @@ an older comment-modus that ignores `"preview"`.
|
|
|
196
197
|
## `unbase uninstall`
|
|
197
198
|
|
|
198
199
|
Refuses while `unbase.pages.json` holds a draft, and lists them: without the kit a draft is built
|
|
199
|
-
and goes live, so each is published or deleted in Unbase first.
|
|
200
|
+
and goes live, so each is published or deleted in Unbase first. It refuses too while the file
|
|
201
|
+
sends old addresses on (`redirects`, left by renaming or deleting a live page), and lists them:
|
|
202
|
+
only the package applies them, so they go into the site's own redirects first. Then it builds the site, removes
|
|
200
203
|
the packages, their lines in `astro.config`, `comment-modus.json`, `unbase.pages.json`,
|
|
201
204
|
`src/content/.schemas/`, the collector in the CSP of `vercel.json` and `public/_headers`, and the
|
|
202
205
|
pointer line, builds again, and compares every page's HTML. Taken out first: comment mode's markers
|
|
@@ -208,6 +211,54 @@ a terminal; `--no-build` skips the comparison; `--no-install` writes `package.js
|
|
|
208
211
|
|
|
209
212
|
The route files, the settings file and the layout's reads stay: they are the site's own code.
|
|
210
213
|
|
|
214
|
+
## `unbase page`
|
|
215
|
+
|
|
216
|
+
Renames, duplicates and deletes a page by the kit's own rule (`planPage` in `@unbase-app/pages`'s
|
|
217
|
+
core, bundled here), so the files change exactly as they do when Unbase's editor does it: the
|
|
218
|
+
same files always give the same changes. It writes them in the working tree and commits nothing;
|
|
219
|
+
review them with `git diff` and commit them yourself.
|
|
220
|
+
|
|
221
|
+
```bash
|
|
222
|
+
npx @unbase-app/cli page rename /funding /financiering --name Financiering
|
|
223
|
+
npx @unbase-app/cli page duplicate /pricing /pricing-2027
|
|
224
|
+
npx @unbase-app/cli page delete /old-offer --redirect /pricing
|
|
225
|
+
npx @unbase-app/cli page list
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
| Command | |
|
|
229
|
+
|---|---|
|
|
230
|
+
| `page rename <from> <to> [--name <name>] [--not-live]` | A new address: the page's files move (on Next.js its folder, on React Router the route's `path`), every quoted link to it or below it in code and content follows, its route file's `pad` too, and the pages under it come along. `--name` sets its name in Unbase (`<to>` the same as `<from>`: only the name) |
|
|
231
|
+
| `page duplicate <path> [<to>] [--name <name>]` | A copy, by default at the first free `<path>-copy`, with its own copy of what only the page owns (its modules and content); a draft where the kit is installed. Named "… (copy)" when the original has a name |
|
|
232
|
+
| `page delete <path> [--redirect <to> \| --not-found] [--not-live]` | Its file and what only it owns; shared parts stay. The links to it are listed, not changed. Visitors to the old address go to the page above it, `--redirect`'s page, or with `--not-found` nowhere |
|
|
233
|
+
| `page list [--not-live]` | Per page: whether it can be renamed, duplicated and deleted here, and if not, why |
|
|
234
|
+
|
|
235
|
+
Every one takes `--dry-run` (print the changes, write nothing), `--json` and `--dir`.
|
|
236
|
+
|
|
237
|
+
**Live pages.** A page counts as live unless `--not-live` says it was never on the live site. A
|
|
238
|
+
live page renamed or deleted leaves a redirect in `unbase.pages.json` (old address → new), which
|
|
239
|
+
the framework's package applies at build from `@unbase-app/astro` 0.3.0, `@unbase-app/vite` 0.3.0
|
|
240
|
+
and `@unbase-app/next` 0.3.0 on (`@unbase-app/pages` 0.4.0). On a site with an older one, or none,
|
|
241
|
+
a live rename or delete is refused (`no_redirects`) instead of breaking the old address: run
|
|
242
|
+
`unbase init` to update, or pass `--not-live` for a page that never was live.
|
|
243
|
+
|
|
244
|
+
**Refusals** exit 1 with the reason and what it means (`--json`: `reason`, `detail`, `message`):
|
|
245
|
+
|
|
246
|
+
| Reason | |
|
|
247
|
+
|---|---|
|
|
248
|
+
| `home` | Home can be duplicated, not renamed or deleted |
|
|
249
|
+
| `nested` | Delete: pages are under it; delete those first |
|
|
250
|
+
| `taken` | The new address is a page already, or the site's own redirects send it on |
|
|
251
|
+
| `address` | Not an address the page can have (plain segments, not under itself), or `--redirect` is not a page |
|
|
252
|
+
| `no_redirects` | A live page leaves a redirect, and this site's build does not apply them (above) |
|
|
253
|
+
| `content_bound` | Duplicate: its content is a collection of its own, and a copy needs code the kit does not write. Unbase's Operator can do it |
|
|
254
|
+
| `content_unregistered` | It reads content by a name no content file goes by |
|
|
255
|
+
| `template` | The page is made by a dynamic route: change it where its data is |
|
|
256
|
+
| `unparsed` | The kit cannot trace the page's route to its file |
|
|
257
|
+
| `missing` | The code makes no page at this address |
|
|
258
|
+
| `same` | Nothing to change |
|
|
259
|
+
| `invalid` | `unbase.pages.json` is not valid |
|
|
260
|
+
| `framework` | Not Astro, Next.js or Vite + React Router |
|
|
261
|
+
|
|
211
262
|
## For programs
|
|
212
263
|
|
|
213
264
|
- **Never asks without a terminal.** A choice without its flag is an error (exit 2) naming the
|
|
@@ -221,10 +272,17 @@ The route files, the settings file and the layout's reads stay: they are the sit
|
|
|
221
272
|
with `state` (`on`, `off`, `waiting`, `not-installed`) and `checks` (`id`, `state`, `message`,
|
|
222
273
|
and for a `missing` or `waiting` check `missing`, `file`, `step`). `uninstall`: `files`
|
|
223
274
|
(every file it touched, the lockfile included), `packages`, `comparison` (`pages`,
|
|
224
|
-
`differences`, `secondBuild`), or `refused: "drafts"` with `drafts
|
|
275
|
+
`differences`, `secondBuild`), or `refused: "drafts"` with `drafts`, or `refused: "redirects"`
|
|
276
|
+
with `redirects`. `page`: `files` (each
|
|
277
|
+
`path` and `action`: `created`, `changed`, `moved` or `copied` with `from`, or `deleted`),
|
|
278
|
+
`links` (files whose links it rewrote), `unlinked` (files that still name the old address),
|
|
279
|
+
`redirects` (what changed in `unbase.pages.json`: source → destination, `null` for one that
|
|
280
|
+
went), `moves` (the addresses), `dryRun`; refused: `reason`, `detail`, `message`. `page list`:
|
|
281
|
+
`pages`, each with `rename`, `duplicate` and `delete` as `{ ok }` or `{ ok: false, reason }`,
|
|
282
|
+
and `redirects` (whether the site's build applies them).
|
|
225
283
|
An error: `{ ok: false, exitCode: 2, error: { code, message, file? } }`.
|
|
226
284
|
- **Exit codes:** `0` passed, `1` failed (doctor found something missing; uninstall refused or
|
|
227
|
-
found a difference), `2` could not run (usage, no Astro site, a config it cannot edit safely, an
|
|
285
|
+
found a difference; a page action refused), `2` could not run (usage, no Astro site, a config it cannot edit safely, an
|
|
228
286
|
install that failed).
|
|
229
287
|
- **No login, no project, no network** beyond the package manager's install: it never calls Unbase,
|
|
230
288
|
and the versions are pinned.
|
package/bin/unbase.mjs
CHANGED
|
@@ -6,6 +6,11 @@
|
|
|
6
6
|
// [--no-install] [--tarballs <dir>] [--yes] [--json] [--dir <path>]
|
|
7
7
|
// unbase doctor [--no-build] [--json] [--dir <path>]
|
|
8
8
|
// unbase uninstall [--yes] [--no-build] [--no-install] [--json] [--dir <path>]
|
|
9
|
+
// unbase page rename <from> <to> [--name <name>] [--not-live]
|
|
10
|
+
// duplicate <path> [<to>] [--name <name>]
|
|
11
|
+
// delete <path> [--redirect <to> | --not-found] [--not-live]
|
|
12
|
+
// list [--not-live]
|
|
13
|
+
// [--dry-run] [--json] [--dir <path>]
|
|
9
14
|
//
|
|
10
15
|
// Exit codes: 0 passed, 1 failed, 2 could not run. Never asks without a
|
|
11
16
|
// terminal: a choice without its flag is an error. Never calls Unbase.
|
|
@@ -17,6 +22,7 @@ import { COULD_NOT_RUN, CouldNotRun } from "../lib/errors.mjs";
|
|
|
17
22
|
import { FRAMEWORKS } from "../lib/frameworks/index.mjs";
|
|
18
23
|
import { init } from "../lib/init.mjs";
|
|
19
24
|
import { parseArgs } from "../lib/args.mjs";
|
|
25
|
+
import { page } from "../lib/page.mjs";
|
|
20
26
|
import { uninstall } from "../lib/uninstall.mjs";
|
|
21
27
|
import { SELF } from "../lib/versions.mjs";
|
|
22
28
|
|
|
@@ -39,12 +45,24 @@ const USAGE = `unbase ${SELF.version}: Unbase in a site (${FRAMEWORKS.map((f) =>
|
|
|
39
45
|
--yes do it without asking
|
|
40
46
|
--no-build skip the comparison
|
|
41
47
|
--no-install change package.json only
|
|
48
|
+
unbase page Rename, duplicate or delete a page by the kit's rule, the changes Unbase's
|
|
49
|
+
editor makes; written in the working tree, never committed
|
|
50
|
+
rename <from> <to> a new address (the links and its route file follow)
|
|
51
|
+
duplicate <path> [<to>] a copy, by default at <path>-copy; a draft where the site has drafts
|
|
52
|
+
delete <path> its file and what only it owns; links to it are listed
|
|
53
|
+
list what each page can do here, and why not
|
|
54
|
+
--name <name> the page's name in Unbase (rename, duplicate)
|
|
55
|
+
--redirect <to> | --not-found
|
|
56
|
+
delete: where visitors to the old address go (default: the page
|
|
57
|
+
above it)
|
|
58
|
+
--not-live the page was never on the live site: no redirect is left
|
|
59
|
+
--dry-run print the changes, write nothing
|
|
42
60
|
|
|
43
61
|
All: --json (for programs), --dir <path> (the site's folder; default: here)
|
|
44
62
|
Exit codes: 0 passed, 1 failed, 2 could not run.
|
|
45
63
|
After init: node_modules/${SELF.name}/AGENTS.md has the steps left to do.`;
|
|
46
64
|
|
|
47
|
-
const COMMANDS = { init, doctor, uninstall };
|
|
65
|
+
const COMMANDS = { init, doctor, uninstall, page };
|
|
48
66
|
|
|
49
67
|
function makeIo(json) {
|
|
50
68
|
const interactive = !json && !!process.stdin.isTTY && !!process.stdout.isTTY;
|
|
@@ -104,6 +122,7 @@ function render(r) {
|
|
|
104
122
|
out.push("", r.ok ? `Passed: ${s.on} on${s.waiting ? `, ${s.waiting} waiting on something outside this repository` : ""}${s.notInstalled ? `, ${s.notInstalled} not installed` : ""}.` : `Failed: ${s.off} feature(s) need a change in the site.`);
|
|
105
123
|
} else if (r.command === "uninstall") {
|
|
106
124
|
if (r.refused === "drafts") out.push(r.message, ...r.drafts.map((d) => ` ${d}`));
|
|
125
|
+
else if (r.refused === "redirects") out.push(r.message, ...Object.entries(r.redirects).map(([from, to]) => ` ${from} → ${to}`));
|
|
107
126
|
else {
|
|
108
127
|
for (const f of r.files ?? []) {
|
|
109
128
|
out.push(` ${f.action} ${f.file}`);
|
|
@@ -115,14 +134,45 @@ function render(r) {
|
|
|
115
134
|
if (d.before) out.push(` before: …${d.before}…`, ` after: …${d.after}…`);
|
|
116
135
|
}
|
|
117
136
|
}
|
|
137
|
+
} else if (r.command === "page") {
|
|
138
|
+
out.push(...renderPage(r));
|
|
118
139
|
}
|
|
119
140
|
return out.join("\n");
|
|
120
141
|
}
|
|
121
142
|
|
|
143
|
+
const CHANGE = { created: "+", changed: "~", moved: "→", copied: "+", deleted: "-" };
|
|
144
|
+
|
|
145
|
+
function renderPage(r) {
|
|
146
|
+
const out = [];
|
|
147
|
+
if (r.action === "list") {
|
|
148
|
+
const cell = (c) => (c.ok ? "yes" : `no (${c.reason})`);
|
|
149
|
+
for (const p of r.pages) {
|
|
150
|
+
out.push(`${p.path}${p.name ? ` "${p.name}"` : ""}${p.draft ? " draft" : ""}`);
|
|
151
|
+
out.push(` rename ${cell(p.rename)} · duplicate ${cell(p.duplicate)} · delete ${cell(p.delete)}`);
|
|
152
|
+
}
|
|
153
|
+
out.push("", r.live ? "Every page counted as live (--not-live: as never live)." : "No page counted as live.");
|
|
154
|
+
out.push(
|
|
155
|
+
r.redirects
|
|
156
|
+
? "This site's build applies the redirects a live rename or delete leaves."
|
|
157
|
+
: `This site's build does not apply redirects yet, so a live page cannot be renamed or deleted with one${r.package ? `: ${r.package} ${r.since} or later does` : ""}.`,
|
|
158
|
+
);
|
|
159
|
+
return out;
|
|
160
|
+
}
|
|
161
|
+
if (!r.ok) return [`unbase page ${r.action} ${r.path}: refused (${r.reason})`, ` ${r.message}`];
|
|
162
|
+
out.push(`${r.message}${r.dryRun ? " (dry run: nothing written)" : ""}`);
|
|
163
|
+
out.push(` files: ${r.files.length}`);
|
|
164
|
+
for (const f of r.files) out.push(` ${CHANGE[f.action] ?? "~"} ${f.path}${f.from ? ` (${f.action} from ${f.from})` : f.action === "deleted" ? " (deleted)" : ""}`);
|
|
165
|
+
if (r.links.length) out.push(` links rewritten: ${r.links.length}`, ...r.links.map((l) => ` ${l}`));
|
|
166
|
+
if (r.unlinked.length) out.push(` links left: ${r.unlinked.length} (they still name ${r.path}; change them yourself)`, ...r.unlinked.map((l) => ` ${l}`));
|
|
167
|
+
const redirects = Object.entries(r.redirects);
|
|
168
|
+
if (redirects.length) out.push(` redirects in unbase.pages.json: ${redirects.length}`, ...redirects.map(([from, to]) => ` ${from} → ${to ?? "(removed)"}`));
|
|
169
|
+
return out;
|
|
170
|
+
}
|
|
171
|
+
|
|
122
172
|
async function main(argv) {
|
|
123
173
|
let json = argv.includes("--json");
|
|
124
174
|
try {
|
|
125
|
-
const { command, flags } = parseArgs(argv);
|
|
175
|
+
const { command, flags, args } = parseArgs(argv);
|
|
126
176
|
if (command === "help") {
|
|
127
177
|
console.log(USAGE);
|
|
128
178
|
return 0;
|
|
@@ -132,7 +182,7 @@ async function main(argv) {
|
|
|
132
182
|
return 0;
|
|
133
183
|
}
|
|
134
184
|
json = !!flags.json;
|
|
135
|
-
const result = await COMMANDS[command](flags, makeIo(json));
|
|
185
|
+
const result = await COMMANDS[command](flags, makeIo(json), args);
|
|
136
186
|
console.log(json ? JSON.stringify(result, null, 2) : render(result));
|
|
137
187
|
return result.exitCode;
|
|
138
188
|
} catch (e) {
|
package/lib/args.mjs
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
// Flags. Every choice is a flag (`--comments` / `--no-comments`, `--yes`):
|
|
3
3
|
// without a terminal a missing one is an error, never a question. Unknown
|
|
4
4
|
// flags are an error too, so a typo in a script fails instead of being
|
|
5
|
-
// ignored.
|
|
5
|
+
// ignored. Only `unbase page` takes arguments besides its flags: what to do
|
|
6
|
+
// and the addresses (`unbase page rename /about /about-us`).
|
|
6
7
|
// ============================================================================
|
|
7
8
|
|
|
8
9
|
import { CouldNotRun } from "./errors.mjs";
|
|
@@ -12,20 +13,30 @@ export const FLAGS = {
|
|
|
12
13
|
init: { comments: "bool", "no-comments": "bool", project: "value", pointer: "optional-value", "no-pointer": "bool", "no-install": "bool", tarballs: "value", yes: "bool", json: "bool", dir: "value" },
|
|
13
14
|
doctor: { json: "bool", "no-build": "bool", dir: "value" },
|
|
14
15
|
uninstall: { yes: "bool", json: "bool", "no-build": "bool", "no-install": "bool", dir: "value" },
|
|
16
|
+
page: { name: "value", "not-live": "bool", redirect: "value", "not-found": "bool", "dry-run": "bool", json: "bool", dir: "value" },
|
|
15
17
|
};
|
|
16
18
|
|
|
17
|
-
/**
|
|
19
|
+
/** Commands that take arguments besides their flags. */
|
|
20
|
+
const ARGUMENTS = new Set(["page"]);
|
|
21
|
+
|
|
22
|
+
/** @returns {{ command: string | null, flags: Record<string, string | true>, args: string[] }} */
|
|
18
23
|
export function parseArgs(argv) {
|
|
19
24
|
const [command, ...rest] = argv;
|
|
20
|
-
if (!command || command === "--help" || command === "-h" || command === "help") return { command: "help", flags: {} };
|
|
21
|
-
if (command === "--version" || command === "-v") return { command: "version", flags: {} };
|
|
25
|
+
if (!command || command === "--help" || command === "-h" || command === "help") return { command: "help", flags: {}, args: [] };
|
|
26
|
+
if (command === "--version" || command === "-v") return { command: "version", flags: {}, args: [] };
|
|
22
27
|
const known = FLAGS[command];
|
|
23
|
-
if (!known) throw new CouldNotRun("usage", `Unknown command "${command}". Commands:
|
|
28
|
+
if (!known) throw new CouldNotRun("usage", `Unknown command "${command}". Commands: ${Object.keys(FLAGS).join(", ")}.`);
|
|
24
29
|
/** @type {Record<string, string | true>} */
|
|
25
30
|
const flags = {};
|
|
31
|
+
/** @type {string[]} */
|
|
32
|
+
const args = [];
|
|
26
33
|
for (let i = 0; i < rest.length; i++) {
|
|
27
34
|
const arg = rest[i];
|
|
28
|
-
if (!arg.startsWith("--"))
|
|
35
|
+
if (!arg.startsWith("--")) {
|
|
36
|
+
if (!ARGUMENTS.has(command)) throw new CouldNotRun("usage", `Unexpected argument "${arg}" for ${command}.`);
|
|
37
|
+
args.push(arg);
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
29
40
|
const [name, inline] = arg.slice(2).split(/=(.*)/s, 2);
|
|
30
41
|
const kind = known[name];
|
|
31
42
|
if (!kind) throw new CouldNotRun("usage", `Unknown flag --${name} for ${command}. Flags: ${Object.keys(known).map((f) => `--${f}`).join(", ")}.`);
|
|
@@ -45,5 +56,6 @@ export function parseArgs(argv) {
|
|
|
45
56
|
}
|
|
46
57
|
if (flags.comments && flags["no-comments"]) throw new CouldNotRun("usage", "Use --comments or --no-comments, not both.");
|
|
47
58
|
if (flags.pointer && flags["no-pointer"]) throw new CouldNotRun("usage", "Use --pointer or --no-pointer, not both.");
|
|
48
|
-
|
|
59
|
+
if (flags.redirect !== undefined && flags["not-found"]) throw new CouldNotRun("usage", "Use --redirect or --not-found, not both.");
|
|
60
|
+
return { command, flags, args };
|
|
49
61
|
}
|
package/lib/page.mjs
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// unbase page — rename, duplicate or delete a page, or list what can be done
|
|
3
|
+
// to each, by the kit's own rule (core.planPage, docs/plan-page-actions.md):
|
|
4
|
+
// the same files give the same changes as Unbase's editor makes. It writes
|
|
5
|
+
// them in the working tree and commits nothing.
|
|
6
|
+
//
|
|
7
|
+
// unbase page rename <from> <to> [--name <name>] [--not-live]
|
|
8
|
+
// unbase page duplicate <path> [<to>] [--name <name>]
|
|
9
|
+
// unbase page delete <path> [--redirect <to> | --not-found] [--not-live]
|
|
10
|
+
// unbase page list [--not-live]
|
|
11
|
+
//
|
|
12
|
+
// A page counts as live unless --not-live says it never was on the live
|
|
13
|
+
// site: a live page that is renamed or deleted leaves a redirect in
|
|
14
|
+
// unbase.pages.json, which only the framework's package applies from the
|
|
15
|
+
// version core.REDIRECTS_SINCE names. Without one the action is refused,
|
|
16
|
+
// never an address broken.
|
|
17
|
+
// ============================================================================
|
|
18
|
+
|
|
19
|
+
import { copyFileSync, mkdirSync, readdirSync, readFileSync, rmdirSync, rmSync, writeFileSync } from "node:fs";
|
|
20
|
+
import { dirname, join } from "node:path";
|
|
21
|
+
import { CouldNotRun, FAILED, PASSED } from "./errors.mjs";
|
|
22
|
+
import { core } from "./readers.mjs";
|
|
23
|
+
import { openSite } from "./site.mjs";
|
|
24
|
+
|
|
25
|
+
export const ACTIONS = ["rename", "duplicate", "delete", "list"];
|
|
26
|
+
|
|
27
|
+
/** Per action: how many addresses it takes (at least, at most), and which of the page flags. */
|
|
28
|
+
const SHAPE = {
|
|
29
|
+
rename: { args: [2, 2], usage: "rename <from> <to>", flags: ["name", "not-live"] },
|
|
30
|
+
duplicate: { args: [1, 2], usage: "duplicate <path> [<to>]", flags: ["name"] },
|
|
31
|
+
delete: { args: [1, 1], usage: "delete <path>", flags: ["redirect", "not-found", "not-live"] },
|
|
32
|
+
list: { args: [0, 0], usage: "list", flags: ["not-live"] },
|
|
33
|
+
};
|
|
34
|
+
const PAGE_FLAGS = ["name", "redirect", "not-found", "not-live"];
|
|
35
|
+
|
|
36
|
+
/** Each framework's package that applies the page file's redirects. */
|
|
37
|
+
const PACKAGE = { astro: "@unbase-app/astro", "vite-react": "@unbase-app/vite", next: "@unbase-app/next" };
|
|
38
|
+
|
|
39
|
+
/** Files the core reads as text: code and content. Everything else is only moved, copied or deleted, by its bytes. */
|
|
40
|
+
const TEXT = /\.(astro|tsx|jsx|ts|js|mjs|cjs|mts|cts|vue|svelte|md|mdx|markdown|html?|json|jsonc|css|scss|sass|less|ya?ml|toml|txt|svg|xml)$/i;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* What each refusal means, in one line. The reasons are core.planPage's (and pageActions'); the
|
|
44
|
+
* plan's table has them too (docs/plan-page-actions.md, Reasons).
|
|
45
|
+
* @type {Record<string, (detail: string, framework: string | null) => string>}
|
|
46
|
+
*/
|
|
47
|
+
export const REASONS = {
|
|
48
|
+
invalid: (d) => `${core.PAGE_FILE} is not valid${d ? ` (${d})` : ""}: fix it first`,
|
|
49
|
+
framework: () => "the kit has no page actions for this framework; it has them for Astro, Next.js and Vite + React Router",
|
|
50
|
+
missing: () => "the code makes no page at this address",
|
|
51
|
+
template: (d) => `the page is made by a dynamic route${d ? ` (${d})` : ""}: change it where its data is`,
|
|
52
|
+
unparsed: (d) => `the kit cannot trace this page's route to its file${d ? ` (${d})` : ""}: change it by hand, or with Unbase's Operator`,
|
|
53
|
+
home: () => "Home can be duplicated, but not renamed or deleted",
|
|
54
|
+
address: (d) => `${d || "that"} is not an address it can go to: plain segments, not under the page itself, and for --redirect a page of the site`,
|
|
55
|
+
taken: (d) => `${d || "the new address"} is a page already, or the site's own redirects send it on`,
|
|
56
|
+
no_redirects: (_d, framework) =>
|
|
57
|
+
`a live page leaves a redirect, and this site's build does not apply ${core.PAGE_FILE}'s redirects: update ${PACKAGE[String(framework)] ?? "the framework's Unbase package"} to ${core.REDIRECTS_SINCE[PACKAGE[String(framework)]] ?? "a newer version"} or later (unbase init), or pass --not-live when the page was never on the live site`,
|
|
58
|
+
kit_missing: (d, framework) => REASONS.no_redirects(d, framework),
|
|
59
|
+
same: () => "nothing to change: the address and the name are what they are",
|
|
60
|
+
nested: (d) => `pages are under it${d ? ` (${d})` : ""}: delete those first`,
|
|
61
|
+
content_bound: (d) => `its content is a collection of its own${d ? ` (${d})` : ""}, and a copy needs code the kit does not write: Unbase's Operator can make it`,
|
|
62
|
+
content_unregistered: (d) => `it reads content by a name no content file goes by${d ? ` (${d})` : ""}`,
|
|
63
|
+
redirect_conflict: (d) => `the site's own redirects already send ${d || "this address"} somewhere else`,
|
|
64
|
+
runtime: () => "the page comes from a CMS: change it there",
|
|
65
|
+
too_large: () => "the site is too large to work out the change",
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/** The reason's line, or the reason itself when the CLI does not know it yet (a newer core). */
|
|
69
|
+
export const explain = (reason, detail = "", framework = null) => (REASONS[reason] ? REASONS[reason](detail ?? "", framework) : `${reason}${detail ? ` (${detail})` : ""}`);
|
|
70
|
+
|
|
71
|
+
/** The app's files as the core reads a commit: every path (git's, or a walk without node_modules, dist, .git), and the text of code and content files. */
|
|
72
|
+
function appFilesOf(site) {
|
|
73
|
+
const paths = site.appPaths;
|
|
74
|
+
const cache = new Map();
|
|
75
|
+
const text = (p) => {
|
|
76
|
+
if (!cache.has(p)) cache.set(p, TEXT.test(p) ? site.text(p) : null);
|
|
77
|
+
return cache.get(p);
|
|
78
|
+
};
|
|
79
|
+
return { paths, text };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Writes a plan's files under `root`: copies first, so a file that moves is read before it goes;
|
|
84
|
+
* then new text; then deletions; then the folders a deletion left empty.
|
|
85
|
+
* @param {string} root
|
|
86
|
+
* @param {{ path: string, content?: string, from?: string, deleted?: true }[]} writes
|
|
87
|
+
*/
|
|
88
|
+
export function applyWrites(root, writes) {
|
|
89
|
+
const at = (p) => join(root, p);
|
|
90
|
+
// A source that is itself written over is read before anything changes.
|
|
91
|
+
const written = new Set(writes.filter((w) => !w.deleted).map((w) => w.path));
|
|
92
|
+
const held = new Map(writes.filter((w) => w.from && written.has(w.from)).map((w) => [w.from, readFileSync(at(/** @type {string} */ (w.from)))]));
|
|
93
|
+
for (const w of writes) {
|
|
94
|
+
if (!w.from) continue;
|
|
95
|
+
mkdirSync(dirname(at(w.path)), { recursive: true });
|
|
96
|
+
if (held.has(w.from)) writeFileSync(at(w.path), held.get(w.from));
|
|
97
|
+
else copyFileSync(at(w.from), at(w.path));
|
|
98
|
+
}
|
|
99
|
+
for (const w of writes) {
|
|
100
|
+
if (w.content === undefined) continue;
|
|
101
|
+
mkdirSync(dirname(at(w.path)), { recursive: true });
|
|
102
|
+
writeFileSync(at(w.path), w.content);
|
|
103
|
+
}
|
|
104
|
+
for (const w of writes) if (w.deleted) rmSync(at(w.path), { force: true });
|
|
105
|
+
for (const w of writes) {
|
|
106
|
+
if (!w.deleted) continue;
|
|
107
|
+
for (let d = dirname(w.path); d && d !== "." && d !== "/"; d = dirname(d)) {
|
|
108
|
+
try {
|
|
109
|
+
if (readdirSync(at(d)).length) break;
|
|
110
|
+
rmdirSync(at(d));
|
|
111
|
+
} catch {
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** A plan's writes as the output says them: created, changed, moved (with `from`), copied (with `from`) or deleted. */
|
|
119
|
+
function described(plan, files) {
|
|
120
|
+
const movedFrom = new Map((plan.renamed ?? []).map((r) => [r.to, r.from]));
|
|
121
|
+
const have = new Set(files.paths);
|
|
122
|
+
return plan.files.map((w) => {
|
|
123
|
+
if (w.deleted) return { path: w.path, action: "deleted" };
|
|
124
|
+
if (movedFrom.has(w.path)) return { path: w.path, action: "moved", from: movedFrom.get(w.path) };
|
|
125
|
+
if (w.from) return { path: w.path, action: "copied", from: w.from };
|
|
126
|
+
return { path: w.path, action: have.has(w.path) || files.text(w.path) !== null ? "changed" : "created" };
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export async function page(flags, io, args = []) {
|
|
131
|
+
const [action, ...addresses] = args;
|
|
132
|
+
const shape = SHAPE[action];
|
|
133
|
+
if (!shape) throw new CouldNotRun("usage", `${action ? `unbase page ${action}: no such action` : "unbase page: say what to do"}. Actions: ${ACTIONS.join(", ")}.`);
|
|
134
|
+
if (addresses.length < shape.args[0] || addresses.length > shape.args[1]) throw new CouldNotRun("usage", `unbase page ${shape.usage}: ${addresses.length < shape.args[0] ? "an address is missing" : `one address too many ("${addresses.at(-1)}")`}.`);
|
|
135
|
+
for (const f of PAGE_FLAGS) if (flags[f] !== undefined && !shape.flags.includes(f)) throw new CouldNotRun("usage", `--${f} is not for unbase page ${action}.`);
|
|
136
|
+
|
|
137
|
+
const site = await openSite(flags.dir ?? process.cwd());
|
|
138
|
+
const framework = site.detection.framework?.id ?? null;
|
|
139
|
+
const files = appFilesOf(site);
|
|
140
|
+
const live = !flags["not-live"];
|
|
141
|
+
const pageFile = core.parsePageFile(files.text(core.PAGE_FILE));
|
|
142
|
+
const base = { command: "page", action, site: site.root, framework, live };
|
|
143
|
+
|
|
144
|
+
if (action === "list") {
|
|
145
|
+
const found = core.discover({ framework, ...files });
|
|
146
|
+
const result = core.pageActions({ framework, ...files, found, live: live ? new Set(found.pages.map((p) => p.path)) : new Set() });
|
|
147
|
+
const pages = Object.entries(result.actions).map(([path, a]) => ({
|
|
148
|
+
path,
|
|
149
|
+
name: pageFile.ok ? (pageFile.pages[path]?.name ?? null) : null,
|
|
150
|
+
draft: pageFile.ok ? pageFile.pages[path]?.draft === true : false,
|
|
151
|
+
under: a.under,
|
|
152
|
+
rename: a.rename,
|
|
153
|
+
duplicate: a.duplicate,
|
|
154
|
+
delete: a.delete,
|
|
155
|
+
}));
|
|
156
|
+
return { ...base, ok: true, exitCode: PASSED, redirects: result.redirects, package: PACKAGE[String(framework)] ?? null, since: core.REDIRECTS_SINCE[PACKAGE[String(framework)]] ?? null, pages };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const at = core.normalisePath(addresses[0]);
|
|
160
|
+
/** @type {Record<string, unknown>} */
|
|
161
|
+
let request;
|
|
162
|
+
if (action === "rename") request = { action, path: at, to: addresses[1], live, ...(flags.name !== undefined ? { name: String(flags.name) } : {}) };
|
|
163
|
+
else if (action === "duplicate") {
|
|
164
|
+
// Named after the original, as Unbase's menu names a copy, when the original has a name.
|
|
165
|
+
const name = flags.name !== undefined ? String(flags.name) : pageFile.ok && pageFile.pages[at]?.name ? `${pageFile.pages[at].name} (copy)` : undefined;
|
|
166
|
+
request = { action, path: at, ...(addresses[1] ? { to: addresses[1] } : {}), ...(name ? { name } : {}), drafts: core.kitInstalled({ framework, text: files.text }) };
|
|
167
|
+
} else request = { action, path: at, live, ...(flags["not-found"] ? { redirect: null } : flags.redirect !== undefined ? { redirect: String(flags.redirect) } : {}) };
|
|
168
|
+
|
|
169
|
+
const plan = core.planPage(/** @type {any} */ (request), { framework, ...files });
|
|
170
|
+
if (!plan.ok) {
|
|
171
|
+
return { ...base, ok: false, exitCode: FAILED, path: at, reason: plan.reason, detail: plan.detail ?? null, message: explain(plan.reason, plan.detail, framework) };
|
|
172
|
+
}
|
|
173
|
+
const dryRun = !!flags["dry-run"];
|
|
174
|
+
const changes = described(plan, files);
|
|
175
|
+
if (!dryRun) {
|
|
176
|
+
io.progress?.(`Writing ${plan.files.length} file(s)…`);
|
|
177
|
+
applyWrites(site.root, plan.files);
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
...base,
|
|
181
|
+
ok: true,
|
|
182
|
+
exitCode: PASSED,
|
|
183
|
+
dryRun,
|
|
184
|
+
path: plan.path,
|
|
185
|
+
to: plan.to,
|
|
186
|
+
message: plan.message,
|
|
187
|
+
files: changes,
|
|
188
|
+
links: plan.links,
|
|
189
|
+
unlinked: plan.unlinked,
|
|
190
|
+
redirects: plan.redirects,
|
|
191
|
+
moves: plan.moves,
|
|
192
|
+
};
|
|
193
|
+
}
|