@prisma/compute-sdk 0.38.0 → 0.39.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -20
- package/dist/config/detect-app.d.ts +4 -0
- package/dist/config/detect-app.d.ts.map +1 -1
- package/dist/config/detect-app.js +27 -8
- package/dist/config/detect-app.js.map +1 -1
- package/dist/config/detect-directory.d.ts +14 -0
- package/dist/config/detect-directory.d.ts.map +1 -0
- package/dist/config/detect-directory.js +80 -0
- package/dist/config/detect-directory.js.map +1 -0
- package/dist/config/detect-repository.d.ts +33 -0
- package/dist/config/detect-repository.d.ts.map +1 -0
- package/dist/config/detect-repository.js +419 -0
- package/dist/config/detect-repository.js.map +1 -0
- package/dist/config/index.d.ts +1 -0
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +1 -0
- package/dist/config/index.js.map +1 -1
- package/package.json +12 -2
- package/src/config/detect-app.ts +38 -8
- package/src/config/detect-directory.ts +112 -0
- package/src/config/detect-repository.ts +581 -0
- package/src/config/index.ts +6 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { access, readFile } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
type ComputeAppManifest,
|
|
6
|
+
computeAppCandidateFilePaths,
|
|
7
|
+
type DetectedComputeApp,
|
|
8
|
+
detectComputeApp,
|
|
9
|
+
} from "./detect-app.ts";
|
|
10
|
+
import { frameworkByKey } from "./frameworks.ts";
|
|
11
|
+
|
|
12
|
+
export interface DetectComputeAppFromDirectoryInput {
|
|
13
|
+
appPath: string;
|
|
14
|
+
signal?: AbortSignal;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface DetectedComputeDirectoryApp extends DetectedComputeApp {
|
|
18
|
+
configFile: {
|
|
19
|
+
path: string;
|
|
20
|
+
standaloneOutput: boolean;
|
|
21
|
+
} | null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Detects one Compute app from a local directory. */
|
|
25
|
+
export async function detectComputeAppFromDirectory(
|
|
26
|
+
input: DetectComputeAppFromDirectoryInput,
|
|
27
|
+
): Promise<DetectedComputeDirectoryApp | null> {
|
|
28
|
+
const manifest = await readManifest(input.appPath, input.signal);
|
|
29
|
+
const candidates = computeAppCandidateFilePaths(manifest);
|
|
30
|
+
const existingCandidates = await Promise.all(
|
|
31
|
+
candidates.map(async (candidate) =>
|
|
32
|
+
(await fileExists(input.appPath, candidate, input.signal))
|
|
33
|
+
? candidate
|
|
34
|
+
: null,
|
|
35
|
+
),
|
|
36
|
+
);
|
|
37
|
+
const filePaths = new Set(
|
|
38
|
+
existingCandidates.filter((candidate) => candidate !== null),
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
const detected = detectComputeApp({ root: "", manifest, filePaths });
|
|
42
|
+
if (!detected) return null;
|
|
43
|
+
|
|
44
|
+
const descriptor = frameworkByKey(detected.framework);
|
|
45
|
+
const configPath = descriptor.detectConfigFiles.find((candidate) =>
|
|
46
|
+
filePaths.has(candidate),
|
|
47
|
+
);
|
|
48
|
+
if (!configPath) {
|
|
49
|
+
return { ...detected, configFile: null };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let content: string;
|
|
53
|
+
try {
|
|
54
|
+
content = await readFile(path.join(input.appPath, configPath), {
|
|
55
|
+
encoding: "utf8",
|
|
56
|
+
signal: input.signal,
|
|
57
|
+
});
|
|
58
|
+
} catch (error) {
|
|
59
|
+
if (input.signal?.aborted) throw error;
|
|
60
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
|
|
61
|
+
return { ...detected, configFile: null };
|
|
62
|
+
}
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
...detected,
|
|
67
|
+
configFile: {
|
|
68
|
+
path: configPath,
|
|
69
|
+
standaloneOutput:
|
|
70
|
+
detected.framework === "nextjs" &&
|
|
71
|
+
/\boutput\s*:\s*["'`]standalone["'`]/.test(content),
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function readManifest(
|
|
77
|
+
appPath: string,
|
|
78
|
+
signal?: AbortSignal,
|
|
79
|
+
): Promise<ComputeAppManifest> {
|
|
80
|
+
signal?.throwIfAborted();
|
|
81
|
+
try {
|
|
82
|
+
const content = await readFile(path.join(appPath, "package.json"), {
|
|
83
|
+
encoding: "utf8",
|
|
84
|
+
signal,
|
|
85
|
+
});
|
|
86
|
+
const parsed = JSON.parse(content) as unknown;
|
|
87
|
+
return parsed && typeof parsed === "object"
|
|
88
|
+
? (parsed as ComputeAppManifest)
|
|
89
|
+
: {};
|
|
90
|
+
} catch (error) {
|
|
91
|
+
if (signal?.aborted) throw error;
|
|
92
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return {};
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function fileExists(
|
|
98
|
+
appPath: string,
|
|
99
|
+
relativePath: string,
|
|
100
|
+
signal?: AbortSignal,
|
|
101
|
+
): Promise<boolean> {
|
|
102
|
+
signal?.throwIfAborted();
|
|
103
|
+
try {
|
|
104
|
+
await access(path.join(appPath, relativePath));
|
|
105
|
+
signal?.throwIfAborted();
|
|
106
|
+
return true;
|
|
107
|
+
} catch (error) {
|
|
108
|
+
if (signal?.aborted) throw error;
|
|
109
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return false;
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,581 @@
|
|
|
1
|
+
import picomatch from "picomatch";
|
|
2
|
+
import { parse as parseYaml } from "yaml";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
type ComputeAppManifest,
|
|
6
|
+
type DetectedComputeApp,
|
|
7
|
+
detectComputeApp,
|
|
8
|
+
} from "./detect-app.ts";
|
|
9
|
+
import { type FrameworkBuildType, frameworkByKey } from "./frameworks.ts";
|
|
10
|
+
import type { ComputeFramework } from "./types.ts";
|
|
11
|
+
|
|
12
|
+
const MAX_PACKAGE_MANIFESTS = 80;
|
|
13
|
+
const PACKAGE_FETCH_BATCH_SIZE = 10;
|
|
14
|
+
const IGNORED_PATH_SEGMENTS = new Set([
|
|
15
|
+
".git",
|
|
16
|
+
".next",
|
|
17
|
+
".output",
|
|
18
|
+
"build",
|
|
19
|
+
"dist",
|
|
20
|
+
"node_modules",
|
|
21
|
+
"vendor",
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
interface ParsedManifest {
|
|
25
|
+
path: string;
|
|
26
|
+
root: string;
|
|
27
|
+
value: ComputeAppManifest;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface DetectComputeRepositoryInput {
|
|
31
|
+
repositoryName: string;
|
|
32
|
+
filePaths: Iterable<string>;
|
|
33
|
+
readFile: (path: string) => Promise<string | null>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface DetectedComputeRepositoryApp {
|
|
37
|
+
id: string;
|
|
38
|
+
target: string;
|
|
39
|
+
name: string;
|
|
40
|
+
root: string;
|
|
41
|
+
framework: ComputeFramework;
|
|
42
|
+
frameworkName: string;
|
|
43
|
+
httpPort: number;
|
|
44
|
+
entry: string;
|
|
45
|
+
build: {
|
|
46
|
+
command: string | null;
|
|
47
|
+
outputDirectory: string;
|
|
48
|
+
entrypoint: string;
|
|
49
|
+
};
|
|
50
|
+
inferredFrom: string[];
|
|
51
|
+
reviewRequired: boolean;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface DetectComputeRepositoryResult {
|
|
55
|
+
apps: DetectedComputeRepositoryApp[];
|
|
56
|
+
warnings: string[];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Detects every deployable app in a remote repository snapshot, including
|
|
61
|
+
* workspace discovery, root-app ownership, and inferred build settings.
|
|
62
|
+
*/
|
|
63
|
+
export async function detectComputeRepository(
|
|
64
|
+
input: DetectComputeRepositoryInput,
|
|
65
|
+
): Promise<DetectComputeRepositoryResult> {
|
|
66
|
+
const filePaths = new Set(input.filePaths);
|
|
67
|
+
const warnings: string[] = [];
|
|
68
|
+
const manifestPaths = [...filePaths]
|
|
69
|
+
.filter(
|
|
70
|
+
(path) =>
|
|
71
|
+
(path === "package.json" || path.endsWith("/package.json")) &&
|
|
72
|
+
!path.split("/").some((segment) => IGNORED_PATH_SEGMENTS.has(segment)),
|
|
73
|
+
)
|
|
74
|
+
.sort((a, b) => pathDepth(a) - pathDepth(b) || a.localeCompare(b));
|
|
75
|
+
|
|
76
|
+
if (manifestPaths.length > MAX_PACKAGE_MANIFESTS) {
|
|
77
|
+
warnings.push(
|
|
78
|
+
`Only the first ${MAX_PACKAGE_MANIFESTS} package manifests were inspected.`,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const manifests: ParsedManifest[] = [];
|
|
83
|
+
for (
|
|
84
|
+
let offset = 0;
|
|
85
|
+
offset < Math.min(manifestPaths.length, MAX_PACKAGE_MANIFESTS);
|
|
86
|
+
offset += PACKAGE_FETCH_BATCH_SIZE
|
|
87
|
+
) {
|
|
88
|
+
const batch = manifestPaths.slice(
|
|
89
|
+
offset,
|
|
90
|
+
offset + PACKAGE_FETCH_BATCH_SIZE,
|
|
91
|
+
);
|
|
92
|
+
const fetched = await Promise.all(
|
|
93
|
+
batch.map(async (path) => {
|
|
94
|
+
try {
|
|
95
|
+
return { path, content: await input.readFile(path) };
|
|
96
|
+
} catch {
|
|
97
|
+
warnings.push(`Skipped unreadable ${path}.`);
|
|
98
|
+
return { path, content: undefined };
|
|
99
|
+
}
|
|
100
|
+
}),
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
for (const item of fetched) {
|
|
104
|
+
if (item.content === undefined) continue;
|
|
105
|
+
if (item.content === null) {
|
|
106
|
+
warnings.push(`Skipped unavailable ${item.path}.`);
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
try {
|
|
110
|
+
const value: unknown = JSON.parse(item.content);
|
|
111
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
112
|
+
warnings.push(`Skipped invalid package manifest in ${item.path}.`);
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
manifests.push({
|
|
116
|
+
path: item.path,
|
|
117
|
+
root: directoryName(item.path),
|
|
118
|
+
value: value as ComputeAppManifest,
|
|
119
|
+
});
|
|
120
|
+
} catch {
|
|
121
|
+
warnings.push(`Skipped invalid JSON in ${item.path}.`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const rootManifest = manifests.find((manifest) => manifest.root === "");
|
|
127
|
+
const workspacePatterns = await readWorkspacePatterns({
|
|
128
|
+
input,
|
|
129
|
+
filePaths,
|
|
130
|
+
rootManifest,
|
|
131
|
+
warnings,
|
|
132
|
+
});
|
|
133
|
+
const candidateManifests = manifests.filter(
|
|
134
|
+
(manifest) =>
|
|
135
|
+
manifest.root === "" ||
|
|
136
|
+
workspacePatterns.length === 0 ||
|
|
137
|
+
matchesWorkspace(manifest.root, workspacePatterns),
|
|
138
|
+
);
|
|
139
|
+
const allDetected = candidateManifests
|
|
140
|
+
.map((manifest) => {
|
|
141
|
+
const detectedApp = detectComputeApp({
|
|
142
|
+
root: manifest.root,
|
|
143
|
+
manifest: manifest.value,
|
|
144
|
+
filePaths,
|
|
145
|
+
});
|
|
146
|
+
return detectedApp ? { manifest, detectedApp } : null;
|
|
147
|
+
})
|
|
148
|
+
.filter((item): item is NonNullable<typeof item> => item !== null);
|
|
149
|
+
const nestedDetected = allDetected.filter(
|
|
150
|
+
(item) => item.manifest.root !== "",
|
|
151
|
+
);
|
|
152
|
+
const nestedAppRoots = nestedDetected.map((item) => item.manifest.root);
|
|
153
|
+
const detected = allDetected.filter(
|
|
154
|
+
(item) =>
|
|
155
|
+
item.manifest.root !== "" ||
|
|
156
|
+
nestedDetected.length === 0 ||
|
|
157
|
+
hasDistinctRootAppSignal(
|
|
158
|
+
item.manifest,
|
|
159
|
+
item.detectedApp,
|
|
160
|
+
filePaths,
|
|
161
|
+
nestedAppRoots,
|
|
162
|
+
),
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
if (detected.length !== allDetected.length) {
|
|
166
|
+
warnings.push(
|
|
167
|
+
"The root package did not identify an application distinct from the detected workspace apps, so it was not added as a separate deploy target.",
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const selected =
|
|
172
|
+
detected.length > 0
|
|
173
|
+
? detected
|
|
174
|
+
: [
|
|
175
|
+
{
|
|
176
|
+
manifest: rootManifest ??
|
|
177
|
+
candidateManifests[0] ?? {
|
|
178
|
+
path: "package.json",
|
|
179
|
+
root: "",
|
|
180
|
+
value: {},
|
|
181
|
+
},
|
|
182
|
+
detectedApp: null,
|
|
183
|
+
},
|
|
184
|
+
];
|
|
185
|
+
|
|
186
|
+
if (detected.length === 0) {
|
|
187
|
+
warnings.push(
|
|
188
|
+
"No supported framework signal was found. Review the Bun entrypoint and build settings.",
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const apps = selected.map((item) =>
|
|
193
|
+
inferApp({
|
|
194
|
+
repositoryName: input.repositoryName,
|
|
195
|
+
filePaths,
|
|
196
|
+
rootManifest,
|
|
197
|
+
manifest: item.manifest,
|
|
198
|
+
detectedApp: item.detectedApp,
|
|
199
|
+
}),
|
|
200
|
+
);
|
|
201
|
+
uniquifyTargets(apps);
|
|
202
|
+
|
|
203
|
+
return { apps, warnings };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async function readWorkspacePatterns(args: {
|
|
207
|
+
input: DetectComputeRepositoryInput;
|
|
208
|
+
filePaths: Set<string>;
|
|
209
|
+
rootManifest: ParsedManifest | undefined;
|
|
210
|
+
warnings: string[];
|
|
211
|
+
}): Promise<string[]> {
|
|
212
|
+
const fromPackageJson = workspacePatternsFromPackageJson(
|
|
213
|
+
args.rootManifest?.value.workspaces,
|
|
214
|
+
);
|
|
215
|
+
if (!args.filePaths.has("pnpm-workspace.yaml")) {
|
|
216
|
+
return fromPackageJson;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
let content: string | null;
|
|
220
|
+
try {
|
|
221
|
+
content = await args.input.readFile("pnpm-workspace.yaml");
|
|
222
|
+
} catch {
|
|
223
|
+
args.warnings.push("Skipped unreadable pnpm-workspace.yaml.");
|
|
224
|
+
return fromPackageJson;
|
|
225
|
+
}
|
|
226
|
+
if (content === null) return fromPackageJson;
|
|
227
|
+
|
|
228
|
+
try {
|
|
229
|
+
const parsed = parseYaml(content) as { packages?: unknown } | null;
|
|
230
|
+
const fromPnpm = Array.isArray(parsed?.packages)
|
|
231
|
+
? parsed.packages.filter(
|
|
232
|
+
(value): value is string => typeof value === "string",
|
|
233
|
+
)
|
|
234
|
+
: [];
|
|
235
|
+
return [...new Set([...fromPackageJson, ...fromPnpm])];
|
|
236
|
+
} catch {
|
|
237
|
+
args.warnings.push("Skipped invalid YAML in pnpm-workspace.yaml.");
|
|
238
|
+
return fromPackageJson;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function workspacePatternsFromPackageJson(workspaces: unknown): string[] {
|
|
243
|
+
if (Array.isArray(workspaces)) {
|
|
244
|
+
return workspaces.filter(
|
|
245
|
+
(value): value is string => typeof value === "string",
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
if (
|
|
249
|
+
workspaces &&
|
|
250
|
+
typeof workspaces === "object" &&
|
|
251
|
+
"packages" in workspaces &&
|
|
252
|
+
Array.isArray(workspaces.packages)
|
|
253
|
+
) {
|
|
254
|
+
return workspaces.packages.filter(
|
|
255
|
+
(value): value is string => typeof value === "string",
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
return [];
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function matchesWorkspace(root: string, patterns: string[]): boolean {
|
|
262
|
+
const included = patterns
|
|
263
|
+
.filter((pattern) => !pattern.startsWith("!"))
|
|
264
|
+
.some((pattern) => picomatch(pattern, { dot: true })(root));
|
|
265
|
+
const excluded = patterns
|
|
266
|
+
.filter((pattern) => pattern.startsWith("!"))
|
|
267
|
+
.some((pattern) => picomatch(pattern.slice(1), { dot: true })(root));
|
|
268
|
+
return included && !excluded;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function hasDistinctRootAppSignal(
|
|
272
|
+
manifest: ParsedManifest,
|
|
273
|
+
detectedApp: DetectedComputeApp,
|
|
274
|
+
filePaths: Set<string>,
|
|
275
|
+
nestedAppRoots: string[],
|
|
276
|
+
): boolean {
|
|
277
|
+
const descriptor = frameworkByKey(detectedApp.framework);
|
|
278
|
+
if (
|
|
279
|
+
descriptor.detectConfigFiles.some((fileName) => filePaths.has(fileName))
|
|
280
|
+
) {
|
|
281
|
+
return true;
|
|
282
|
+
}
|
|
283
|
+
const entrypoint = detectedApp.entrypoint;
|
|
284
|
+
if (
|
|
285
|
+
entrypoint &&
|
|
286
|
+
!nestedAppRoots.some((root) => isPathInside(entrypoint, root))
|
|
287
|
+
) {
|
|
288
|
+
return true;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const sourcePaths = [...filePaths].filter(
|
|
292
|
+
(path) => !nestedAppRoots.some((root) => isPathInside(path, root)),
|
|
293
|
+
);
|
|
294
|
+
switch (detectedApp.buildType) {
|
|
295
|
+
case "nextjs":
|
|
296
|
+
if (
|
|
297
|
+
sourcePaths.some((path) =>
|
|
298
|
+
["app/", "pages/", "src/app/", "src/pages/"].some((prefix) =>
|
|
299
|
+
path.startsWith(prefix),
|
|
300
|
+
),
|
|
301
|
+
)
|
|
302
|
+
) {
|
|
303
|
+
return true;
|
|
304
|
+
}
|
|
305
|
+
break;
|
|
306
|
+
case "nuxt":
|
|
307
|
+
if (
|
|
308
|
+
filePaths.has("app.vue") ||
|
|
309
|
+
sourcePaths.some(
|
|
310
|
+
(path) => path.startsWith("pages/") || path.startsWith("server/"),
|
|
311
|
+
)
|
|
312
|
+
) {
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
315
|
+
break;
|
|
316
|
+
case "astro":
|
|
317
|
+
if (sourcePaths.some((path) => path.startsWith("src/pages/"))) {
|
|
318
|
+
return true;
|
|
319
|
+
}
|
|
320
|
+
break;
|
|
321
|
+
case "nestjs":
|
|
322
|
+
if (filePaths.has("src/main.ts") || filePaths.has("src/main.js")) {
|
|
323
|
+
return true;
|
|
324
|
+
}
|
|
325
|
+
break;
|
|
326
|
+
case "tanstack-start":
|
|
327
|
+
if (
|
|
328
|
+
sourcePaths.some(
|
|
329
|
+
(path) =>
|
|
330
|
+
path.startsWith("app/routes/") || path.startsWith("src/routes/"),
|
|
331
|
+
)
|
|
332
|
+
) {
|
|
333
|
+
return true;
|
|
334
|
+
}
|
|
335
|
+
break;
|
|
336
|
+
case "custom":
|
|
337
|
+
case "bun":
|
|
338
|
+
break;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
const buildScript = readBuildScript(manifest.value.scripts);
|
|
342
|
+
if (!buildScript) return false;
|
|
343
|
+
switch (detectedApp.buildType) {
|
|
344
|
+
case "nextjs":
|
|
345
|
+
return /(^|\s)next\s+build(?:\s|$)/.test(buildScript);
|
|
346
|
+
case "nuxt":
|
|
347
|
+
return /(^|\s)(?:nuxt|nuxi)\s+build(?:\s|$)/.test(buildScript);
|
|
348
|
+
case "astro":
|
|
349
|
+
return /(^|\s)astro\s+build(?:\s|$)/.test(buildScript);
|
|
350
|
+
case "nestjs":
|
|
351
|
+
return /(^|\s)nest\s+build(?:\s|$)/.test(buildScript);
|
|
352
|
+
case "tanstack-start":
|
|
353
|
+
return /(^|\s)vite\s+build(?:\s|$)/.test(buildScript);
|
|
354
|
+
case "custom":
|
|
355
|
+
case "bun":
|
|
356
|
+
return false;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function inferApp(args: {
|
|
361
|
+
repositoryName: string;
|
|
362
|
+
filePaths: Set<string>;
|
|
363
|
+
rootManifest: ParsedManifest | undefined;
|
|
364
|
+
manifest: ParsedManifest;
|
|
365
|
+
detectedApp: DetectedComputeApp | null;
|
|
366
|
+
}): DetectedComputeRepositoryApp {
|
|
367
|
+
const packageName =
|
|
368
|
+
typeof args.manifest.value.name === "string"
|
|
369
|
+
? args.manifest.value.name
|
|
370
|
+
: undefined;
|
|
371
|
+
const target = normalizeTarget(
|
|
372
|
+
packageName ?? lastPathSegment(args.manifest.root) ?? args.repositoryName,
|
|
373
|
+
);
|
|
374
|
+
const buildScript = readBuildScript(args.manifest.value.scripts);
|
|
375
|
+
const packageManager = resolveRemotePackageManager({
|
|
376
|
+
manifest: args.manifest,
|
|
377
|
+
rootManifest: args.rootManifest,
|
|
378
|
+
filePaths: args.filePaths,
|
|
379
|
+
});
|
|
380
|
+
const descriptor = frameworkByKey(args.detectedApp?.framework ?? "bun");
|
|
381
|
+
const inferredBuild = inferBuildSettings({
|
|
382
|
+
buildType: args.detectedApp?.buildType ?? descriptor.buildType,
|
|
383
|
+
buildScript,
|
|
384
|
+
packageManager,
|
|
385
|
+
});
|
|
386
|
+
const entry = args.detectedApp?.entrypoint ?? "";
|
|
387
|
+
const detectionSource =
|
|
388
|
+
args.detectedApp?.evidence.kind === "dependency"
|
|
389
|
+
? `${args.detectedApp.evidence.path} dependency ${args.detectedApp.evidence.value}`
|
|
390
|
+
: (args.detectedApp?.evidence.path ?? "Bun fallback");
|
|
391
|
+
|
|
392
|
+
return {
|
|
393
|
+
id: args.manifest.root || "root",
|
|
394
|
+
target,
|
|
395
|
+
name: target,
|
|
396
|
+
root: args.manifest.root ? `./${args.manifest.root}` : "./",
|
|
397
|
+
framework: descriptor.key,
|
|
398
|
+
frameworkName: descriptor.displayName,
|
|
399
|
+
httpPort: descriptor.defaultHttpPort,
|
|
400
|
+
entry,
|
|
401
|
+
build: {
|
|
402
|
+
command: inferredBuild.command,
|
|
403
|
+
outputDirectory: inferredBuild.outputDirectory,
|
|
404
|
+
entrypoint: inferredBuild.entrypoint,
|
|
405
|
+
},
|
|
406
|
+
inferredFrom: [
|
|
407
|
+
detectionSource,
|
|
408
|
+
buildScript
|
|
409
|
+
? `${args.manifest.path} scripts.build`
|
|
410
|
+
: inferredBuild.source,
|
|
411
|
+
].filter((value, index, values) => values.indexOf(value) === index),
|
|
412
|
+
reviewRequired:
|
|
413
|
+
args.detectedApp === null || (descriptor.usesEntrypoint && !entry),
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function inferBuildSettings(args: {
|
|
418
|
+
buildType: FrameworkBuildType;
|
|
419
|
+
buildScript: string | null;
|
|
420
|
+
packageManager: string | null;
|
|
421
|
+
}): DetectedComputeRepositoryApp["build"] & { source: string } {
|
|
422
|
+
const scriptCommand = args.buildScript
|
|
423
|
+
? args.packageManager
|
|
424
|
+
? `${args.packageManager} run build`
|
|
425
|
+
: args.buildScript
|
|
426
|
+
: null;
|
|
427
|
+
switch (args.buildType) {
|
|
428
|
+
case "nextjs":
|
|
429
|
+
return {
|
|
430
|
+
command: scriptCommand ?? "next build",
|
|
431
|
+
outputDirectory: "",
|
|
432
|
+
entrypoint: "",
|
|
433
|
+
source: "Next.js build strategy",
|
|
434
|
+
};
|
|
435
|
+
case "nuxt":
|
|
436
|
+
return {
|
|
437
|
+
command: scriptCommand ?? "nuxt build",
|
|
438
|
+
outputDirectory: ".output",
|
|
439
|
+
entrypoint: "",
|
|
440
|
+
source: "Nuxt defaults",
|
|
441
|
+
};
|
|
442
|
+
case "astro":
|
|
443
|
+
return {
|
|
444
|
+
command: scriptCommand ?? "astro build",
|
|
445
|
+
outputDirectory: "dist",
|
|
446
|
+
entrypoint: "",
|
|
447
|
+
source: "Astro defaults",
|
|
448
|
+
};
|
|
449
|
+
case "nestjs":
|
|
450
|
+
return {
|
|
451
|
+
command: scriptCommand ?? "nest build",
|
|
452
|
+
outputDirectory: "dist",
|
|
453
|
+
entrypoint: "",
|
|
454
|
+
source: "NestJS defaults",
|
|
455
|
+
};
|
|
456
|
+
case "tanstack-start":
|
|
457
|
+
return {
|
|
458
|
+
command: scriptCommand ?? "vite build",
|
|
459
|
+
outputDirectory: ".output",
|
|
460
|
+
entrypoint: "",
|
|
461
|
+
source: "TanStack Start defaults",
|
|
462
|
+
};
|
|
463
|
+
case "custom":
|
|
464
|
+
return {
|
|
465
|
+
command: scriptCommand,
|
|
466
|
+
outputDirectory: "dist",
|
|
467
|
+
entrypoint: "server.js",
|
|
468
|
+
source: "Custom defaults",
|
|
469
|
+
};
|
|
470
|
+
case "bun":
|
|
471
|
+
return {
|
|
472
|
+
command: scriptCommand,
|
|
473
|
+
outputDirectory: ".",
|
|
474
|
+
entrypoint: "",
|
|
475
|
+
source: "Bun defaults",
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
function resolveRemotePackageManager(args: {
|
|
481
|
+
manifest: ParsedManifest;
|
|
482
|
+
rootManifest: ParsedManifest | undefined;
|
|
483
|
+
filePaths: Set<string>;
|
|
484
|
+
}): string | null {
|
|
485
|
+
const fromField = packageManagerFromField(args.manifest.value.packageManager);
|
|
486
|
+
if (fromField) return fromField;
|
|
487
|
+
const fromRoot = packageManagerFromField(
|
|
488
|
+
args.rootManifest?.value.packageManager,
|
|
489
|
+
);
|
|
490
|
+
if (fromRoot) return fromRoot;
|
|
491
|
+
|
|
492
|
+
for (const root of pathLineage(args.manifest.root)) {
|
|
493
|
+
if (
|
|
494
|
+
args.filePaths.has(joinRepoPath(root, "bun.lock")) ||
|
|
495
|
+
args.filePaths.has(joinRepoPath(root, "bun.lockb"))
|
|
496
|
+
) {
|
|
497
|
+
return "bun";
|
|
498
|
+
}
|
|
499
|
+
if (args.filePaths.has(joinRepoPath(root, "pnpm-lock.yaml"))) return "pnpm";
|
|
500
|
+
if (args.filePaths.has(joinRepoPath(root, "yarn.lock"))) return "yarn";
|
|
501
|
+
if (args.filePaths.has(joinRepoPath(root, "package-lock.json"))) {
|
|
502
|
+
return "npm";
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
return null;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
function packageManagerFromField(value: unknown): string | null {
|
|
509
|
+
if (typeof value !== "string") return null;
|
|
510
|
+
const name = value.split("@", 1)[0];
|
|
511
|
+
return name === "bun" || name === "pnpm" || name === "yarn" || name === "npm"
|
|
512
|
+
? name
|
|
513
|
+
: null;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
function readBuildScript(scripts: unknown): string | null {
|
|
517
|
+
if (!scripts || typeof scripts !== "object" || !("build" in scripts)) {
|
|
518
|
+
return null;
|
|
519
|
+
}
|
|
520
|
+
return typeof scripts.build === "string" && scripts.build.trim()
|
|
521
|
+
? scripts.build.trim()
|
|
522
|
+
: null;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
function pathLineage(root: string): string[] {
|
|
526
|
+
const parts = root.split("/").filter(Boolean);
|
|
527
|
+
const lineage: string[] = [];
|
|
528
|
+
for (let length = parts.length; length >= 0; length--) {
|
|
529
|
+
lineage.push(parts.slice(0, length).join("/"));
|
|
530
|
+
}
|
|
531
|
+
return lineage;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
function directoryName(path: string): string {
|
|
535
|
+
const index = path.lastIndexOf("/");
|
|
536
|
+
return index === -1 ? "" : path.slice(0, index);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function lastPathSegment(path: string): string | null {
|
|
540
|
+
return path.split("/").filter(Boolean).at(-1) ?? null;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
function pathDepth(path: string): number {
|
|
544
|
+
return path.split("/").length;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
function joinRepoPath(...parts: string[]): string {
|
|
548
|
+
return parts.filter(Boolean).join("/").replace(/\/+/g, "/");
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
function isPathInside(path: string, root: string): boolean {
|
|
552
|
+
return path === root || path.startsWith(`${root}/`);
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
function normalizeTarget(value: string): string {
|
|
556
|
+
const unscoped = value.split("/").filter(Boolean).at(-1) ?? "app";
|
|
557
|
+
const normalized = unscoped
|
|
558
|
+
.toLowerCase()
|
|
559
|
+
.replace(/[^a-z0-9-]+/g, "-")
|
|
560
|
+
.replace(/^-+|-+$/g, "");
|
|
561
|
+
return normalized || "app";
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
function uniquifyTargets(apps: DetectedComputeRepositoryApp[]): void {
|
|
565
|
+
const reservedTargets = new Set(apps.map((app) => app.target));
|
|
566
|
+
const usedTargets = new Set<string>();
|
|
567
|
+
for (const app of apps) {
|
|
568
|
+
const baseTarget = app.target;
|
|
569
|
+
let target = baseTarget;
|
|
570
|
+
let suffix = 2;
|
|
571
|
+
while (usedTargets.has(target)) {
|
|
572
|
+
do {
|
|
573
|
+
target = `${baseTarget}-${suffix}`;
|
|
574
|
+
suffix += 1;
|
|
575
|
+
} while (usedTargets.has(target) || reservedTargets.has(target));
|
|
576
|
+
}
|
|
577
|
+
usedTargets.add(target);
|
|
578
|
+
app.target = target;
|
|
579
|
+
app.name = target;
|
|
580
|
+
}
|
|
581
|
+
}
|
package/src/config/index.ts
CHANGED
|
@@ -13,6 +13,12 @@ export {
|
|
|
13
13
|
type DetectedComputeApp,
|
|
14
14
|
detectComputeApp,
|
|
15
15
|
} from "./detect-app.ts";
|
|
16
|
+
export {
|
|
17
|
+
type DetectComputeRepositoryInput,
|
|
18
|
+
type DetectComputeRepositoryResult,
|
|
19
|
+
type DetectedComputeRepositoryApp,
|
|
20
|
+
detectComputeRepository,
|
|
21
|
+
} from "./detect-repository.ts";
|
|
16
22
|
export {
|
|
17
23
|
ASTRO_CONFIG_FILENAMES,
|
|
18
24
|
CONFIG_BACKED_BUILD_TYPES,
|