library
Prebuilt pebble components: drop-in custom elements, no compiler needed
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runAucun
0Étoiles
0Forks
2Issues
| 1 | /** |
| 2 | * The declaration files a package ships: those reachable from its |
| 3 | * types/<name>/index.d.ts, nothing else. |
| 4 | * |
| 5 | * tsc emits declarations for every package into one dist/types/ (one run, see |
| 6 | * scripts/build.ts), including modules no public type reaches (dev.d.ts, an |
| 7 | * internal module like toaster's lifetime.d.ts). Shipping the whole tree put |
| 8 | * every component's types in every package, and an unreached file's bare |
| 9 | * import (`@pebble/pebble`) in a package that does not depend on it. |
| 10 | * |
| 11 | * The walk follows `import … from`, `export … from`, `import("…")` and |
| 12 | * `import "…"`. It throws when a relative import leaves types/<name>/ (a |
| 13 | * package's types must stand alone) or a bare import is not in `declared` |
| 14 | * (the package's dependencies and peerDependencies): either would break a |
| 15 | * consumer's type check. |
| 16 | */ |
| 17 | import { existsSync, readFileSync } from "node:fs"; |
| 18 | import { dirname, join, relative } from "node:path"; |
| 19 | |
| 20 | const SPECIFIERS = /(?:\bfrom\s*|\bimport\s*\(\s*|\bimport\s+)["']([^"']+)["']/g; |
| 21 | |
| 22 | /** The file a relative specifier names: `x.d.ts`, or `x/index.d.ts`. */ |
| 23 | function resolved(from: string, specifier: string): string | null { |
| 24 | const base = join(dirname(from), specifier.replace(/\.(js|ts)$/, "")); |
| 25 | for (const candidate of [`${base}.d.ts`, join(base, "index.d.ts")]) if (existsSync(candidate)) return candidate; |
| 26 | return null; |
| 27 | } |
| 28 | |
| 29 | /** Paths under types/<name>/ to ship, relative to it, sorted. */ |
| 30 | export function shipped(typesRoot: string, name: string, declared: readonly string[]): string[] { |
| 31 | const root = join(typesRoot, name); |
| 32 | const entry = join(root, "index.d.ts"); |
| 33 | if (!existsSync(entry)) throw new Error(`${name}: no ${relative(typesRoot, entry)} to start from`); |
| 34 | const seen = new Set<string>([entry]); |
| 35 | const pending = [entry]; |
| 36 | while (pending.length > 0) { |
| 37 | const file = pending.pop()!; |
| 38 | for (const [, specifier] of readFileSync(file, "utf8").matchAll(SPECIFIERS)) { |
| 39 | if (specifier.startsWith(".")) { |
| 40 | const target = resolved(file, specifier); |
| 41 | if (target === null) throw new Error(`${name}: ${relative(typesRoot, file)} imports "${specifier}", which has no declaration`); |
| 42 | if (relative(root, target).startsWith("..")) { |
| 43 | throw new Error(`${name}: ${relative(typesRoot, file)} imports "${specifier}", outside types/${name}/`); |
| 44 | } |
| 45 | if (!seen.has(target)) { |
| 46 | seen.add(target); |
| 47 | pending.push(target); |
| 48 | } |
| 49 | } else { |
| 50 | const pkg = specifier.startsWith("@") ? specifier.split("/").slice(0, 2).join("/") : specifier.split("/")[0]; |
| 51 | if (!declared.includes(pkg)) { |
| 52 | throw new Error(`${name}: ${relative(typesRoot, file)} imports "${specifier}", but ${pkg} is not a dependency of the package`); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | return [...seen].map(file => relative(root, file)).sort(); |
| 58 | } |