@opentf/afw-cli 0.0.0-stage → 1.31.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/src/runtime.js ADDED
@@ -0,0 +1,135 @@
1
+ // Host primitives for the SSG library on the ES-Runtime (`esdev`).
2
+ //
3
+ // Everything the prerender needs from the host that is not the bundler: awaited
4
+ // filesystem helpers (`runtime:fs` is async-only by design) and package
5
+ // resolution anchored at the *project* rather than at this file.
6
+
7
+ import { exists as fsExists, file, mkdir, readDir, realPath, remove, write } from "runtime:fs";
8
+ import { dirname, fromFileURL, join } from "runtime:path";
9
+
10
+ // ------------------------------------------------------------------ filesystem
11
+
12
+ /**
13
+ * Whether `path` exists. Also false when it sits outside the write/read grant —
14
+ * a caller asking "is this here?" wants an answer, not a capability error.
15
+ */
16
+ export async function exists(path) {
17
+ try {
18
+ return await fsExists(path);
19
+ } catch {
20
+ return false;
21
+ }
22
+ }
23
+
24
+ export const readText = (path) => file(path).text();
25
+
26
+ /** Write `data`, creating the parent directory — `writeFileSync` never had to be told. */
27
+ export async function writeFile(path, data) {
28
+ await mkdir(dirname(path), { recursive: true });
29
+ await write(path, data);
30
+ }
31
+
32
+ export const mkdirp = (dir) => mkdir(dir, { recursive: true });
33
+
34
+ /** Remove a tree, tolerating "already gone" (the `force: true` of `rmSync`). */
35
+ export async function rmrf(dir) {
36
+ try {
37
+ await remove(dir, { recursive: true });
38
+ } catch {}
39
+ }
40
+
41
+ /** Directory entries, or `[]` when the directory is missing. */
42
+ export async function readEntries(dir) {
43
+ try {
44
+ return await readDir(dir);
45
+ } catch {
46
+ return [];
47
+ }
48
+ }
49
+
50
+ /** Nearest ancestor directory of `from` (inclusive) that contains `name`. */
51
+ export async function findUp(name, from) {
52
+ let dir = from;
53
+ for (;;) {
54
+ if (await exists(join(dir, name))) return dir;
55
+ const parent = dirname(dir);
56
+ if (parent === dir) return null;
57
+ dir = parent;
58
+ }
59
+ }
60
+
61
+ // ------------------------------------------------------------------ resolution
62
+
63
+ /**
64
+ * Resolve `sub` ("." or "./x") through a package's `exports` map, the way a node
65
+ * resolver would. `conditions` are tried in order — JS wants `import`/`default`, a
66
+ * stylesheet commonly sits behind `style`.
67
+ */
68
+ export function throughExports(exports, sub, conditions = ["import", "default"]) {
69
+ if (exports == null) return null;
70
+ if (typeof exports === "string") return sub === "." ? exports : null;
71
+ if (Array.isArray(exports)) {
72
+ for (const e of exports) {
73
+ const r = throughExports(e, sub, conditions);
74
+ if (r) return r;
75
+ }
76
+ return null;
77
+ }
78
+ const keys = Object.keys(exports);
79
+ if (keys.some((k) => k.startsWith("."))) {
80
+ if (exports[sub] !== undefined) return throughExports(exports[sub], ".", conditions);
81
+ for (const k of keys) {
82
+ const star = k.indexOf("*"); // "./x/*" subpath patterns
83
+ if (star < 0) continue;
84
+ const pre = k.slice(0, star);
85
+ const post = k.slice(star + 1);
86
+ if (sub.length >= pre.length + post.length && sub.startsWith(pre) && sub.endsWith(post)) {
87
+ const filled = sub.slice(pre.length, sub.length - post.length);
88
+ const target = throughExports(exports[k], ".", conditions);
89
+ if (target) return target.replace("*", filled);
90
+ }
91
+ }
92
+ return null;
93
+ }
94
+ for (const c of conditions) {
95
+ if (exports[c] !== undefined) return throughExports(exports[c], ".", conditions);
96
+ }
97
+ return null;
98
+ }
99
+
100
+ /**
101
+ * Resolve a bare specifier to an absolute file, anchored at `fromDir` — the project
102
+ * root, so the app's own copy of a package wins. Return its canonical path so
103
+ * aliases and package imports share one module instance under symlinked installs.
104
+ *
105
+ * `import.meta.resolve` cannot do this: it resolves against *this* module, which is
106
+ * the CLI's location, not the project's. It is still the fallback for the case where
107
+ * the walk finds nothing (an install layout that hoists somewhere unusual).
108
+ */
109
+ export async function resolveFrom(spec, fromDir, conditions) {
110
+ const m = /^((?:@[^/]+\/)?[^/]+)(?:\/(.*))?$/.exec(spec);
111
+ if (!m) throw new Error(`cannot resolve ${JSON.stringify(spec)}`);
112
+ const [, pkg, rest] = m;
113
+ const sub = rest ? `./${rest}` : ".";
114
+
115
+ for (let dir = fromDir; ; ) {
116
+ const pkgDir = join(dir, "node_modules", pkg);
117
+ const manifest = join(pkgDir, "package.json");
118
+ if (await exists(manifest)) {
119
+ let json = {};
120
+ try {
121
+ json = JSON.parse(await readText(manifest));
122
+ } catch {}
123
+ const target =
124
+ throughExports(json.exports, sub, conditions) ??
125
+ (sub === "." ? (json.module ?? json.main ?? "index.js") : rest);
126
+ const abs = join(pkgDir, target);
127
+ if (await exists(abs)) return realPath(abs);
128
+ }
129
+ const up = dirname(dir);
130
+ if (up === dir) break;
131
+ dir = up;
132
+ }
133
+
134
+ return realPath(fromFileURL(import.meta.resolve(spec)));
135
+ }