|
| 1 | +import { spawnSync } from 'node:child_process' |
| 2 | +import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs' |
| 3 | +import { dirname, resolve } from 'node:path' |
| 4 | +import process from 'node:process' |
| 5 | +import { fileURLToPath } from 'node:url' |
| 6 | + |
| 7 | +const rootDir = resolve(dirname(fileURLToPath(import.meta.url)), '..') |
| 8 | +const ecosystemDir = resolve(rootDir, '.ecosystem') |
| 9 | +const devtoolsDir = resolve(ecosystemDir, 'devtools') |
| 10 | + |
| 11 | +const REPO_URL = 'https://github.com/vitejs/devtools.git' |
| 12 | +const RELEASES_API = 'https://api.github.com/repos/vitejs/devtools/releases/latest' |
| 13 | +const KEEP = process.env.ECOSYSTEM_KEEP === '1' |
| 14 | + |
| 15 | +interface PackageManifest { |
| 16 | + name: string |
| 17 | + version: string |
| 18 | + pnpm?: { |
| 19 | + overrides?: Record<string, string> |
| 20 | + [key: string]: unknown |
| 21 | + } |
| 22 | + [key: string]: unknown |
| 23 | +} |
| 24 | + |
| 25 | +async function main(): Promise<void> { |
| 26 | + const ref = await resolveRef() |
| 27 | + log(`Target: vitejs/devtools @ ${ref}`) |
| 28 | + |
| 29 | + const tarball = packDevframe() |
| 30 | + log(`Packed devframe: ${tarball}`) |
| 31 | + |
| 32 | + prepareClone(ref) |
| 33 | + patchPackageJson(devtoolsDir, tarball) |
| 34 | + |
| 35 | + run('pnpm', ['install', '--no-frozen-lockfile'], devtoolsDir) |
| 36 | + run('pnpm', ['build'], devtoolsDir) |
| 37 | + run('pnpm', ['test'], devtoolsDir) |
| 38 | + |
| 39 | + log('All downstream checks passed') |
| 40 | +} |
| 41 | + |
| 42 | +async function resolveRef(): Promise<string> { |
| 43 | + const override = process.env.ECOSYSTEM_DEVTOOLS_REF |
| 44 | + if (override) |
| 45 | + return override |
| 46 | + |
| 47 | + const headers: Record<string, string> = { 'user-agent': 'devframe-ecosystem-ci' } |
| 48 | + if (process.env.GITHUB_TOKEN) |
| 49 | + headers.authorization = `Bearer ${process.env.GITHUB_TOKEN}` |
| 50 | + |
| 51 | + const res = await fetch(RELEASES_API, { headers }) |
| 52 | + if (!res.ok) |
| 53 | + throw new Error(`Failed to query latest release: ${res.status} ${res.statusText}`) |
| 54 | + const data = await res.json() as { tag_name?: string } |
| 55 | + if (!data.tag_name) |
| 56 | + throw new Error('GitHub release payload missing tag_name') |
| 57 | + return data.tag_name |
| 58 | +} |
| 59 | + |
| 60 | +function packDevframe(): string { |
| 61 | + const devframePkg = resolve(rootDir, 'packages', 'devframe') |
| 62 | + const pkg = readManifest(resolve(devframePkg, 'package.json')) |
| 63 | + const expected = `${pkg.name}-${pkg.version}.tgz` |
| 64 | + |
| 65 | + mkdirSync(ecosystemDir, { recursive: true }) |
| 66 | + for (const f of readdirSync(ecosystemDir)) { |
| 67 | + if (/^devframe-\d.*\.tgz$/.test(f)) |
| 68 | + rmSync(resolve(ecosystemDir, f)) |
| 69 | + } |
| 70 | + |
| 71 | + run('pnpm', ['pack', '--pack-destination', ecosystemDir], devframePkg) |
| 72 | + |
| 73 | + const tarball = resolve(ecosystemDir, expected) |
| 74 | + if (!existsSync(tarball)) |
| 75 | + throw new Error(`Expected packed tarball not found: ${tarball}`) |
| 76 | + return tarball |
| 77 | +} |
| 78 | + |
| 79 | +function prepareClone(ref: string): void { |
| 80 | + mkdirSync(ecosystemDir, { recursive: true }) |
| 81 | + |
| 82 | + if (KEEP && existsSync(devtoolsDir)) { |
| 83 | + log(`Reusing existing clone at ${devtoolsDir} (ECOSYSTEM_KEEP=1)`) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + if (existsSync(devtoolsDir)) |
| 88 | + rmSync(devtoolsDir, { recursive: true, force: true }) |
| 89 | + |
| 90 | + run('git', ['clone', '--depth', '1', '--branch', ref, REPO_URL, devtoolsDir]) |
| 91 | +} |
| 92 | + |
| 93 | +function patchPackageJson(repoDir: string, tarball: string): void { |
| 94 | + const pkgPath = resolve(repoDir, 'package.json') |
| 95 | + const pkg = readManifest(pkgPath) |
| 96 | + pkg.pnpm ??= {} |
| 97 | + pkg.pnpm.overrides = { ...(pkg.pnpm.overrides ?? {}), devframe: `file:${tarball}` } |
| 98 | + writeFileSync(pkgPath, `${JSON.stringify(pkg, null, 2)}\n`) |
| 99 | + log(`Patched ${pkgPath}: devframe -> file:${tarball}`) |
| 100 | +} |
| 101 | + |
| 102 | +function readManifest(file: string): PackageManifest { |
| 103 | + return JSON.parse(readFileSync(file, 'utf8')) as PackageManifest |
| 104 | +} |
| 105 | + |
| 106 | +function run(cmd: string, args: string[], cwd: string = rootDir): void { |
| 107 | + log(`$ ${cmd} ${args.join(' ')} (in ${cwd})`) |
| 108 | + const result = spawnSync(cmd, args, { cwd, stdio: 'inherit', shell: false }) |
| 109 | + if (result.status !== 0) { |
| 110 | + throw new Error( |
| 111 | + `Command failed (exit ${result.status ?? 'unknown'}): ${cmd} ${args.join(' ')}`, |
| 112 | + ) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +function log(msg: string): void { |
| 117 | + console.log(`[ecosystem-ci] ${msg}`) |
| 118 | +} |
| 119 | + |
| 120 | +main().catch((err) => { |
| 121 | + console.error(err instanceof Error ? err.message : err) |
| 122 | + process.exit(1) |
| 123 | +}) |
0 commit comments