@prisma/compute-sdk 0.47.0 → 0.49.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 +38 -18
- package/dist/errors.d.ts +2 -2
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/service/build/build-service.d.ts +3 -3
- package/dist/service/build/build-service.d.ts.map +1 -1
- package/dist/service/build/build-service.js +32 -15
- package/dist/service/build/build-service.js.map +1 -1
- package/dist/service/build/{build-output.d.ts → output-directories.d.ts} +3 -3
- package/dist/service/build/output-directories.d.ts.map +1 -0
- package/dist/service/build/{build-output.js → output-directories.js} +2 -2
- package/dist/service/build/output-directories.js.map +1 -0
- package/dist/service/index.d.ts +3 -3
- package/dist/service/index.d.ts.map +1 -1
- package/dist/service/index.js +3 -3
- package/dist/service/index.js.map +1 -1
- package/dist/service/launcher-contract/start-plan.d.ts +3 -3
- package/dist/service/launcher-contract/start-plan.d.ts.map +1 -1
- package/dist/service/launcher-contract/start-plan.js +3 -3
- package/dist/service/launcher-contract/start-plan.js.map +1 -1
- package/dist/service/output/build-output.d.ts +47 -0
- package/dist/service/output/build-output.d.ts.map +1 -0
- package/dist/service/output/build-output.js +97 -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/exclude.d.ts +6 -0
- package/dist/service/output/exclude.d.ts.map +1 -0
- package/dist/service/{artifact → output}/exclude.js +2 -2
- package/dist/service/output/exclude.js.map +1 -0
- package/dist/service/output/launcher-file.d.ts +3 -0
- package/dist/service/output/launcher-file.d.ts.map +1 -0
- package/dist/service/{artifact → output}/launcher-file.js +1 -1
- package/dist/service/output/launcher-file.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/dist/service/settings/index.d.ts +1 -1
- package/dist/service/settings/index.d.ts.map +1 -1
- package/dist/service/settings/resolve.d.ts +2 -2
- package/dist/service/settings/resolve.d.ts.map +1 -1
- package/dist/service/settings/resolve.js.map +1 -1
- package/dist/service/settings/start-order.d.ts +2 -2
- package/dist/service/settings/start-order.d.ts.map +1 -1
- package/dist/service/settings/types.d.ts +1 -1
- package/dist/service/settings/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/errors.ts +2 -2
- package/src/service/build/build-service.ts +44 -22
- package/src/service/build/{build-output.ts → output-directories.ts} +3 -3
- package/src/service/index.ts +5 -5
- package/src/service/launcher/main.ts +2 -2
- package/src/service/launcher-contract/start-plan.ts +4 -4
- package/src/service/output/build-output.ts +174 -0
- package/src/service/output/copy.ts +69 -0
- package/src/service/output/entrypoint.ts +250 -0
- package/src/service/{artifact → output}/exclude.ts +2 -2
- package/src/service/{artifact → output}/launcher-file.ts +1 -1
- 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/src/service/settings/index.ts +1 -1
- package/src/service/settings/resolve.ts +2 -2
- package/src/service/settings/start-order.ts +2 -2
- package/src/service/settings/types.ts +1 -1
- package/dist/service/artifact/exclude.d.ts +0 -6
- package/dist/service/artifact/exclude.d.ts.map +0 -1
- package/dist/service/artifact/exclude.js.map +0 -1
- package/dist/service/artifact/launcher-file.d.ts +0 -3
- package/dist/service/artifact/launcher-file.d.ts.map +0 -1
- package/dist/service/artifact/launcher-file.js.map +0 -1
- package/dist/service/artifact/write-artifact.d.ts +0 -16
- package/dist/service/artifact/write-artifact.d.ts.map +0 -1
- package/dist/service/artifact/write-artifact.js +0 -24
- package/dist/service/artifact/write-artifact.js.map +0 -1
- package/dist/service/build/build-output.d.ts.map +0 -1
- package/dist/service/build/build-output.js.map +0 -1
- package/src/service/artifact/write-artifact.ts +0 -50
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// The build output of a Service: the folder a build hands to the caller, and the file Compute starts in it.
|
|
2
|
+
import { copyFileSync, mkdirSync, realpathSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { rm } from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import {
|
|
6
|
+
LAUNCHER_FILE,
|
|
7
|
+
START_PLAN_FILE,
|
|
8
|
+
type StartPlan,
|
|
9
|
+
} from "../launcher-contract/start-plan.ts";
|
|
10
|
+
import type {
|
|
11
|
+
OutputDirectories,
|
|
12
|
+
PackageJson,
|
|
13
|
+
Start,
|
|
14
|
+
} from "../settings/types.ts";
|
|
15
|
+
import { Copy } from "./copy.ts";
|
|
16
|
+
import { excludedFromBuildOutput } from "./exclude.ts";
|
|
17
|
+
import { launcherFile } from "./launcher-file.ts";
|
|
18
|
+
import { inOutputDirectory, sourceFiles } from "./source-files.ts";
|
|
19
|
+
import {
|
|
20
|
+
type ServerTrace,
|
|
21
|
+
traceServer,
|
|
22
|
+
type Untraceable,
|
|
23
|
+
} from "./trace/trace.ts";
|
|
24
|
+
|
|
25
|
+
/** Where the build output of a traced server is written, relative to the Service folder. */
|
|
26
|
+
export const BUILD_OUTPUT_FOLDER = path.join(".prisma", "output");
|
|
27
|
+
|
|
28
|
+
/** What a build hands to the caller: a folder and the file Compute starts in it. */
|
|
29
|
+
export type BuildOutput = {
|
|
30
|
+
/** The folder to copy or upload, without what `excludedFromBuildOutput` names. A real path: no part of it is a symbolic link. */
|
|
31
|
+
dir: string;
|
|
32
|
+
/** The file Compute starts, relative to `dir`. */
|
|
33
|
+
entrypoint: string;
|
|
34
|
+
} & (
|
|
35
|
+
| {
|
|
36
|
+
/** A folder of static files that the build wrote. Nothing else is needed to serve it. */
|
|
37
|
+
kind: "static site";
|
|
38
|
+
}
|
|
39
|
+
| {
|
|
40
|
+
/** The source files and the trace of the server: the files of `node_modules` that it loads. */
|
|
41
|
+
kind: "traced server";
|
|
42
|
+
trace: TraceSummary;
|
|
43
|
+
}
|
|
44
|
+
| {
|
|
45
|
+
/** The whole Service folder, with all of `node_modules`, because the server could not be traced. */
|
|
46
|
+
kind: "whole folder";
|
|
47
|
+
reason: Untraceable;
|
|
48
|
+
}
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
/** What the build output of a traced server holds, for the caller to show. Paths are relative to the Service folder. */
|
|
52
|
+
export interface TraceSummary {
|
|
53
|
+
files: number;
|
|
54
|
+
bytes: number;
|
|
55
|
+
entrypoints: string[];
|
|
56
|
+
bins: string[];
|
|
57
|
+
optionalPeerDependencies: string[];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const SCRIPT_FILE = /\.(c|m)?js$/;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Writes the build output for how the Service starts, with the Launcher and the Start plan in it.
|
|
64
|
+
*
|
|
65
|
+
* - Static files: the folder the build wrote.
|
|
66
|
+
* - A start command: the source files and the trace of the server it starts, in `.prisma/output`.
|
|
67
|
+
* - A start command whose server cannot be traced: the whole Service folder.
|
|
68
|
+
*/
|
|
69
|
+
export async function writeBuildOutput(options: {
|
|
70
|
+
serviceDir: string;
|
|
71
|
+
start: Start;
|
|
72
|
+
/** The port the user stated, or null. */
|
|
73
|
+
port: number | null;
|
|
74
|
+
packageJson: PackageJson;
|
|
75
|
+
outputDirectories: OutputDirectories;
|
|
76
|
+
signal?: AbortSignal;
|
|
77
|
+
}): Promise<BuildOutput> {
|
|
78
|
+
const { start } = options;
|
|
79
|
+
// The trace holds real paths, so every path here starts from the real path of the Service folder.
|
|
80
|
+
const serviceDir = realpathSync(options.serviceDir);
|
|
81
|
+
if (start.kind === "static") {
|
|
82
|
+
const dir = path.join(serviceDir, start.dir);
|
|
83
|
+
writeLauncher(dir, { kind: "static", dir: ".", port: options.port });
|
|
84
|
+
|
|
85
|
+
return { dir, entrypoint: LAUNCHER_FILE, kind: "static site" };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const plan: StartPlan = {
|
|
89
|
+
kind: "command",
|
|
90
|
+
command: start.command,
|
|
91
|
+
port: options.port,
|
|
92
|
+
};
|
|
93
|
+
const source = await sourceFiles(
|
|
94
|
+
serviceDir,
|
|
95
|
+
options.outputDirectories,
|
|
96
|
+
options.signal,
|
|
97
|
+
);
|
|
98
|
+
const trace = await traceServer({
|
|
99
|
+
serviceDir,
|
|
100
|
+
command: start.command,
|
|
101
|
+
packageJson: options.packageJson,
|
|
102
|
+
builtScripts: source.filter(
|
|
103
|
+
(file) =>
|
|
104
|
+
SCRIPT_FILE.test(file) &&
|
|
105
|
+
inOutputDirectory(serviceDir, options.outputDirectories, file),
|
|
106
|
+
),
|
|
107
|
+
signal: options.signal,
|
|
108
|
+
});
|
|
109
|
+
const dir = path.join(serviceDir, BUILD_OUTPUT_FOLDER);
|
|
110
|
+
// An earlier build may have written this folder. It must not stay in a whole-folder build output.
|
|
111
|
+
await rm(dir, { recursive: true, force: true });
|
|
112
|
+
if (typeof trace === "string") {
|
|
113
|
+
writeLauncher(serviceDir, plan);
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
dir: serviceDir,
|
|
117
|
+
entrypoint: LAUNCHER_FILE,
|
|
118
|
+
kind: "whole folder",
|
|
119
|
+
reason: trace,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const copied = await copyTracedServer(serviceDir, dir, source, trace);
|
|
124
|
+
writeLauncher(dir, plan);
|
|
125
|
+
|
|
126
|
+
const relative = (file: string) => path.relative(serviceDir, file);
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
dir,
|
|
130
|
+
entrypoint: LAUNCHER_FILE,
|
|
131
|
+
kind: "traced server",
|
|
132
|
+
trace: {
|
|
133
|
+
files: copied.files,
|
|
134
|
+
bytes: copied.bytes,
|
|
135
|
+
entrypoints: trace.entrypoints.map(relative),
|
|
136
|
+
bins: trace.bins,
|
|
137
|
+
optionalPeerDependencies: trace.optionalPeerDependencies,
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Copies the source files and the trace into the build output folder, each to the place it has in the Service folder. */
|
|
143
|
+
async function copyTracedServer(
|
|
144
|
+
serviceDir: string,
|
|
145
|
+
dir: string,
|
|
146
|
+
source: string[],
|
|
147
|
+
trace: ServerTrace,
|
|
148
|
+
): Promise<Copy> {
|
|
149
|
+
const copy = new Copy(serviceDir, dir);
|
|
150
|
+
|
|
151
|
+
for (const file of source) {
|
|
152
|
+
await copy.file(file);
|
|
153
|
+
}
|
|
154
|
+
for (const file of trace.files) {
|
|
155
|
+
if (!excludedFromBuildOutput(path.basename(file))) {
|
|
156
|
+
await copy.file(file);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
for (const installed of trace.packages) {
|
|
160
|
+
await copy.file(installed.link);
|
|
161
|
+
await copy.packageFolder(installed.dir);
|
|
162
|
+
}
|
|
163
|
+
for (const bin of trace.bins) {
|
|
164
|
+
await copy.file(path.join(serviceDir, "node_modules", ".bin", bin));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return copy;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function writeLauncher(dir: string, plan: StartPlan): void {
|
|
171
|
+
mkdirSync(path.join(dir, path.dirname(LAUNCHER_FILE)), { recursive: true });
|
|
172
|
+
copyFileSync(launcherFile(), path.join(dir, LAUNCHER_FILE));
|
|
173
|
+
writeFileSync(path.join(dir, START_PLAN_FILE), JSON.stringify(plan));
|
|
174
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// Copies files of the Service folder to the same place in the build output folder.
|
|
2
|
+
import {
|
|
3
|
+
copyFile,
|
|
4
|
+
lstat,
|
|
5
|
+
mkdir,
|
|
6
|
+
readdir,
|
|
7
|
+
readlink,
|
|
8
|
+
symlink,
|
|
9
|
+
} from "node:fs/promises";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import { excludedFromBuildOutput } from "./exclude.ts";
|
|
12
|
+
|
|
13
|
+
/** Copies paths from one folder to the same relative place in another. A link stays a link. */
|
|
14
|
+
export class Copy {
|
|
15
|
+
files = 0;
|
|
16
|
+
bytes = 0;
|
|
17
|
+
readonly #from: string;
|
|
18
|
+
readonly #to: string;
|
|
19
|
+
readonly #done = new Set<string>();
|
|
20
|
+
|
|
21
|
+
constructor(from: string, to: string) {
|
|
22
|
+
this.#from = from;
|
|
23
|
+
this.#to = to;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** One file or link. A folder is only created: its files are copied when they are named. */
|
|
27
|
+
async file(source: string): Promise<void> {
|
|
28
|
+
if (this.#done.has(source)) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
this.#done.add(source);
|
|
32
|
+
|
|
33
|
+
const info = await lstat(source).catch(() => null);
|
|
34
|
+
if (info === null) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const destination = path.join(this.#to, path.relative(this.#from, source));
|
|
39
|
+
await mkdir(path.dirname(destination), { recursive: true });
|
|
40
|
+
if (info.isSymbolicLink()) {
|
|
41
|
+
await symlink(await readlink(source), destination);
|
|
42
|
+
} else if (info.isDirectory()) {
|
|
43
|
+
await mkdir(destination, { recursive: true });
|
|
44
|
+
} else if (info.isFile()) {
|
|
45
|
+
await copyFile(source, destination);
|
|
46
|
+
this.files++;
|
|
47
|
+
this.bytes += info.size;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A package folder with everything in it, except the packages in its own `node_modules`
|
|
53
|
+
* and what `excludedFromBuildOutput` names.
|
|
54
|
+
*/
|
|
55
|
+
async packageFolder(dir: string): Promise<void> {
|
|
56
|
+
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
|
57
|
+
const full = path.join(dir, entry.name);
|
|
58
|
+
if (excludedFromBuildOutput(entry.name)) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (!entry.isDirectory()) {
|
|
63
|
+
await this.file(full);
|
|
64
|
+
} else if (entry.name !== "node_modules") {
|
|
65
|
+
await this.packageFolder(full);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
// Reads a start command: the entrypoints of the server, and the bins it runs.
|
|
2
|
+
// The command is never changed. The Launcher runs it as written.
|
|
3
|
+
import { existsSync, realpathSync, statSync } from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import type { PackageJson } from "../settings/types.ts";
|
|
6
|
+
import { excludedFromBuildOutput } from "./exclude.ts";
|
|
7
|
+
|
|
8
|
+
/** What a start command runs. */
|
|
9
|
+
export interface StartCommand {
|
|
10
|
+
/**
|
|
11
|
+
* The files the server starts from, such as `dist/main.js` in `node dist/main.js`,
|
|
12
|
+
* and the files a runtime option preloads, such as `./setup.js` in `node --require ./setup.js server.js`. Absolute.
|
|
13
|
+
*/
|
|
14
|
+
entrypoints: string[];
|
|
15
|
+
/** The names in `node_modules/.bin` that the command runs, such as `tsx` in `tsx src/index.ts`. */
|
|
16
|
+
bins: string[];
|
|
17
|
+
/** The packages a runtime option preloads by name, such as `dotenv` in `node -r dotenv/config server.js`. */
|
|
18
|
+
preloadedPackages: string[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const SOURCE_FILE = /\.(c|m)?(j|t)sx?$/;
|
|
22
|
+
const RUNTIMES = new Set(["node", "bun", "deno", "tsx", "ts-node"]);
|
|
23
|
+
const PACKAGE_MANAGERS = new Set(["npm", "pnpm", "yarn", "bun"]);
|
|
24
|
+
/** Runtime options that preload a module before the entrypoint: `--require ./setup.js` or `--require=./setup.js`. */
|
|
25
|
+
const PRELOAD_OPTIONS = new Set([
|
|
26
|
+
"-r",
|
|
27
|
+
"--require",
|
|
28
|
+
"--import",
|
|
29
|
+
"--loader",
|
|
30
|
+
"--experimental-loader",
|
|
31
|
+
"--preload",
|
|
32
|
+
]);
|
|
33
|
+
/** Node.js reads the value of a preload option as a file only when it is a path. Any other value names a package. */
|
|
34
|
+
const FILE_PATH = /^\.{0,2}\//;
|
|
35
|
+
/** Words that run the command after them. */
|
|
36
|
+
const PREFIXES = new Set([
|
|
37
|
+
"cross-env",
|
|
38
|
+
"dotenv",
|
|
39
|
+
"dotenvx",
|
|
40
|
+
"env",
|
|
41
|
+
"exec",
|
|
42
|
+
"npx",
|
|
43
|
+
"bunx",
|
|
44
|
+
"pnpx",
|
|
45
|
+
]);
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The entrypoints, bins and preloaded packages of `command`, and of the `package.json` scripts it runs.
|
|
49
|
+
* A word that is neither a runtime, a script nor a bin is left out: it is a shell
|
|
50
|
+
* built-in or something the machine provides.
|
|
51
|
+
*/
|
|
52
|
+
export function readStartCommand(
|
|
53
|
+
serviceDir: string,
|
|
54
|
+
command: string,
|
|
55
|
+
packageJson: PackageJson,
|
|
56
|
+
): StartCommand {
|
|
57
|
+
const found: StartCommand = {
|
|
58
|
+
entrypoints: [],
|
|
59
|
+
bins: [],
|
|
60
|
+
preloadedPackages: [],
|
|
61
|
+
};
|
|
62
|
+
read(serviceDir, command, packageJson, found, new Set());
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
entrypoints: [...new Set(found.entrypoints)],
|
|
66
|
+
bins: [...new Set(found.bins)],
|
|
67
|
+
preloadedPackages: [...new Set(found.preloadedPackages)],
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function read(
|
|
72
|
+
serviceDir: string,
|
|
73
|
+
command: string,
|
|
74
|
+
packageJson: PackageJson,
|
|
75
|
+
found: StartCommand,
|
|
76
|
+
seenScripts: Set<string>,
|
|
77
|
+
): void {
|
|
78
|
+
const scripts = packageJson.scripts ?? {};
|
|
79
|
+
|
|
80
|
+
for (const simple of command.split(/&&|\|\||;|\|/)) {
|
|
81
|
+
const [name, ...rest] = wordsOf(simple);
|
|
82
|
+
if (name === undefined) {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const positional = rest.filter((word) => !word.startsWith("-"));
|
|
87
|
+
const script = scriptName(name, positional, scripts);
|
|
88
|
+
if (script !== undefined) {
|
|
89
|
+
if (!seenScripts.has(script)) {
|
|
90
|
+
seenScripts.add(script);
|
|
91
|
+
// npm runs `prestart` and `poststart` around `start`.
|
|
92
|
+
for (const hook of [`pre${script}`, script, `post${script}`]) {
|
|
93
|
+
const text = scripts[hook];
|
|
94
|
+
if (text !== undefined) {
|
|
95
|
+
read(serviceDir, text, packageJson, found, seenScripts);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// A runtime takes one file. A bin may take several: `react-router-serve ./build/server/index.js`.
|
|
103
|
+
const runtime = RUNTIMES.has(name)
|
|
104
|
+
? readRuntimeWords(rest, (word) =>
|
|
105
|
+
resolveFile(serviceDir, word, packageJson),
|
|
106
|
+
)
|
|
107
|
+
: null;
|
|
108
|
+
const files =
|
|
109
|
+
runtime?.files ?? positional.filter((word) => SOURCE_FILE.test(word));
|
|
110
|
+
found.preloadedPackages.push(...(runtime?.packages ?? []));
|
|
111
|
+
for (const word of files) {
|
|
112
|
+
const file = resolveFile(serviceDir, word, packageJson);
|
|
113
|
+
if (file !== null) {
|
|
114
|
+
found.entrypoints.push(file);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (existsSync(path.join(serviceDir, "node_modules", ".bin", name))) {
|
|
119
|
+
found.bins.push(name);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** The words of one simple command, without the variable assignments and prefixes in front of it. */
|
|
125
|
+
function wordsOf(simple: string): string[] {
|
|
126
|
+
let words = simple.trim().split(/\s+/).filter(Boolean);
|
|
127
|
+
|
|
128
|
+
for (;;) {
|
|
129
|
+
const [first] = words;
|
|
130
|
+
if (first === undefined) {
|
|
131
|
+
return [];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const isAssignment = /^[A-Za-z_][A-Za-z0-9_]*=/.test(first);
|
|
135
|
+
if (!isAssignment && !PREFIXES.has(first) && first !== "--") {
|
|
136
|
+
return words;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
words = words.slice(1);
|
|
140
|
+
while (words[0]?.startsWith("-") && words[0] !== "--") {
|
|
141
|
+
words = words.slice(1);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The files a runtime runs and the packages it preloads, from the words after its name.
|
|
148
|
+
*
|
|
149
|
+
* Its entrypoint is the first word that names a script by its extension. With none, it is the
|
|
150
|
+
* first word that names a file, as `bin/www` in `node ./bin/www`. The value of another option
|
|
151
|
+
* can come first, as `.env` in `node --env-file .env server.js`, so a script wins over it
|
|
152
|
+
* and a dotenv file is never the entrypoint.
|
|
153
|
+
*/
|
|
154
|
+
function readRuntimeWords(
|
|
155
|
+
words: string[],
|
|
156
|
+
resolve: (word: string) => string | null,
|
|
157
|
+
): {
|
|
158
|
+
files: string[];
|
|
159
|
+
packages: string[];
|
|
160
|
+
} {
|
|
161
|
+
const files: string[] = [];
|
|
162
|
+
const packages: string[] = [];
|
|
163
|
+
let withoutExtension: string | undefined;
|
|
164
|
+
|
|
165
|
+
for (let index = 0; index < words.length; index++) {
|
|
166
|
+
const word = words[index] ?? "";
|
|
167
|
+
const [option = "", ...value] = word.split("=");
|
|
168
|
+
if (PRELOAD_OPTIONS.has(option)) {
|
|
169
|
+
const preloaded = value.length > 0 ? value.join("=") : words[++index];
|
|
170
|
+
if (preloaded === undefined) {
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (FILE_PATH.test(preloaded)) {
|
|
175
|
+
files.push(preloaded);
|
|
176
|
+
} else {
|
|
177
|
+
packages.push(packageNameOf(preloaded));
|
|
178
|
+
}
|
|
179
|
+
} else if (!word.startsWith("-")) {
|
|
180
|
+
const file = resolve(word);
|
|
181
|
+
if (file !== null && SOURCE_FILE.test(file)) {
|
|
182
|
+
return { files: [...files, word], packages };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const isDotenv = excludedFromBuildOutput(path.basename(file ?? ""));
|
|
186
|
+
if (file !== null && !isDotenv) {
|
|
187
|
+
withoutExtension ??= word;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (withoutExtension !== undefined) {
|
|
193
|
+
files.push(withoutExtension);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return { files, packages };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** The package a module name is in: `dotenv` for `dotenv/config`, `@scope/name` for `@scope/name/register`. */
|
|
200
|
+
function packageNameOf(moduleName: string): string {
|
|
201
|
+
return moduleName
|
|
202
|
+
.split("/")
|
|
203
|
+
.slice(0, moduleName.startsWith("@") ? 2 : 1)
|
|
204
|
+
.join("/");
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** The `package.json` script that `npm run x`, `npm start` or `yarn x` runs, if the words name one. */
|
|
208
|
+
function scriptName(
|
|
209
|
+
packageManager: string,
|
|
210
|
+
positional: string[],
|
|
211
|
+
scripts: Record<string, string>,
|
|
212
|
+
): string | undefined {
|
|
213
|
+
if (!PACKAGE_MANAGERS.has(packageManager)) {
|
|
214
|
+
return undefined;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const [first, second] = positional;
|
|
218
|
+
const name = first === "run" || first === "run-script" ? second : first;
|
|
219
|
+
|
|
220
|
+
return name !== undefined && scripts[name] !== undefined ? name : undefined;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** The file a word names, as Node.js resolves a path to run: the file, with an extension added, or a folder's index. */
|
|
224
|
+
function resolveFile(
|
|
225
|
+
serviceDir: string,
|
|
226
|
+
word: string,
|
|
227
|
+
packageJson: PackageJson,
|
|
228
|
+
): string | null {
|
|
229
|
+
const target = path.resolve(serviceDir, word);
|
|
230
|
+
const candidates = [
|
|
231
|
+
target,
|
|
232
|
+
`${target}.js`,
|
|
233
|
+
`${target}.mjs`,
|
|
234
|
+
`${target}.cjs`,
|
|
235
|
+
`${target}.ts`,
|
|
236
|
+
path.join(target, "index.js"),
|
|
237
|
+
path.join(target, "index.mjs"),
|
|
238
|
+
];
|
|
239
|
+
if (target === serviceDir) {
|
|
240
|
+
candidates.unshift(
|
|
241
|
+
path.resolve(serviceDir, packageJson.main ?? "index.js"),
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const found = candidates.find(
|
|
246
|
+
(file) => existsSync(file) && statSync(file).isFile(),
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
return found === undefined ? null : realpathSync(found);
|
|
250
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* True for a file or folder that never goes into
|
|
2
|
+
* True for a file or folder that never goes into a build output, at any depth.
|
|
3
3
|
* A CI checkout keeps a token in `.git`, and dotenv files hold local secrets.
|
|
4
4
|
*/
|
|
5
|
-
export function
|
|
5
|
+
export function excludedFromBuildOutput(name: string): boolean {
|
|
6
6
|
return name === ".git" || name === ".env" || name.startsWith(".env.");
|
|
7
7
|
}
|
|
@@ -4,7 +4,7 @@ import { fileURLToPath } from "node:url";
|
|
|
4
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
5
|
const BUNDLED_LAUNCHER = "../../../dist/service/launcher.mjs";
|
|
6
6
|
|
|
7
|
-
/** The path of the Launcher as one file, ready to copy into
|
|
7
|
+
/** The path of the Launcher as one file, ready to copy into a build output. */
|
|
8
8
|
export function launcherFile(): string {
|
|
9
9
|
const file = fileURLToPath(new URL(BUNDLED_LAUNCHER, import.meta.url));
|
|
10
10
|
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// The files of a Service that are its own: the repository's files and the output directories.
|
|
2
|
+
import { readdir } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import type { OutputDirectories } from "../settings/types.ts";
|
|
5
|
+
import { excludedFromBuildOutput } from "./exclude.ts";
|
|
6
|
+
|
|
7
|
+
/** Build caches inside an output directory. Nothing reads them at run time. */
|
|
8
|
+
const CACHE_FOLDERS = new Set(["cache", ".cache"]);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Every file of the Service folder except the installed `node_modules`, `.prisma`,
|
|
12
|
+
* and what `excludedFromBuildOutput` names.
|
|
13
|
+
*
|
|
14
|
+
* An output directory is kept with a `node_modules` folder of its own: Nuxt and
|
|
15
|
+
* Next.js put the packages their server needs there. Its cache folders are left out.
|
|
16
|
+
*/
|
|
17
|
+
export async function sourceFiles(
|
|
18
|
+
serviceDir: string,
|
|
19
|
+
outputDirectories: OutputDirectories,
|
|
20
|
+
signal?: AbortSignal,
|
|
21
|
+
): Promise<string[]> {
|
|
22
|
+
const outputDirs = new Set(outputDirectories.newDirs);
|
|
23
|
+
const found: string[] = [];
|
|
24
|
+
|
|
25
|
+
async function walk(dir: string, inOutputDir: boolean): Promise<void> {
|
|
26
|
+
signal?.throwIfAborted();
|
|
27
|
+
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
|
28
|
+
const full = path.join(dir, entry.name);
|
|
29
|
+
if (excludedFromBuildOutput(entry.name)) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (!entry.isDirectory()) {
|
|
34
|
+
found.push(full);
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const atRoot = dir === serviceDir;
|
|
39
|
+
const output = inOutputDir || (atRoot && outputDirs.has(entry.name));
|
|
40
|
+
const left =
|
|
41
|
+
(entry.name === "node_modules" && !output) ||
|
|
42
|
+
(atRoot && entry.name === ".prisma") ||
|
|
43
|
+
(inOutputDir && CACHE_FOLDERS.has(entry.name));
|
|
44
|
+
if (!left) {
|
|
45
|
+
await walk(full, output);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
await walk(serviceDir, false);
|
|
51
|
+
|
|
52
|
+
return found;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** True when `file` is in one of the output directories. */
|
|
56
|
+
export function inOutputDirectory(
|
|
57
|
+
serviceDir: string,
|
|
58
|
+
outputDirectories: OutputDirectories,
|
|
59
|
+
file: string,
|
|
60
|
+
): boolean {
|
|
61
|
+
const [top] = path.relative(serviceDir, file).split(path.sep);
|
|
62
|
+
|
|
63
|
+
return top !== undefined && outputDirectories.newDirs.includes(top);
|
|
64
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// A bin the start command runs, such as `tsx` or `react-router-serve`.
|
|
2
|
+
import { realpathSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import {
|
|
5
|
+
addWithDependencies,
|
|
6
|
+
findPackage,
|
|
7
|
+
type InstalledPackage,
|
|
8
|
+
packageDirOf,
|
|
9
|
+
readManifest,
|
|
10
|
+
} from "./packages.ts";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The packages of the bins, each with the packages it depends on.
|
|
14
|
+
*
|
|
15
|
+
* A bin loads its own files by path, which imports do not show: tracing `tsx` misses
|
|
16
|
+
* a file it needs. So the package of a bin goes into the build output whole.
|
|
17
|
+
*/
|
|
18
|
+
export function binPackages(
|
|
19
|
+
serviceDir: string,
|
|
20
|
+
bins: string[],
|
|
21
|
+
): Map<string, InstalledPackage> {
|
|
22
|
+
const found = new Map<string, InstalledPackage>();
|
|
23
|
+
|
|
24
|
+
for (const name of bins) {
|
|
25
|
+
const bin = path.join(serviceDir, "node_modules", ".bin", name);
|
|
26
|
+
const dir = packageDirOf(realpathSync(bin));
|
|
27
|
+
if (dir === null) {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Under pnpm the package is reached through a link in `node_modules`, which the bin's own link points at.
|
|
32
|
+
const packageName = readManifest(dir).name;
|
|
33
|
+
const installed =
|
|
34
|
+
packageName === undefined ? null : findPackage(serviceDir, packageName);
|
|
35
|
+
addWithDependencies(installed ?? { link: dir, dir }, found);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return found;
|
|
39
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Follows imports from files with `@vercel/nft`.
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { nodeFileTrace } from "@vercel/nft";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Every file that `files` import, directly or through other files. Absolute paths.
|
|
7
|
+
*
|
|
8
|
+
* Compute runs a Service with Bun, which reads a package's `bun` export condition.
|
|
9
|
+
* nft's `conditions` replaces the default set, so the imports are followed twice.
|
|
10
|
+
*/
|
|
11
|
+
export async function traceImports(
|
|
12
|
+
files: string[],
|
|
13
|
+
serviceDir: string,
|
|
14
|
+
): Promise<string[]> {
|
|
15
|
+
if (files.length === 0) {
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const base = path.parse(serviceDir).root;
|
|
20
|
+
const traces = await Promise.all(
|
|
21
|
+
[undefined, ["bun"]].map((conditions) =>
|
|
22
|
+
nodeFileTrace(files, {
|
|
23
|
+
base,
|
|
24
|
+
processCwd: serviceDir,
|
|
25
|
+
...(conditions ? { conditions } : {}),
|
|
26
|
+
}),
|
|
27
|
+
),
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
return [
|
|
31
|
+
...new Set(
|
|
32
|
+
traces.flatMap((trace) =>
|
|
33
|
+
[...trace.fileList].map((file) => path.join(base, file)),
|
|
34
|
+
),
|
|
35
|
+
),
|
|
36
|
+
];
|
|
37
|
+
}
|