@prisma/compute-sdk 0.48.0 → 0.50.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 +47 -2
- package/dist/nextjs-build.d.ts +14 -0
- package/dist/nextjs-build.d.ts.map +1 -1
- package/dist/nextjs-build.js +2 -2
- package/dist/nextjs-build.js.map +1 -1
- package/dist/service/build/build-service.d.ts +2 -1
- package/dist/service/build/build-service.d.ts.map +1 -1
- package/dist/service/build/build-service.js +58 -15
- package/dist/service/build/build-service.js.map +1 -1
- package/dist/service/frameworks/framework.d.ts +26 -0
- package/dist/service/frameworks/framework.d.ts.map +1 -0
- package/dist/service/frameworks/framework.js +2 -0
- package/dist/service/frameworks/framework.js.map +1 -0
- package/dist/service/frameworks/index.d.ts +7 -0
- package/dist/service/frameworks/index.d.ts.map +1 -0
- package/dist/service/frameworks/index.js +8 -0
- package/dist/service/frameworks/index.js.map +1 -0
- package/dist/service/frameworks/nextjs.d.ts +3 -0
- package/dist/service/frameworks/nextjs.d.ts.map +1 -0
- package/dist/service/frameworks/nextjs.js +77 -0
- package/dist/service/frameworks/nextjs.js.map +1 -0
- package/dist/service/index.d.ts +1 -1
- package/dist/service/index.d.ts.map +1 -1
- package/dist/service/index.js.map +1 -1
- package/dist/service/output/build-output.d.ts +55 -0
- package/dist/service/output/build-output.d.ts.map +1 -0
- package/dist/service/output/build-output.js +98 -0
- package/dist/service/output/build-output.js.map +1 -0
- package/dist/service/output/copy.d.ts +15 -0
- package/dist/service/output/copy.d.ts.map +1 -0
- package/dist/service/output/copy.js +59 -0
- package/dist/service/output/copy.js.map +1 -0
- package/dist/service/output/entrypoint.d.ts +20 -0
- package/dist/service/output/entrypoint.d.ts.map +1 -0
- package/dist/service/output/entrypoint.js +183 -0
- package/dist/service/output/entrypoint.js.map +1 -0
- package/dist/service/output/source-files.d.ts +12 -0
- package/dist/service/output/source-files.d.ts.map +1 -0
- package/dist/service/output/source-files.js +46 -0
- package/dist/service/output/source-files.js.map +1 -0
- package/dist/service/output/trace/bins.d.ts +9 -0
- package/dist/service/output/trace/bins.d.ts.map +1 -0
- package/dist/service/output/trace/bins.js +26 -0
- package/dist/service/output/trace/bins.js.map +1 -0
- package/dist/service/output/trace/imports.d.ts +8 -0
- package/dist/service/output/trace/imports.d.ts.map +1 -0
- package/dist/service/output/trace/imports.js +24 -0
- package/dist/service/output/trace/imports.js.map +1 -0
- package/dist/service/output/trace/optional-peer-dependencies.d.ts +13 -0
- package/dist/service/output/trace/optional-peer-dependencies.d.ts.map +1 -0
- package/dist/service/output/trace/optional-peer-dependencies.js +34 -0
- package/dist/service/output/trace/optional-peer-dependencies.js.map +1 -0
- package/dist/service/output/trace/packages.d.ts +22 -0
- package/dist/service/output/trace/packages.d.ts.map +1 -0
- package/dist/service/output/trace/packages.js +50 -0
- package/dist/service/output/trace/packages.js.map +1 -0
- package/dist/service/output/trace/trace.d.ts +32 -0
- package/dist/service/output/trace/trace.d.ts.map +1 -0
- package/dist/service/output/trace/trace.js +53 -0
- package/dist/service/output/trace/trace.js.map +1 -0
- package/package.json +1 -1
- package/src/nextjs-build.ts +4 -2
- package/src/service/build/build-service.ts +74 -17
- package/src/service/frameworks/framework.ts +28 -0
- package/src/service/frameworks/index.ts +12 -0
- package/src/service/frameworks/nextjs.ts +98 -0
- package/src/service/index.ts +1 -1
- package/src/service/output/build-output.ts +181 -0
- package/src/service/output/copy.ts +69 -0
- package/src/service/output/entrypoint.ts +250 -0
- package/src/service/output/source-files.ts +64 -0
- package/src/service/output/trace/bins.ts +39 -0
- package/src/service/output/trace/imports.ts +37 -0
- package/src/service/output/trace/optional-peer-dependencies.ts +50 -0
- package/src/service/output/trace/packages.ts +81 -0
- package/src/service/output/trace/trace.ts +99 -0
- package/dist/service/output/write-build-output.d.ts +0 -16
- package/dist/service/output/write-build-output.d.ts.map +0 -1
- package/dist/service/output/write-build-output.js +0 -24
- package/dist/service/output/write-build-output.js.map +0 -1
- package/src/service/output/write-build-output.ts +0 -50
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** A package as it is found: `link` is the path it is reached by, a link under pnpm. `dir` holds its files. */
|
|
2
|
+
export interface InstalledPackage {
|
|
3
|
+
link: string;
|
|
4
|
+
dir: string;
|
|
5
|
+
}
|
|
6
|
+
/** The fields of a dependency's `package.json` that tracing reads. */
|
|
7
|
+
export interface PackageManifest {
|
|
8
|
+
name?: string;
|
|
9
|
+
dependencies?: Record<string, string>;
|
|
10
|
+
optionalDependencies?: Record<string, string>;
|
|
11
|
+
peerDependenciesMeta?: Record<string, {
|
|
12
|
+
optional?: boolean;
|
|
13
|
+
}>;
|
|
14
|
+
}
|
|
15
|
+
export declare function readManifest(packageDir: string): PackageManifest;
|
|
16
|
+
/** Finds a package the way Node.js does: in the `node_modules` of `from` and of each folder above it. */
|
|
17
|
+
export declare function findPackage(from: string, name: string): InstalledPackage | null;
|
|
18
|
+
/** The folder of the package that holds `file`: the nearest folder with a `package.json` that sits in a `node_modules`. */
|
|
19
|
+
export declare function packageDirOf(file: string): string | null;
|
|
20
|
+
/** Adds a package and, in turn, every package it depends on. */
|
|
21
|
+
export declare function addWithDependencies(installed: InstalledPackage, found: Map<string, InstalledPackage>): void;
|
|
22
|
+
//# sourceMappingURL=packages.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"packages.d.ts","sourceRoot":"","sources":["../../../../src/service/output/trace/packages.ts"],"names":[],"mappings":"AAIA,+GAA+G;AAC/G,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;CACb;AAED,sEAAsE;AACtE,MAAM,WAAW,eAAe;IAC9B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACtC,oBAAoB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC9C,oBAAoB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE;QAAE,QAAQ,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;CAC/D;AAED,wBAAgB,YAAY,CAAC,UAAU,EAAE,MAAM,GAAG,eAAe,CAQhE;AAED,yGAAyG;AACzG,wBAAgB,WAAW,CACzB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,GACX,gBAAgB,GAAG,IAAI,CASzB;AAED,2HAA2H;AAC3H,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAcxD;AAED,gEAAgE;AAChE,wBAAgB,mBAAmB,CACjC,SAAS,EAAE,gBAAgB,EAC3B,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,gBAAgB,CAAC,GACnC,IAAI,CAgBN"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// Finds packages in `node_modules`, for the parts of a trace that take whole packages.
|
|
2
|
+
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
export function readManifest(packageDir) {
|
|
5
|
+
try {
|
|
6
|
+
return JSON.parse(readFileSync(path.join(packageDir, "package.json"), "utf8"));
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return {};
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/** Finds a package the way Node.js does: in the `node_modules` of `from` and of each folder above it. */
|
|
13
|
+
export function findPackage(from, name) {
|
|
14
|
+
for (let dir = from; dir !== path.dirname(dir); dir = path.dirname(dir)) {
|
|
15
|
+
const link = path.join(dir, "node_modules", name);
|
|
16
|
+
if (existsSync(path.join(link, "package.json"))) {
|
|
17
|
+
return { link, dir: realpathSync(link) };
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
/** The folder of the package that holds `file`: the nearest folder with a `package.json` that sits in a `node_modules`. */
|
|
23
|
+
export function packageDirOf(file) {
|
|
24
|
+
for (let dir = path.dirname(file); dir !== path.dirname(dir); dir = path.dirname(dir)) {
|
|
25
|
+
const parent = path.basename(path.dirname(dir));
|
|
26
|
+
const inNodeModules = parent === "node_modules" || parent.startsWith("@");
|
|
27
|
+
if (inNodeModules && existsSync(path.join(dir, "package.json"))) {
|
|
28
|
+
return dir;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
/** Adds a package and, in turn, every package it depends on. */
|
|
34
|
+
export function addWithDependencies(installed, found) {
|
|
35
|
+
if (found.has(installed.link)) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
found.set(installed.link, installed);
|
|
39
|
+
const manifest = readManifest(installed.dir);
|
|
40
|
+
for (const name of Object.keys({
|
|
41
|
+
...manifest.dependencies,
|
|
42
|
+
...manifest.optionalDependencies,
|
|
43
|
+
})) {
|
|
44
|
+
const dependency = findPackage(installed.dir, name);
|
|
45
|
+
if (dependency !== null) {
|
|
46
|
+
addWithDependencies(dependency, found);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=packages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"packages.js","sourceRoot":"","sources":["../../../../src/service/output/trace/packages.ts"],"names":[],"mappings":"AAAA,uFAAuF;AACvF,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACjE,OAAO,IAAI,MAAM,WAAW,CAAC;AAgB7B,MAAM,UAAU,YAAY,CAAC,UAAkB;IAC7C,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CACf,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,cAAc,CAAC,EAAE,MAAM,CAAC,CAC5D,CAAC;IACJ,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC;AAED,yGAAyG;AACzG,MAAM,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,IAAI,CAAC,EAAE,CAAC;QAC9B,OAAO;IACT,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;IACrC,MAAM,QAAQ,GAAG,YAAY,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IAC7C,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC;QAC7B,GAAG,QAAQ,CAAC,YAAY;QACxB,GAAG,QAAQ,CAAC,oBAAoB;KACjC,CAAC,EAAE,CAAC;QACH,MAAM,UAAU,GAAG,WAAW,CAAC,SAAS,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;QACpD,IAAI,UAAU,KAAK,IAAI,EAAE,CAAC;YACxB,mBAAmB,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;QACzC,CAAC;IACH,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { PackageJson } from "../../settings/types.ts";
|
|
2
|
+
import { type InstalledPackage } from "./packages.ts";
|
|
3
|
+
/** What a server loads, found from its start command. */
|
|
4
|
+
export interface ServerTrace {
|
|
5
|
+
/** The files the server starts from. */
|
|
6
|
+
entrypoints: string[];
|
|
7
|
+
/** The bins the start command runs. */
|
|
8
|
+
bins: string[];
|
|
9
|
+
/** Every file the entrypoints and the built scripts import. */
|
|
10
|
+
files: string[];
|
|
11
|
+
/** Packages that go in whole: those of the bins, those the start command preloads, and the optional peer dependencies. */
|
|
12
|
+
packages: InstalledPackage[];
|
|
13
|
+
/** The names of the optional peer dependencies among `packages`. */
|
|
14
|
+
optionalPeerDependencies: string[];
|
|
15
|
+
}
|
|
16
|
+
/** Why a server could not be traced. */
|
|
17
|
+
export type Untraceable = "the start command runs no file and no bin that can be followed" | "the server imports files from outside the Service folder";
|
|
18
|
+
/**
|
|
19
|
+
* Traces the server that `command` starts:
|
|
20
|
+
* 1. the imports of its entrypoints, and of the scripts the build wrote;
|
|
21
|
+
* 2. the packages of the bins it runs, and the packages it preloads;
|
|
22
|
+
* 3. the optional peer dependencies of what was traced.
|
|
23
|
+
*/
|
|
24
|
+
export declare function traceServer(options: {
|
|
25
|
+
serviceDir: string;
|
|
26
|
+
command: string;
|
|
27
|
+
packageJson: PackageJson;
|
|
28
|
+
/** Scripts in the output directories. A framework's server loads them by path, and they import packages. */
|
|
29
|
+
builtScripts: string[];
|
|
30
|
+
signal?: AbortSignal;
|
|
31
|
+
}): Promise<ServerTrace | Untraceable>;
|
|
32
|
+
//# sourceMappingURL=trace.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trace.d.ts","sourceRoot":"","sources":["../../../../src/service/output/trace/trace.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAK3D,OAAO,EAGL,KAAK,gBAAgB,EACtB,MAAM,eAAe,CAAC;AAEvB,yDAAyD;AACzD,MAAM,WAAW,WAAW;IAC1B,wCAAwC;IACxC,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,uCAAuC;IACvC,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,+DAA+D;IAC/D,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,0HAA0H;IAC1H,QAAQ,EAAE,gBAAgB,EAAE,CAAC;IAC7B,oEAAoE;IACpE,wBAAwB,EAAE,MAAM,EAAE,CAAC;CACpC;AAED,wCAAwC;AACxC,MAAM,MAAM,WAAW,GACnB,gEAAgE,GAChE,0DAA0D,CAAC;AAE/D;;;;;GAKG;AACH,wBAAsB,WAAW,CAAC,OAAO,EAAE;IACzC,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,WAAW,CAAC;IACzB,4GAA4G;IAC5G,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB,GAAG,OAAO,CAAC,WAAW,GAAG,WAAW,CAAC,CAoDrC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// The trace of a server: every file it loads from `node_modules`.
|
|
2
|
+
import { realpathSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { readStartCommand } from "../entrypoint.js";
|
|
5
|
+
import { binPackages } from "./bins.js";
|
|
6
|
+
import { traceImports } from "./imports.js";
|
|
7
|
+
import { optionalPeerDependencies } from "./optional-peer-dependencies.js";
|
|
8
|
+
import { addWithDependencies, findPackage, } from "./packages.js";
|
|
9
|
+
/**
|
|
10
|
+
* Traces the server that `command` starts:
|
|
11
|
+
* 1. the imports of its entrypoints, and of the scripts the build wrote;
|
|
12
|
+
* 2. the packages of the bins it runs, and the packages it preloads;
|
|
13
|
+
* 3. the optional peer dependencies of what was traced.
|
|
14
|
+
*/
|
|
15
|
+
export async function traceServer(options) {
|
|
16
|
+
const { packageJson } = options;
|
|
17
|
+
// Traced files are real paths, so the Service folder they are compared with must be its real path too.
|
|
18
|
+
const serviceDir = realpathSync(options.serviceDir);
|
|
19
|
+
const { entrypoints, bins, preloadedPackages } = readStartCommand(serviceDir, options.command, packageJson);
|
|
20
|
+
if (entrypoints.length === 0 && bins.length === 0) {
|
|
21
|
+
return "the start command runs no file and no bin that can be followed";
|
|
22
|
+
}
|
|
23
|
+
options.signal?.throwIfAborted();
|
|
24
|
+
const files = await traceImports([...entrypoints, ...options.builtScripts], serviceDir);
|
|
25
|
+
const packages = binPackages(serviceDir, bins);
|
|
26
|
+
// A preloaded package goes in whole, as a bin's package does: `tsx` loads its own files by path.
|
|
27
|
+
for (const name of preloadedPackages) {
|
|
28
|
+
const preloaded = findPackage(serviceDir, name);
|
|
29
|
+
if (preloaded !== null) {
|
|
30
|
+
addWithDependencies(preloaded, packages);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const peers = optionalPeerDependencies(serviceDir, packageJson, files);
|
|
34
|
+
for (const peer of peers) {
|
|
35
|
+
addWithDependencies(peer, packages);
|
|
36
|
+
}
|
|
37
|
+
const inside = (file) => {
|
|
38
|
+
const relative = path.relative(serviceDir, file);
|
|
39
|
+
return !relative.startsWith("..") && !path.isAbsolute(relative);
|
|
40
|
+
};
|
|
41
|
+
if (!files.every(inside) ||
|
|
42
|
+
![...packages.values()].every((found) => inside(found.dir))) {
|
|
43
|
+
return "the server imports files from outside the Service folder";
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
entrypoints,
|
|
47
|
+
bins,
|
|
48
|
+
files,
|
|
49
|
+
packages: [...packages.values()],
|
|
50
|
+
optionalPeerDependencies: peers.map((peer) => peer.name),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=trace.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trace.js","sourceRoot":"","sources":["../../../../src/service/output/trace/trace.ts"],"names":[],"mappings":"AAAA,kEAAkE;AAClE,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AAC3E,OAAO,EACL,mBAAmB,EACnB,WAAW,GAEZ,MAAM,eAAe,CAAC;AAqBvB;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,OAOjC;IACC,MAAM,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC;IAChC,uGAAuG;IACvG,MAAM,UAAU,GAAG,YAAY,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IACpD,MAAM,EAAE,WAAW,EAAE,IAAI,EAAE,iBAAiB,EAAE,GAAG,gBAAgB,CAC/D,UAAU,EACV,OAAO,CAAC,OAAO,EACf,WAAW,CACZ,CAAC;IACF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,CAAC,GAAG,CAAC,CAAC,EAC3D,CAAC;QACD,OAAO,0DAA0D,CAAC;IACpE,CAAC;IAED,OAAO;QACL,WAAW;QACX,IAAI;QACJ,KAAK;QACL,QAAQ,EAAE,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC;QAChC,wBAAwB,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC;KACzD,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
package/src/nextjs-build.ts
CHANGED
|
@@ -196,7 +196,7 @@ function isExcludedFromFullTree(basename: string): boolean {
|
|
|
196
196
|
);
|
|
197
197
|
}
|
|
198
198
|
|
|
199
|
-
async function copyStaticAssets(options: {
|
|
199
|
+
export async function copyStaticAssets(options: {
|
|
200
200
|
appPath: string;
|
|
201
201
|
artifactDir: string;
|
|
202
202
|
outputRoot: string;
|
|
@@ -233,7 +233,9 @@ async function copyStaticAssets(options: {
|
|
|
233
233
|
* entrypoint is the shallowest match. `node_modules` is skipped because
|
|
234
234
|
* third-party packages may ship their own `server.js`.
|
|
235
235
|
*/
|
|
236
|
-
async function findStandaloneEntrypoint(
|
|
236
|
+
export async function findStandaloneEntrypoint(
|
|
237
|
+
artifactDir: string,
|
|
238
|
+
): Promise<string> {
|
|
237
239
|
const rootStat = await stat(path.join(artifactDir, "server.js")).catch(
|
|
238
240
|
() => null,
|
|
239
241
|
);
|
|
@@ -4,10 +4,10 @@ import { existsSync, readFileSync } from "node:fs";
|
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { Result } from "better-result";
|
|
6
6
|
import { ServiceBuildError } from "../../errors.ts";
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} from "../
|
|
7
|
+
import type { Framework } from "../frameworks/framework.ts";
|
|
8
|
+
import { findFramework } from "../frameworks/index.ts";
|
|
9
|
+
import { type BuildOutput, writeBuildOutput } from "../output/build-output.ts";
|
|
10
|
+
import { readPackageJson } from "../settings/package-json.ts";
|
|
11
11
|
import {
|
|
12
12
|
resolveServiceBuildSettings,
|
|
13
13
|
resolveStartAfterBuild,
|
|
@@ -67,6 +67,7 @@ const NO_START: Failure = {
|
|
|
67
67
|
|
|
68
68
|
/**
|
|
69
69
|
* Runs the Service's install and build commands, decides how the Service starts, and writes the build output.
|
|
70
|
+
* A framework's own build output comes before the one this package writes.
|
|
70
71
|
*
|
|
71
72
|
* A failed install, a failed build and a missing start are expected outcomes:
|
|
72
73
|
* they come back as a `ServiceBuildError`, not as a thrown error.
|
|
@@ -81,15 +82,49 @@ export async function buildService(
|
|
|
81
82
|
options.config,
|
|
82
83
|
options.signal,
|
|
83
84
|
);
|
|
85
|
+
const { packageJson } = await readPackageJson(read);
|
|
86
|
+
// A start the user states in the Service config is final, so no framework changes the build.
|
|
87
|
+
const startIsFromConfig =
|
|
88
|
+
settings.start !== "after build" && settings.start.from === "config";
|
|
89
|
+
const framework = startIsFromConfig ? null : findFramework(packageJson);
|
|
84
90
|
const steps: BuildStep[] = [];
|
|
85
91
|
const fail = (failure: Failure) =>
|
|
86
92
|
Result.err(new ServiceBuildError({ ...failure, settings, steps }));
|
|
87
|
-
|
|
88
|
-
|
|
93
|
+
const done = (start: Resolved<Start>, output: BuildOutput) =>
|
|
94
|
+
Result.ok({
|
|
95
|
+
settings: {
|
|
96
|
+
...settings,
|
|
97
|
+
start,
|
|
98
|
+
notes: [...settings.notes, ...buildOutputNotes(output)],
|
|
99
|
+
},
|
|
100
|
+
start,
|
|
101
|
+
steps,
|
|
102
|
+
output,
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const outputDirectories = await installAndBuild(
|
|
106
|
+
settings,
|
|
107
|
+
framework,
|
|
108
|
+
options,
|
|
109
|
+
steps,
|
|
110
|
+
);
|
|
89
111
|
if (outputDirectories.isErr()) {
|
|
90
112
|
return fail(outputDirectories.error);
|
|
91
113
|
}
|
|
92
114
|
|
|
115
|
+
const port = settings.port?.value ?? null;
|
|
116
|
+
const fromFramework = await framework?.buildOutput?.({
|
|
117
|
+
serviceDir: dir,
|
|
118
|
+
port,
|
|
119
|
+
signal: options.signal,
|
|
120
|
+
});
|
|
121
|
+
if (fromFramework) {
|
|
122
|
+
return done(
|
|
123
|
+
{ value: fromFramework.start, from: "build output" },
|
|
124
|
+
fromFramework.output,
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
93
128
|
const start =
|
|
94
129
|
settings.start === "after build"
|
|
95
130
|
? await resolveStartAfterBuild(
|
|
@@ -102,24 +137,39 @@ export async function buildService(
|
|
|
102
137
|
return fail(NO_START);
|
|
103
138
|
}
|
|
104
139
|
|
|
105
|
-
const
|
|
106
|
-
const output = writeBuildOutput({
|
|
140
|
+
const output = await writeBuildOutput({
|
|
107
141
|
serviceDir: dir,
|
|
108
142
|
start: start.value,
|
|
109
143
|
port,
|
|
144
|
+
packageJson,
|
|
145
|
+
outputDirectories: outputDirectories.value,
|
|
146
|
+
signal: options.signal,
|
|
110
147
|
});
|
|
111
148
|
|
|
112
|
-
return
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
149
|
+
return done(start, output);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** What the user should know about a build output that is not the trace of their start command. */
|
|
153
|
+
function buildOutputNotes(output: BuildOutput): string[] {
|
|
154
|
+
if (output.kind === "whole folder") {
|
|
155
|
+
return [
|
|
156
|
+
`The build output is the whole folder, with all of node_modules: ${output.reason}.`,
|
|
157
|
+
];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (output.kind === "standalone output") {
|
|
161
|
+
return [
|
|
162
|
+
`The build output is the standalone output of ${output.framework}. It starts with its own server file, not with a start script.`,
|
|
163
|
+
];
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return [];
|
|
118
167
|
}
|
|
119
168
|
|
|
120
169
|
/** Runs the install, then the build. Returns what the build created, or the step that failed. */
|
|
121
170
|
async function installAndBuild(
|
|
122
171
|
settings: ServiceBuildSettings,
|
|
172
|
+
framework: Framework | null,
|
|
123
173
|
options: BuildServiceOptions,
|
|
124
174
|
steps: BuildStep[],
|
|
125
175
|
): Promise<Result<OutputDirectories, Failure>> {
|
|
@@ -133,7 +183,7 @@ async function installAndBuild(
|
|
|
133
183
|
}
|
|
134
184
|
|
|
135
185
|
const beforeBuild = snapshotFolders(dir);
|
|
136
|
-
const build = buildStep(settings, env);
|
|
186
|
+
const build = buildStep(settings, framework, env);
|
|
137
187
|
const built = await runStep(build, run);
|
|
138
188
|
if (!built.ok) {
|
|
139
189
|
return Result.err(stepFailed(build, built.output));
|
|
@@ -142,15 +192,22 @@ async function installAndBuild(
|
|
|
142
192
|
return Result.ok(outputDirectoriesSince(dir, beforeBuild));
|
|
143
193
|
}
|
|
144
194
|
|
|
145
|
-
/** The build runs in production mode. The caller's variables win over these. */
|
|
195
|
+
/** The build runs in production mode, with the framework's variables. The caller's variables win over these. */
|
|
146
196
|
function buildStep(
|
|
147
197
|
settings: ServiceBuildSettings,
|
|
198
|
+
framework: Framework | null,
|
|
148
199
|
extraEnv: Record<string, string> = {},
|
|
149
200
|
): StepToRun {
|
|
150
201
|
return {
|
|
151
202
|
name: "build",
|
|
152
203
|
command: settings.build.value,
|
|
153
|
-
env: {
|
|
204
|
+
env: {
|
|
205
|
+
...process.env,
|
|
206
|
+
CI: "true",
|
|
207
|
+
NODE_ENV: "production",
|
|
208
|
+
...framework?.buildEnv,
|
|
209
|
+
...extraEnv,
|
|
210
|
+
},
|
|
154
211
|
};
|
|
155
212
|
}
|
|
156
213
|
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// A framework definition: how the build of one Service detects a framework, and what the build does differently for it.
|
|
2
|
+
import type { BuildOutput } from "../output/build-output.ts";
|
|
3
|
+
import type { PackageJson, Start } from "../settings/types.ts";
|
|
4
|
+
|
|
5
|
+
export interface Framework {
|
|
6
|
+
/** The name the framework goes by, such as `Next.js`. The build output it produces carries the name. */
|
|
7
|
+
name: string;
|
|
8
|
+
/** True when the Service uses the framework. */
|
|
9
|
+
detect(packageJson: PackageJson): boolean;
|
|
10
|
+
/** Variables for the build command. */
|
|
11
|
+
buildEnv?: Record<string, string>;
|
|
12
|
+
/**
|
|
13
|
+
* Runs after the build. Returns the build output that the framework's build wrote,
|
|
14
|
+
* or null when it wrote none. The build then writes its own build output.
|
|
15
|
+
*/
|
|
16
|
+
buildOutput?(service: {
|
|
17
|
+
serviceDir: string;
|
|
18
|
+
/** The port the user stated, or null. */
|
|
19
|
+
port: number | null;
|
|
20
|
+
signal?: AbortSignal;
|
|
21
|
+
}): Promise<FrameworkBuildOutput | null>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The build output that a framework's build wrote, and how the Service starts from it. */
|
|
25
|
+
export interface FrameworkBuildOutput {
|
|
26
|
+
output: BuildOutput;
|
|
27
|
+
start: Start;
|
|
28
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// The framework definitions that the build of one Service knows, in detection order.
|
|
2
|
+
import type { PackageJson } from "../settings/types.ts";
|
|
3
|
+
import type { Framework } from "./framework.ts";
|
|
4
|
+
import { nextjs } from "./nextjs.ts";
|
|
5
|
+
|
|
6
|
+
/** The first definition that detects the Service is its framework. */
|
|
7
|
+
export const FRAMEWORKS: Framework[] = [nextjs];
|
|
8
|
+
|
|
9
|
+
/** The framework the Service uses, or null when no definition detects it. */
|
|
10
|
+
export function findFramework(packageJson: PackageJson): Framework | null {
|
|
11
|
+
return FRAMEWORKS.find((framework) => framework.detect(packageJson)) ?? null;
|
|
12
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// The Next.js framework definition: `next build` writes standalone output, and that becomes the build output.
|
|
2
|
+
import { statSync } from "node:fs";
|
|
3
|
+
import { rm } from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { stageStandaloneArtifact } from "../../artifact-stage.ts";
|
|
6
|
+
import {
|
|
7
|
+
copyStaticAssets,
|
|
8
|
+
findStandaloneEntrypoint,
|
|
9
|
+
} from "../../nextjs-build.ts";
|
|
10
|
+
import { LAUNCHER_FILE } from "../launcher-contract/start-plan.ts";
|
|
11
|
+
import { BUILD_OUTPUT_FOLDER, writeLauncher } from "../output/build-output.ts";
|
|
12
|
+
import type { Framework } from "./framework.ts";
|
|
13
|
+
|
|
14
|
+
const NAME = "Next.js";
|
|
15
|
+
|
|
16
|
+
/** `next start` alone, with options at most. A command joined to it with `&&`, `;` or `|` is something else. */
|
|
17
|
+
const NEXT_START = /^\s*next\s+start(\s+[^&|;]*)?$/;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The standalone server replaces the start script. That is only right when the script does nothing
|
|
21
|
+
* but start Next.js: a migration in front of it, a `prestart` script or a custom server would not run.
|
|
22
|
+
*/
|
|
23
|
+
function startsOnlyNext(scripts: Record<string, string>): boolean {
|
|
24
|
+
const { start, prestart, poststart } = scripts;
|
|
25
|
+
|
|
26
|
+
return (
|
|
27
|
+
(start === undefined || NEXT_START.test(start)) &&
|
|
28
|
+
prestart === undefined &&
|
|
29
|
+
poststart === undefined
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The output directory of `next build`, relative to the Service folder. */
|
|
34
|
+
const OUTPUT_DIRECTORY = ".next";
|
|
35
|
+
|
|
36
|
+
/** The Launcher runs the start command in a shell, and the path of the server file comes from the file system. */
|
|
37
|
+
function shellWord(filePath: string): string {
|
|
38
|
+
return /^[\w./-]+$/.test(filePath)
|
|
39
|
+
? filePath
|
|
40
|
+
: `'${filePath.replaceAll("'", "'\\''")}'`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const nextjs: Framework = {
|
|
44
|
+
name: NAME,
|
|
45
|
+
|
|
46
|
+
detect: ({ dependencies, devDependencies, scripts }) =>
|
|
47
|
+
"next" in { ...dependencies, ...devDependencies } &&
|
|
48
|
+
startsOnlyNext(scripts ?? {}),
|
|
49
|
+
|
|
50
|
+
// `next build` then writes standalone output, as it does for `output: "standalone"` in next.config.
|
|
51
|
+
buildEnv: { NEXT_PRIVATE_STANDALONE: "true" },
|
|
52
|
+
|
|
53
|
+
async buildOutput({ serviceDir, port, signal }) {
|
|
54
|
+
const standaloneDir = path.join(serviceDir, OUTPUT_DIRECTORY, "standalone");
|
|
55
|
+
// A Next.js version that does not read the variable writes no standalone output.
|
|
56
|
+
if (!statSync(standaloneDir, { throwIfNoEntry: false })?.isDirectory()) {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const dir = path.join(serviceDir, BUILD_OUTPUT_FOLDER);
|
|
61
|
+
await rm(dir, { recursive: true, force: true });
|
|
62
|
+
await stageStandaloneArtifact({
|
|
63
|
+
standaloneDir,
|
|
64
|
+
artifactDir: dir,
|
|
65
|
+
appPath: serviceDir,
|
|
66
|
+
signal,
|
|
67
|
+
});
|
|
68
|
+
// Standalone output with no server file cannot start. The build then writes its own build output.
|
|
69
|
+
const server = await findStandaloneEntrypoint(dir).catch(() => null);
|
|
70
|
+
if (server === null) {
|
|
71
|
+
await rm(dir, { recursive: true, force: true });
|
|
72
|
+
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Standalone output leaves out `public` and `.next/static`. The server serves them from its own folder.
|
|
77
|
+
await copyStaticAssets({
|
|
78
|
+
appPath: serviceDir,
|
|
79
|
+
artifactDir: dir,
|
|
80
|
+
outputRoot: OUTPUT_DIRECTORY,
|
|
81
|
+
entrypoint: server,
|
|
82
|
+
signal,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const command = `node ${shellWord(server)}`;
|
|
86
|
+
writeLauncher(dir, { kind: "command", command, port });
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
start: { kind: "command", command },
|
|
90
|
+
output: {
|
|
91
|
+
dir,
|
|
92
|
+
entrypoint: LAUNCHER_FILE,
|
|
93
|
+
kind: "standalone output",
|
|
94
|
+
framework: NAME,
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
},
|
|
98
|
+
};
|
package/src/service/index.ts
CHANGED
|
@@ -7,8 +7,8 @@ export {
|
|
|
7
7
|
buildService,
|
|
8
8
|
} from "./build/build-service.ts";
|
|
9
9
|
export type { BuildStep } from "./build/run-step.ts";
|
|
10
|
+
export type { BuildOutput, TraceSummary } from "./output/build-output.ts";
|
|
10
11
|
export { excludedFromBuildOutput } from "./output/exclude.ts";
|
|
11
|
-
export type { BuildOutput } from "./output/write-build-output.ts";
|
|
12
12
|
export {
|
|
13
13
|
isDevCommand,
|
|
14
14
|
type OutputDirectories,
|