payload-live-preview 1.0.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/LICENSE +21 -0
- package/README.md +389 -0
- package/dist/adapters/astro/RichText.astro +55 -0
- package/dist/adapters/astro/index.cjs +370 -0
- package/dist/adapters/astro/index.cjs.map +1 -0
- package/dist/adapters/astro/index.d.cts +105 -0
- package/dist/adapters/astro/index.d.ts +105 -0
- package/dist/adapters/astro/index.js +364 -0
- package/dist/adapters/astro/index.js.map +1 -0
- package/dist/adapters/astro/middleware-entry.cjs +303 -0
- package/dist/adapters/astro/middleware-entry.cjs.map +1 -0
- package/dist/adapters/astro/middleware-entry.d.cts +15 -0
- package/dist/adapters/astro/middleware-entry.d.ts +15 -0
- package/dist/adapters/astro/middleware-entry.js +297 -0
- package/dist/adapters/astro/middleware-entry.js.map +1 -0
- package/dist/adapters/nextjs/index.cjs +309 -0
- package/dist/adapters/nextjs/index.cjs.map +1 -0
- package/dist/adapters/nextjs/index.d.cts +75 -0
- package/dist/adapters/nextjs/index.d.ts +75 -0
- package/dist/adapters/nextjs/index.js +306 -0
- package/dist/adapters/nextjs/index.js.map +1 -0
- package/dist/adapters/nuxt/index.cjs +299 -0
- package/dist/adapters/nuxt/index.cjs.map +1 -0
- package/dist/adapters/nuxt/index.d.cts +126 -0
- package/dist/adapters/nuxt/index.d.ts +126 -0
- package/dist/adapters/nuxt/index.js +294 -0
- package/dist/adapters/nuxt/index.js.map +1 -0
- package/dist/adapters/sveltekit/index.cjs +275 -0
- package/dist/adapters/sveltekit/index.cjs.map +1 -0
- package/dist/adapters/sveltekit/index.d.cts +80 -0
- package/dist/adapters/sveltekit/index.d.ts +80 -0
- package/dist/adapters/sveltekit/index.js +273 -0
- package/dist/adapters/sveltekit/index.js.map +1 -0
- package/dist/bind-BSgxoQhN.d.cts +1137 -0
- package/dist/bind-BSgxoQhN.d.ts +1137 -0
- package/dist/codegen-astro.cjs +612 -0
- package/dist/codegen-astro.cjs.map +1 -0
- package/dist/codegen-astro.d.cts +54 -0
- package/dist/codegen-astro.d.ts +54 -0
- package/dist/codegen-astro.js +610 -0
- package/dist/codegen-astro.js.map +1 -0
- package/dist/codegen-cli.cjs +669 -0
- package/dist/codegen-cli.cjs.map +1 -0
- package/dist/codegen-cli.d.cts +19 -0
- package/dist/codegen-cli.d.ts +19 -0
- package/dist/codegen-cli.js +667 -0
- package/dist/codegen-cli.js.map +1 -0
- package/dist/codegen.cjs +551 -0
- package/dist/codegen.cjs.map +1 -0
- package/dist/codegen.d.cts +162 -0
- package/dist/codegen.d.ts +162 -0
- package/dist/codegen.js +547 -0
- package/dist/codegen.js.map +1 -0
- package/dist/core.cjs +3684 -0
- package/dist/core.cjs.map +1 -0
- package/dist/core.d.cts +24 -0
- package/dist/core.d.ts +24 -0
- package/dist/core.js +3657 -0
- package/dist/core.js.map +1 -0
- package/dist/index.cjs +4083 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +472 -0
- package/dist/index.d.ts +472 -0
- package/dist/index.js +4040 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware-BwIlf0jW.d.cts +168 -0
- package/dist/middleware-BwIlf0jW.d.ts +168 -0
- package/dist/payload.cjs +31 -0
- package/dist/payload.cjs.map +1 -0
- package/dist/payload.d.cts +87 -0
- package/dist/payload.d.ts +87 -0
- package/dist/payload.js +29 -0
- package/dist/payload.js.map +1 -0
- package/dist/preview-request-DAvxm18T.d.cts +68 -0
- package/dist/preview-request-DAvxm18T.d.ts +68 -0
- package/package.json +186 -0
|
@@ -0,0 +1,612 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var fs = require('fs');
|
|
4
|
+
var promises = require('fs/promises');
|
|
5
|
+
var path = require('path');
|
|
6
|
+
var tsMorph = require('ts-morph');
|
|
7
|
+
|
|
8
|
+
// src/codegen/astro-plugin.ts
|
|
9
|
+
var SCALAR_TYPE_MAP = {
|
|
10
|
+
text: "string",
|
|
11
|
+
textarea: "string",
|
|
12
|
+
email: "string",
|
|
13
|
+
richText: "unknown",
|
|
14
|
+
number: "number",
|
|
15
|
+
checkbox: "boolean",
|
|
16
|
+
date: "string",
|
|
17
|
+
code: "string",
|
|
18
|
+
point: "unknown",
|
|
19
|
+
ui: "unknown",
|
|
20
|
+
radio: "string"
|
|
21
|
+
};
|
|
22
|
+
function extractField(literal) {
|
|
23
|
+
const type = readStringProperty(literal, "type");
|
|
24
|
+
if (type === void 0) return void 0;
|
|
25
|
+
const STRUCTURAL_TYPES = /* @__PURE__ */ new Set(["tabs", "row", "collapsible"]);
|
|
26
|
+
const name = readStringProperty(literal, "name");
|
|
27
|
+
if (name === void 0 && !STRUCTURAL_TYPES.has(type)) return void 0;
|
|
28
|
+
const required = readBooleanProperty(literal, "required") ?? false;
|
|
29
|
+
const localized = readBooleanProperty(literal, "localized") ?? false;
|
|
30
|
+
const base = { name: name ?? "__structural", required, localized };
|
|
31
|
+
switch (type) {
|
|
32
|
+
case "array":
|
|
33
|
+
return {
|
|
34
|
+
...base,
|
|
35
|
+
kind: "array",
|
|
36
|
+
fields: extractNestedFields(literal)
|
|
37
|
+
};
|
|
38
|
+
case "blocks":
|
|
39
|
+
return {
|
|
40
|
+
...base,
|
|
41
|
+
kind: "blocks",
|
|
42
|
+
blocks: extractBlocks(literal)
|
|
43
|
+
};
|
|
44
|
+
case "group":
|
|
45
|
+
return {
|
|
46
|
+
...base,
|
|
47
|
+
kind: "group",
|
|
48
|
+
fields: extractNestedFields(literal)
|
|
49
|
+
};
|
|
50
|
+
case "tabs":
|
|
51
|
+
case "row":
|
|
52
|
+
case "collapsible": {
|
|
53
|
+
const flattened = extractNestedFieldsFromStructural(literal);
|
|
54
|
+
return {
|
|
55
|
+
...base,
|
|
56
|
+
kind: "group",
|
|
57
|
+
fields: flattened
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
case "relationship":
|
|
61
|
+
return {
|
|
62
|
+
...base,
|
|
63
|
+
kind: "relationship",
|
|
64
|
+
target: readRelationTarget(literal),
|
|
65
|
+
hasMany: readBooleanProperty(literal, "hasMany") ?? false
|
|
66
|
+
};
|
|
67
|
+
case "upload":
|
|
68
|
+
return {
|
|
69
|
+
...base,
|
|
70
|
+
kind: "upload",
|
|
71
|
+
target: readRelationTargetSingle(literal) ?? "media"
|
|
72
|
+
};
|
|
73
|
+
case "json":
|
|
74
|
+
return { ...base, kind: "json" };
|
|
75
|
+
case "select":
|
|
76
|
+
return {
|
|
77
|
+
...base,
|
|
78
|
+
kind: "select",
|
|
79
|
+
options: readSelectOptions(literal),
|
|
80
|
+
hasMany: readBooleanProperty(literal, "hasMany") ?? false
|
|
81
|
+
};
|
|
82
|
+
default: {
|
|
83
|
+
const typeRef = SCALAR_TYPE_MAP[type] ?? "unknown";
|
|
84
|
+
return { ...base, kind: "scalar", typeRef };
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function extractNestedFields(literal) {
|
|
89
|
+
const fieldsLiteral = readArrayProperty(literal, "fields");
|
|
90
|
+
if (!fieldsLiteral) return [];
|
|
91
|
+
const out = [];
|
|
92
|
+
for (const element of fieldsLiteral.getElements()) {
|
|
93
|
+
if (!tsMorph.Node.isObjectLiteralExpression(element)) continue;
|
|
94
|
+
const f = extractField(element);
|
|
95
|
+
if (!f) continue;
|
|
96
|
+
if (f.kind === "group" && f.name === "__structural") {
|
|
97
|
+
out.push(...f.fields);
|
|
98
|
+
} else {
|
|
99
|
+
out.push(f);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return out;
|
|
103
|
+
}
|
|
104
|
+
function extractNestedFieldsFromStructural(literal) {
|
|
105
|
+
const tabsLiteral = readArrayProperty(literal, "tabs");
|
|
106
|
+
if (tabsLiteral) {
|
|
107
|
+
const out = [];
|
|
108
|
+
for (const tabElement of tabsLiteral.getElements()) {
|
|
109
|
+
if (!tsMorph.Node.isObjectLiteralExpression(tabElement)) continue;
|
|
110
|
+
const tabName = readStringProperty(tabElement, "name");
|
|
111
|
+
const tabFields = extractNestedFields(tabElement);
|
|
112
|
+
if (tabName !== void 0) {
|
|
113
|
+
out.push({
|
|
114
|
+
kind: "group",
|
|
115
|
+
name: tabName,
|
|
116
|
+
required: false,
|
|
117
|
+
localized: false,
|
|
118
|
+
fields: tabFields
|
|
119
|
+
});
|
|
120
|
+
} else {
|
|
121
|
+
out.push(...tabFields);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return out;
|
|
125
|
+
}
|
|
126
|
+
return extractNestedFields(literal);
|
|
127
|
+
}
|
|
128
|
+
function extractBlocks(literal) {
|
|
129
|
+
const blocksLiteral = readArrayProperty(literal, "blocks");
|
|
130
|
+
if (!blocksLiteral) return [];
|
|
131
|
+
const out = [];
|
|
132
|
+
for (const element of blocksLiteral.getElements()) {
|
|
133
|
+
if (!tsMorph.Node.isObjectLiteralExpression(element)) continue;
|
|
134
|
+
const slug = readStringProperty(element, "slug");
|
|
135
|
+
if (slug === void 0) continue;
|
|
136
|
+
out.push({
|
|
137
|
+
slug,
|
|
138
|
+
typeName: toPascalCase(slug),
|
|
139
|
+
fields: extractNestedFields(element)
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
return out;
|
|
143
|
+
}
|
|
144
|
+
function readRelationTarget(literal) {
|
|
145
|
+
const property = literal.getProperty("relationTo");
|
|
146
|
+
if (!property || !tsMorph.Node.isPropertyAssignment(property)) return "unknown";
|
|
147
|
+
const initialiser = property.getInitializer();
|
|
148
|
+
if (initialiser && tsMorph.Node.isStringLiteral(initialiser)) return initialiser.getLiteralValue();
|
|
149
|
+
if (initialiser && tsMorph.Node.isArrayLiteralExpression(initialiser)) {
|
|
150
|
+
return initialiser.getElements().filter(tsMorph.Node.isStringLiteral).map((el) => el.getLiteralValue());
|
|
151
|
+
}
|
|
152
|
+
return "unknown";
|
|
153
|
+
}
|
|
154
|
+
function readRelationTargetSingle(literal) {
|
|
155
|
+
const target = readRelationTarget(literal);
|
|
156
|
+
if (typeof target === "string") return target;
|
|
157
|
+
return target[0];
|
|
158
|
+
}
|
|
159
|
+
function readSelectOptions(literal) {
|
|
160
|
+
const optionsLiteral = readArrayProperty(literal, "options");
|
|
161
|
+
if (!optionsLiteral) return [];
|
|
162
|
+
const out = [];
|
|
163
|
+
for (const element of optionsLiteral.getElements()) {
|
|
164
|
+
if (tsMorph.Node.isStringLiteral(element)) {
|
|
165
|
+
out.push(element.getLiteralValue());
|
|
166
|
+
} else if (tsMorph.Node.isObjectLiteralExpression(element)) {
|
|
167
|
+
const value = readStringProperty(element, "value");
|
|
168
|
+
if (value !== void 0) out.push(value);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return out;
|
|
172
|
+
}
|
|
173
|
+
function readStringProperty(literal, name) {
|
|
174
|
+
const property = literal.getProperty(name);
|
|
175
|
+
if (!property || !isPropertyAssignment(property)) return void 0;
|
|
176
|
+
const initialiser = property.getInitializer();
|
|
177
|
+
if (initialiser && tsMorph.Node.isStringLiteral(initialiser)) return initialiser.getLiteralValue();
|
|
178
|
+
if (initialiser && tsMorph.Node.isNoSubstitutionTemplateLiteral(initialiser)) {
|
|
179
|
+
return initialiser.getLiteralValue();
|
|
180
|
+
}
|
|
181
|
+
return void 0;
|
|
182
|
+
}
|
|
183
|
+
function readBooleanProperty(literal, name) {
|
|
184
|
+
const property = literal.getProperty(name);
|
|
185
|
+
if (!property || !isPropertyAssignment(property)) return void 0;
|
|
186
|
+
const initialiser = property.getInitializer();
|
|
187
|
+
if (!initialiser) return void 0;
|
|
188
|
+
if (initialiser.getKind() === tsMorph.SyntaxKind.TrueKeyword) return true;
|
|
189
|
+
if (initialiser.getKind() === tsMorph.SyntaxKind.FalseKeyword) return false;
|
|
190
|
+
return void 0;
|
|
191
|
+
}
|
|
192
|
+
function readArrayProperty(literal, name) {
|
|
193
|
+
const property = literal.getProperty(name);
|
|
194
|
+
if (!property || !isPropertyAssignment(property)) return void 0;
|
|
195
|
+
const initialiser = property.getInitializer();
|
|
196
|
+
if (initialiser && tsMorph.Node.isArrayLiteralExpression(initialiser)) return initialiser;
|
|
197
|
+
return void 0;
|
|
198
|
+
}
|
|
199
|
+
function isPropertyAssignment(node) {
|
|
200
|
+
return tsMorph.Node.isPropertyAssignment(node);
|
|
201
|
+
}
|
|
202
|
+
function toPascalCase(slug) {
|
|
203
|
+
return slug.split(/[-_\s]+/).filter(Boolean).map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// src/codegen/parser/extract-schema.ts
|
|
207
|
+
function extractSchema(options) {
|
|
208
|
+
const project = options.project ?? new tsMorph.Project(
|
|
209
|
+
options.tsConfigFilePath !== void 0 ? { tsConfigFilePath: options.tsConfigFilePath } : { skipAddingFilesFromTsConfig: true }
|
|
210
|
+
);
|
|
211
|
+
const sourceFile = project.addSourceFileAtPathIfExists(options.configPath);
|
|
212
|
+
if (!sourceFile) {
|
|
213
|
+
return {
|
|
214
|
+
globals: [],
|
|
215
|
+
collections: [],
|
|
216
|
+
diagnostics: [`Could not open ${options.configPath}`]
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
project.resolveSourceFileDependencies();
|
|
220
|
+
const diagnostics = [];
|
|
221
|
+
const configLiteral = findConfigLiteral(sourceFile, diagnostics);
|
|
222
|
+
if (!configLiteral) {
|
|
223
|
+
return {
|
|
224
|
+
globals: [],
|
|
225
|
+
collections: [],
|
|
226
|
+
diagnostics: [
|
|
227
|
+
...diagnostics,
|
|
228
|
+
"Could not locate a buildConfig({...}) call or `export default {...}` in the config file."
|
|
229
|
+
]
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
const globals = extractSlugList(configLiteral, "globals", diagnostics);
|
|
233
|
+
const collections = extractSlugList(configLiteral, "collections", diagnostics);
|
|
234
|
+
return { globals, collections, diagnostics };
|
|
235
|
+
}
|
|
236
|
+
function findConfigLiteral(sourceFile, diagnostics) {
|
|
237
|
+
const defaultExport = sourceFile.getExportSymbols().find((sym) => sym.getName() === "default");
|
|
238
|
+
if (!defaultExport) {
|
|
239
|
+
diagnostics.push("payload.config.ts has no default export.");
|
|
240
|
+
return void 0;
|
|
241
|
+
}
|
|
242
|
+
const declarations = defaultExport.getDeclarations();
|
|
243
|
+
for (const decl of declarations) {
|
|
244
|
+
const candidate = unwrapToObjectLiteral(decl);
|
|
245
|
+
if (candidate) return candidate;
|
|
246
|
+
}
|
|
247
|
+
for (const call of sourceFile.getDescendantsOfKind(tsMorph.SyntaxKind.CallExpression)) {
|
|
248
|
+
const expr = call.getExpression();
|
|
249
|
+
if (tsMorph.Node.isIdentifier(expr) && expr.getText() === "buildConfig") {
|
|
250
|
+
const arg = call.getArguments()[0];
|
|
251
|
+
if (arg && tsMorph.Node.isObjectLiteralExpression(arg)) return arg;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return void 0;
|
|
255
|
+
}
|
|
256
|
+
function unwrapToObjectLiteral(node) {
|
|
257
|
+
if (tsMorph.Node.isExportAssignment(node)) {
|
|
258
|
+
return unwrapExpression(node.getExpression());
|
|
259
|
+
}
|
|
260
|
+
if (tsMorph.Node.isVariableDeclaration(node)) {
|
|
261
|
+
const initialiser = node.getInitializer();
|
|
262
|
+
if (initialiser) return unwrapExpression(initialiser);
|
|
263
|
+
}
|
|
264
|
+
return void 0;
|
|
265
|
+
}
|
|
266
|
+
function unwrapExpression(node) {
|
|
267
|
+
if (!node) return void 0;
|
|
268
|
+
if (tsMorph.Node.isObjectLiteralExpression(node)) return node;
|
|
269
|
+
if (tsMorph.Node.isCallExpression(node)) {
|
|
270
|
+
const arg = node.getArguments()[0];
|
|
271
|
+
if (arg && tsMorph.Node.isObjectLiteralExpression(arg)) return arg;
|
|
272
|
+
}
|
|
273
|
+
if (tsMorph.Node.isAsExpression(node)) {
|
|
274
|
+
return unwrapExpression(node.getExpression());
|
|
275
|
+
}
|
|
276
|
+
if (tsMorph.Node.isSatisfiesExpression(node)) {
|
|
277
|
+
return unwrapExpression(node.getExpression());
|
|
278
|
+
}
|
|
279
|
+
if (tsMorph.Node.isIdentifier(node)) {
|
|
280
|
+
return resolveIdentifierToObjectLiteral(node);
|
|
281
|
+
}
|
|
282
|
+
return void 0;
|
|
283
|
+
}
|
|
284
|
+
function resolveIdentifierToObjectLiteral(identifier) {
|
|
285
|
+
for (const decl of identifier.getDefinitionNodes()) {
|
|
286
|
+
const result = unwrapToObjectLiteral(decl);
|
|
287
|
+
if (result) return result;
|
|
288
|
+
}
|
|
289
|
+
return void 0;
|
|
290
|
+
}
|
|
291
|
+
function extractSlugList(configLiteral, key, diagnostics) {
|
|
292
|
+
const property = configLiteral.getProperty(key);
|
|
293
|
+
if (!property) return [];
|
|
294
|
+
if (!tsMorph.Node.isPropertyAssignment(property)) return [];
|
|
295
|
+
const initialiser = property.getInitializer();
|
|
296
|
+
if (!initialiser) return [];
|
|
297
|
+
const array = resolveToArrayLiteral(initialiser);
|
|
298
|
+
if (!array) return [];
|
|
299
|
+
return extractEntries(array, diagnostics);
|
|
300
|
+
}
|
|
301
|
+
function extractEntries(array, diagnostics) {
|
|
302
|
+
const out = [];
|
|
303
|
+
for (const element of array.getElements()) {
|
|
304
|
+
if (tsMorph.Node.isSpreadElement(element)) {
|
|
305
|
+
const expandedArray = resolveToArrayLiteral(element.getExpression());
|
|
306
|
+
if (!expandedArray) {
|
|
307
|
+
diagnostics.push(
|
|
308
|
+
`Could not resolve spread "${element.getText().slice(0, 80)}" to an array literal.`
|
|
309
|
+
);
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
out.push(...extractEntries(expandedArray, diagnostics));
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
const literal = resolveToObjectLiteral(element);
|
|
316
|
+
if (!literal) {
|
|
317
|
+
diagnostics.push(
|
|
318
|
+
`Could not resolve config entry "${element.getText().slice(0, 80)}" to an object literal.`
|
|
319
|
+
);
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
const slug = readStringProperty2(literal, "slug");
|
|
323
|
+
if (slug === void 0) {
|
|
324
|
+
diagnostics.push(`Skipping a config entry \u2014 no string \`slug\` field found.`);
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
out.push({
|
|
328
|
+
slug,
|
|
329
|
+
typeName: toPascalCase(slug),
|
|
330
|
+
fields: extractFieldList(literal)
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
return out;
|
|
334
|
+
}
|
|
335
|
+
function extractFieldList(configEntry) {
|
|
336
|
+
const fieldsProperty = configEntry.getProperty("fields");
|
|
337
|
+
if (!fieldsProperty || !tsMorph.Node.isPropertyAssignment(fieldsProperty)) return [];
|
|
338
|
+
const initialiser = fieldsProperty.getInitializer();
|
|
339
|
+
if (!initialiser || !tsMorph.Node.isArrayLiteralExpression(initialiser)) return [];
|
|
340
|
+
const out = [];
|
|
341
|
+
for (const element of initialiser.getElements()) {
|
|
342
|
+
if (tsMorph.Node.isSpreadElement(element)) {
|
|
343
|
+
const expanded = resolveToArrayLiteral(element.getExpression());
|
|
344
|
+
if (!expanded) continue;
|
|
345
|
+
for (const inner of expanded.getElements()) {
|
|
346
|
+
const literal2 = resolveToObjectLiteral(inner);
|
|
347
|
+
if (!literal2) continue;
|
|
348
|
+
const field2 = extractField(literal2);
|
|
349
|
+
if (!field2) continue;
|
|
350
|
+
if (field2.kind === "group" && field2.name === "__structural") {
|
|
351
|
+
out.push(...field2.fields);
|
|
352
|
+
} else {
|
|
353
|
+
out.push(field2);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
const literal = resolveToObjectLiteral(element);
|
|
359
|
+
if (!literal) continue;
|
|
360
|
+
const field = extractField(literal);
|
|
361
|
+
if (!field) continue;
|
|
362
|
+
if (field.kind === "group" && field.name === "__structural") {
|
|
363
|
+
out.push(...field.fields);
|
|
364
|
+
} else {
|
|
365
|
+
out.push(field);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return out;
|
|
369
|
+
}
|
|
370
|
+
function resolveToObjectLiteral(node) {
|
|
371
|
+
if (tsMorph.Node.isObjectLiteralExpression(node)) return node;
|
|
372
|
+
if (tsMorph.Node.isIdentifier(node)) return resolveIdentifierToObjectLiteral(node);
|
|
373
|
+
if (tsMorph.Node.isAsExpression(node)) return resolveToObjectLiteral(node.getExpression());
|
|
374
|
+
if (tsMorph.Node.isSatisfiesExpression(node)) return resolveToObjectLiteral(node.getExpression());
|
|
375
|
+
if (tsMorph.Node.isCallExpression(node)) {
|
|
376
|
+
const arg = node.getArguments()[0];
|
|
377
|
+
if (arg) return resolveToObjectLiteral(arg);
|
|
378
|
+
}
|
|
379
|
+
return void 0;
|
|
380
|
+
}
|
|
381
|
+
function resolveToArrayLiteral(node) {
|
|
382
|
+
if (tsMorph.Node.isArrayLiteralExpression(node)) return node;
|
|
383
|
+
if (tsMorph.Node.isAsExpression(node)) return resolveToArrayLiteral(node.getExpression());
|
|
384
|
+
if (tsMorph.Node.isSatisfiesExpression(node)) return resolveToArrayLiteral(node.getExpression());
|
|
385
|
+
if (tsMorph.Node.isIdentifier(node)) {
|
|
386
|
+
for (const decl of node.getDefinitionNodes()) {
|
|
387
|
+
if (tsMorph.Node.isVariableDeclaration(decl)) {
|
|
388
|
+
const initialiser = decl.getInitializer();
|
|
389
|
+
if (initialiser) {
|
|
390
|
+
const resolved = resolveToArrayLiteral(initialiser);
|
|
391
|
+
if (resolved) return resolved;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
return void 0;
|
|
397
|
+
}
|
|
398
|
+
function readStringProperty2(literal, name) {
|
|
399
|
+
const property = literal.getProperty(name);
|
|
400
|
+
if (!property || !tsMorph.Node.isPropertyAssignment(property)) return void 0;
|
|
401
|
+
const initialiser = property.getInitializer();
|
|
402
|
+
if (initialiser && tsMorph.Node.isStringLiteral(initialiser)) return initialiser.getLiteralValue();
|
|
403
|
+
return void 0;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// src/codegen/emit/emit-types.ts
|
|
407
|
+
var DEFAULT_HEADER = "// Auto-generated by payload-live-preview/codegen.\n// Do not edit by hand \u2014 run `pll-codegen` to regenerate.\n/* eslint-disable */";
|
|
408
|
+
function emitTypes(schema, options = {}) {
|
|
409
|
+
const lines = [];
|
|
410
|
+
lines.push(options.header ?? DEFAULT_HEADER);
|
|
411
|
+
lines.push("");
|
|
412
|
+
lines.push('import type { PayloadMedia, PayloadRelationship } from "payload-live-preview";');
|
|
413
|
+
lines.push("");
|
|
414
|
+
const seenAuxNames = /* @__PURE__ */ new Set();
|
|
415
|
+
for (const slug of [...schema.globals, ...schema.collections]) {
|
|
416
|
+
emitSlug(slug, lines, seenAuxNames, options);
|
|
417
|
+
lines.push("");
|
|
418
|
+
}
|
|
419
|
+
if (schema.globals.length > 0) {
|
|
420
|
+
lines.push("export type PayloadGlobals = {");
|
|
421
|
+
for (const g of schema.globals) {
|
|
422
|
+
lines.push(` '${g.slug}': ${g.typeName};`);
|
|
423
|
+
}
|
|
424
|
+
lines.push("};");
|
|
425
|
+
lines.push("");
|
|
426
|
+
}
|
|
427
|
+
if (schema.collections.length > 0) {
|
|
428
|
+
lines.push("export type PayloadCollections = {");
|
|
429
|
+
for (const c of schema.collections) {
|
|
430
|
+
lines.push(` '${c.slug}': ${c.typeName};`);
|
|
431
|
+
}
|
|
432
|
+
lines.push("};");
|
|
433
|
+
lines.push("");
|
|
434
|
+
}
|
|
435
|
+
const slugAliases = [];
|
|
436
|
+
if (schema.globals.length > 0) slugAliases.push("keyof PayloadGlobals");
|
|
437
|
+
if (schema.collections.length > 0) slugAliases.push("keyof PayloadCollections");
|
|
438
|
+
if (slugAliases.length > 0) {
|
|
439
|
+
lines.push(`export type PayloadSlug = ${slugAliases.join(" | ")};`);
|
|
440
|
+
lines.push("");
|
|
441
|
+
}
|
|
442
|
+
return lines.join("\n").replace(/\n{3,}/g, "\n\n");
|
|
443
|
+
}
|
|
444
|
+
function emitSlug(slug, lines, seenAuxNames, options) {
|
|
445
|
+
emitAuxiliaries(slug.typeName, slug.fields, lines, seenAuxNames, options);
|
|
446
|
+
lines.push(`export interface ${slug.typeName} {`);
|
|
447
|
+
for (const field of slug.fields) {
|
|
448
|
+
emitFieldMember(field, slug.typeName, lines, options);
|
|
449
|
+
}
|
|
450
|
+
lines.push("}");
|
|
451
|
+
}
|
|
452
|
+
function emitAuxiliaries(prefix, fields, lines, seenAuxNames, options) {
|
|
453
|
+
for (const field of fields) {
|
|
454
|
+
const auxName = nestedTypeName(prefix, field.name);
|
|
455
|
+
if (field.kind === "array" || field.kind === "group") {
|
|
456
|
+
if (seenAuxNames.has(auxName)) continue;
|
|
457
|
+
seenAuxNames.add(auxName);
|
|
458
|
+
emitAuxiliaries(auxName, field.fields, lines, seenAuxNames, options);
|
|
459
|
+
lines.push(`export interface ${auxName} {`);
|
|
460
|
+
for (const inner of field.fields) {
|
|
461
|
+
emitFieldMember(inner, auxName, lines, options);
|
|
462
|
+
}
|
|
463
|
+
lines.push("}");
|
|
464
|
+
lines.push("");
|
|
465
|
+
} else if (field.kind === "blocks") {
|
|
466
|
+
for (const block of field.blocks) {
|
|
467
|
+
const blockTypeName = nestedTypeName(auxName, block.typeName);
|
|
468
|
+
if (seenAuxNames.has(blockTypeName)) continue;
|
|
469
|
+
seenAuxNames.add(blockTypeName);
|
|
470
|
+
emitAuxiliaries(blockTypeName, block.fields, lines, seenAuxNames, options);
|
|
471
|
+
emitBlockInterface(blockTypeName, block, lines, options);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
function emitBlockInterface(typeName, block, lines, options) {
|
|
477
|
+
lines.push(`export interface ${typeName} {`);
|
|
478
|
+
lines.push(` blockType: '${block.slug}';`);
|
|
479
|
+
for (const field of block.fields) {
|
|
480
|
+
emitFieldMember(field, typeName, lines, options);
|
|
481
|
+
}
|
|
482
|
+
lines.push("}");
|
|
483
|
+
lines.push("");
|
|
484
|
+
}
|
|
485
|
+
function emitFieldMember(field, parent, lines, options) {
|
|
486
|
+
if (options.markLocalizedAsDeprecated && field.localized) {
|
|
487
|
+
lines.push(` /** @localized */`);
|
|
488
|
+
}
|
|
489
|
+
const optional = field.required ? "" : "?";
|
|
490
|
+
lines.push(` ${field.name}${optional}: ${renderFieldType(field, parent)};`);
|
|
491
|
+
}
|
|
492
|
+
function renderFieldType(field, parent) {
|
|
493
|
+
switch (field.kind) {
|
|
494
|
+
case "scalar":
|
|
495
|
+
return field.typeRef;
|
|
496
|
+
case "array":
|
|
497
|
+
return `${nestedTypeName(parent, field.name)}[]`;
|
|
498
|
+
case "blocks": {
|
|
499
|
+
if (field.blocks.length === 0) return "unknown[]";
|
|
500
|
+
const variants = field.blocks.map((b) => nestedTypeName(nestedTypeName(parent, field.name), b.typeName)).join(" | ");
|
|
501
|
+
return `(${variants})[]`;
|
|
502
|
+
}
|
|
503
|
+
case "group":
|
|
504
|
+
return nestedTypeName(parent, field.name);
|
|
505
|
+
case "relationship": {
|
|
506
|
+
const targetExpr = Array.isArray(field.target) ? field.target.map((t) => `'${t}'`).join(" | ") : `'${field.target}'`;
|
|
507
|
+
const base = `PayloadRelationship<${targetExpr}>`;
|
|
508
|
+
return field.hasMany ? `${base}[]` : base;
|
|
509
|
+
}
|
|
510
|
+
case "upload":
|
|
511
|
+
return "PayloadMedia";
|
|
512
|
+
case "json":
|
|
513
|
+
return "unknown";
|
|
514
|
+
case "select": {
|
|
515
|
+
if (field.options.length === 0) return field.hasMany ? "string[]" : "string";
|
|
516
|
+
const literal = field.options.map((o) => `'${o}'`).join(" | ");
|
|
517
|
+
return field.hasMany ? `(${literal})[]` : literal;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
function nestedTypeName(parent, child) {
|
|
522
|
+
return `${parent}${toPascalCase(child)}`;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// src/codegen/index.ts
|
|
526
|
+
async function generateTypes(options) {
|
|
527
|
+
const cwd = options.cwd ?? process.cwd();
|
|
528
|
+
const configPath = path.isAbsolute(options.configPath) ? options.configPath : path.resolve(cwd, options.configPath);
|
|
529
|
+
const schema = extractSchema({
|
|
530
|
+
configPath,
|
|
531
|
+
...options.project !== void 0 ? { project: options.project } : {},
|
|
532
|
+
...options.tsConfigFilePath !== void 0 ? { tsConfigFilePath: options.tsConfigFilePath } : {}
|
|
533
|
+
});
|
|
534
|
+
const code = emitTypes(schema, options);
|
|
535
|
+
const result = options.outFile ? {
|
|
536
|
+
code,
|
|
537
|
+
diagnostics: schema.diagnostics,
|
|
538
|
+
schema,
|
|
539
|
+
outFile: path.isAbsolute(options.outFile) ? options.outFile : path.resolve(cwd, options.outFile)
|
|
540
|
+
} : { code, diagnostics: schema.diagnostics, schema };
|
|
541
|
+
if (result.outFile) {
|
|
542
|
+
await promises.mkdir(path.dirname(result.outFile), { recursive: true });
|
|
543
|
+
await promises.writeFile(result.outFile, code, "utf8");
|
|
544
|
+
}
|
|
545
|
+
return result;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// src/codegen/astro-plugin.ts
|
|
549
|
+
function livePreviewCodegen(options) {
|
|
550
|
+
const watchEnabled = options.watch ?? true;
|
|
551
|
+
let watcher;
|
|
552
|
+
let active = false;
|
|
553
|
+
let queued = false;
|
|
554
|
+
async function regenerate(logger) {
|
|
555
|
+
if (active) {
|
|
556
|
+
queued = true;
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
active = true;
|
|
560
|
+
try {
|
|
561
|
+
const result = await generateTypes({
|
|
562
|
+
configPath: options.configPath,
|
|
563
|
+
outFile: options.outPath
|
|
564
|
+
});
|
|
565
|
+
if (!options.quiet) {
|
|
566
|
+
logger?.info(
|
|
567
|
+
`pll-codegen: ${result.schema.globals.length} globals, ${result.schema.collections.length} collections \u2192 ${options.outPath}`
|
|
568
|
+
);
|
|
569
|
+
for (const diagnostic of result.diagnostics) {
|
|
570
|
+
logger?.warn(`pll-codegen: ${diagnostic}`);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
} catch (err) {
|
|
574
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
575
|
+
logger?.error(`pll-codegen failed: ${message}`);
|
|
576
|
+
} finally {
|
|
577
|
+
active = false;
|
|
578
|
+
if (queued) {
|
|
579
|
+
queued = false;
|
|
580
|
+
void regenerate(logger);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
return {
|
|
585
|
+
name: "payload-live-preview:codegen",
|
|
586
|
+
hooks: {
|
|
587
|
+
"astro:config:setup": async ({ command, logger }) => {
|
|
588
|
+
await regenerate(logger);
|
|
589
|
+
if (watchEnabled && command === "dev") {
|
|
590
|
+
try {
|
|
591
|
+
watcher = fs.watch(options.configPath, () => {
|
|
592
|
+
void regenerate(logger);
|
|
593
|
+
});
|
|
594
|
+
} catch (err) {
|
|
595
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
596
|
+
logger?.warn(`pll-codegen watcher unavailable: ${message}`);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
},
|
|
600
|
+
"astro:server:start": () => {
|
|
601
|
+
},
|
|
602
|
+
"astro:build:start": () => {
|
|
603
|
+
watcher?.close();
|
|
604
|
+
watcher = void 0;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
exports.livePreviewCodegen = livePreviewCodegen;
|
|
611
|
+
//# sourceMappingURL=codegen-astro.cjs.map
|
|
612
|
+
//# sourceMappingURL=codegen-astro.cjs.map
|