@se-studio/project-build 1.1.20 → 1.2.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 +6 -0
- package/dist/generate-marketing-routes.mjs +141 -0
- package/dist/marketing-routes-cms.mjs +436 -0
- package/dist/marketing-routes.mjs +2309 -0
- package/package.json +4 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @se-studio/project-build
|
|
2
2
|
|
|
3
|
+
## 1.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 08c5d67: Add a `generate-marketing-routes` bin. Category-path article types (`tagInPath` with no articles prefix) write `/{slug}/`, `/{slug}/[tag]/`, and `/{slug}/[tag]/[...slugs]/`. Flat routes stay one segment, and an index slug may still differ from the article slug. Tag types with `show: false` are not routes. `tagTypeRoutes: false` skips every tag type.
|
|
8
|
+
|
|
3
9
|
## 1.1.20
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Write CMS page.ts files, marketing flags, and the markdown rewrite module
|
|
4
|
+
* from src/lib/marketing-routes.json.
|
|
5
|
+
*
|
|
6
|
+
* Does not overwrite an existing file unless --force is passed.
|
|
7
|
+
* Home, level, tags, people, article, and `[tag]` pages are generated when
|
|
8
|
+
* the json describes them. A person with no type is `/{peopleSlug}/{person}/`.
|
|
9
|
+
* A person with a type is a static `/{peopleSlug}/{type}/{person}/` segment,
|
|
10
|
+
* or `/{type}/{person}/` when there is no person prefix. The legacy generator
|
|
11
|
+
* still writes `/peopleType/{peopleType}/{person}/`. Named
|
|
12
|
+
* `personBases[].types` stay under the people base. `[level1]` stays a generic CMS page.
|
|
13
|
+
* Files are written into the `(cms-routes)` tree that already serves the
|
|
14
|
+
* app (`(default)`, a locale copy, or an inner group such as `(with-chrome)`).
|
|
15
|
+
*
|
|
16
|
+
* `--from-cms` reads custom types, person types, article types, and tag types,
|
|
17
|
+
* then rewrites the section fields of marketing-routes.json. A custom type is
|
|
18
|
+
* an optional person, tag, or article prefix. DRAFT_ONLY=true uses the preview
|
|
19
|
+
* API. Otherwise it uses the delivery API. Shell values for DRAFT_ONLY and the
|
|
20
|
+
* Contentful tokens override `.env.local`.
|
|
21
|
+
*
|
|
22
|
+
* Usage:
|
|
23
|
+
* node scripts/generate-marketing-routes.mjs <app-dir> [--from-cms] [--write] [--force]
|
|
24
|
+
* generate-marketing-routes <app-dir> [--from-cms] [--write] [--force]
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import fs from 'node:fs';
|
|
28
|
+
import path from 'node:path';
|
|
29
|
+
import { fileURLToPath } from 'node:url';
|
|
30
|
+
import {
|
|
31
|
+
fetchContentSlugs,
|
|
32
|
+
mergeCmsEnv,
|
|
33
|
+
planFromCms,
|
|
34
|
+
sectionConfigFromPlan,
|
|
35
|
+
} from './marketing-routes-cms.mjs';
|
|
36
|
+
import { readMarketingRoutes, syncGeneratedRoutes } from './marketing-routes.mjs';
|
|
37
|
+
|
|
38
|
+
const args = process.argv.slice(2).filter((arg) => !arg.startsWith('--'));
|
|
39
|
+
const write = process.argv.includes('--write') || process.argv.includes('--force');
|
|
40
|
+
const force = process.argv.includes('--force');
|
|
41
|
+
const fromCms = process.argv.includes('--from-cms');
|
|
42
|
+
const app = args[0];
|
|
43
|
+
|
|
44
|
+
if (!app) {
|
|
45
|
+
console.error(
|
|
46
|
+
'Usage: generate-marketing-routes <app-dir> [--from-cms] [--write] [--force]',
|
|
47
|
+
);
|
|
48
|
+
process.exit(2);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const appRoot = path.resolve(app);
|
|
52
|
+
|
|
53
|
+
if (fromCms) {
|
|
54
|
+
const hand = readMarketingRoutes(appRoot);
|
|
55
|
+
if (!hand) {
|
|
56
|
+
console.error(`No src/lib/marketing-routes.json under ${appRoot}`);
|
|
57
|
+
process.exit(1);
|
|
58
|
+
}
|
|
59
|
+
const env = mergeCmsEnv(readEnvFile(path.join(appRoot, '.env.local')), process.env);
|
|
60
|
+
const [customTypes, personTypes, articleTypes, tagTypes] = await Promise.all([
|
|
61
|
+
fetchContentSlugs(env, 'customType'),
|
|
62
|
+
fetchContentSlugs(env, 'personType'),
|
|
63
|
+
fetchContentSlugs(env, 'articleType', ['slug', 'name', 'indexPageSlug']),
|
|
64
|
+
fetchContentSlugs(env, 'tagType', ['slug', 'name', 'show']),
|
|
65
|
+
]);
|
|
66
|
+
const plan = planFromCms({
|
|
67
|
+
customTypes,
|
|
68
|
+
personTypes,
|
|
69
|
+
articleTypes,
|
|
70
|
+
tagTypes,
|
|
71
|
+
peopleSlug: hand.peopleSlug,
|
|
72
|
+
tagsSlug: hand.tagsSlug,
|
|
73
|
+
articlesSlug: hand.articlesSlug,
|
|
74
|
+
tagInPath: hand.tagInPath === true,
|
|
75
|
+
tagTypeRoutes: hand.tagTypeRoutes !== false,
|
|
76
|
+
});
|
|
77
|
+
if (!plan.ok) {
|
|
78
|
+
console.error(plan.problems.join('\n'));
|
|
79
|
+
process.exit(1);
|
|
80
|
+
}
|
|
81
|
+
const next = sectionConfigFromPlan(hand, plan);
|
|
82
|
+
const configPath = path.join(appRoot, 'src/lib/marketing-routes.json');
|
|
83
|
+
if (!write) {
|
|
84
|
+
console.log(JSON.stringify(next, null, 2));
|
|
85
|
+
process.exit(0);
|
|
86
|
+
}
|
|
87
|
+
fs.writeFileSync(configPath, `${JSON.stringify(next, null, 2)}\n`);
|
|
88
|
+
console.log(`Updated ${configPath}`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const result = syncGeneratedRoutes(appRoot, { write, force });
|
|
92
|
+
for (const note of result.tagSkips) {
|
|
93
|
+
console.log(note);
|
|
94
|
+
}
|
|
95
|
+
if (!write) {
|
|
96
|
+
console.log(
|
|
97
|
+
`${result.count} route file(s) described. Pass --write to create missing files.`,
|
|
98
|
+
);
|
|
99
|
+
if (result.skipped.length > 0) {
|
|
100
|
+
console.log(`Existing, left untouched: ${result.skipped.join(', ')}`);
|
|
101
|
+
}
|
|
102
|
+
} else {
|
|
103
|
+
console.log(
|
|
104
|
+
result.wrote.length > 0 ? `Wrote ${result.wrote.join(', ')}` : 'No files written.',
|
|
105
|
+
);
|
|
106
|
+
if (result.skipped.length > 0) {
|
|
107
|
+
console.log(`Skipped existing (pass --force to replace): ${result.skipped.join(', ')}`);
|
|
108
|
+
}
|
|
109
|
+
if (result.removed.length > 0) {
|
|
110
|
+
console.log(`Removed ${result.removed.join(', ')}`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* @param {string} file
|
|
116
|
+
* @returns {Record<string, string>}
|
|
117
|
+
*/
|
|
118
|
+
function readEnvFile(file) {
|
|
119
|
+
/** @type {Record<string, string>} */
|
|
120
|
+
const env = {};
|
|
121
|
+
if (!fs.existsSync(file)) {
|
|
122
|
+
return env;
|
|
123
|
+
}
|
|
124
|
+
for (const line of fs.readFileSync(file, 'utf8').split('\n')) {
|
|
125
|
+
const trimmed = line.trim();
|
|
126
|
+
if (!trimmed || trimmed.startsWith('#') || !trimmed.includes('=')) {
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
const index = trimmed.indexOf('=');
|
|
130
|
+
const key = trimmed.slice(0, index).trim();
|
|
131
|
+
let value = trimmed.slice(index + 1).trim();
|
|
132
|
+
if (
|
|
133
|
+
(value.startsWith('"') && value.endsWith('"')) ||
|
|
134
|
+
(value.startsWith("'") && value.endsWith("'"))
|
|
135
|
+
) {
|
|
136
|
+
value = value.slice(1, -1);
|
|
137
|
+
}
|
|
138
|
+
env[key] = value;
|
|
139
|
+
}
|
|
140
|
+
return env;
|
|
141
|
+
}
|
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plan marketing routes from Contentful custom types, person types, article
|
|
3
|
+
* types, and tag types. No network. The CLI fetches entries and passes them here.
|
|
4
|
+
*
|
|
5
|
+
* Person types, article types, and tag types each create their own routes.
|
|
6
|
+
* A custom type is an optional extra prefix. Its role is the slug, or the
|
|
7
|
+
* name when the slug is not a known role: person/people, tag/tags, or
|
|
8
|
+
* article/articles. Any other custom type is an error. At most one prefix
|
|
9
|
+
* per role.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const SEGMENT = /^[a-z0-9-]+$/;
|
|
13
|
+
|
|
14
|
+
/** @type {Record<string, Set<string>>} */
|
|
15
|
+
const PREFIX_ROLES = {
|
|
16
|
+
person: new Set(['person', 'people']),
|
|
17
|
+
tag: new Set(['tag', 'tags']),
|
|
18
|
+
article: new Set(['article', 'articles']),
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {NodeJS.ProcessEnv | Record<string, string | undefined>} env
|
|
23
|
+
*/
|
|
24
|
+
const CMS_ENV_KEYS = [
|
|
25
|
+
'DRAFT_ONLY',
|
|
26
|
+
'CONTENTFUL_SPACE_ID',
|
|
27
|
+
'CONTENTFUL_ENVIRONMENT',
|
|
28
|
+
'CONTENTFUL_ACCESS_TOKEN',
|
|
29
|
+
'CONTENTFUL_PREVIEW_ACCESS_TOKEN',
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Shell values override the file. An empty shell value leaves the file value.
|
|
34
|
+
*
|
|
35
|
+
* @param {Record<string, string>} fileEnv
|
|
36
|
+
* @param {NodeJS.ProcessEnv | Record<string, string | undefined>} shellEnv
|
|
37
|
+
* @returns {Record<string, string>}
|
|
38
|
+
*/
|
|
39
|
+
export function mergeCmsEnv(fileEnv, shellEnv) {
|
|
40
|
+
/** @type {Record<string, string>} */
|
|
41
|
+
const merged = { ...fileEnv };
|
|
42
|
+
for (const key of CMS_ENV_KEYS) {
|
|
43
|
+
const value = shellEnv[key];
|
|
44
|
+
if (typeof value === 'string' && value.length > 0) {
|
|
45
|
+
merged[key] = value;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return merged;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function contentfulReadTarget(env) {
|
|
52
|
+
const draft = env.DRAFT_ONLY === 'true';
|
|
53
|
+
if (draft) {
|
|
54
|
+
return {
|
|
55
|
+
draft: true,
|
|
56
|
+
host: 'https://preview.contentful.com',
|
|
57
|
+
token: env.CONTENTFUL_PREVIEW_ACCESS_TOKEN ?? '',
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
draft: false,
|
|
62
|
+
host: 'https://cdn.contentful.com',
|
|
63
|
+
token: env.CONTENTFUL_ACCESS_TOKEN ?? '',
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @param {string | undefined} slug
|
|
69
|
+
* @param {string} label
|
|
70
|
+
* @param {string[]} problems
|
|
71
|
+
* @returns {string | null}
|
|
72
|
+
*/
|
|
73
|
+
function segmentOrNull(slug, label, problems) {
|
|
74
|
+
if (typeof slug !== 'string' || slug.length === 0) {
|
|
75
|
+
problems.push(`${label} has no slug`);
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
if (slug.includes('/') || !SEGMENT.test(slug)) {
|
|
79
|
+
problems.push(`${label} slug is not a single segment: ${slug}`);
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
return slug;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* One or more URL segments. Used when the article type slug is the public path.
|
|
87
|
+
*
|
|
88
|
+
* @param {string | undefined} slug
|
|
89
|
+
* @param {string} label
|
|
90
|
+
* @param {string[]} problems
|
|
91
|
+
* @returns {string | null}
|
|
92
|
+
*/
|
|
93
|
+
function pathOrNull(slug, label, problems) {
|
|
94
|
+
if (typeof slug !== 'string' || slug.length === 0) {
|
|
95
|
+
problems.push(`${label} has no slug`);
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
const parts = slug.split('/');
|
|
99
|
+
if (parts.some((part) => !SEGMENT.test(part))) {
|
|
100
|
+
problems.push(`${label} slug is not a path of segments: ${slug}`);
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
return slug;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* A declared hand slug (`categories`) is that prefix even when the word is not
|
|
108
|
+
* `tags`. Role words still win when the slug is `people`, `tags`, or `articles`.
|
|
109
|
+
*
|
|
110
|
+
* @param {string} slug
|
|
111
|
+
* @param {{ person?: string | null, tag?: string | null, article?: string | null }} declared
|
|
112
|
+
*/
|
|
113
|
+
function declaredRole(slug, declared) {
|
|
114
|
+
if (declared.person && declared.person === slug) return 'person';
|
|
115
|
+
if (declared.tag && declared.tag === slug) return 'tag';
|
|
116
|
+
if (declared.article && declared.article === slug) return 'article';
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @param {{ slug?: string, name?: string }} entry
|
|
122
|
+
* @param {{ person?: string | null, tag?: string | null, article?: string | null }} declared
|
|
123
|
+
* @returns {{ role: string, slug: string } | { error: string }}
|
|
124
|
+
*/
|
|
125
|
+
function prefixOf(entry, declared) {
|
|
126
|
+
const slug = typeof entry?.slug === 'string' ? entry.slug : '';
|
|
127
|
+
const name = typeof entry?.name === 'string' ? entry.name.toLowerCase() : '';
|
|
128
|
+
/** @param {string} value */
|
|
129
|
+
const roleFor = (value) => {
|
|
130
|
+
for (const [role, names] of Object.entries(PREFIX_ROLES)) {
|
|
131
|
+
if (names.has(value)) {
|
|
132
|
+
return role;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return null;
|
|
136
|
+
};
|
|
137
|
+
const slugRole = roleFor(slug.toLowerCase());
|
|
138
|
+
const nameRole = roleFor(name);
|
|
139
|
+
if (slugRole && nameRole && slugRole !== nameRole) {
|
|
140
|
+
return { error: `custom type ${slug} slug is ${slugRole} but name is ${nameRole}` };
|
|
141
|
+
}
|
|
142
|
+
const named = slugRole || nameRole;
|
|
143
|
+
const fromHand = declaredRole(slug, declared);
|
|
144
|
+
if (named && fromHand && named !== fromHand) {
|
|
145
|
+
return {
|
|
146
|
+
error: `custom type ${slug} is a ${named} prefix but the hand file assigns it to ${fromHand}`,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
const role = named || fromHand;
|
|
150
|
+
if (!role) {
|
|
151
|
+
return {
|
|
152
|
+
error: `custom type ${slug || '(no slug)'} is not a person, tag, or article prefix`,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
return { role, slug };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* An explicit hand slug wins, including null. A missing key or undefined uses
|
|
160
|
+
* the inferred custom type. The CLI always passes these keys, so a JSON file
|
|
161
|
+
* that omits one arrives as undefined. Pass problems to validate a declared slug.
|
|
162
|
+
*
|
|
163
|
+
* @param {Record<string, unknown>} input
|
|
164
|
+
* @param {string} key
|
|
165
|
+
* @param {string | null} inferred
|
|
166
|
+
* @param {string[] | null} [problems]
|
|
167
|
+
* @returns {string | null}
|
|
168
|
+
*/
|
|
169
|
+
function handSlug(input, key, inferred, problems = null) {
|
|
170
|
+
if (!Object.prototype.hasOwnProperty.call(input, key)) {
|
|
171
|
+
return inferred;
|
|
172
|
+
}
|
|
173
|
+
const value = input[key];
|
|
174
|
+
if (value === undefined) return inferred;
|
|
175
|
+
if (value === null) return null;
|
|
176
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
177
|
+
problems?.push(`${key} must be a non-empty slug or null`);
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
if (!problems) return value;
|
|
181
|
+
return segmentOrNull(value, key, problems);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* @param {Map<string, string>} used
|
|
186
|
+
* @param {string} rest
|
|
187
|
+
* @param {string} label
|
|
188
|
+
* @param {string[]} problems
|
|
189
|
+
*/
|
|
190
|
+
function claimRest(used, rest, label, problems) {
|
|
191
|
+
const previous = used.get(rest);
|
|
192
|
+
if (previous) {
|
|
193
|
+
problems.push(`route ${rest} is used by ${previous} and ${label}`);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
used.set(rest, label);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* @param {{
|
|
201
|
+
* customTypes?: { slug?: string, name?: string }[],
|
|
202
|
+
* personTypes?: { slug?: string, name?: string }[],
|
|
203
|
+
* articleTypes?: { slug?: string, name?: string, indexPageSlug?: string }[],
|
|
204
|
+
* tagTypes?: { slug?: string, name?: string, show?: boolean }[],
|
|
205
|
+
* peopleSlug?: string | null,
|
|
206
|
+
* tagsSlug?: string | null,
|
|
207
|
+
* articlesSlug?: string | null,
|
|
208
|
+
* tagInPath?: boolean,
|
|
209
|
+
* tagTypeRoutes?: boolean,
|
|
210
|
+
* }} input
|
|
211
|
+
*/
|
|
212
|
+
export function planFromCms(input) {
|
|
213
|
+
/** @type {string[]} */
|
|
214
|
+
const problems = [];
|
|
215
|
+
const declared = {
|
|
216
|
+
person: handSlug(input, 'peopleSlug', null),
|
|
217
|
+
tag: handSlug(input, 'tagsSlug', null),
|
|
218
|
+
article: handSlug(input, 'articlesSlug', null),
|
|
219
|
+
};
|
|
220
|
+
/** @type {Record<string, string | null>} */
|
|
221
|
+
const prefixes = { person: null, tag: null, article: null };
|
|
222
|
+
for (const entry of input.customTypes ?? []) {
|
|
223
|
+
const slug = segmentOrNull(entry?.slug, 'custom type', problems);
|
|
224
|
+
if (!slug) {
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
const prefix = prefixOf({ ...entry, slug }, declared);
|
|
228
|
+
if ('error' in prefix) {
|
|
229
|
+
problems.push(prefix.error);
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
if (prefixes[prefix.role]) {
|
|
233
|
+
problems.push(
|
|
234
|
+
`two ${prefix.role} custom types (${prefixes[prefix.role]}, ${prefix.slug})`,
|
|
235
|
+
);
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
prefixes[prefix.role] = prefix.slug;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** @type {string[]} */
|
|
242
|
+
const personTypes = [];
|
|
243
|
+
for (const entry of input.personTypes ?? []) {
|
|
244
|
+
const slug = segmentOrNull(entry?.slug, 'person type', problems);
|
|
245
|
+
if (slug) {
|
|
246
|
+
personTypes.push(slug);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
const tagTypeRoutes = input.tagTypeRoutes !== false;
|
|
250
|
+
/** @type {string[]} */
|
|
251
|
+
const tagTypes = [];
|
|
252
|
+
for (const entry of input.tagTypes ?? []) {
|
|
253
|
+
if (entry?.show === false || !tagTypeRoutes) {
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
const slug = segmentOrNull(entry?.slug, 'tag type', problems);
|
|
257
|
+
if (slug) {
|
|
258
|
+
tagTypes.push(slug);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
const peopleSlug = handSlug(input, 'peopleSlug', prefixes.person, problems);
|
|
262
|
+
const tagsSlug = handSlug(input, 'tagsSlug', prefixes.tag, problems);
|
|
263
|
+
const articlesSlug = handSlug(input, 'articlesSlug', prefixes.article, problems);
|
|
264
|
+
const categoryPath = input.tagInPath === true && !articlesSlug;
|
|
265
|
+
/** @type {{ slug: string, indexSlug: string }[]} */
|
|
266
|
+
const articleTypes = [];
|
|
267
|
+
for (const entry of input.articleTypes ?? []) {
|
|
268
|
+
const slug = categoryPath
|
|
269
|
+
? pathOrNull(entry?.slug, 'article type', problems)
|
|
270
|
+
: segmentOrNull(entry?.slug, 'article type', problems);
|
|
271
|
+
if (!slug) {
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
const indexSource = entry?.indexPageSlug || slug;
|
|
275
|
+
const indexSlug = categoryPath
|
|
276
|
+
? pathOrNull(indexSource, 'article type index', problems)
|
|
277
|
+
: segmentOrNull(indexSource, 'article type index', problems);
|
|
278
|
+
if (!indexSlug) {
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
if (categoryPath && indexSlug !== slug) {
|
|
282
|
+
problems.push(
|
|
283
|
+
`${slug} indexPageSlug must equal the article type slug when the category is in the path`,
|
|
284
|
+
);
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
articleTypes.push({ slug, indexSlug });
|
|
288
|
+
}
|
|
289
|
+
/** @type {Map<string, string>} */
|
|
290
|
+
const used = new Map();
|
|
291
|
+
if (peopleSlug) {
|
|
292
|
+
claimRest(used, peopleSlug, 'person prefix', problems);
|
|
293
|
+
claimRest(used, `${peopleSlug}/[person]`, 'untyped person', problems);
|
|
294
|
+
}
|
|
295
|
+
for (const type of personTypes) {
|
|
296
|
+
const rest = peopleSlug ? `${peopleSlug}/${type}` : type;
|
|
297
|
+
claimRest(used, rest, `person type ${type}`, problems);
|
|
298
|
+
claimRest(used, `${rest}/[person]`, `person type ${type} person`, problems);
|
|
299
|
+
}
|
|
300
|
+
if (tagsSlug) {
|
|
301
|
+
claimRest(used, tagsSlug, 'tag prefix', problems);
|
|
302
|
+
}
|
|
303
|
+
for (const type of tagTypes) {
|
|
304
|
+
const rest = tagsSlug ? `${tagsSlug}/${type}` : type;
|
|
305
|
+
claimRest(used, rest, `tag type ${type}`, problems);
|
|
306
|
+
claimRest(used, `${rest}/[tag]`, `tag type ${type} tag`, problems);
|
|
307
|
+
}
|
|
308
|
+
if (articlesSlug) {
|
|
309
|
+
claimRest(used, articlesSlug, 'article prefix', problems);
|
|
310
|
+
}
|
|
311
|
+
for (const type of articleTypes) {
|
|
312
|
+
if (categoryPath) {
|
|
313
|
+
claimRest(used, type.slug, `${type.slug} index`, problems);
|
|
314
|
+
claimRest(used, `${type.slug}/[tag]`, `${type.slug} category`, problems);
|
|
315
|
+
claimRest(used, `${type.slug}/[tag]/[...slugs]`, `${type.slug} article`, problems);
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
const indexRest = articlesSlug ? `${articlesSlug}/${type.indexSlug}` : type.indexSlug;
|
|
319
|
+
const articleRest = articlesSlug ? `${articlesSlug}/${type.slug}` : type.slug;
|
|
320
|
+
claimRest(used, indexRest, `${type.slug} index`, problems);
|
|
321
|
+
if (articleRest !== indexRest) {
|
|
322
|
+
claimRest(used, articleRest, `${type.slug} articles`, problems);
|
|
323
|
+
}
|
|
324
|
+
claimRest(used, `${articleRest}/[article]`, `${type.slug} article`, problems);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (problems.length > 0) {
|
|
328
|
+
return { ok: false, problems };
|
|
329
|
+
}
|
|
330
|
+
const hasArticles = articleTypes.length > 0 || Boolean(articlesSlug);
|
|
331
|
+
const hasTags = tagTypes.length > 0 || Boolean(tagsSlug);
|
|
332
|
+
const hasPeople = personTypes.length > 0 || Boolean(peopleSlug);
|
|
333
|
+
return {
|
|
334
|
+
ok: true,
|
|
335
|
+
peopleSlug,
|
|
336
|
+
tagsSlug,
|
|
337
|
+
articlesSlug,
|
|
338
|
+
personTypes: [...new Set(personTypes)].sort(),
|
|
339
|
+
tagTypes: [...new Set(tagTypes)].sort(),
|
|
340
|
+
articleTypes: articleTypes.sort((a, b) => a.slug.localeCompare(b.slug)),
|
|
341
|
+
flags: {
|
|
342
|
+
enableArticleTypeIndex: hasArticles,
|
|
343
|
+
enableArticleTypeTagIndex: false,
|
|
344
|
+
enableTag: hasTags,
|
|
345
|
+
enableTagsIndex: Boolean(tagsSlug),
|
|
346
|
+
enablePerson: hasPeople,
|
|
347
|
+
enablePeopleIndex: Boolean(peopleSlug),
|
|
348
|
+
enablePrimaryTagPartOfSlug: false,
|
|
349
|
+
},
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Keep hand-written app switches. Section paths and flags come from the plan.
|
|
355
|
+
*
|
|
356
|
+
* @param {Record<string, unknown>} hand
|
|
357
|
+
* @param {ReturnType<typeof planFromCms> & { ok: true }} plan
|
|
358
|
+
*/
|
|
359
|
+
export function sectionConfigFromPlan(hand, plan) {
|
|
360
|
+
const tagInPath =
|
|
361
|
+
hand.tagInPath === true ||
|
|
362
|
+
(Array.isArray(hand.articleBases) && hand.articleBases.some((base) => base?.tagInPath === true));
|
|
363
|
+
/** @type {Record<string, unknown>} */
|
|
364
|
+
const next = {
|
|
365
|
+
catchAll: hand.catchAll !== false,
|
|
366
|
+
pageTest: hand.pageTest === true,
|
|
367
|
+
tagInPath,
|
|
368
|
+
peopleSlug: plan.peopleSlug,
|
|
369
|
+
tagsSlug: plan.tagsSlug,
|
|
370
|
+
articlesSlug: plan.articlesSlug,
|
|
371
|
+
personTypes: plan.personTypes,
|
|
372
|
+
tagTypes: plan.tagTypes,
|
|
373
|
+
articleTypes: plan.articleTypes,
|
|
374
|
+
};
|
|
375
|
+
if (hand.tagTypeRoutes === false) {
|
|
376
|
+
next.tagTypeRoutes = false;
|
|
377
|
+
}
|
|
378
|
+
return next;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* @param {Record<string, string>} env
|
|
383
|
+
* @param {string} contentType
|
|
384
|
+
* @param {string[]} [fields]
|
|
385
|
+
*/
|
|
386
|
+
export async function fetchContentSlugs(env, contentType, fields = ['slug', 'name']) {
|
|
387
|
+
const target = contentfulReadTarget(env);
|
|
388
|
+
if (!target.token) {
|
|
389
|
+
throw new Error(
|
|
390
|
+
target.draft
|
|
391
|
+
? 'DRAFT_ONLY=true but CONTENTFUL_PREVIEW_ACCESS_TOKEN is missing'
|
|
392
|
+
: 'CONTENTFUL_ACCESS_TOKEN is missing',
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
const space = env.CONTENTFUL_SPACE_ID;
|
|
396
|
+
const environment = env.CONTENTFUL_ENVIRONMENT_NAME || env.CONTENTFUL_ENVIRONMENT || 'master';
|
|
397
|
+
if (!space) {
|
|
398
|
+
throw new Error('CONTENTFUL_SPACE_ID is missing');
|
|
399
|
+
}
|
|
400
|
+
/** @type {{ slug?: string, name?: string }[]} */
|
|
401
|
+
const entries = [];
|
|
402
|
+
let skip = 0;
|
|
403
|
+
for (;;) {
|
|
404
|
+
const url = new URL(
|
|
405
|
+
`${target.host}/spaces/${space}/environments/${environment}/entries`,
|
|
406
|
+
);
|
|
407
|
+
url.searchParams.set('content_type', contentType);
|
|
408
|
+
url.searchParams.set('select', ['sys.id', ...fields.map((field) => `fields.${field}`)].join(','));
|
|
409
|
+
url.searchParams.set('limit', '100');
|
|
410
|
+
url.searchParams.set('skip', String(skip));
|
|
411
|
+
const response = await fetch(url, {
|
|
412
|
+
headers: { Authorization: `Bearer ${target.token}` },
|
|
413
|
+
});
|
|
414
|
+
if (!response.ok) {
|
|
415
|
+
const body = await response.text();
|
|
416
|
+
throw new Error(`${contentType} ${response.status}: ${body.slice(0, 300)}`);
|
|
417
|
+
}
|
|
418
|
+
const payload = await response.json();
|
|
419
|
+
for (const item of payload.items ?? []) {
|
|
420
|
+
/** @type {{ slug?: string, name?: string, indexPageSlug?: string }} */
|
|
421
|
+
const row = { slug: item.fields?.slug, name: item.fields?.name };
|
|
422
|
+
if (typeof item.fields?.indexPageSlug === 'string') {
|
|
423
|
+
row.indexPageSlug = item.fields.indexPageSlug;
|
|
424
|
+
}
|
|
425
|
+
if (typeof item.fields?.show === 'boolean') {
|
|
426
|
+
row.show = item.fields.show;
|
|
427
|
+
}
|
|
428
|
+
entries.push(row);
|
|
429
|
+
}
|
|
430
|
+
skip += (payload.items ?? []).length;
|
|
431
|
+
if (skip >= (payload.total ?? 0) || (payload.items ?? []).length === 0) {
|
|
432
|
+
break;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
return entries;
|
|
436
|
+
}
|