@pantheon-systems/p1-next-sdk 0.24.0 → 0.25.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/README.md +17 -1
- package/bin/lib/cli.js +143 -25
- package/bin/lib/detect.js +14 -2
- package/bin/lib/fs-ops.js +2 -1
- package/bin/lib/localized-pages.js +522 -0
- package/bin/lib/messages.js +38 -23
- package/bin/templates/localized-pages/app/[[...puckPath]]/layout.tsx.template +29 -0
- package/bin/templates/localized-pages/app/[[...puckPath]]/page.tsx.template +24 -0
- package/bin/templates/localized-pages/app/p1/layout.tsx.template +11 -0
- package/bin/templates/localized-pages/components/html-shell.tsx.template +28 -0
- package/bin/templates/localized-pages/components/locale-suggestion.module.css.template +74 -0
- package/bin/templates/localized-pages/components/locale-suggestion.tsx.template +94 -0
- package/bin/templates/localized-pages/legacy/app/[...puckPath]/page.tsx.template +23 -0
- package/bin/templates/localized-pages/legacy/app/layout.tsx.template +37 -0
- package/bin/templates/localized-pages/legacy/app/layout.v1.tsx.template +27 -0
- package/bin/templates/localized-pages/legacy/app/page.tsx.template +19 -0
- package/bin/templates/localized-pages/lib/locale-name.ts.template +9 -0
- package/dist/pages-handler.js +2 -2
- package/docs/migrations/editor-layout.md +213 -0
- package/docs/migrations/localized-pages.md +208 -0
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -197,10 +197,26 @@ transpilePackages: [
|
|
|
197
197
|
`p1-next-sdk` dispatches on the first argument:
|
|
198
198
|
|
|
199
199
|
```bash
|
|
200
|
-
npx @pantheon-systems/p1-next-sdk migrate
|
|
200
|
+
npx @pantheon-systems/p1-next-sdk migrate <migration> # was: npx p1-migrate
|
|
201
201
|
npx @pantheon-systems/p1-next-sdk@latest enable-registry
|
|
202
202
|
```
|
|
203
203
|
|
|
204
|
+
### migrate
|
|
205
|
+
|
|
206
|
+
Updates an existing app's files to what newer P1 packages expect, one migration at a time, on a clean git tree. Run it with no migration to list them:
|
|
207
|
+
|
|
208
|
+
```bash
|
|
209
|
+
npx @pantheon-systems/p1-next-sdk migrate # list the migrations
|
|
210
|
+
npx @pantheon-systems/p1-next-sdk migrate localized-pages --dry-run # preview one
|
|
211
|
+
npx @pantheon-systems/p1-next-sdk migrate localized-pages # apply it
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
- `editor-layout`: moves the editor into the persistent `app/p1/(editor)/` layout. Guide: `docs/migrations/editor-layout.md`.
|
|
215
|
+
- `localized-pages`: serves every published page from `app/[[...puckPath]]/` under a root layout that sets `<html lang>`. Guide: `docs/migrations/localized-pages.md`.
|
|
216
|
+
- `locale-prompt`: adds the starter's saved-language prompt to the page client, after `localized-pages`.
|
|
217
|
+
|
|
218
|
+
Each changes a file only when it matches a scaffolded version; otherwise it stops before writing anything and says what to do by hand. The guides ship in this package, under `node_modules/@pantheon-systems/p1-next-sdk/docs/migrations/`.
|
|
219
|
+
|
|
204
220
|
### enable-registry
|
|
205
221
|
|
|
206
222
|
Wires an existing app up to the P1 component registry, so `shadcn add @p1/…` works in a project
|
package/bin/lib/cli.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Codemod orchestration.
|
|
2
|
+
* Codemod orchestration. Each migration is a subcommand that detects its own
|
|
3
|
+
* state, so a re-run changes nothing.
|
|
3
4
|
*
|
|
4
5
|
* `migrate()` is pure-ish and testable (takes a dir, returns a result, throws
|
|
5
6
|
* BailError on unrecognized input). `runCLI()` wraps it with argv parsing and
|
|
@@ -9,8 +10,16 @@
|
|
|
9
10
|
* the app is never left half-migrated.
|
|
10
11
|
*/
|
|
11
12
|
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
13
|
+
import { existsSync } from "node:fs";
|
|
14
|
+
import { dirname, join, relative } from "node:path";
|
|
15
|
+
import {
|
|
16
|
+
detectApp,
|
|
17
|
+
assertSuiteVersions,
|
|
18
|
+
isAtLeast,
|
|
19
|
+
isRouteSpecial,
|
|
20
|
+
LOCALIZATION_MIN_VERSION,
|
|
21
|
+
} from "./detect.js";
|
|
22
|
+
import { planLocalePrompt, planLocalizedPages } from "./localized-pages.js";
|
|
14
23
|
import { assertCleanTree } from "./git.js";
|
|
15
24
|
import * as fsops from "./fs-ops.js";
|
|
16
25
|
import * as msg from "./messages.js";
|
|
@@ -66,14 +75,11 @@ function partialMessage({ catchAll, p1Dir }, dir) {
|
|
|
66
75
|
);
|
|
67
76
|
}
|
|
68
77
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const force = opts.force ?? false;
|
|
72
|
-
const dryRun = opts.dryRun ?? false;
|
|
73
|
-
|
|
78
|
+
/** Move the editor into the persistent `(editor)` layout. */
|
|
79
|
+
async function migrateEditorLayout(dir, { force, dryRun }) {
|
|
74
80
|
const app = detectApp(dir);
|
|
75
81
|
if (app.status === "already-migrated") {
|
|
76
|
-
msg.alreadyMigrated();
|
|
82
|
+
msg.alreadyMigrated(MIGRATIONS["editor-layout"].title);
|
|
77
83
|
return { changed: false };
|
|
78
84
|
}
|
|
79
85
|
if (app.status === "not-found") {
|
|
@@ -91,7 +97,7 @@ export async function migrate(opts = {}) {
|
|
|
91
97
|
// Runs on --dry-run too: a plan the installed suite cannot render is not a
|
|
92
98
|
// plan worth previewing.
|
|
93
99
|
const suite = assertSuiteVersions(dir);
|
|
94
|
-
if (suite.status === "unverified") msg.versionsUnverified();
|
|
100
|
+
if (suite.status === "unverified") msg.versionsUnverified("editor-layout");
|
|
95
101
|
// Unverifiable means unverifiable, not old — attempt the step, matching the
|
|
96
102
|
// clean-tree check's bias toward proceeding.
|
|
97
103
|
const chatbot = suite.status !== "ok" || suite.chatbot;
|
|
@@ -111,45 +117,152 @@ export async function migrate(opts = {}) {
|
|
|
111
117
|
const { p1Pages, page } = splitPageFile(fsops.read(join(catchAll, "page.tsx")));
|
|
112
118
|
const layout = buildLayoutFile();
|
|
113
119
|
|
|
114
|
-
|
|
115
|
-
[
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
+
return apply(dir, "editor-layout", {
|
|
121
|
+
writes: [
|
|
122
|
+
[join(newCatchAll, "editor-client.tsx"), newEditorClient],
|
|
123
|
+
[join(newCatchAll, "p1-pages.tsx"), p1Pages],
|
|
124
|
+
[join(newCatchAll, "page.tsx"), page],
|
|
125
|
+
[join(editorGroup, "layout.tsx"), layout],
|
|
126
|
+
],
|
|
127
|
+
removes: [catchAll],
|
|
128
|
+
warnings: [],
|
|
129
|
+
}, { dryRun });
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function findAppDir(dir) {
|
|
133
|
+
const appDir = [join(dir, "app"), join(dir, "src", "app")].find((p) => existsSync(p));
|
|
134
|
+
if (!appDir) {
|
|
135
|
+
throw new BailError(`Could not find an app/ directory under ${dir}. Run this from your project root.`);
|
|
136
|
+
}
|
|
137
|
+
return appDir;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The localized-pages and locale-prompt migrations. A project with nothing
|
|
142
|
+
* left to do never needs a verifiable install; anything else checks the
|
|
143
|
+
* installed suite before reporting why the plan cannot proceed, since the
|
|
144
|
+
* guide a bail points to ships with the version that check asks for.
|
|
145
|
+
*/
|
|
146
|
+
function localizationMigration(planner) {
|
|
147
|
+
return async (dir, { command, force, dryRun }) => {
|
|
148
|
+
const appDir = findAppDir(dir);
|
|
149
|
+
let plan;
|
|
150
|
+
let bail;
|
|
151
|
+
try {
|
|
152
|
+
plan = planner(dir, appDir);
|
|
153
|
+
} catch (err) {
|
|
154
|
+
if (!(err instanceof BailError)) throw err;
|
|
155
|
+
bail = err;
|
|
156
|
+
}
|
|
157
|
+
if (plan && plan.writes.length === 0 && plan.removes.length === 0) {
|
|
158
|
+
for (const warning of plan.warnings) msg.warning(warning);
|
|
159
|
+
msg.alreadyMigrated(MIGRATIONS[command].title);
|
|
160
|
+
return { changed: false };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const title = MIGRATIONS[command].title;
|
|
164
|
+
const suite = assertSuiteVersions(dir, { migration: title });
|
|
165
|
+
if (suite.status === "unverified") msg.versionsUnverified(command);
|
|
166
|
+
else if (!isAtLeast(suite.version, LOCALIZATION_MIN_VERSION)) {
|
|
167
|
+
throw new BailError(
|
|
168
|
+
`Installed P1 packages are at ${suite.version}, but ${title} needs ` +
|
|
169
|
+
`${LOCALIZATION_MIN_VERSION} or newer. Upgrade every @pantheon-systems/* dependency ` +
|
|
170
|
+
"together, reinstall, then re-run.",
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
if (bail) throw bail;
|
|
174
|
+
|
|
175
|
+
if (!force && !dryRun && assertCleanTree(dir).status === "no-repo") msg.noGitRepo();
|
|
176
|
+
return apply(dir, command, plan, { dryRun });
|
|
177
|
+
};
|
|
178
|
+
}
|
|
120
179
|
|
|
180
|
+
/** Write a computed plan, or describe it on --dry-run. */
|
|
181
|
+
function apply(dir, command, { writes, removes, warnings }, { dryRun }) {
|
|
121
182
|
for (const [path] of writes) fsops.assertWithin(dir, path);
|
|
122
|
-
fsops.assertWithin(dir,
|
|
183
|
+
for (const path of removes) fsops.assertWithin(dir, path);
|
|
184
|
+
for (const warning of warnings) msg.warning(warning);
|
|
185
|
+
|
|
186
|
+
const shown = {
|
|
187
|
+
command,
|
|
188
|
+
writes: writes.map(([path]) => show(dir, path)),
|
|
189
|
+
removes: removes.map((path) => show(dir, path)),
|
|
190
|
+
};
|
|
123
191
|
|
|
124
192
|
if (dryRun) {
|
|
125
|
-
msg.dryRunPlan(
|
|
193
|
+
msg.dryRunPlan(shown);
|
|
126
194
|
return { changed: false, dryRun: true };
|
|
127
195
|
}
|
|
128
196
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
197
|
+
for (const [path, content] of writes) {
|
|
198
|
+
fsops.ensureDir(dirname(path));
|
|
199
|
+
fsops.write(path, content);
|
|
200
|
+
}
|
|
201
|
+
for (const path of removes) fsops.remove(path);
|
|
132
202
|
|
|
133
|
-
msg.success(
|
|
203
|
+
msg.success(shown);
|
|
134
204
|
return { changed: true };
|
|
135
205
|
}
|
|
136
206
|
|
|
207
|
+
/** The subcommands, in the order a project that needs all of them runs them. */
|
|
208
|
+
export const MIGRATIONS = {
|
|
209
|
+
"editor-layout": {
|
|
210
|
+
title: "the persistent (editor) layout",
|
|
211
|
+
summary: "Move the editor into the persistent app/p1/(editor) layout",
|
|
212
|
+
run: migrateEditorLayout,
|
|
213
|
+
},
|
|
214
|
+
"localized-pages": {
|
|
215
|
+
title: "localized published pages",
|
|
216
|
+
summary: "Serve every published page from app/[[...puckPath]], with <html lang> per page",
|
|
217
|
+
run: localizationMigration(planLocalizedPages),
|
|
218
|
+
},
|
|
219
|
+
"locale-prompt": {
|
|
220
|
+
title: "the saved-language prompt",
|
|
221
|
+
summary: "Add the starter's saved-language prompt to the page client (after localized-pages)",
|
|
222
|
+
run: localizationMigration(planLocalePrompt),
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
function unknownMigration(name) {
|
|
227
|
+
return new BailError(
|
|
228
|
+
`Unknown migration: ${name}. Choose one of: ${Object.keys(MIGRATIONS).join(", ")}.`,
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export async function migrate(opts = {}) {
|
|
233
|
+
const command = opts.command;
|
|
234
|
+
if (!Object.hasOwn(MIGRATIONS, command ?? "")) {
|
|
235
|
+
throw command ? unknownMigration(command) : new BailError("Name a migration to run.");
|
|
236
|
+
}
|
|
237
|
+
return MIGRATIONS[command].run(opts.dir ?? process.cwd(), {
|
|
238
|
+
command,
|
|
239
|
+
force: opts.force ?? false,
|
|
240
|
+
dryRun: opts.dryRun ?? false,
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
|
|
137
244
|
/**
|
|
138
245
|
* Strict on purpose: a silently-ignored `--dryrun` would run the real migration,
|
|
139
246
|
* and a `--dir` given with a space would migrate the current directory instead
|
|
140
247
|
* of the one the user named.
|
|
141
248
|
*/
|
|
142
249
|
export function parseArgs(argv) {
|
|
143
|
-
const opts = { dir: process.cwd(), force: false, dryRun: false, help: false };
|
|
250
|
+
const opts = { command: null, dir: process.cwd(), force: false, dryRun: false, help: false };
|
|
144
251
|
for (const arg of argv) {
|
|
145
252
|
if (arg === "--force" || arg === "-f") opts.force = true;
|
|
146
253
|
else if (arg === "--dry-run") opts.dryRun = true;
|
|
147
254
|
else if (arg === "--help" || arg === "-h") opts.help = true;
|
|
148
255
|
else if (arg.startsWith("--dir=")) opts.dir = arg.slice("--dir=".length);
|
|
149
|
-
else {
|
|
256
|
+
else if (arg.startsWith("-")) {
|
|
150
257
|
throw new BailError(
|
|
151
258
|
`Unrecognized argument: ${arg}. Run with --help to see the supported options.`,
|
|
152
259
|
);
|
|
260
|
+
} else if (!Object.hasOwn(MIGRATIONS, arg)) {
|
|
261
|
+
throw unknownMigration(arg);
|
|
262
|
+
} else if (opts.command !== null) {
|
|
263
|
+
throw new BailError(`Run one migration at a time: got ${opts.command} and ${arg}.`);
|
|
264
|
+
} else {
|
|
265
|
+
opts.command = arg;
|
|
153
266
|
}
|
|
154
267
|
}
|
|
155
268
|
return opts;
|
|
@@ -166,7 +279,12 @@ export async function runCLI(argv = process.argv.slice(2)) {
|
|
|
166
279
|
return;
|
|
167
280
|
}
|
|
168
281
|
if (opts.help) {
|
|
169
|
-
msg.help();
|
|
282
|
+
msg.help(MIGRATIONS);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
if (opts.command === null) {
|
|
286
|
+
msg.help(MIGRATIONS);
|
|
287
|
+
process.exitCode = 1;
|
|
170
288
|
return;
|
|
171
289
|
}
|
|
172
290
|
try {
|
package/bin/lib/detect.js
CHANGED
|
@@ -80,6 +80,13 @@ export const MIN_SUITE_VERSION = "0.8.0";
|
|
|
80
80
|
*/
|
|
81
81
|
export const CHATBOT_MIN_VERSION = "0.16.0";
|
|
82
82
|
|
|
83
|
+
/**
|
|
84
|
+
* The oldest release the localized-pages and locale-prompt migrations' output
|
|
85
|
+
* runs against: the root layout calls `getDocumentLanguage` and the language
|
|
86
|
+
* prompt imports `@pantheon-systems/p1-next-sdk/localization`.
|
|
87
|
+
*/
|
|
88
|
+
export const LOCALIZATION_MIN_VERSION = "0.24.0";
|
|
89
|
+
|
|
83
90
|
/**
|
|
84
91
|
* The lockstep-versioned packages a consumer app actually installs.
|
|
85
92
|
* `create-p1-starter-kit` is in the same `fixed` group but only ever scaffolds,
|
|
@@ -110,6 +117,11 @@ function parseVersion(version) {
|
|
|
110
117
|
return match ? [Number(match[1]), Number(match[2]), Number(match[3])] : null;
|
|
111
118
|
}
|
|
112
119
|
|
|
120
|
+
/** Whether a verified suite `version` is at or above `floor`. */
|
|
121
|
+
export function isAtLeast(version, floor) {
|
|
122
|
+
return !isOlderThan(parseVersion(version), parseVersion(floor));
|
|
123
|
+
}
|
|
124
|
+
|
|
113
125
|
function isOlderThan(version, floor) {
|
|
114
126
|
for (let i = 0; i < 3; i++) {
|
|
115
127
|
if (version[i] !== floor[i]) return version[i] < floor[i];
|
|
@@ -134,7 +146,7 @@ function isOlderThan(version, floor) {
|
|
|
134
146
|
* `chatbot` in the result says whether the chatbot rewrite's output would
|
|
135
147
|
* resolve against what is installed; the caller skips that step when it is false.
|
|
136
148
|
*/
|
|
137
|
-
export function assertSuiteVersions(dir) {
|
|
149
|
+
export function assertSuiteVersions(dir, { migration = "the persistent (editor) layout" } = {}) {
|
|
138
150
|
const modules = join(dir, "node_modules");
|
|
139
151
|
const resolved = SUITE.map((pkg) => ({
|
|
140
152
|
pkg,
|
|
@@ -177,7 +189,7 @@ export function assertSuiteVersions(dir) {
|
|
|
177
189
|
|
|
178
190
|
if (isOlderThan(parsed, parseVersion(MIN_SUITE_VERSION))) {
|
|
179
191
|
throw new BailError(
|
|
180
|
-
`Installed P1 packages are at ${version}, but
|
|
192
|
+
`Installed P1 packages are at ${version}, but ${migration} ` +
|
|
181
193
|
`needs ${MIN_SUITE_VERSION} or newer. This codemod runs at the latest published ` +
|
|
182
194
|
"version because npx fetches it from the registry, so it can restructure routes " +
|
|
183
195
|
`your installed version cannot render. Note a "^${version}" range will not resolve ` +
|
package/bin/lib/fs-ops.js
CHANGED
|
@@ -27,6 +27,7 @@ export function write(path, content) {
|
|
|
27
27
|
writeFileSync(path, content);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
/** Remove a file, or a directory and everything in it. */
|
|
31
|
+
export function remove(path) {
|
|
31
32
|
rmSync(path, { recursive: true, force: true });
|
|
32
33
|
}
|