zooid 0.7.3 → 0.8.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.
@@ -1,27 +1,39 @@
1
1
  import { existsSync } from 'node:fs'
2
2
  import { dirname, join, resolve } from 'node:path'
3
+ import { fetchWebBundle, type FetchWebBundleOptions } from './fetch.js'
3
4
 
4
5
  const ENV_OVERRIDE = 'ZOOID_DEV_WEB_ROOT_OVERRIDE'
5
6
 
6
- export function resolveWebRoot(cliRoot: string): string {
7
+ export interface EnsureWebRootOptions {
8
+ cliRoot: string
9
+ cacheDir: string
10
+ version: string | undefined
11
+ fetchBundle?: (opts: FetchWebBundleOptions) => Promise<string>
12
+ onProgress?: (msg: string) => void
13
+ }
14
+
15
+ export async function ensureWebRoot(opts: EnsureWebRootOptions): Promise<string> {
7
16
  const override = process.env[ENV_OVERRIDE]
8
17
  if (override && existsSync(join(override, 'index.html'))) return resolve(override)
9
18
 
10
- const published = join(cliRoot, 'dist', 'web')
11
- if (existsSync(join(published, 'index.html'))) return published
12
-
13
- const fromSource = webSourcePackage(cliRoot)
19
+ const fromSource = webSourcePackage(opts.cliRoot)
14
20
  if (fromSource && existsSync(join(fromSource, 'dist', 'index.html'))) {
15
21
  return join(fromSource, 'dist')
16
22
  }
17
23
 
18
- throw new Error(
19
- `@zoon/web build not found.\n Tried: ${published}\n Tried: ${fromSource ?? '(no monorepo source)'}\n` +
20
- `Run \`pnpm -C zoon/packages/web build\` (or set ${ENV_OVERRIDE} to a built dist).`,
21
- )
24
+ if (!opts.version) {
25
+ throw new Error(
26
+ `No @zooid/zoon-web version pin (zooid.zoonWebVersion) in the cli package.json ` +
27
+ `and no monorepo sibling build found.\n` +
28
+ `Set ${ENV_OVERRIDE} to a built dist, or run from the monorepo.`,
29
+ )
30
+ }
31
+ opts.onProgress?.(`Fetching @zooid/zoon-web ${opts.version}…`)
32
+ const fetchBundleFn = opts.fetchBundle ?? fetchWebBundle
33
+ return fetchBundleFn({ version: opts.version, cacheDir: opts.cacheDir })
22
34
  }
23
35
 
24
- // Returns the path to the in-tree @zoon/web package (zoon/packages/web) if the
36
+ // Returns the path to the in-tree @zooid/zoon-web package (zoon/packages/web) if the
25
37
  // CLI is running from the monorepo source. Returns null when running from an
26
38
  // installed package (no sibling zoon/ tree).
27
39
  export function webSourcePackage(cliRoot: string): string | null {
@@ -0,0 +1,28 @@
1
+ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
2
+ import { tmpdir } from 'node:os'
3
+ import { dirname, join } from 'node:path'
4
+ import { createHash } from 'node:crypto'
5
+ import * as tar from 'tar'
6
+
7
+ export function makeBundleTgz(
8
+ files: Record<string, string> = {
9
+ 'package/package.json': '{"name":"@zooid/zoon-web"}',
10
+ 'package/dist/index.html': '<html>zoon</html>',
11
+ 'package/dist/assets/app.js': '// app',
12
+ },
13
+ ): { tgz: Buffer; integrity: string } {
14
+ const dir = mkdtempSync(join(tmpdir(), 'zooid-tgz-'))
15
+ try {
16
+ for (const [path, content] of Object.entries(files)) {
17
+ mkdirSync(join(dir, dirname(path)), { recursive: true })
18
+ writeFileSync(join(dir, path), content)
19
+ }
20
+ const tgz = tar
21
+ .create({ sync: true, gzip: true, cwd: dir, portable: true }, ['package'])
22
+ .read() as Buffer
23
+ const integrity = 'sha512-' + createHash('sha512').update(tgz).digest('base64')
24
+ return { tgz, integrity }
25
+ } finally {
26
+ rmSync(dir, { recursive: true, force: true })
27
+ }
28
+ }