Repository navigation
Expand file tree
/
Copy pathutil.ts
More file actions
197 lines (180 loc) · 6.31 KB
/
Copy pathutil.ts
File metadata and controls
197 lines (180 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { existsSync, readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
export function assertUnreachable(value: never, message = JSON.stringify(value)): never {
throw Error("An unreachable state reached!\n" + message);
}
/**
* Resolve `external` build patterns to a concrete `{ name: version }` map by
* matching them against the packages installed in the project's `node_modules`.
*
* Patterns are matched as literal names or trailing-`*` prefixes, so
* `@opentelemetry/*` matches every installed `@opentelemetry/...` package and
* `import-in-the-middle` matches exactly that package. Node builtins and
* `node:`-prefixed specifiers are ignored (they are provided by the runtime).
*
* The resolved version is read from each package's own `package.json` so the
* generated `server/package.json` pins exactly what was used at build time.
*/
export function resolveExternalDependencies(props: {
external: Array<string>;
nodeModulesDir: string;
}): Record<string, string> {
const { external, nodeModulesDir } = props;
const patterns = external.filter(
(pattern) => !pattern.startsWith("node:") && !isNodeBuiltin(pattern),
);
if (patterns.length === 0) return {};
const installed = listInstalledPackages(nodeModulesDir);
const resolved: Record<string, string> = {};
for (const name of installed) {
if (!patterns.some((pattern) => matchesPattern({ name, pattern }))) continue;
const version = readInstalledVersion({ name, nodeModulesDir });
if (version != null) resolved[name] = version;
}
for (const pattern of patterns) {
if (pattern.endsWith("*")) continue;
if (resolved[pattern] != null) continue;
const version = readInstalledVersion({ name: pattern, nodeModulesDir });
if (version != null) resolved[pattern] = version;
}
return resolved;
}
function matchesPattern(props: { name: string; pattern: string }): boolean {
const { name, pattern } = props;
if (pattern.endsWith("*")) return name.startsWith(pattern.slice(0, -1));
return name === pattern;
}
function listInstalledPackages(nodeModulesDir: string): Array<string> {
if (!existsSync(nodeModulesDir)) return [];
const names: Array<string> = [];
for (const entry of readdirSync(nodeModulesDir, { withFileTypes: true })) {
if (!entry.isDirectory() && !entry.isSymbolicLink()) continue;
if (entry.name.startsWith(".")) continue;
if (entry.name.startsWith("@")) {
const scopeDir = join(nodeModulesDir, entry.name);
for (const scoped of readdirSync(scopeDir, { withFileTypes: true })) {
if (!scoped.isDirectory() && !scoped.isSymbolicLink()) continue;
names.push(`${entry.name}/${scoped.name}`);
}
continue;
}
names.push(entry.name);
}
return names;
}
function readInstalledVersion(props: { name: string; nodeModulesDir: string }): string | undefined {
const { name, nodeModulesDir } = props;
const manifest = join(nodeModulesDir, name, "package.json");
if (!existsSync(manifest)) return undefined;
try {
const parsed: unknown = JSON.parse(readFileSync(manifest, "utf8"));
if (
typeof parsed === "object" &&
parsed != null &&
"version" in parsed &&
typeof parsed.version === "string"
) {
return parsed.version;
}
} catch {
return undefined;
}
return undefined;
}
const nodeBuiltins = new Set([
"assert",
"async_hooks",
"buffer",
"child_process",
"cluster",
"console",
"constants",
"crypto",
"dgram",
"diagnostics_channel",
"dns",
"domain",
"events",
"fs",
"http",
"http2",
"https",
"inspector",
"module",
"net",
"os",
"path",
"perf_hooks",
"process",
"punycode",
"querystring",
"readline",
"repl",
"stream",
"string_decoder",
"sys",
"timers",
"tls",
"trace_events",
"tty",
"url",
"util",
"v8",
"vm",
"wasi",
"worker_threads",
"zlib",
]);
function isNodeBuiltin(specifier: string): boolean {
const name = specifier.startsWith("node:") ? specifier.slice(5) : specifier;
return nodeBuiltins.has(name);
}
/**
* Detect the package manager used by the project from lockfiles in `cwd`,
* falling back to the `npm_config_user_agent` env var, then to `npm`.
*/
export function detectPackageManager(cwd: string): "bun" | "pnpm" | "yarn" | "npm" {
if (existsSync(join(cwd, "bun.lock")) || existsSync(join(cwd, "bun.lockb"))) return "bun";
if (existsSync(join(cwd, "pnpm-lock.yaml"))) return "pnpm";
if (existsSync(join(cwd, "yarn.lock"))) return "yarn";
if (existsSync(join(cwd, "package-lock.json"))) return "npm";
const userAgent = process.env.npm_config_user_agent ?? "";
if (userAgent.startsWith("bun")) return "bun";
if (userAgent.startsWith("pnpm")) return "pnpm";
if (userAgent.startsWith("yarn")) return "yarn";
return "npm";
}
/**
* Build the `install` argv for a given package manager. Installs only the
* dependencies declared in the target directory's `package.json`, without
* touching the project lockfile.
*/
export function installCommand(manager: "bun" | "pnpm" | "yarn" | "npm"): Array<string> {
if (manager === "bun") return ["bun", "install", "--no-save"];
if (manager === "pnpm") return ["pnpm", "install", "--prod", "--no-lockfile"];
if (manager === "yarn") return ["yarn", "install", "--production"];
if (manager === "npm") return ["npm", "install", "--omit=dev", "--no-package-lock"];
return assertUnreachable(manager);
}
export function computeRoutes(files: Array<string>): Array<string> {
return [
...new Set(
files
.map((file) => {
const segments = file.split("/");
// A top-level file (e.g. `favicon.ico`) or a file one level deep
// (e.g. `_app/version.json`) maps to an exact route. Files nested
// deeper collapse to a two-segment wildcard (e.g. `_app/immutable/*`).
//
// Deliberately never emit a bare one-segment wildcard such as
// `_app/*`: SvelteKit serves dynamic endpoints under `_app/` too
// (e.g. remote functions at `_app/remote/*`), and a greedy `_app/*`
// S3 route would shadow them, sending those dynamic requests to the
// asset bucket (404/403) instead of the Lambda default behaviour.
if (segments.length <= 2) return file;
return `${segments[0]}/${segments[1]}/*`;
})
.filter((route): route is string => route != null),
),
];
}