@mapomodule/mcp 0.0.0-stage → 1.0.0-beta.2
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 +509 -2
- package/bin/mapo-mcp.mjs +3 -0
- package/dist/build.d.mts +18 -0
- package/dist/build.d.ts +18 -0
- package/dist/build.mjs +351 -0
- package/dist/cli.d.mts +7 -0
- package/dist/cli.d.ts +7 -0
- package/dist/cli.mjs +274 -0
- package/dist/index.d.mts +68 -0
- package/dist/index.d.ts +68 -0
- package/dist/index.mjs +1 -0
- package/dist/knowledge/api.json +1 -0
- package/dist/knowledge/docs.json +1 -0
- package/dist/module.d.mts +16 -0
- package/dist/module.d.ts +16 -0
- package/dist/module.json +9 -0
- package/dist/module.mjs +124 -0
- package/dist/runtime/core/doctor.d.ts +0 -0
- package/dist/runtime/core/doctor.js +128 -0
- package/dist/runtime/core/endpoint.d.ts +0 -0
- package/dist/runtime/core/endpoint.js +8 -0
- package/dist/runtime/core/indexer.d.ts +0 -0
- package/dist/runtime/core/indexer.js +194 -0
- package/dist/runtime/core/instructions.d.ts +0 -0
- package/dist/runtime/core/instructions.js +10 -0
- package/dist/runtime/core/knowledge.d.ts +0 -0
- package/dist/runtime/core/knowledge.js +76 -0
- package/dist/runtime/core/openapi.d.ts +0 -0
- package/dist/runtime/core/openapi.js +229 -0
- package/dist/runtime/core/overview.d.ts +0 -0
- package/dist/runtime/core/overview.js +15 -0
- package/dist/runtime/core/prompts.d.ts +0 -0
- package/dist/runtime/core/prompts.js +108 -0
- package/dist/runtime/core/recipes.d.ts +0 -0
- package/dist/runtime/core/recipes.js +496 -0
- package/dist/runtime/core/resources.d.ts +0 -0
- package/dist/runtime/core/resources.js +92 -0
- package/dist/runtime/core/scaffold/fields.d.ts +0 -0
- package/dist/runtime/core/scaffold/fields.js +64 -0
- package/dist/runtime/core/scaffold/index.d.ts +0 -0
- package/dist/runtime/core/scaffold/index.js +74 -0
- package/dist/runtime/core/scaffold/templates.d.ts +0 -0
- package/dist/runtime/core/scaffold/templates.js +349 -0
- package/dist/runtime/core/schema-source.d.ts +0 -0
- package/dist/runtime/core/schema-source.js +70 -0
- package/dist/runtime/core/search.d.ts +0 -0
- package/dist/runtime/core/search.js +173 -0
- package/dist/runtime/core/tokenize.d.ts +0 -0
- package/dist/runtime/core/tokenize.js +96 -0
- package/dist/runtime/core/tool-spec.d.ts +0 -0
- package/dist/runtime/core/tool-spec.js +3 -0
- package/dist/runtime/core/tools/api.d.ts +0 -0
- package/dist/runtime/core/tools/api.js +192 -0
- package/dist/runtime/core/tools/app.d.ts +0 -0
- package/dist/runtime/core/tools/app.js +141 -0
- package/dist/runtime/core/tools/backend.d.ts +0 -0
- package/dist/runtime/core/tools/backend.js +143 -0
- package/dist/runtime/core/tools/docs.d.ts +0 -0
- package/dist/runtime/core/tools/docs.js +154 -0
- package/dist/runtime/core/tools/scaffold.d.ts +0 -0
- package/dist/runtime/core/tools/scaffold.js +127 -0
- package/dist/runtime/core/types.d.ts +0 -0
- package/dist/runtime/core/types.js +0 -0
- package/dist/runtime/core/write.d.ts +0 -0
- package/dist/runtime/core/write.js +53 -0
- package/dist/runtime/mcp/handlers/mapo/index.js +7 -0
- package/dist/runtime/mcp/handlers/mapo/prompts/crud-page.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/prompts/custom-field.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/prompts/debug-form.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/prompts/migrate-v1.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/prompts/theme.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/resources/app-manifest.js +19 -0
- package/dist/runtime/mcp/handlers/mapo/resources/docs.js +34 -0
- package/dist/runtime/mcp/handlers/mapo/resources/fields.js +34 -0
- package/dist/runtime/mcp/handlers/mapo/resources/knowledge-index.js +22 -0
- package/dist/runtime/mcp/handlers/mapo/resources/overview.js +20 -0
- package/dist/runtime/mcp/handlers/mapo/tools/backend-schema.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/component-api.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/composable-api.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/doctor.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/get-doc.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/inspect-app.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/list-field-types.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/list-recipes.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/scaffold.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/search-docs.js +3 -0
- package/dist/runtime/nitro/context.d.ts +0 -0
- package/dist/runtime/nitro/context.js +21 -0
- package/dist/runtime/nitro/to-mcp-prompt.d.ts +0 -0
- package/dist/runtime/nitro/to-mcp-prompt.js +23 -0
- package/dist/runtime/nitro/to-mcp-tool.d.ts +0 -0
- package/dist/runtime/nitro/to-mcp-tool.js +28 -0
- package/dist/runtime/types/virtual-modules.d.ts +6 -0
- package/dist/types.d.mts +9 -0
- package/package.json +73 -4
- package/src/__tests__/cli.test.ts +137 -0
- package/src/__tests__/doctor.test.ts +145 -0
- package/src/__tests__/endpoint.test.ts +31 -0
- package/src/__tests__/fixtures/drf-schema.json +142 -0
- package/src/__tests__/fixtures.ts +198 -0
- package/src/__tests__/indexer.test.ts +114 -0
- package/src/__tests__/knowledge.test.ts +225 -0
- package/src/__tests__/openapi.test.ts +256 -0
- package/src/__tests__/overview.test.ts +105 -0
- package/src/__tests__/recipes.test.ts +55 -0
- package/src/__tests__/resources.test.ts +69 -0
- package/src/__tests__/scaffold.test.ts +153 -0
- package/src/__tests__/search.test.ts +105 -0
- package/src/__tests__/tools.test.ts +226 -0
- package/src/__tests__/write.test.ts +134 -0
- package/src/build/extract-components.ts +135 -0
- package/src/build/extract-composables.ts +125 -0
- package/src/build/extract-fields.ts +250 -0
- package/src/build/index.ts +103 -0
- package/src/cli.ts +375 -0
- package/src/index.ts +65 -0
- package/src/module.ts +213 -0
- package/src/runtime/core/doctor.ts +204 -0
- package/src/runtime/core/endpoint.ts +27 -0
- package/src/runtime/core/indexer.ts +285 -0
- package/src/runtime/core/instructions.ts +18 -0
- package/src/runtime/core/knowledge.ts +121 -0
- package/src/runtime/core/openapi.ts +417 -0
- package/src/runtime/core/overview.ts +44 -0
- package/src/runtime/core/prompts.ts +150 -0
- package/src/runtime/core/recipes.ts +574 -0
- package/src/runtime/core/resources.ts +150 -0
- package/src/runtime/core/scaffold/fields.ts +127 -0
- package/src/runtime/core/scaffold/index.ts +120 -0
- package/src/runtime/core/scaffold/templates.ts +394 -0
- package/src/runtime/core/schema-source.ts +123 -0
- package/src/runtime/core/search.ts +282 -0
- package/src/runtime/core/tokenize.ts +146 -0
- package/src/runtime/core/tool-spec.ts +74 -0
- package/src/runtime/core/tools/api.ts +300 -0
- package/src/runtime/core/tools/app.ts +201 -0
- package/src/runtime/core/tools/backend.ts +197 -0
- package/src/runtime/core/tools/docs.ts +218 -0
- package/src/runtime/core/tools/scaffold.ts +176 -0
- package/src/runtime/core/types.ts +133 -0
- package/src/runtime/core/write.ts +119 -0
- package/src/runtime/mcp/handlers/mapo/index.ts +16 -0
- package/src/runtime/mcp/handlers/mapo/prompts/crud-page.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/prompts/custom-field.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/prompts/debug-form.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/prompts/migrate-v1.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/prompts/theme.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/resources/app-manifest.ts +28 -0
- package/src/runtime/mcp/handlers/mapo/resources/docs.ts +41 -0
- package/src/runtime/mcp/handlers/mapo/resources/fields.ts +37 -0
- package/src/runtime/mcp/handlers/mapo/resources/knowledge-index.ts +24 -0
- package/src/runtime/mcp/handlers/mapo/resources/overview.ts +22 -0
- package/src/runtime/mcp/handlers/mapo/tools/backend-schema.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/component-api.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/composable-api.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/doctor.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/get-doc.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/inspect-app.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/list-field-types.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/list-recipes.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/scaffold.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/search-docs.ts +4 -0
- package/src/runtime/nitro/context.ts +41 -0
- package/src/runtime/nitro/to-mcp-prompt.ts +30 -0
- package/src/runtime/nitro/to-mcp-tool.ts +35 -0
- package/src/runtime/types/virtual-modules.d.ts +6 -0
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resource payloads, shared by both transports.
|
|
3
|
+
*
|
|
4
|
+
* Resources are what a client can attach to the conversation without calling a
|
|
5
|
+
* tool, so they carry whole documents: a doc page, the contract of a field
|
|
6
|
+
* type, or the index of everything available.
|
|
7
|
+
*/
|
|
8
|
+
import { RECIPES } from "./recipes.js";
|
|
9
|
+
import { getDoc, listDocs } from "./search.js";
|
|
10
|
+
import type { ApiKnowledge, DocsKnowledge } from "./types.js";
|
|
11
|
+
|
|
12
|
+
export const DOCS_URI_PREFIX = "mapo://docs/";
|
|
13
|
+
export const FIELD_URI_PREFIX = "mapo://fields/";
|
|
14
|
+
export const INDEX_URI = "mapo://knowledge/index";
|
|
15
|
+
export const MANIFEST_URI = "mapo://app/manifest";
|
|
16
|
+
export const OVERVIEW_URI = "mapo://overview";
|
|
17
|
+
|
|
18
|
+
/** The docs page that explains how Mapo fits together (see `overview.ts`). */
|
|
19
|
+
export const OVERVIEW_DOC = "guide/concepts.md";
|
|
20
|
+
|
|
21
|
+
/** Everything the knowledge base contains, as a compact browsable map. */
|
|
22
|
+
export function renderKnowledgeIndex(
|
|
23
|
+
knowledge: DocsKnowledge,
|
|
24
|
+
api: ApiKnowledge,
|
|
25
|
+
): string {
|
|
26
|
+
const pages = listDocs(knowledge)
|
|
27
|
+
.map(
|
|
28
|
+
(doc) =>
|
|
29
|
+
`- \`${doc.path}\` — ${doc.title} (${doc.headings.length} sections)`,
|
|
30
|
+
)
|
|
31
|
+
.join("\n");
|
|
32
|
+
|
|
33
|
+
const recipes = RECIPES.map(
|
|
34
|
+
(recipe) => `- ${recipe.task} → \`${recipe.doc}\``,
|
|
35
|
+
).join("\n");
|
|
36
|
+
|
|
37
|
+
const components = api.components
|
|
38
|
+
.map((component) => component.name)
|
|
39
|
+
.join(", ");
|
|
40
|
+
const fields = api.fieldTypes.map((field) => field.type).join(", ");
|
|
41
|
+
const composables = api.composables
|
|
42
|
+
.map((composable) => composable.name)
|
|
43
|
+
.join(", ");
|
|
44
|
+
|
|
45
|
+
return [
|
|
46
|
+
"# Mapo knowledge base",
|
|
47
|
+
"",
|
|
48
|
+
`Generated ${knowledge.generatedAt}.`,
|
|
49
|
+
"",
|
|
50
|
+
`Start with the overview: the \`${OVERVIEW_URI}\` resource (\`${OVERVIEW_DOC}\`).`,
|
|
51
|
+
"",
|
|
52
|
+
"## Recipes",
|
|
53
|
+
recipes,
|
|
54
|
+
"",
|
|
55
|
+
"## Documentation pages",
|
|
56
|
+
pages,
|
|
57
|
+
"",
|
|
58
|
+
"## Components",
|
|
59
|
+
components,
|
|
60
|
+
"",
|
|
61
|
+
"## Field types",
|
|
62
|
+
fields,
|
|
63
|
+
"",
|
|
64
|
+
"## Auto-imported composables",
|
|
65
|
+
composables,
|
|
66
|
+
].join("\n");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** One documentation page, whole. */
|
|
70
|
+
export function renderDocResource(
|
|
71
|
+
knowledge: DocsKnowledge,
|
|
72
|
+
path: string,
|
|
73
|
+
): string | null {
|
|
74
|
+
const doc = getDoc(knowledge, path, { maxChars: 100_000 });
|
|
75
|
+
if (!doc) return null;
|
|
76
|
+
return `# ${doc.title}\n\nsource: \`${doc.path}\`\n\n${doc.content}`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The full contract of a field type: attrs, defaults, component, docs. */
|
|
80
|
+
export function renderFieldResource(
|
|
81
|
+
api: ApiKnowledge,
|
|
82
|
+
type: string,
|
|
83
|
+
): string | null {
|
|
84
|
+
const field = api.fieldTypes.find((entry) => entry.type === type);
|
|
85
|
+
if (!field) return null;
|
|
86
|
+
|
|
87
|
+
const attrs = field.attrs.length
|
|
88
|
+
? field.attrs
|
|
89
|
+
.map(
|
|
90
|
+
(attr) =>
|
|
91
|
+
`- \`${attr.name}\`${attr.type ? `: ${attr.type}` : ""}${attr.required ? " (required)" : ""}` +
|
|
92
|
+
`${attr.description ? ` — ${attr.description}` : ""}`,
|
|
93
|
+
)
|
|
94
|
+
.join("\n")
|
|
95
|
+
: "_none_";
|
|
96
|
+
|
|
97
|
+
const common = api.fieldCommon
|
|
98
|
+
.map((member) => `\`${member.name}\``)
|
|
99
|
+
.join(", ");
|
|
100
|
+
|
|
101
|
+
return [
|
|
102
|
+
`# Field type \`${field.type}\``,
|
|
103
|
+
"",
|
|
104
|
+
field.description ?? "",
|
|
105
|
+
"",
|
|
106
|
+
`- component: ${field.component ?? "(unmapped)"}`,
|
|
107
|
+
`- descriptor: ${field.descriptor ?? "(none)"}`,
|
|
108
|
+
field.defaultAttrs
|
|
109
|
+
? `- registry default attrs: \`${JSON.stringify(field.defaultAttrs)}\``
|
|
110
|
+
: "",
|
|
111
|
+
"",
|
|
112
|
+
"## attrs",
|
|
113
|
+
attrs,
|
|
114
|
+
"",
|
|
115
|
+
"## Shared descriptor properties",
|
|
116
|
+
common,
|
|
117
|
+
"",
|
|
118
|
+
field.docs.length
|
|
119
|
+
? `## Docs\n${field.docs.map((doc) => `- \`${doc}\``).join("\n")}`
|
|
120
|
+
: "",
|
|
121
|
+
]
|
|
122
|
+
.filter(Boolean)
|
|
123
|
+
.join("\n");
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Doc pages as resource entries, for clients that list what is attachable. */
|
|
127
|
+
export function docResourceList(
|
|
128
|
+
knowledge: DocsKnowledge,
|
|
129
|
+
): Array<{ uri: string; name: string; description: string; mimeType: string }> {
|
|
130
|
+
return listDocs(knowledge).map((doc) => ({
|
|
131
|
+
uri: `${DOCS_URI_PREFIX}${doc.path}`,
|
|
132
|
+
name: doc.title,
|
|
133
|
+
description: `${doc.section} · ${doc.headings.slice(0, 5).join(", ")}`,
|
|
134
|
+
mimeType: "text/markdown",
|
|
135
|
+
}));
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Field types as resource entries. */
|
|
139
|
+
export function fieldResourceList(
|
|
140
|
+
api: ApiKnowledge,
|
|
141
|
+
): Array<{ uri: string; name: string; description: string; mimeType: string }> {
|
|
142
|
+
return api.fieldTypes.map((field) => ({
|
|
143
|
+
uri: `${FIELD_URI_PREFIX}${field.type}`,
|
|
144
|
+
name: `Field type: ${field.type}`,
|
|
145
|
+
description:
|
|
146
|
+
field.description ??
|
|
147
|
+
`Rendered by ${field.component ?? "a custom component"}`,
|
|
148
|
+
mimeType: "text/markdown",
|
|
149
|
+
}));
|
|
150
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Field specs: the compact syntax the scaffold tool accepts for descriptors.
|
|
3
|
+
*
|
|
4
|
+
* `"title:text:Title"` → `{ key: "title", type: "text", label: "Title" }`.
|
|
5
|
+
* Types are validated against the registry extracted from source, so the
|
|
6
|
+
* scaffold can never emit a field type that does not exist.
|
|
7
|
+
*/
|
|
8
|
+
import type { ApiKnowledge } from "../types.js";
|
|
9
|
+
|
|
10
|
+
export interface ParsedField {
|
|
11
|
+
key: string;
|
|
12
|
+
type: string;
|
|
13
|
+
label: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class UnknownFieldTypeError extends Error {
|
|
17
|
+
constructor(
|
|
18
|
+
readonly type: string,
|
|
19
|
+
readonly available: string[],
|
|
20
|
+
) {
|
|
21
|
+
super(
|
|
22
|
+
`Unknown field type "${type}". Registered types: ${available.join(", ")}. ` +
|
|
23
|
+
`Custom types are added with defineFormField() — see uikit/form/registry.md.`,
|
|
24
|
+
);
|
|
25
|
+
this.name = "UnknownFieldTypeError";
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* `published_at` / `coverImage` → `Published at` / `Cover image`.
|
|
31
|
+
* Sentence case, not Title Case: it is what the rest of the admin uses, and an
|
|
32
|
+
* explicit label is one `:` away when the guess is wrong.
|
|
33
|
+
*/
|
|
34
|
+
export function humanise(key: string): string {
|
|
35
|
+
return key
|
|
36
|
+
.replace(/[_-]+/g, " ")
|
|
37
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
38
|
+
.replace(/\s+/g, " ")
|
|
39
|
+
.trim()
|
|
40
|
+
.toLowerCase()
|
|
41
|
+
.replace(/^./, (char) => char.toUpperCase());
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @param specs `key[:type[:label]]`, type defaults to `text`
|
|
46
|
+
* @param api when given, unknown types are rejected instead of generated
|
|
47
|
+
*/
|
|
48
|
+
export function parseFields(
|
|
49
|
+
specs: string[],
|
|
50
|
+
api?: ApiKnowledge,
|
|
51
|
+
): ParsedField[] {
|
|
52
|
+
const known = api ? new Set(api.fieldTypes.map((field) => field.type)) : null;
|
|
53
|
+
|
|
54
|
+
return specs.map((spec) => {
|
|
55
|
+
const [key = "", type = "text", ...rest] = spec.split(":");
|
|
56
|
+
if (!key.trim()) throw new Error(`Empty field key in "${spec}".`);
|
|
57
|
+
|
|
58
|
+
if (known && !known.has(type)) {
|
|
59
|
+
throw new UnknownFieldTypeError(type, [...known].sort());
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
key: key.trim(),
|
|
64
|
+
type: type.trim(),
|
|
65
|
+
label: rest.join(":").trim() || humanise(key.trim()),
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Attrs a field type cannot work without, so the skeleton is honest about them. */
|
|
71
|
+
function requiredAttrs(type: string, api?: ApiKnowledge): string[] {
|
|
72
|
+
const field = api?.fieldTypes.find((entry) => entry.type === type);
|
|
73
|
+
return (field?.attrs ?? [])
|
|
74
|
+
.filter((attr) => attr.required)
|
|
75
|
+
.map((attr) => attr.name);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Renders a descriptor array literal, indented to sit inside a `const`. */
|
|
79
|
+
export function renderDescriptors(
|
|
80
|
+
fields: ParsedField[],
|
|
81
|
+
api: ApiKnowledge | undefined,
|
|
82
|
+
indent = " ",
|
|
83
|
+
): string {
|
|
84
|
+
return fields
|
|
85
|
+
.map((field) => {
|
|
86
|
+
const attrs = requiredAttrs(field.type, api);
|
|
87
|
+
const attrsLine = attrs.length
|
|
88
|
+
? `\n${indent} attrs: { ${attrs.map((name) => `${name}: undefined /* TODO */`).join(", ")} },`
|
|
89
|
+
: "";
|
|
90
|
+
|
|
91
|
+
return (
|
|
92
|
+
`${indent}{\n` +
|
|
93
|
+
`${indent} key: "${field.key}",\n` +
|
|
94
|
+
`${indent} type: "${field.type}",\n` +
|
|
95
|
+
`${indent} label: "${field.label}",${attrsLine}\n` +
|
|
96
|
+
`${indent}},`
|
|
97
|
+
);
|
|
98
|
+
})
|
|
99
|
+
.join("\n");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Interface members for the model type, inferred from the field types. */
|
|
103
|
+
export function renderModel(fields: ParsedField[]): string {
|
|
104
|
+
const tsType = (type: string): string => {
|
|
105
|
+
if (["number", "slider"].includes(type)) return "number";
|
|
106
|
+
if (["boolean", "switch"].includes(type)) return "boolean";
|
|
107
|
+
if (
|
|
108
|
+
[
|
|
109
|
+
"fks",
|
|
110
|
+
"m2m",
|
|
111
|
+
"media",
|
|
112
|
+
"media-m2m",
|
|
113
|
+
"enhanced-media",
|
|
114
|
+
"repeater",
|
|
115
|
+
"seo",
|
|
116
|
+
"map",
|
|
117
|
+
].includes(type)
|
|
118
|
+
) {
|
|
119
|
+
return "unknown";
|
|
120
|
+
}
|
|
121
|
+
return "string";
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
return fields
|
|
125
|
+
.map((field) => ` ${field.key}: ${tsType(field.type)};`)
|
|
126
|
+
.join("\n");
|
|
127
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scaffold entry point: turns a `kind` plus a few arguments into files.
|
|
3
|
+
*
|
|
4
|
+
* Generation is pure — no filesystem access here. Writing is a separate,
|
|
5
|
+
* guarded step (`../write.ts`), so the default behaviour stays a dry run and
|
|
6
|
+
* the dangerous part has one place to audit.
|
|
7
|
+
*/
|
|
8
|
+
import { parseFields } from "./fields.js";
|
|
9
|
+
import * as templates from "./templates.js";
|
|
10
|
+
import type { TemplateInput, TemplateOutput } from "./templates.js";
|
|
11
|
+
import type { ApiKnowledge } from "../types.js";
|
|
12
|
+
|
|
13
|
+
export const SCAFFOLD_KINDS = [
|
|
14
|
+
"list-page",
|
|
15
|
+
"detail-page",
|
|
16
|
+
"standalone-form",
|
|
17
|
+
"custom-field",
|
|
18
|
+
"login-page",
|
|
19
|
+
"theme-override",
|
|
20
|
+
"backend-proxy",
|
|
21
|
+
"menu-page",
|
|
22
|
+
"media-page",
|
|
23
|
+
] as const;
|
|
24
|
+
|
|
25
|
+
export type ScaffoldKind = (typeof SCAFFOLD_KINDS)[number];
|
|
26
|
+
|
|
27
|
+
/** What each kind produces, for the tool's own documentation. */
|
|
28
|
+
export const SCAFFOLD_DESCRIPTIONS: Record<ScaffoldKind, string> = {
|
|
29
|
+
"list-page": "MapoList page with typed columns",
|
|
30
|
+
"detail-page": "MapoDetail page with field descriptors",
|
|
31
|
+
"standalone-form": "MapoForm driven by useCrud and your own submit handler",
|
|
32
|
+
"custom-field": "field component plus the plugin registering its type",
|
|
33
|
+
"login-page": "MapoLogin page wired to the configured auth endpoints",
|
|
34
|
+
"theme-override":
|
|
35
|
+
"CSS token file plus the nuxt.config fragment that loads it",
|
|
36
|
+
"backend-proxy": "Nitro middleware proxying /api/* to a backend",
|
|
37
|
+
"menu-page": "MapoMenuManager page for a navigation tree",
|
|
38
|
+
"media-page": "MapoMediaManager page",
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** Kinds that describe a resource and therefore accept fields. */
|
|
42
|
+
const RESOURCE_KINDS = new Set<ScaffoldKind>([
|
|
43
|
+
"list-page",
|
|
44
|
+
"detail-page",
|
|
45
|
+
"standalone-form",
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
export interface ScaffoldOptions {
|
|
49
|
+
kind: ScaffoldKind;
|
|
50
|
+
/** Resource or type name, e.g. `articles` or `star-rating`. */
|
|
51
|
+
name?: string;
|
|
52
|
+
/** REST endpoint; defaults to `/api/<name>`. */
|
|
53
|
+
endpoint?: string;
|
|
54
|
+
/** Route base; defaults to `/<name>`. */
|
|
55
|
+
route?: string;
|
|
56
|
+
/** `key[:type[:label]]` specs. */
|
|
57
|
+
fields?: string[];
|
|
58
|
+
api?: ApiKnowledge;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** `articles` → `Article`; `star-rating` → `StarRating`. */
|
|
62
|
+
function toModelName(name: string): string {
|
|
63
|
+
const pascal = name
|
|
64
|
+
.split(/[^A-Za-z0-9]+/)
|
|
65
|
+
.filter(Boolean)
|
|
66
|
+
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
67
|
+
.join("");
|
|
68
|
+
|
|
69
|
+
// Singularise the obvious plurals; anything else stays as written.
|
|
70
|
+
if (/ies$/.test(pascal)) return `${pascal.slice(0, -3)}y`;
|
|
71
|
+
if (/(ses|xes|zes|ches|shes)$/.test(pascal)) return pascal.slice(0, -2);
|
|
72
|
+
if (/[^s]s$/.test(pascal)) return pascal.slice(0, -1);
|
|
73
|
+
return pascal;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function scaffold(options: ScaffoldOptions): TemplateOutput {
|
|
77
|
+
const name = (options.name ?? "items").trim().replace(/^\/+|\/+$/g, "");
|
|
78
|
+
const route = (options.route ?? `/${name}`).replace(/\/+$/, "");
|
|
79
|
+
const endpoint = (options.endpoint ?? `/api/${name}`).replace(/\/+$/, "");
|
|
80
|
+
|
|
81
|
+
const fields = RESOURCE_KINDS.has(options.kind)
|
|
82
|
+
? parseFields(
|
|
83
|
+
options.fields?.length ? options.fields : ["title:text"],
|
|
84
|
+
options.api,
|
|
85
|
+
)
|
|
86
|
+
: [];
|
|
87
|
+
|
|
88
|
+
const input: TemplateInput = {
|
|
89
|
+
name,
|
|
90
|
+
model: toModelName(name),
|
|
91
|
+
endpoint,
|
|
92
|
+
route,
|
|
93
|
+
fields,
|
|
94
|
+
...(options.api ? { api: options.api } : {}),
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
switch (options.kind) {
|
|
98
|
+
case "list-page":
|
|
99
|
+
return templates.listPage(input);
|
|
100
|
+
case "detail-page":
|
|
101
|
+
return templates.detailPage(input);
|
|
102
|
+
case "standalone-form":
|
|
103
|
+
return templates.standaloneForm(input);
|
|
104
|
+
case "custom-field":
|
|
105
|
+
return templates.customField(input);
|
|
106
|
+
case "login-page":
|
|
107
|
+
return templates.loginPage();
|
|
108
|
+
case "theme-override":
|
|
109
|
+
return templates.themeOverride();
|
|
110
|
+
case "backend-proxy":
|
|
111
|
+
return templates.backendProxy(input);
|
|
112
|
+
case "menu-page":
|
|
113
|
+
return templates.menuPage(input);
|
|
114
|
+
case "media-page":
|
|
115
|
+
return templates.mediaPage(input);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export type { TemplateOutput as ScaffoldResult } from "./templates.js";
|
|
120
|
+
export { UnknownFieldTypeError } from "./fields.js";
|