@prisma/compute-sdk 0.44.0 → 0.45.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 +196 -0
- package/dist/archive.d.ts +2 -1
- package/dist/archive.d.ts.map +1 -1
- package/dist/archive.js +8 -4
- package/dist/archive.js.map +1 -1
- package/dist/build-strategy.d.ts +7 -0
- package/dist/build-strategy.d.ts.map +1 -1
- package/dist/build-strategy.js +3 -0
- package/dist/build-strategy.js.map +1 -1
- package/dist/compute-client.d.ts.map +1 -1
- package/dist/compute-client.js +1 -1
- package/dist/compute-client.js.map +1 -1
- package/dist/config/frameworks.d.ts +1 -1
- package/dist/errors.d.ts +14 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +3 -0
- package/dist/errors.js.map +1 -1
- package/dist/package-manager.d.ts +6 -0
- package/dist/package-manager.d.ts.map +1 -0
- package/dist/package-manager.js +21 -0
- package/dist/package-manager.js.map +1 -0
- package/dist/service/artifact/exclude.d.ts +6 -0
- package/dist/service/artifact/exclude.d.ts.map +1 -0
- package/dist/service/artifact/exclude.js +8 -0
- package/dist/service/artifact/exclude.js.map +1 -0
- package/dist/service/artifact/launcher-file.d.ts +3 -0
- package/dist/service/artifact/launcher-file.d.ts.map +1 -0
- package/dist/service/artifact/launcher-file.js +13 -0
- package/dist/service/artifact/launcher-file.js.map +1 -0
- package/dist/service/artifact/write-artifact.d.ts +16 -0
- package/dist/service/artifact/write-artifact.d.ts.map +1 -0
- package/dist/service/artifact/write-artifact.js +24 -0
- package/dist/service/artifact/write-artifact.js.map +1 -0
- package/dist/service/build/build-output.d.ts +15 -0
- package/dist/service/build/build-output.d.ts.map +1 -0
- package/dist/service/build/build-output.js +63 -0
- package/dist/service/build/build-output.js.map +1 -0
- package/dist/service/build/build-service.d.ts +30 -0
- package/dist/service/build/build-service.d.ts.map +1 -0
- package/dist/service/build/build-service.js +95 -0
- package/dist/service/build/build-service.js.map +1 -0
- package/dist/service/build/run-step.d.ts +32 -0
- package/dist/service/build/run-step.d.ts.map +1 -0
- package/dist/service/build/run-step.js +29 -0
- package/dist/service/build/run-step.js.map +1 -0
- package/dist/service/index.d.ts +7 -0
- package/dist/service/index.d.ts.map +1 -0
- package/dist/service/index.js +7 -0
- package/dist/service/index.js.map +1 -0
- package/dist/service/launcher-contract/events.d.ts +22 -0
- package/dist/service/launcher-contract/events.d.ts.map +1 -0
- package/dist/service/launcher-contract/events.js +39 -0
- package/dist/service/launcher-contract/events.js.map +1 -0
- package/dist/service/launcher-contract/start-plan.d.ts +20 -0
- package/dist/service/launcher-contract/start-plan.d.ts.map +1 -0
- package/dist/service/launcher-contract/start-plan.js +10 -0
- package/dist/service/launcher-contract/start-plan.js.map +1 -0
- package/dist/service/launcher.mjs +482 -0
- package/dist/service/settings/dev-command.d.ts +6 -0
- package/dist/service/settings/dev-command.d.ts.map +1 -0
- package/dist/service/settings/dev-command.js +46 -0
- package/dist/service/settings/dev-command.js.map +1 -0
- package/dist/service/settings/index.d.ts +5 -0
- package/dist/service/settings/index.d.ts.map +1 -0
- package/dist/service/settings/index.js +3 -0
- package/dist/service/settings/index.js.map +1 -0
- package/dist/service/settings/install.d.ts +15 -0
- package/dist/service/settings/install.d.ts.map +1 -0
- package/dist/service/settings/install.js +58 -0
- package/dist/service/settings/install.js.map +1 -0
- package/dist/service/settings/node-version.d.ts +9 -0
- package/dist/service/settings/node-version.d.ts.map +1 -0
- package/dist/service/settings/node-version.js +18 -0
- package/dist/service/settings/node-version.js.map +1 -0
- package/dist/service/settings/package-json.d.ts +14 -0
- package/dist/service/settings/package-json.d.ts.map +1 -0
- package/dist/service/settings/package-json.js +51 -0
- package/dist/service/settings/package-json.js.map +1 -0
- package/dist/service/settings/resolve.d.ts +9 -0
- package/dist/service/settings/resolve.d.ts.map +1 -0
- package/dist/service/settings/resolve.js +49 -0
- package/dist/service/settings/resolve.js.map +1 -0
- package/dist/service/settings/start-order.d.ts +21 -0
- package/dist/service/settings/start-order.d.ts.map +1 -0
- package/dist/service/settings/start-order.js +138 -0
- package/dist/service/settings/start-order.js.map +1 -0
- package/dist/service/settings/types.d.ts +66 -0
- package/dist/service/settings/types.d.ts.map +1 -0
- package/dist/service/settings/types.js +2 -0
- package/dist/service/settings/types.js.map +1 -0
- package/dist/workspace.d.ts +4 -1
- package/dist/workspace.d.ts.map +1 -1
- package/dist/workspace.js +12 -23
- package/dist/workspace.js.map +1 -1
- package/package.json +16 -2
- package/src/archive.ts +9 -2
- package/src/build-strategy.ts +15 -1
- package/src/compute-client.ts +1 -0
- package/src/errors.ts +13 -0
- package/src/package-manager.ts +27 -0
- package/src/service/artifact/exclude.ts +7 -0
- package/src/service/artifact/launcher-file.ts +18 -0
- package/src/service/artifact/write-artifact.ts +50 -0
- package/src/service/build/build-output.ts +95 -0
- package/src/service/build/build-service.ts +171 -0
- package/src/service/build/run-step.ts +65 -0
- package/src/service/index.ts +24 -0
- package/src/service/launcher/bin-targets.ts +143 -0
- package/src/service/launcher/bin-wrappers.ts +48 -0
- package/src/service/launcher/bun-shell.ts +17 -0
- package/src/service/launcher/listening-ports.ts +112 -0
- package/src/service/launcher/main.ts +54 -0
- package/src/service/launcher/package-manager-shims.ts +147 -0
- package/src/service/launcher/proc.ts +112 -0
- package/src/service/launcher/run-command.ts +82 -0
- package/src/service/launcher/serve-static.ts +102 -0
- package/src/service/launcher-contract/events.ts +53 -0
- package/src/service/launcher-contract/start-plan.ts +18 -0
- package/src/service/settings/dev-command.ts +65 -0
- package/src/service/settings/index.ts +17 -0
- package/src/service/settings/install.ts +105 -0
- package/src/service/settings/node-version.ts +26 -0
- package/src/service/settings/package-json.ts +71 -0
- package/src/service/settings/resolve.ts +80 -0
- package/src/service/settings/start-order.ts +213 -0
- package/src/service/settings/types.ts +73 -0
- package/src/workspace.ts +20 -35
package/src/archive.ts
CHANGED
|
@@ -11,6 +11,7 @@ const COMPUTE_MANIFEST_VERSION = "1" as const;
|
|
|
11
11
|
* - Files are added under the `bundle/` prefix.
|
|
12
12
|
* - Symlinks are preserved as symlink entries.
|
|
13
13
|
* - A synthetic `compute.manifest.json` entry is injected at the tar root.
|
|
14
|
+
* - Entries whose file or folder name `exclude` accepts are left out.
|
|
14
15
|
*
|
|
15
16
|
* Returns the gzipped archive as a Uint8Array.
|
|
16
17
|
*/
|
|
@@ -18,13 +19,14 @@ export async function createArchive(
|
|
|
18
19
|
directory: string,
|
|
19
20
|
entrypoint: string,
|
|
20
21
|
signal?: AbortSignal,
|
|
22
|
+
exclude?: (name: string) => boolean,
|
|
21
23
|
): Promise<Uint8Array> {
|
|
22
24
|
signal?.throwIfAborted();
|
|
23
25
|
const packer = pack();
|
|
24
26
|
const rootPath = path.resolve(directory);
|
|
25
27
|
|
|
26
28
|
// Walk the directory and add all files
|
|
27
|
-
await addDirectory(packer, rootPath, rootPath, "bundle", signal);
|
|
29
|
+
await addDirectory(packer, rootPath, rootPath, "bundle", signal, exclude);
|
|
28
30
|
|
|
29
31
|
// Inject the manifest as a synthetic entry
|
|
30
32
|
const manifest = JSON.stringify(
|
|
@@ -50,6 +52,7 @@ async function addDirectory(
|
|
|
50
52
|
fsPath: string,
|
|
51
53
|
tarPrefix: string,
|
|
52
54
|
signal?: AbortSignal,
|
|
55
|
+
exclude?: (name: string) => boolean,
|
|
53
56
|
): Promise<void> {
|
|
54
57
|
signal?.throwIfAborted();
|
|
55
58
|
const directoryPath = resolvePathWithinRoot(rootPath, fsPath);
|
|
@@ -57,6 +60,10 @@ async function addDirectory(
|
|
|
57
60
|
|
|
58
61
|
for (const entry of entries) {
|
|
59
62
|
signal?.throwIfAborted();
|
|
63
|
+
if (exclude?.(entry.name)) {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
|
|
60
67
|
const fullPath = path.join(directoryPath, entry.name);
|
|
61
68
|
const tarPath = `${tarPrefix}/${entry.name}`;
|
|
62
69
|
|
|
@@ -64,7 +71,7 @@ async function addDirectory(
|
|
|
64
71
|
const linkStat = await lstat(fullPath);
|
|
65
72
|
await addSymlink(packer, rootPath, fullPath, tarPath, linkStat, signal);
|
|
66
73
|
} else if (entry.isDirectory()) {
|
|
67
|
-
await addDirectory(packer, rootPath, fullPath, tarPath, signal);
|
|
74
|
+
await addDirectory(packer, rootPath, fullPath, tarPath, signal, exclude);
|
|
68
75
|
} else if (entry.isFile()) {
|
|
69
76
|
const fileStat = await lstat(fullPath);
|
|
70
77
|
await addFile(packer, rootPath, fullPath, tarPath, fileStat, signal);
|
package/src/build-strategy.ts
CHANGED
|
@@ -26,6 +26,12 @@ export interface BuildArtifact {
|
|
|
26
26
|
*/
|
|
27
27
|
defaultPortMapping?: PortMapping;
|
|
28
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Leaves a file or folder out of the uploaded archive when it returns true
|
|
31
|
+
* for the entry's name. Applies at every depth.
|
|
32
|
+
*/
|
|
33
|
+
exclude?(name: string): boolean;
|
|
34
|
+
|
|
29
35
|
/**
|
|
30
36
|
* Optional cleanup callback. Called by the core after the archive has been
|
|
31
37
|
* created and the artifact directory is no longer needed. Strategies that
|
|
@@ -49,10 +55,17 @@ export interface BuildStrategy {
|
|
|
49
55
|
export class PreBuilt implements BuildStrategy {
|
|
50
56
|
readonly #appPath: string;
|
|
51
57
|
readonly #entrypoint: string;
|
|
58
|
+
readonly #exclude: ((name: string) => boolean) | undefined;
|
|
52
59
|
|
|
53
|
-
constructor(options: {
|
|
60
|
+
constructor(options: {
|
|
61
|
+
appPath: string;
|
|
62
|
+
entrypoint: string;
|
|
63
|
+
/** Leaves out every file or folder whose name this accepts, at every depth. */
|
|
64
|
+
exclude?: (name: string) => boolean;
|
|
65
|
+
}) {
|
|
54
66
|
this.#appPath = options.appPath;
|
|
55
67
|
this.#entrypoint = options.entrypoint;
|
|
68
|
+
this.#exclude = options.exclude;
|
|
56
69
|
}
|
|
57
70
|
|
|
58
71
|
async canBuild(_signal?: AbortSignal): Promise<boolean> {
|
|
@@ -82,6 +95,7 @@ export class PreBuilt implements BuildStrategy {
|
|
|
82
95
|
return {
|
|
83
96
|
directory: this.#appPath,
|
|
84
97
|
entrypoint: normalized.split(path.sep).join("/"),
|
|
98
|
+
...(this.#exclude ? { exclude: this.#exclude } : {}),
|
|
85
99
|
};
|
|
86
100
|
}
|
|
87
101
|
}
|
package/src/compute-client.ts
CHANGED
package/src/errors.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { TaggedError } from "better-result";
|
|
2
|
+
import type { BuildStep } from "./service/build/run-step.ts";
|
|
3
|
+
import type { ServiceBuildSettings } from "./service/settings/types.ts";
|
|
2
4
|
|
|
3
5
|
export class AuthenticationError extends TaggedError("AuthenticationError")<{
|
|
4
6
|
statusCode: 401;
|
|
@@ -187,3 +189,14 @@ export type DeploymentOperationError =
|
|
|
187
189
|
| ApiError
|
|
188
190
|
| TimeoutError
|
|
189
191
|
| DeploymentFailedError;
|
|
192
|
+
|
|
193
|
+
/** A build of one Service that produced no Artifact. `code` is stable across releases. */
|
|
194
|
+
export class ServiceBuildError extends TaggedError("ServiceBuildError")<{
|
|
195
|
+
code: "install_failed" | "build_failed" | "no_start";
|
|
196
|
+
/** For the user. */
|
|
197
|
+
message: string;
|
|
198
|
+
settings: ServiceBuildSettings;
|
|
199
|
+
steps: BuildStep[];
|
|
200
|
+
/** The end of the failed command's output, under a line that gives its exit code. */
|
|
201
|
+
output?: string;
|
|
202
|
+
}>() {}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// The package manager facts every part of the SDK shares. This file imports nothing,
|
|
2
|
+
// so the Build rules can use it in a browser or a Worker.
|
|
3
|
+
|
|
4
|
+
export type PackageManager = "bun" | "pnpm" | "yarn" | "npm";
|
|
5
|
+
|
|
6
|
+
/** Each lockfile and the package manager that writes it. When a folder holds several, the first one wins. */
|
|
7
|
+
export const LOCKFILES: ReadonlyArray<
|
|
8
|
+
readonly [file: string, packageManager: PackageManager]
|
|
9
|
+
> = [
|
|
10
|
+
["bun.lock", "bun"],
|
|
11
|
+
["bun.lockb", "bun"],
|
|
12
|
+
["pnpm-lock.yaml", "pnpm"],
|
|
13
|
+
["yarn.lock", "yarn"],
|
|
14
|
+
["package-lock.json", "npm"],
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
/** The package manager a `packageManager` field such as `pnpm@10.1.0` names, or null when it names none. */
|
|
18
|
+
export function packageManagerFromField(value: unknown): PackageManager | null {
|
|
19
|
+
if (typeof value !== "string") {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const name = value.split("@")[0];
|
|
24
|
+
return name === "bun" || name === "pnpm" || name === "yarn" || name === "npm"
|
|
25
|
+
? name
|
|
26
|
+
: null;
|
|
27
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* True for a file or folder that never goes into an Artifact, at any depth.
|
|
3
|
+
* A CI checkout keeps a token in `.git`, and dotenv files hold local secrets.
|
|
4
|
+
*/
|
|
5
|
+
export function excludedFromArtifact(name: string): boolean {
|
|
6
|
+
return name === ".git" || name === ".env" || name.startsWith(".env.");
|
|
7
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
|
|
4
|
+
/** The one-file Launcher, relative to this folder. `src/` and `dist/` hold this folder at the same depth, so one path fits both. */
|
|
5
|
+
const BUNDLED_LAUNCHER = "../../../dist/service/launcher.mjs";
|
|
6
|
+
|
|
7
|
+
/** The path of the Launcher as one file, ready to copy into an Artifact. */
|
|
8
|
+
export function launcherFile(): string {
|
|
9
|
+
const file = fileURLToPath(new URL(BUNDLED_LAUNCHER, import.meta.url));
|
|
10
|
+
|
|
11
|
+
if (!existsSync(file)) {
|
|
12
|
+
throw new Error(
|
|
13
|
+
`The Launcher is not built: ${file} does not exist. Run "bun scripts/bundle-launcher.ts" in the SDK package to build it.`,
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return file;
|
|
18
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { copyFileSync, mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import {
|
|
4
|
+
LAUNCHER_FILE,
|
|
5
|
+
START_PLAN_FILE,
|
|
6
|
+
type StartPlan,
|
|
7
|
+
} from "../launcher-contract/start-plan.ts";
|
|
8
|
+
import type { Start } from "../settings/types.ts";
|
|
9
|
+
import { launcherFile } from "./launcher-file.ts";
|
|
10
|
+
|
|
11
|
+
/** What a build hands to the caller: a folder and the file Compute starts in it. */
|
|
12
|
+
export interface ServiceArtifact {
|
|
13
|
+
/** The folder to copy or upload, without what `excludedFromArtifact` names. */
|
|
14
|
+
dir: string;
|
|
15
|
+
/** The file Compute starts, relative to `dir`. */
|
|
16
|
+
entrypoint: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Picks the Artifact folder, and writes the Launcher and the Start plan into it. */
|
|
20
|
+
export function writeArtifact(options: {
|
|
21
|
+
serviceDir: string;
|
|
22
|
+
start: Start;
|
|
23
|
+
/** The port the user stated, or null. */
|
|
24
|
+
port: number | null;
|
|
25
|
+
}): ServiceArtifact {
|
|
26
|
+
const dir = artifactDir(options.serviceDir, options.start);
|
|
27
|
+
|
|
28
|
+
mkdirSync(join(dir, dirname(LAUNCHER_FILE)), { recursive: true });
|
|
29
|
+
copyFileSync(launcherFile(), join(dir, LAUNCHER_FILE));
|
|
30
|
+
writeFileSync(
|
|
31
|
+
join(dir, START_PLAN_FILE),
|
|
32
|
+
JSON.stringify(startPlan(options.start, options.port)),
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
return { dir, entrypoint: LAUNCHER_FILE };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** A static site needs no source and no `node_modules`, so its Artifact is the built folder alone. */
|
|
39
|
+
function artifactDir(serviceDir: string, start: Start): string {
|
|
40
|
+
return start.kind === "static" ? join(serviceDir, start.dir) : serviceDir;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function startPlan(start: Start, port: number | null): StartPlan {
|
|
44
|
+
if (start.kind === "static") {
|
|
45
|
+
// The Artifact of a static site is the site's own folder, so the plan serves the Artifact folder itself.
|
|
46
|
+
return { kind: "static", dir: ".", port };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return { kind: "command", command: start.command, port };
|
|
50
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// Finds what the build created by comparing the Service folder before and after the build.
|
|
2
|
+
// The Build rules must not guess output folders by framework.
|
|
3
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import type { BuildOutput } from "../settings/types.ts";
|
|
6
|
+
|
|
7
|
+
/** The folders of the Service folder at one moment. Taken before the build, it tells what the build created. */
|
|
8
|
+
export interface FolderSnapshot {
|
|
9
|
+
topLevelDirs: Set<string>;
|
|
10
|
+
staticDirs: Set<string>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Folders that never hold build output. */
|
|
14
|
+
const SKIPPED_DIRS = new Set(["node_modules", ".git"]);
|
|
15
|
+
|
|
16
|
+
/** How many levels below the Service folder a static site is looked for. */
|
|
17
|
+
const STATIC_SITE_DEPTH = 3;
|
|
18
|
+
|
|
19
|
+
/** How many of the scripts and styles a page links to are checked. */
|
|
20
|
+
const CHECKED_LINKS = 5;
|
|
21
|
+
|
|
22
|
+
/** A script or style the page loads from its own site, such as `src="/assets/app.js"`. The group is the path. */
|
|
23
|
+
const LOCAL_SCRIPT_OR_STYLE =
|
|
24
|
+
/(?:src|href)="(\/[^"/][^"?#]*\.(?:js|mjs|css))["?#]/g;
|
|
25
|
+
|
|
26
|
+
export function snapshotFolders(serviceDir: string): FolderSnapshot {
|
|
27
|
+
return {
|
|
28
|
+
topLevelDirs: new Set(topLevelDirs(serviceDir)),
|
|
29
|
+
staticDirs: new Set(dirsWithIndexHtml(serviceDir, STATIC_SITE_DEPTH)),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** What the build created: the folders that are in the Service folder now and were not in the snapshot. */
|
|
34
|
+
export function buildOutputSince(
|
|
35
|
+
serviceDir: string,
|
|
36
|
+
before: FolderSnapshot,
|
|
37
|
+
): BuildOutput {
|
|
38
|
+
return {
|
|
39
|
+
newDirs: topLevelDirs(serviceDir).filter(
|
|
40
|
+
(dir) => !before.topLevelDirs.has(dir),
|
|
41
|
+
),
|
|
42
|
+
newStaticDirs: dirsWithIndexHtml(serviceDir, STATIC_SITE_DEPTH)
|
|
43
|
+
.filter((dir) => !before.staticDirs.has(dir))
|
|
44
|
+
.filter((dir) => isSelfContained(join(serviceDir, dir)))
|
|
45
|
+
.sort(shallowestFirst),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* True when the site in `dir` holds the first scripts and styles its `index.html` links to.
|
|
51
|
+
* A page copied out of a server build does not, and cannot be served alone.
|
|
52
|
+
*/
|
|
53
|
+
export function isSelfContained(dir: string): boolean {
|
|
54
|
+
const html = readFileSync(join(dir, "index.html"), "utf8");
|
|
55
|
+
const links = [...html.matchAll(LOCAL_SCRIPT_OR_STYLE)]
|
|
56
|
+
.map((match) => match[1] ?? "")
|
|
57
|
+
.slice(0, CHECKED_LINKS);
|
|
58
|
+
|
|
59
|
+
return links.every((link) => existsSync(join(dir, link)));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function topLevelDirs(serviceDir: string): string[] {
|
|
63
|
+
return readdirSync(serviceDir, { withFileTypes: true })
|
|
64
|
+
.filter((entry) => entry.isDirectory() && !SKIPPED_DIRS.has(entry.name))
|
|
65
|
+
.map((entry) => entry.name);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Folders that hold an `index.html`, relative to `serviceDir`, at most `depth` levels below `dir`. */
|
|
69
|
+
function dirsWithIndexHtml(
|
|
70
|
+
serviceDir: string,
|
|
71
|
+
depth: number,
|
|
72
|
+
dir = ".",
|
|
73
|
+
): string[] {
|
|
74
|
+
const found: string[] = [];
|
|
75
|
+
|
|
76
|
+
for (const entry of readdirSync(join(serviceDir, dir), {
|
|
77
|
+
withFileTypes: true,
|
|
78
|
+
})) {
|
|
79
|
+
if (entry.isFile() && entry.name === "index.html") {
|
|
80
|
+
found.push(dir);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (entry.isDirectory() && !SKIPPED_DIRS.has(entry.name) && depth > 0) {
|
|
84
|
+
found.push(
|
|
85
|
+
...dirsWithIndexHtml(serviceDir, depth - 1, join(dir, entry.name)),
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return found;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function shallowestFirst(a: string, b: string): number {
|
|
94
|
+
return a.split("/").length - b.split("/").length || a.localeCompare(b);
|
|
95
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// Builds one Service folder into an Artifact. Runs on the Build runner: it needs a shell,
|
|
2
|
+
// the repository's package manager, and the checkout. It never starts the Service.
|
|
3
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { Result } from "better-result";
|
|
6
|
+
import { ServiceBuildError } from "../../errors.ts";
|
|
7
|
+
import {
|
|
8
|
+
type ServiceArtifact,
|
|
9
|
+
writeArtifact,
|
|
10
|
+
} from "../artifact/write-artifact.ts";
|
|
11
|
+
import {
|
|
12
|
+
resolveServiceBuildSettings,
|
|
13
|
+
resolveStartAfterBuild,
|
|
14
|
+
} from "../settings/resolve.ts";
|
|
15
|
+
import type {
|
|
16
|
+
BuildOutput,
|
|
17
|
+
ReadFile,
|
|
18
|
+
Resolved,
|
|
19
|
+
ServiceBuildSettings,
|
|
20
|
+
ServiceConfig,
|
|
21
|
+
Start,
|
|
22
|
+
} from "../settings/types.ts";
|
|
23
|
+
import { buildOutputSince, snapshotFolders } from "./build-output.ts";
|
|
24
|
+
import {
|
|
25
|
+
type BuildStep,
|
|
26
|
+
runStep,
|
|
27
|
+
type StepRun,
|
|
28
|
+
type StepToRun,
|
|
29
|
+
} from "./run-step.ts";
|
|
30
|
+
|
|
31
|
+
export interface BuildServiceOptions {
|
|
32
|
+
/** The Service folder. */
|
|
33
|
+
dir: string;
|
|
34
|
+
config?: ServiceConfig;
|
|
35
|
+
/** Extra variables for the install and build commands. */
|
|
36
|
+
env?: Record<string, string>;
|
|
37
|
+
/** Receives each command as it starts, and its output. */
|
|
38
|
+
log?: (text: string) => void;
|
|
39
|
+
signal?: AbortSignal;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface BuiltService {
|
|
43
|
+
/** The Build settings, with the start resolved. */
|
|
44
|
+
settings: ServiceBuildSettings;
|
|
45
|
+
start: Resolved<Start>;
|
|
46
|
+
steps: BuildStep[];
|
|
47
|
+
artifact: ServiceArtifact;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** What makes a `ServiceBuildError`, beside the Build settings and the steps. */
|
|
51
|
+
type Failure = Pick<ServiceBuildError, "code" | "message" | "output">;
|
|
52
|
+
|
|
53
|
+
/** The build failures have stable codes: the CLI, Console and the fix task key on them. */
|
|
54
|
+
const STEP_FAILURES = {
|
|
55
|
+
install: { code: "install_failed", label: "Install" },
|
|
56
|
+
build: { code: "build_failed", label: "Build" },
|
|
57
|
+
} as const;
|
|
58
|
+
|
|
59
|
+
const NO_START: Failure = {
|
|
60
|
+
code: "no_start",
|
|
61
|
+
message:
|
|
62
|
+
"No start command. Add a start script to package.json, or set start or static in prisma.config.ts.",
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Runs the Service's install and build commands, decides how the Service starts, and writes the Artifact.
|
|
67
|
+
*
|
|
68
|
+
* A failed install, a failed build and a missing start are expected outcomes:
|
|
69
|
+
* they come back as a `ServiceBuildError`, not as a thrown error.
|
|
70
|
+
*/
|
|
71
|
+
export async function buildService(
|
|
72
|
+
options: BuildServiceOptions,
|
|
73
|
+
): Promise<Result<BuiltService, ServiceBuildError>> {
|
|
74
|
+
const { dir } = options;
|
|
75
|
+
const read = readFrom(dir);
|
|
76
|
+
const settings = await resolveServiceBuildSettings(
|
|
77
|
+
read,
|
|
78
|
+
options.config,
|
|
79
|
+
options.signal,
|
|
80
|
+
);
|
|
81
|
+
const steps: BuildStep[] = [];
|
|
82
|
+
const fail = (failure: Failure) =>
|
|
83
|
+
Result.err(new ServiceBuildError({ ...failure, settings, steps }));
|
|
84
|
+
|
|
85
|
+
const output = await installAndBuild(settings, options, steps);
|
|
86
|
+
if (output.isErr()) {
|
|
87
|
+
return fail(output.error);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const start =
|
|
91
|
+
settings.start === "after build"
|
|
92
|
+
? await resolveStartAfterBuild(read, output.value, options.signal)
|
|
93
|
+
: settings.start;
|
|
94
|
+
if (start === null) {
|
|
95
|
+
return fail(NO_START);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const port = settings.port?.value ?? null;
|
|
99
|
+
const artifact = writeArtifact({ serviceDir: dir, start: start.value, port });
|
|
100
|
+
|
|
101
|
+
return Result.ok({
|
|
102
|
+
settings: { ...settings, start },
|
|
103
|
+
start,
|
|
104
|
+
steps,
|
|
105
|
+
artifact,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Runs the install, then the build. Returns what the build created, or the step that failed. */
|
|
110
|
+
async function installAndBuild(
|
|
111
|
+
settings: ServiceBuildSettings,
|
|
112
|
+
options: BuildServiceOptions,
|
|
113
|
+
steps: BuildStep[],
|
|
114
|
+
): Promise<Result<BuildOutput, Failure>> {
|
|
115
|
+
const { dir, env, signal } = options;
|
|
116
|
+
const run: StepRun = { dir, steps, log: options.log ?? (() => {}), signal };
|
|
117
|
+
|
|
118
|
+
const install = installStep(settings, env);
|
|
119
|
+
const installed = await runStep(install, run);
|
|
120
|
+
if (!installed.ok) {
|
|
121
|
+
return Result.err(stepFailed(install, installed.output));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const beforeBuild = snapshotFolders(dir);
|
|
125
|
+
const build = buildStep(settings, env);
|
|
126
|
+
const built = await runStep(build, run);
|
|
127
|
+
if (!built.ok) {
|
|
128
|
+
return Result.err(stepFailed(build, built.output));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return Result.ok(buildOutputSince(dir, beforeBuild));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** The build runs in production mode. The caller's variables win over these. */
|
|
135
|
+
function buildStep(
|
|
136
|
+
settings: ServiceBuildSettings,
|
|
137
|
+
extraEnv: Record<string, string> = {},
|
|
138
|
+
): StepToRun {
|
|
139
|
+
return {
|
|
140
|
+
name: "build",
|
|
141
|
+
command: settings.build.value,
|
|
142
|
+
env: { ...process.env, CI: "true", NODE_ENV: "production", ...extraEnv },
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The install never runs in production mode: dev dependencies hold the build tools.
|
|
148
|
+
* The caller's variables do not change that, because a package manager in production mode skips those tools.
|
|
149
|
+
*/
|
|
150
|
+
function installStep(
|
|
151
|
+
settings: ServiceBuildSettings,
|
|
152
|
+
extraEnv: Record<string, string> = {},
|
|
153
|
+
): StepToRun {
|
|
154
|
+
return {
|
|
155
|
+
name: "install",
|
|
156
|
+
command: settings.install.value,
|
|
157
|
+
env: { ...process.env, CI: "true", ...extraEnv, NODE_ENV: "development" },
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function stepFailed(step: StepToRun, output: string): Failure {
|
|
162
|
+
const { code, label } = STEP_FAILURES[step.name];
|
|
163
|
+
|
|
164
|
+
return { code, message: `${label} failed: ${step.command}`, output };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Reads the files of the Service folder for the Build rules. */
|
|
168
|
+
function readFrom(dir: string): ReadFile {
|
|
169
|
+
return (path) =>
|
|
170
|
+
existsSync(join(dir, path)) ? readFileSync(join(dir, path), "utf8") : null;
|
|
171
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { runChildProcess } from "../../workspace.ts";
|
|
2
|
+
|
|
3
|
+
/** One command the build ran. */
|
|
4
|
+
export interface BuildStep {
|
|
5
|
+
name: "install" | "build";
|
|
6
|
+
command: string;
|
|
7
|
+
seconds: number;
|
|
8
|
+
ok: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface StepToRun {
|
|
12
|
+
name: BuildStep["name"];
|
|
13
|
+
/** Null when the Build settings have no command for this step. The step is then skipped. */
|
|
14
|
+
command: string | null;
|
|
15
|
+
env: NodeJS.ProcessEnv;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Where the steps of one build run, and where they report. */
|
|
19
|
+
export interface StepRun {
|
|
20
|
+
/** The Service folder. */
|
|
21
|
+
dir: string;
|
|
22
|
+
/** Every step that ran is added here, also a failed one. */
|
|
23
|
+
steps: BuildStep[];
|
|
24
|
+
log: (text: string) => void;
|
|
25
|
+
signal?: AbortSignal;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** How a step ended. `output` is the end of the failed command's output, under a line that gives its exit code. */
|
|
29
|
+
export type StepEnd = { ok: true } | { ok: false; output: string };
|
|
30
|
+
|
|
31
|
+
/** Runs one step's command in the Service folder, timed, with its output passed to `log` line by line. */
|
|
32
|
+
export async function runStep(
|
|
33
|
+
{ name, command, env }: StepToRun,
|
|
34
|
+
{ dir, steps, log, signal }: StepRun,
|
|
35
|
+
): Promise<StepEnd> {
|
|
36
|
+
if (command === null) {
|
|
37
|
+
return { ok: true };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
signal?.throwIfAborted();
|
|
41
|
+
log(`\n$ ${command}\n`);
|
|
42
|
+
const started = Date.now();
|
|
43
|
+
const end = await runChildProcess({
|
|
44
|
+
command,
|
|
45
|
+
args: [],
|
|
46
|
+
cwd: dir,
|
|
47
|
+
env,
|
|
48
|
+
// The command is a line for the shell, such as `tsc && vite build`.
|
|
49
|
+
shell: true,
|
|
50
|
+
failurePrefix: "Service",
|
|
51
|
+
failedStep: name,
|
|
52
|
+
onOutput: (line) => log(`${line}\n`),
|
|
53
|
+
signal,
|
|
54
|
+
}).then(
|
|
55
|
+
(): StepEnd => ({ ok: true }),
|
|
56
|
+
// It rejects only when the command fails or cannot start. Its message ends with the command's output.
|
|
57
|
+
(error: Error): StepEnd => ({ ok: false, output: error.message }),
|
|
58
|
+
);
|
|
59
|
+
signal?.throwIfAborted();
|
|
60
|
+
|
|
61
|
+
const seconds = Math.round((Date.now() - started) / 1000);
|
|
62
|
+
steps.push({ name, command, seconds, ok: end.ok });
|
|
63
|
+
|
|
64
|
+
return end;
|
|
65
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// The public API of `@prisma/compute-sdk/service`: resolve the Build settings of one Service and build it into an Artifact.
|
|
2
|
+
// Nothing here deploys the Artifact. The caller does that.
|
|
3
|
+
export { ServiceBuildError } from "../errors.ts";
|
|
4
|
+
export { excludedFromArtifact } from "./artifact/exclude.ts";
|
|
5
|
+
export type { ServiceArtifact } from "./artifact/write-artifact.ts";
|
|
6
|
+
export {
|
|
7
|
+
type BuildServiceOptions,
|
|
8
|
+
type BuiltService,
|
|
9
|
+
buildService,
|
|
10
|
+
} from "./build/build-service.ts";
|
|
11
|
+
export type { BuildStep } from "./build/run-step.ts";
|
|
12
|
+
export {
|
|
13
|
+
type BuildOutput,
|
|
14
|
+
isDevCommand,
|
|
15
|
+
type PackageManager,
|
|
16
|
+
type ReadFile,
|
|
17
|
+
type Resolved,
|
|
18
|
+
resolveServiceBuildSettings,
|
|
19
|
+
resolveStartAfterBuild,
|
|
20
|
+
type ServiceBuildSettings,
|
|
21
|
+
type ServiceConfig,
|
|
22
|
+
type Source,
|
|
23
|
+
type Start,
|
|
24
|
+
} from "./settings/index.ts";
|