|
| 1 | +import { chmodSync, cpSync, mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { spawnSync } from "node:child_process"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | + |
| 7 | +const repoRoot = fileURLToPath(new URL("..", import.meta.url)); |
| 8 | +const packageDir = fileURLToPath(new URL("../apps/server", import.meta.url)); |
| 9 | +const packageJsonPath = fileURLToPath(new URL("../apps/server/package.json", import.meta.url)); |
| 10 | +const rootPackageJsonPath = fileURLToPath(new URL("../package.json", import.meta.url)); |
| 11 | +const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf8")); |
| 12 | +const rootPackageJson = JSON.parse(readFileSync(rootPackageJsonPath, "utf8")); |
| 13 | +const version = packageJson.version; |
| 14 | +const isDryRun = process.argv.includes("--dry-run"); |
| 15 | + |
| 16 | +if (!/^\d+\.\d+\.\d+-alpha\.\d+$/.test(version)) { |
| 17 | + throw new Error( |
| 18 | + `Refusing alpha publish because apps/server/package.json is "${version}". Use bun run version:alpha first.`, |
| 19 | + ); |
| 20 | +} |
| 21 | + |
| 22 | +const build = spawnSync("bun", ["run", "build", "--filter=t3"], { |
| 23 | + cwd: repoRoot, |
| 24 | + stdio: "inherit", |
| 25 | +}); |
| 26 | + |
| 27 | +if (build.status !== 0) { |
| 28 | + process.exit(build.status ?? 1); |
| 29 | +} |
| 30 | + |
| 31 | +function readWorkspaceCatalog(rootManifest) { |
| 32 | + const workspaces = rootManifest?.workspaces; |
| 33 | + if (!workspaces || typeof workspaces !== "object" || Array.isArray(workspaces)) { |
| 34 | + return {}; |
| 35 | + } |
| 36 | + |
| 37 | + const catalog = workspaces.catalog; |
| 38 | + if (!catalog || typeof catalog !== "object" || Array.isArray(catalog)) { |
| 39 | + return {}; |
| 40 | + } |
| 41 | + |
| 42 | + return catalog; |
| 43 | +} |
| 44 | + |
| 45 | +function resolveCatalogDependencies(dependencies, catalog, dependencySourceLabel) { |
| 46 | + return Object.fromEntries( |
| 47 | + Object.entries(dependencies ?? {}).map(([dependencyName, spec]) => { |
| 48 | + if (typeof spec !== "string" || !spec.startsWith("catalog:")) { |
| 49 | + return [dependencyName, spec]; |
| 50 | + } |
| 51 | + |
| 52 | + const catalogKey = spec.slice("catalog:".length).trim(); |
| 53 | + const lookupKey = catalogKey.length > 0 ? catalogKey : dependencyName; |
| 54 | + const resolvedSpec = catalog[lookupKey]; |
| 55 | + if (typeof resolvedSpec !== "string" || resolvedSpec.length === 0) { |
| 56 | + throw new Error( |
| 57 | + `Unable to resolve '${spec}' for ${dependencySourceLabel} dependency '${dependencyName}'.`, |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + return [dependencyName, resolvedSpec]; |
| 62 | + }), |
| 63 | + ); |
| 64 | +} |
| 65 | + |
| 66 | +const publishDir = mkdtempSync(path.join(os.tmpdir(), "t3-alpha-publish-")); |
| 67 | +cpSync(path.join(packageDir, "dist"), path.join(publishDir, "dist"), { recursive: true }); |
| 68 | +mkdirSync(path.join(publishDir, "bin"), { recursive: true }); |
| 69 | +writeFileSync( |
| 70 | + path.join(publishDir, "bin", "t3"), |
| 71 | + '#!/usr/bin/env node\nimport "../dist/index.mjs";\n', |
| 72 | +); |
| 73 | +chmodSync(path.join(publishDir, "bin", "t3"), 0o755); |
| 74 | + |
| 75 | +const publishManifest = { |
| 76 | + name: packageJson.name, |
| 77 | + version: packageJson.version, |
| 78 | + type: packageJson.type, |
| 79 | + bin: { |
| 80 | + t3: "bin/t3", |
| 81 | + }, |
| 82 | + main: packageJson.main, |
| 83 | + files: ["dist", "bin"], |
| 84 | + dependencies: resolveCatalogDependencies( |
| 85 | + packageJson.dependencies, |
| 86 | + readWorkspaceCatalog(rootPackageJson), |
| 87 | + "apps/server", |
| 88 | + ), |
| 89 | +}; |
| 90 | + |
| 91 | +writeFileSync(path.join(publishDir, "package.json"), `${JSON.stringify(publishManifest, null, 2)}\n`); |
| 92 | + |
| 93 | +const publishArgs = ["publish", "--tag", "alpha"]; |
| 94 | +if (isDryRun) { |
| 95 | + publishArgs.push("--dry-run"); |
| 96 | +} |
| 97 | + |
| 98 | +const publish = spawnSync("npm", publishArgs, { |
| 99 | + cwd: publishDir, |
| 100 | + stdio: "inherit", |
| 101 | +}); |
| 102 | + |
| 103 | +rmSync(publishDir, { recursive: true, force: true }); |
| 104 | +process.exit(publish.status ?? 1); |
0 commit comments