@tschk/moonshine-cli 0.3.7 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/adopt/conventions.ts +39 -0
- package/src/adopt/frameworks.ts +155 -0
- package/src/adopt/gaps.ts +188 -0
- package/src/adopt/plan.ts +232 -0
- package/src/adopt/project.ts +71 -0
- package/src/adopt/report.ts +167 -0
- package/src/adopt/scan.ts +282 -0
- package/src/adopt/templates.ts +180 -0
- package/src/adopt/types.ts +66 -0
- package/src/adopt.ts +42 -1237
- package/src/compile.ts +18 -9
- package/src/config.ts +1 -1
- package/src/new.ts +112 -102
- package/src/preview.ts +18 -4
package/package.json
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** Host conventions present in the tree, named the way the user named them. */
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
|
|
5
|
+
/** Next conventions present in the tree, named the way the user named them. */
|
|
6
|
+
export function findConventions(projectDir: string, files: string[]): string[] {
|
|
7
|
+
const seen: string[] = [];
|
|
8
|
+
for (const candidate of [
|
|
9
|
+
"next.config.js",
|
|
10
|
+
"next.config.mjs",
|
|
11
|
+
"next.config.ts",
|
|
12
|
+
"middleware.ts",
|
|
13
|
+
"middleware.js",
|
|
14
|
+
"src/middleware.ts",
|
|
15
|
+
"instrumentation.ts",
|
|
16
|
+
]) {
|
|
17
|
+
if (existsSync(join(projectDir, candidate))) seen.push(candidate);
|
|
18
|
+
}
|
|
19
|
+
for (const file of files) {
|
|
20
|
+
const base = file.split("/").pop()!;
|
|
21
|
+
const stem = base.replace(/\.[jt]sx?$/, "");
|
|
22
|
+
if (
|
|
23
|
+
[
|
|
24
|
+
"layout",
|
|
25
|
+
"route",
|
|
26
|
+
"loading",
|
|
27
|
+
"template",
|
|
28
|
+
"not-found",
|
|
29
|
+
"error",
|
|
30
|
+
"default",
|
|
31
|
+
].includes(stem)
|
|
32
|
+
) {
|
|
33
|
+
seen.push(file);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return seen;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Source-level Next features with no moonshine equivalent yet. */
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/** Which hosts moonshine knows, and how each one's specifiers map onto it. */
|
|
2
|
+
import { readJson } from "./scan.js";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import {
|
|
5
|
+
nextAliases,
|
|
6
|
+
resolveAlias as resolveNextAlias,
|
|
7
|
+
} from "@tschk/moonshine-next/aliases";
|
|
8
|
+
import {
|
|
9
|
+
reactRouterAliases,
|
|
10
|
+
resolveAlias as resolveReactRouterAlias,
|
|
11
|
+
} from "@tschk/moonshine-react-router/aliases";
|
|
12
|
+
import {
|
|
13
|
+
tanstackAliases,
|
|
14
|
+
resolveAlias as resolveTanstackAlias,
|
|
15
|
+
} from "@tschk/moonshine-tanstack/aliases";
|
|
16
|
+
import {
|
|
17
|
+
wakuAliases,
|
|
18
|
+
resolveAlias as resolveWakuAlias,
|
|
19
|
+
} from "@tschk/moonshine-waku/aliases";
|
|
20
|
+
|
|
21
|
+
export type Framework =
|
|
22
|
+
| "next-app"
|
|
23
|
+
| "next-pages"
|
|
24
|
+
| "react-router"
|
|
25
|
+
| "tanstack"
|
|
26
|
+
| "waku"
|
|
27
|
+
| "svelte"
|
|
28
|
+
| "vue"
|
|
29
|
+
| "astro"
|
|
30
|
+
| "angular"
|
|
31
|
+
| "vite-react"
|
|
32
|
+
| "react"
|
|
33
|
+
| "unknown";
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Frameworks adopted by compiling their templates to View IR, not by aliasing
|
|
37
|
+
* imports, and the globs whose files crepuscularity's dispatcher claims.
|
|
38
|
+
*
|
|
39
|
+
* Angular is keyed on the Angular CLI's `*.component.html`, on `*.ng.html` and
|
|
40
|
+
* on a bare `.ng`. Plain `.html` is deliberately absent: the dispatcher does not
|
|
41
|
+
* claim it, so a project's `index.html` must not be swept in here either.
|
|
42
|
+
*/
|
|
43
|
+
const TEMPLATE_FRAMEWORKS: Readonly<Record<string, readonly string[]>> = {
|
|
44
|
+
svelte: ["**/*.svelte"],
|
|
45
|
+
vue: ["**/*.vue"],
|
|
46
|
+
astro: ["**/*.astro"],
|
|
47
|
+
angular: ["**/*.component.html", "**/*.ng.html", "**/*.ng"],
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export function templateGlobs(
|
|
51
|
+
framework: Framework,
|
|
52
|
+
): readonly string[] | undefined {
|
|
53
|
+
return TEMPLATE_FRAMEWORKS[framework];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* A sample only the real frontend compiles into control flow. The dispatcher
|
|
58
|
+
* falls back to generic markup for an extension it does not know, and that
|
|
59
|
+
* fallback is silent — it yields plausible-looking IR with the framework's own
|
|
60
|
+
* constructs quietly dropped. Probing for the structural node proves the
|
|
61
|
+
* installed parser build actually carries the frontend.
|
|
62
|
+
*/
|
|
63
|
+
const FRONTEND_PROBES: Readonly<
|
|
64
|
+
Record<string, { file: string; source: string }>
|
|
65
|
+
> = {
|
|
66
|
+
astro: { file: "probe.astro", source: "{ok && <p>y</p>}" },
|
|
67
|
+
angular: { file: "probe.ng.html", source: "@if (a) { <p>y</p> }" },
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/** Version of the parser build actually loaded, for the refusal message. */
|
|
71
|
+
export async function parserVersion(): Promise<string | undefined> {
|
|
72
|
+
try {
|
|
73
|
+
const entry = Bun.resolveSync(
|
|
74
|
+
"@tschk/crepuscularity-wasm",
|
|
75
|
+
import.meta.dir,
|
|
76
|
+
);
|
|
77
|
+
const pkg = readJson<{ version?: string }>(
|
|
78
|
+
join(dirname(entry), "package.json"),
|
|
79
|
+
);
|
|
80
|
+
return pkg?.version;
|
|
81
|
+
} catch {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export async function hasFrontend(framework: Framework): Promise<boolean> {
|
|
87
|
+
const probe = FRONTEND_PROBES[framework];
|
|
88
|
+
if (!probe) return true;
|
|
89
|
+
try {
|
|
90
|
+
const { parseTemplate } = await import("@tschk/crepuscularity-wasm");
|
|
91
|
+
const ir = parseTemplate(probe.source, probe.file);
|
|
92
|
+
return (ir.root ?? []).some((node) => node.kind === "if");
|
|
93
|
+
} catch {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** A host package moonshine reimplements, and how its specifiers map to files. */
|
|
99
|
+
export type HostAdapter = {
|
|
100
|
+
/** Package the aliases resolve into. */
|
|
101
|
+
pkg: string;
|
|
102
|
+
/** Bare specifier → adapter subpath. */
|
|
103
|
+
aliases: Readonly<Record<string, string>>;
|
|
104
|
+
resolveAlias: (specifier: string) => string | undefined;
|
|
105
|
+
/** A specifier whose backing file sits directly in `src/`, used to locate it. */
|
|
106
|
+
reference: string;
|
|
107
|
+
/** Specifiers in the source that belong to this host, aliasable or not. */
|
|
108
|
+
imports: RegExp;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const ADAPTERS = {
|
|
112
|
+
next: {
|
|
113
|
+
pkg: "@tschk/moonshine-next",
|
|
114
|
+
aliases: nextAliases,
|
|
115
|
+
resolveAlias: resolveNextAlias,
|
|
116
|
+
reference: "next/link",
|
|
117
|
+
imports: /^next(?:\/.*)?$/,
|
|
118
|
+
},
|
|
119
|
+
"react-router": {
|
|
120
|
+
pkg: "@tschk/moonshine-react-router",
|
|
121
|
+
aliases: reactRouterAliases,
|
|
122
|
+
resolveAlias: resolveReactRouterAlias,
|
|
123
|
+
reference: "react-router",
|
|
124
|
+
imports: /^(?:react-router|react-router-dom|@remix-run\/[^/]+)$/,
|
|
125
|
+
},
|
|
126
|
+
tanstack: {
|
|
127
|
+
pkg: "@tschk/moonshine-tanstack",
|
|
128
|
+
aliases: tanstackAliases,
|
|
129
|
+
resolveAlias: resolveTanstackAlias,
|
|
130
|
+
reference: "@tanstack/react-router",
|
|
131
|
+
imports: /^@tanstack\/(?:react-router|router-core|react-start|start)$/,
|
|
132
|
+
},
|
|
133
|
+
waku: {
|
|
134
|
+
pkg: "@tschk/moonshine-waku",
|
|
135
|
+
aliases: wakuAliases,
|
|
136
|
+
resolveAlias: resolveWakuAlias,
|
|
137
|
+
reference: "waku/router",
|
|
138
|
+
imports: /^waku(?:\/.*)?$/,
|
|
139
|
+
},
|
|
140
|
+
} as const satisfies Record<string, HostAdapter>;
|
|
141
|
+
|
|
142
|
+
const FRAMEWORK_ADAPTER: Readonly<
|
|
143
|
+
Partial<Record<Framework, keyof typeof ADAPTERS>>
|
|
144
|
+
> = {
|
|
145
|
+
"next-app": "next",
|
|
146
|
+
"next-pages": "next",
|
|
147
|
+
"react-router": "react-router",
|
|
148
|
+
tanstack: "tanstack",
|
|
149
|
+
waku: "waku",
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
export function adapterFor(framework: Framework): HostAdapter | undefined {
|
|
153
|
+
const id = FRAMEWORK_ADAPTER[framework];
|
|
154
|
+
return id ? ADAPTERS[id] : undefined;
|
|
155
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/** What does not carry over: per host, and per compiled template. */
|
|
2
|
+
import { templateGlobs } from "./frameworks.js";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
import { readFile } from "node:fs/promises";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import type { Framework } from "./frameworks.js";
|
|
7
|
+
import type { AdoptScan, TemplateFile } from "./types.js";
|
|
8
|
+
|
|
9
|
+
export async function findManualWork(
|
|
10
|
+
projectDir: string,
|
|
11
|
+
files: string[],
|
|
12
|
+
scan: Pick<AdoptScan, "framework" | "routesDir">,
|
|
13
|
+
): Promise<string[]> {
|
|
14
|
+
const manual: string[] = [];
|
|
15
|
+
const note = (text: string) => {
|
|
16
|
+
if (!manual.includes(text)) manual.push(text);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
for (const candidate of [
|
|
20
|
+
"middleware.ts",
|
|
21
|
+
"middleware.js",
|
|
22
|
+
"src/middleware.ts",
|
|
23
|
+
]) {
|
|
24
|
+
if (existsSync(join(projectDir, candidate))) {
|
|
25
|
+
note(
|
|
26
|
+
`${candidate}: Next middleware does not run. Moonshine has no edge middleware phase — port it to a route loader or a Bun server wrapper.`,
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
for (const candidate of [
|
|
31
|
+
"next.config.js",
|
|
32
|
+
"next.config.mjs",
|
|
33
|
+
"next.config.ts",
|
|
34
|
+
]) {
|
|
35
|
+
if (existsSync(join(projectDir, candidate))) {
|
|
36
|
+
note(
|
|
37
|
+
`${candidate}: not read. redirects/rewrites/headers/images/webpack options are all ignored.`,
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const results: { file: string; source: string }[] = Array.from({
|
|
43
|
+
length: files.length,
|
|
44
|
+
}) as { file: string; source: string }[];
|
|
45
|
+
let currentIndex = 0;
|
|
46
|
+
|
|
47
|
+
const worker = async () => {
|
|
48
|
+
while (currentIndex < files.length) {
|
|
49
|
+
const index = currentIndex++;
|
|
50
|
+
const file = files[index];
|
|
51
|
+
const source = await readFile(join(projectDir, file), "utf8");
|
|
52
|
+
results[index] = { file, source };
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const concurrency = 100;
|
|
57
|
+
const workers = Array.from(
|
|
58
|
+
{ length: Math.min(concurrency, files.length) },
|
|
59
|
+
worker,
|
|
60
|
+
);
|
|
61
|
+
await Promise.all(workers);
|
|
62
|
+
|
|
63
|
+
const fileContents = results;
|
|
64
|
+
|
|
65
|
+
for (const item of fileContents) {
|
|
66
|
+
if (!item) continue;
|
|
67
|
+
const { file, source } = item;
|
|
68
|
+
const stem = file
|
|
69
|
+
.split("/")
|
|
70
|
+
.pop()!
|
|
71
|
+
.replace(/\.[jt]sx?$/, "");
|
|
72
|
+
|
|
73
|
+
if (
|
|
74
|
+
/export\s+(?:const|async\s+function|function)\s+generateMetadata/.test(
|
|
75
|
+
source,
|
|
76
|
+
)
|
|
77
|
+
) {
|
|
78
|
+
note(
|
|
79
|
+
`${file}: generateMetadata() is not called. Moonshine emits its own <head>; move tags into a layout.`,
|
|
80
|
+
);
|
|
81
|
+
} else if (/export\s+const\s+metadata\b/.test(source)) {
|
|
82
|
+
note(
|
|
83
|
+
`${file}: the metadata export is not read. Moonshine emits its own <head>; move tags into a layout.`,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
if (/export\s+const\s+revalidate\b/.test(source)) {
|
|
87
|
+
note(`${file}: ISR (export const revalidate) is not implemented.`);
|
|
88
|
+
}
|
|
89
|
+
if (/^\s*["']use server["']/m.test(source)) {
|
|
90
|
+
note(`${file}: server actions ("use server") are not supported.`);
|
|
91
|
+
}
|
|
92
|
+
if (/export\s+default\s+async\s+function/.test(source) && stem === "page") {
|
|
93
|
+
note(
|
|
94
|
+
`${file}: async server component. Moonshine renders with react-dom/server, which cannot await a component — move the fetch into a loader.`,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
if (stem === "layout" && /<html\b/.test(source)) {
|
|
98
|
+
note(
|
|
99
|
+
`${file}: returns <html>/<body>, but moonshine already emits the document shell. Return a fragment instead.`,
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
if (["loading", "template", "not-found", "default"].includes(stem)) {
|
|
103
|
+
note(`${file}: ${stem}.tsx has no moonshine equivalent and is ignored.`);
|
|
104
|
+
}
|
|
105
|
+
if (/getServerSideProps|getStaticProps|getStaticPaths/.test(source)) {
|
|
106
|
+
note(
|
|
107
|
+
`${file}: getServerSideProps/getStaticProps are not called. Rename to a moonshine loader.`,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (scan.routesDir) {
|
|
113
|
+
for (const file of files) {
|
|
114
|
+
if (!file.startsWith(`${scan.routesDir}/`)) continue;
|
|
115
|
+
const segments = file.split("/");
|
|
116
|
+
if (segments.some((s) => s.startsWith("@"))) {
|
|
117
|
+
note(`${file}: parallel routes (@slot) are dropped.`);
|
|
118
|
+
}
|
|
119
|
+
if (segments.some((s) => /^\(\.+\)/.test(s))) {
|
|
120
|
+
note(`${file}: intercepting routes are dropped.`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return manual;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Behavioural gaps in the reimplementations the app actually imports. */
|
|
129
|
+
export const ALIAS_CAVEATS: Readonly<Record<string, string>> = {
|
|
130
|
+
"next/image":
|
|
131
|
+
"next/image renders a plain <img>: no resizing, no AVIF/WebP negotiation, no blur placeholder.",
|
|
132
|
+
"next/link":
|
|
133
|
+
"next/link navigates through the moonshine router; prefetch is a browser <link rel=prefetch> hint, not an RSC payload warm-up.",
|
|
134
|
+
"next/headers":
|
|
135
|
+
"next/headers reads the moonshine request context; cookie mutations do not propagate to the response.",
|
|
136
|
+
"next/font/google":
|
|
137
|
+
"next/font/google emits a Google Fonts <link> instead of self-hosting; call fontLinks() in your layout head or the family never loads.",
|
|
138
|
+
"next/dynamic":
|
|
139
|
+
"next/dynamic lazy-loads on the client only; `ssr: false` is the effective behaviour either way.",
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* The honest boundary of the template path: crepuscularity compiles the markup
|
|
144
|
+
* only. Nothing inside `<script>` runs, so component logic has to be ported.
|
|
145
|
+
*/
|
|
146
|
+
export function templateManualWork(
|
|
147
|
+
framework: Framework,
|
|
148
|
+
templates: TemplateFile[],
|
|
149
|
+
): string[] {
|
|
150
|
+
if (!templateGlobs(framework) || templates.length === 0) return [];
|
|
151
|
+
const boundary: Readonly<Record<string, string>> = {
|
|
152
|
+
svelte:
|
|
153
|
+
"<script> blocks are not executed: runes, stores, reactive statements, lifecycle hooks and event handler bodies are compiled as template text, not run. Port that logic to moonshine signals.",
|
|
154
|
+
vue: "<script> blocks are not executed: the Composition API, reactive refs, computed properties and lifecycle hooks do not run. Port that logic to moonshine signals.",
|
|
155
|
+
astro:
|
|
156
|
+
"The `---` frontmatter is blanked, never executed: imports, top-level awaits, data fetching and `Astro.props` do not run. Only the markup below it is compiled.",
|
|
157
|
+
angular:
|
|
158
|
+
"The component class is not executed: constructors, decorators, DI, `@Input`/`@Output`, pipes and lifecycle hooks do not run. Only the template is compiled.",
|
|
159
|
+
};
|
|
160
|
+
const rejects: Readonly<Record<string, string>> = {
|
|
161
|
+
astro:
|
|
162
|
+
"Astro templates that use an imported component (`<Layout>`, any uppercase tag), `<slot />`, `{...spread}`, `transition:*` or `define:vars` are parse errors — the frontend compiles markup and resolves no modules. In a real Astro app most page files use components, so expect per-file failures rather than a whole-app adoption.",
|
|
163
|
+
angular:
|
|
164
|
+
"Angular templates that use `<ng-template>`, `<ng-content>`, `*ngSwitch`, `[ngStyle]`/`[style.x]`, `#templateRef`, `@switch`, `@defer` or `@empty` are parse errors.",
|
|
165
|
+
};
|
|
166
|
+
const notes: string[] = [boundary[framework]!];
|
|
167
|
+
if (rejects[framework]) notes.push(rejects[framework]!);
|
|
168
|
+
notes.push(
|
|
169
|
+
"Route modules embed the View IR produced at adopt time. Re-run `moonshine adopt --force` after editing a template.",
|
|
170
|
+
);
|
|
171
|
+
if (framework === "angular") {
|
|
172
|
+
notes.push(
|
|
173
|
+
"Angular has no file-based routing, so no component is mounted at a URL. The generated IR is compiled but unrouted until you write route modules that render it.",
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
for (const template of templates) {
|
|
177
|
+
if (!template.ok) {
|
|
178
|
+
notes.push(
|
|
179
|
+
`${template.file}: did not compile — ${template.error}. No route was generated for it; unsupported template constructs are parse errors, not silent drops.`,
|
|
180
|
+
);
|
|
181
|
+
} else if (!template.route) {
|
|
182
|
+
notes.push(
|
|
183
|
+
`${template.file}: compiled (${template.nodes} nodes) but maps to no URL, so it is not mounted. Its IR is in ${template.generated} — import that from a route module to render it.`,
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return notes;
|
|
188
|
+
}
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/** The changes adoption would make, and applying them. */
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { dirname, join, relative } from "node:path";
|
|
4
|
+
import { adapterFor, templateGlobs } from "./frameworks.js";
|
|
5
|
+
import type { AdoptChange, AdoptPlan, AdoptScan, Framework } from "./types.js";
|
|
6
|
+
import { RUNTIME_DEPS, readJson, toPosix } from "./scan.js";
|
|
7
|
+
import { planTemplateRoutes } from "./templates.js";
|
|
8
|
+
import { scanProject } from "./project.js";
|
|
9
|
+
import { detectMoonshineRoot, packagePath } from "../new.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Where the project will find `@tschk/moonshine-next` on disk. Only installed
|
|
13
|
+
* `node_modules` trees count: adopt normally runs before `bun install`, and
|
|
14
|
+
* `Bun.resolveSync` would then answer with whatever version happens to sit in
|
|
15
|
+
* the global module cache — a copy that can predate the adapter's current
|
|
16
|
+
* surface, leaving specifiers pointing at files that do not exist.
|
|
17
|
+
*/
|
|
18
|
+
function adapterDir(projectDir: string, pkg: string): string {
|
|
19
|
+
const parts = pkg.split("/");
|
|
20
|
+
let dir = projectDir;
|
|
21
|
+
for (;;) {
|
|
22
|
+
const candidate = join(dir, "node_modules", ...parts);
|
|
23
|
+
if (existsSync(candidate)) return candidate;
|
|
24
|
+
const parent = dirname(dir);
|
|
25
|
+
if (parent === dir) break;
|
|
26
|
+
dir = parent;
|
|
27
|
+
}
|
|
28
|
+
return join(projectDir, "node_modules", ...parts);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* `compilerOptions.paths` entries pointing every aliasable `next/*` specifier
|
|
33
|
+
* at a real file. Bun applies tsconfig paths in both `bun run` and `Bun.build`,
|
|
34
|
+
* but only for relative targets — a bare package specifier is not remapped.
|
|
35
|
+
*/
|
|
36
|
+
export function aliasPaths(
|
|
37
|
+
projectDir: string,
|
|
38
|
+
framework: Framework = "next-app",
|
|
39
|
+
): Record<string, string[]> {
|
|
40
|
+
const adapter = adapterFor(framework);
|
|
41
|
+
if (!adapter) return {};
|
|
42
|
+
const pkgDir = adapterDir(projectDir, adapter.pkg);
|
|
43
|
+
const srcDir = dirname(adapter.resolveAlias(adapter.reference)!);
|
|
44
|
+
const paths: Record<string, string[]> = {};
|
|
45
|
+
for (const specifier of Object.keys(adapter.aliases)) {
|
|
46
|
+
const source = adapter.resolveAlias(specifier);
|
|
47
|
+
if (!source) continue;
|
|
48
|
+
const file = join(pkgDir, "src", relative(srcDir, source));
|
|
49
|
+
const rel = toPosix(relative(projectDir, file));
|
|
50
|
+
paths[specifier] = [rel.startsWith(".") ? rel : `./${rel}`];
|
|
51
|
+
}
|
|
52
|
+
return paths;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function planTsconfig(scan: AdoptScan): AdoptChange {
|
|
56
|
+
const path = join(scan.projectDir, "tsconfig.json");
|
|
57
|
+
const existing =
|
|
58
|
+
readJson<{ compilerOptions?: Record<string, unknown> }>(path) ?? {};
|
|
59
|
+
const compilerOptions = { ...existing.compilerOptions };
|
|
60
|
+
const previous = (compilerOptions.paths ?? {}) as Record<string, string[]>;
|
|
61
|
+
const wanted = aliasPaths(scan.projectDir, scan.framework);
|
|
62
|
+
const paths = { ...previous, ...wanted };
|
|
63
|
+
compilerOptions.baseUrl = compilerOptions.baseUrl ?? ".";
|
|
64
|
+
compilerOptions.jsx = compilerOptions.jsx ?? "react-jsx";
|
|
65
|
+
compilerOptions.paths = paths;
|
|
66
|
+
|
|
67
|
+
const alreadyApplied = Object.entries(wanted).every(
|
|
68
|
+
([key, value]) => previous[key]?.[0] === value[0],
|
|
69
|
+
);
|
|
70
|
+
const adapter = adapterFor(scan.framework);
|
|
71
|
+
const added = Object.entries(wanted).filter(
|
|
72
|
+
([key, value]) => previous[key]?.[0] !== value[0],
|
|
73
|
+
);
|
|
74
|
+
const details: string[] = [];
|
|
75
|
+
if (existing.compilerOptions?.baseUrl === undefined) {
|
|
76
|
+
details.push(`compilerOptions.baseUrl = "."`);
|
|
77
|
+
}
|
|
78
|
+
if (existing.compilerOptions?.jsx === undefined) {
|
|
79
|
+
details.push(`compilerOptions.jsx = "react-jsx"`);
|
|
80
|
+
}
|
|
81
|
+
for (const [key, value] of added) {
|
|
82
|
+
details.push(`compilerOptions.paths["${key}"] = "${value[0]}"`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
file: "tsconfig.json",
|
|
87
|
+
action: existsSync(path) ? "modify" : "create",
|
|
88
|
+
details,
|
|
89
|
+
summary: !adapter
|
|
90
|
+
? "baseUrl and jsx set; this framework needs no import aliases"
|
|
91
|
+
: alreadyApplied
|
|
92
|
+
? `already aliases ${Object.keys(wanted).length} specifiers`
|
|
93
|
+
: `aliases ${Object.keys(wanted).length} specifiers to ${adapter.pkg}`,
|
|
94
|
+
alreadyApplied,
|
|
95
|
+
contents: JSON.stringify({ ...existing, compilerOptions }, null, 2) + "\n",
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function planConfig(scan: AdoptScan, force: boolean): AdoptChange {
|
|
100
|
+
const path = join(scan.projectDir, "moonshine.config.ts");
|
|
101
|
+
const exists = existsSync(path);
|
|
102
|
+
const entries: Record<string, unknown> = {
|
|
103
|
+
runtime: "bun",
|
|
104
|
+
adapter: "bun",
|
|
105
|
+
renderer: "react",
|
|
106
|
+
...(scan.routesDir && { routesDir: scan.routesDir }),
|
|
107
|
+
...(scan.convention && { convention: scan.convention }),
|
|
108
|
+
};
|
|
109
|
+
const contents = `import { defineConfig } from "@tschk/moonshine-framework";
|
|
110
|
+
|
|
111
|
+
export default defineConfig(${JSON.stringify(entries, null, 2)});
|
|
112
|
+
`;
|
|
113
|
+
const current = exists ? readFileSync(path, "utf8") : "";
|
|
114
|
+
const alreadyApplied = exists && (current === contents || !force);
|
|
115
|
+
return {
|
|
116
|
+
file: "moonshine.config.ts",
|
|
117
|
+
action: alreadyApplied ? "none" : exists ? "modify" : "create",
|
|
118
|
+
details: alreadyApplied
|
|
119
|
+
? []
|
|
120
|
+
: Object.entries(entries).map(
|
|
121
|
+
([key, value]) => `defineConfig.${key} = ${JSON.stringify(value)}`,
|
|
122
|
+
),
|
|
123
|
+
summary: alreadyApplied
|
|
124
|
+
? "already present, left untouched"
|
|
125
|
+
: `${exists ? "rewritten" : "created"} (routesDir ${scan.routesDir ?? "src/routes"}, convention ${scan.convention ?? "moonshine"})`,
|
|
126
|
+
alreadyApplied,
|
|
127
|
+
...(alreadyApplied ? {} : { contents }),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function planPackageJson(scan: AdoptScan): AdoptChange {
|
|
132
|
+
const path = join(scan.projectDir, "package.json");
|
|
133
|
+
const pkg =
|
|
134
|
+
readJson<{
|
|
135
|
+
dependencies?: Record<string, string>;
|
|
136
|
+
scripts?: Record<string, string>;
|
|
137
|
+
}>(path) ?? {};
|
|
138
|
+
const root = detectMoonshineRoot();
|
|
139
|
+
const version = `^${cliVersion()}`;
|
|
140
|
+
|
|
141
|
+
const dependencies = { ...pkg.dependencies };
|
|
142
|
+
const added: string[] = [];
|
|
143
|
+
const adapter = adapterFor(scan.framework);
|
|
144
|
+
const template = templateGlobs(scan.framework) !== undefined;
|
|
145
|
+
for (const name of [
|
|
146
|
+
...RUNTIME_DEPS,
|
|
147
|
+
...(adapter ? [adapter.pkg] : []),
|
|
148
|
+
...(template ? ["@tschk/crepus-moonshine"] : []),
|
|
149
|
+
] as string[]) {
|
|
150
|
+
if (dependencies[name]) continue;
|
|
151
|
+
const spec = packagePath(name, root, version);
|
|
152
|
+
if (!spec) continue;
|
|
153
|
+
dependencies[name] = spec;
|
|
154
|
+
added.push(name);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const scripts = { ...pkg.scripts };
|
|
158
|
+
const repointed: string[] = [];
|
|
159
|
+
const mapping: Record<string, string> = {
|
|
160
|
+
dev: "moonshine dev",
|
|
161
|
+
build: "moonshine build",
|
|
162
|
+
start: "moonshine preview",
|
|
163
|
+
};
|
|
164
|
+
for (const [key, value] of Object.entries(mapping)) {
|
|
165
|
+
const current = scripts[key];
|
|
166
|
+
if (
|
|
167
|
+
current !== undefined &&
|
|
168
|
+
!/^(next|vite|remix|react-router|waku)(\s|$)/.test(current)
|
|
169
|
+
) {
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
if (current === value) continue;
|
|
173
|
+
scripts[key] = value;
|
|
174
|
+
repointed.push(key);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const alreadyApplied = added.length === 0 && repointed.length === 0;
|
|
178
|
+
const parts: string[] = [];
|
|
179
|
+
if (added.length) parts.push(`+${added.length} dependencies`);
|
|
180
|
+
if (repointed.length)
|
|
181
|
+
parts.push(`scripts ${repointed.join(", ")} → moonshine`);
|
|
182
|
+
|
|
183
|
+
return {
|
|
184
|
+
file: "package.json",
|
|
185
|
+
action: existsSync(path) ? "modify" : "create",
|
|
186
|
+
details: [
|
|
187
|
+
...added.map(
|
|
188
|
+
(name) => `dependencies["${name}"] = "${dependencies[name]}"`,
|
|
189
|
+
),
|
|
190
|
+
...repointed.map((key) => `scripts["${key}"] = "${scripts[key]}"`),
|
|
191
|
+
],
|
|
192
|
+
summary: alreadyApplied ? "already adopted" : parts.join(", "),
|
|
193
|
+
alreadyApplied,
|
|
194
|
+
contents: JSON.stringify({ ...pkg, dependencies, scripts }, null, 2) + "\n",
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function cliVersion(): string {
|
|
199
|
+
const pkg = readJson<{ version?: string }>(
|
|
200
|
+
join(import.meta.dir, "..", "package.json"),
|
|
201
|
+
);
|
|
202
|
+
return pkg?.version ?? "0.3.2";
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export async function planAdoption(
|
|
206
|
+
projectDir: string,
|
|
207
|
+
options: { force?: boolean } = {},
|
|
208
|
+
): Promise<AdoptPlan> {
|
|
209
|
+
const scan = await scanProject(projectDir);
|
|
210
|
+
return {
|
|
211
|
+
scan,
|
|
212
|
+
changes: [
|
|
213
|
+
planTsconfig(scan),
|
|
214
|
+
planConfig(scan, options.force ?? false),
|
|
215
|
+
planPackageJson(scan),
|
|
216
|
+
...(await planTemplateRoutes(scan)),
|
|
217
|
+
],
|
|
218
|
+
// Aliasing covers every specifier the adapter implements, so nothing in the
|
|
219
|
+
// user's source is edited. Unaliasable specifiers are reported, not rewritten,
|
|
220
|
+
// because there is no moonshine implementation to rewrite them to.
|
|
221
|
+
rewritten: [],
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export function applyPlan(plan: AdoptPlan): void {
|
|
226
|
+
for (const change of plan.changes) {
|
|
227
|
+
if (change.contents === undefined) continue;
|
|
228
|
+
const path = join(plan.scan.projectDir, change.file);
|
|
229
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
230
|
+
writeFileSync(path, change.contents);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/** One scan of a project directory, composed from the pieces around it. */
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { join, relative, resolve } from "node:path";
|
|
4
|
+
import { adapterFor, templateGlobs } from "./frameworks.js";
|
|
5
|
+
import type { AdoptScan } from "./types.js";
|
|
6
|
+
import {
|
|
7
|
+
detectFramework,
|
|
8
|
+
findHostImports,
|
|
9
|
+
readJson,
|
|
10
|
+
sourceFiles,
|
|
11
|
+
toPosix,
|
|
12
|
+
type PackageJson,
|
|
13
|
+
} from "./scan.js";
|
|
14
|
+
import { compileTemplates } from "./templates.js";
|
|
15
|
+
import { findConventions } from "./conventions.js";
|
|
16
|
+
import { ALIAS_CAVEATS, findManualWork, templateManualWork } from "./gaps.js";
|
|
17
|
+
|
|
18
|
+
export async function scanProject(projectDir: string): Promise<AdoptScan> {
|
|
19
|
+
const dir = resolve(projectDir);
|
|
20
|
+
const pkg = readJson<PackageJson>(join(dir, "package.json"));
|
|
21
|
+
const files = sourceFiles(dir);
|
|
22
|
+
const detected = detectFramework(dir, pkg);
|
|
23
|
+
const imports = await findHostImports(
|
|
24
|
+
dir,
|
|
25
|
+
files,
|
|
26
|
+
adapterFor(detected.framework),
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
const globs = templateGlobs(detected.framework);
|
|
30
|
+
const templates = globs
|
|
31
|
+
? await compileTemplates(dir, detected.framework, globs)
|
|
32
|
+
: [];
|
|
33
|
+
|
|
34
|
+
const routes: { path: string; file: string }[] = [];
|
|
35
|
+
if (
|
|
36
|
+
detected.routesDir &&
|
|
37
|
+
detected.convention &&
|
|
38
|
+
existsSync(join(dir, detected.routesDir))
|
|
39
|
+
) {
|
|
40
|
+
const { discoverRoutes } = await import("@tschk/moonshine-compiler");
|
|
41
|
+
const discovered = await discoverRoutes({
|
|
42
|
+
routesDir: join(dir, detected.routesDir),
|
|
43
|
+
convention: detected.convention,
|
|
44
|
+
});
|
|
45
|
+
for (const route of discovered) {
|
|
46
|
+
routes.push({
|
|
47
|
+
path: route.path,
|
|
48
|
+
file: toPosix(relative(dir, route.file)),
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
projectDir: dir,
|
|
55
|
+
framework: detected.framework,
|
|
56
|
+
...(detected.routesDir && { routesDir: detected.routesDir }),
|
|
57
|
+
...(detected.convention && { convention: detected.convention }),
|
|
58
|
+
routes,
|
|
59
|
+
templates,
|
|
60
|
+
imports,
|
|
61
|
+
conventions: findConventions(dir, files),
|
|
62
|
+
manual: [
|
|
63
|
+
...templateManualWork(detected.framework, templates),
|
|
64
|
+
...(await findManualWork(dir, files, detected)),
|
|
65
|
+
...[...new Set(imports.map((i) => i.specifier))]
|
|
66
|
+
.sort()
|
|
67
|
+
.map((s) => ALIAS_CAVEATS[s])
|
|
68
|
+
.filter((note): note is string => note !== undefined),
|
|
69
|
+
],
|
|
70
|
+
};
|
|
71
|
+
}
|