-
Notifications
You must be signed in to change notification settings - Fork 5.4k
feat: publishing flow for alpha #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "name": "t3", | ||
| "version": "0.1.0", | ||
| "version": "0.1.0-alpha.1", | ||
| "bin": { | ||
| "t3": "./dist/index.mjs" | ||
| }, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { chmodSync, cpSync, mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; | ||
| import { spawnSync } from "node:child_process"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| const repoRoot = fileURLToPath(new URL("..", import.meta.url)); | ||
| const packageDir = fileURLToPath(new URL("../apps/server", import.meta.url)); | ||
| const packageJsonPath = fileURLToPath(new URL("../apps/server/package.json", import.meta.url)); | ||
| const rootPackageJsonPath = fileURLToPath(new URL("../package.json", import.meta.url)); | ||
| const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf8")); | ||
| const rootPackageJson = JSON.parse(readFileSync(rootPackageJsonPath, "utf8")); | ||
| const version = packageJson.version; | ||
| const isDryRun = process.argv.includes("--dry-run"); | ||
|
|
||
| if (!/^\d+\.\d+\.\d+-alpha\.\d+$/.test(version)) { | ||
| throw new Error( | ||
| `Refusing alpha publish because apps/server/package.json is "${version}". Use bun run version:alpha first.`, | ||
| ); | ||
| } | ||
|
|
||
| const build = spawnSync("bun", ["run", "build", "--filter=t3"], { | ||
| cwd: repoRoot, | ||
| stdio: "inherit", | ||
| }); | ||
|
|
||
| if (build.status !== 0) { | ||
| process.exit(build.status ?? 1); | ||
| } | ||
|
|
||
| function readWorkspaceCatalog(rootManifest) { | ||
| const workspaces = rootManifest?.workspaces; | ||
| if (!workspaces || typeof workspaces !== "object" || Array.isArray(workspaces)) { | ||
| return {}; | ||
| } | ||
|
|
||
| const catalog = workspaces.catalog; | ||
| if (!catalog || typeof catalog !== "object" || Array.isArray(catalog)) { | ||
| return {}; | ||
| } | ||
|
|
||
| return catalog; | ||
| } | ||
|
|
||
| function resolveCatalogDependencies(dependencies, catalog, dependencySourceLabel) { | ||
| return Object.fromEntries( | ||
| Object.entries(dependencies ?? {}).map(([dependencyName, spec]) => { | ||
| if (typeof spec !== "string" || !spec.startsWith("catalog:")) { | ||
| return [dependencyName, spec]; | ||
| } | ||
|
|
||
| const catalogKey = spec.slice("catalog:".length).trim(); | ||
| const lookupKey = catalogKey.length > 0 ? catalogKey : dependencyName; | ||
| const resolvedSpec = catalog[lookupKey]; | ||
| if (typeof resolvedSpec !== "string" || resolvedSpec.length === 0) { | ||
| throw new Error( | ||
| `Unable to resolve '${spec}' for ${dependencySourceLabel} dependency '${dependencyName}'.`, | ||
| ); | ||
| } | ||
|
|
||
| return [dependencyName, resolvedSpec]; | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| const publishDir = mkdtempSync(path.join(os.tmpdir(), "t3-alpha-publish-")); | ||
| cpSync(path.join(packageDir, "dist"), path.join(publishDir, "dist"), { recursive: true }); | ||
| mkdirSync(path.join(publishDir, "bin"), { recursive: true }); | ||
| writeFileSync( | ||
| path.join(publishDir, "bin", "t3"), | ||
| '#!/usr/bin/env node\nimport "../dist/index.mjs";\n', | ||
| ); | ||
| chmodSync(path.join(publishDir, "bin", "t3"), 0o755); | ||
|
|
||
| const publishManifest = { | ||
| name: packageJson.name, | ||
| version: packageJson.version, | ||
| type: packageJson.type, | ||
| bin: { | ||
| t3: "bin/t3", | ||
| }, | ||
| main: packageJson.main, | ||
| files: ["dist", "bin"], | ||
| dependencies: resolveCatalogDependencies( | ||
| packageJson.dependencies, | ||
| readWorkspaceCatalog(rootPackageJson), | ||
| "apps/server", | ||
| ), | ||
| }; | ||
|
|
||
| writeFileSync(path.join(publishDir, "package.json"), `${JSON.stringify(publishManifest, null, 2)}\n`); | ||
|
|
||
| const publishArgs = ["publish", "--tag", "alpha"]; | ||
| if (isDryRun) { | ||
| publishArgs.push("--dry-run"); | ||
| } | ||
|
|
||
| const publish = spawnSync("npm", publishArgs, { | ||
| cwd: publishDir, | ||
| stdio: "inherit", | ||
| }); | ||
|
|
||
| rmSync(publishDir, { recursive: true, force: true }); | ||
| process.exit(publish.status ?? 1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { readFileSync, writeFileSync } from "node:fs"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| const packageJsonPath = fileURLToPath(new URL("../apps/server/package.json", import.meta.url)); | ||
| const args = process.argv.slice(2); | ||
| const dryRun = args.includes("--dry-run"); | ||
| const versionArg = args.find((arg) => arg !== "--dry-run"); | ||
|
|
||
| const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf8")); | ||
| const currentVersion = packageJson.version; | ||
|
|
||
| const alphaVersionPattern = /^\d+\.\d+\.\d+-alpha\.\d+$/; | ||
| const alphaCounterPattern = /^(?<base>\d+\.\d+\.\d+)-alpha\.(?<counter>\d+)$/; | ||
|
|
||
| function nextAlphaVersion(version) { | ||
| const alphaMatch = version.match(alphaCounterPattern); | ||
| if (alphaMatch?.groups) { | ||
| const counter = Number.parseInt(alphaMatch.groups.counter, 10); | ||
| return `${alphaMatch.groups.base}-alpha.${counter + 1}`; | ||
| } | ||
|
|
||
| if (version.includes("-")) { | ||
| throw new Error( | ||
| `Cannot derive an alpha version from prerelease version "${version}". Pass an explicit version instead.`, | ||
| ); | ||
| } | ||
|
|
||
| return `${version}-alpha.0`; | ||
| } | ||
|
|
||
| const nextVersion = versionArg ?? nextAlphaVersion(currentVersion); | ||
|
|
||
| if (!alphaVersionPattern.test(nextVersion)) { | ||
| throw new Error( | ||
| `Expected an explicit alpha version like "0.1.0-alpha.0", received "${nextVersion}".`, | ||
| ); | ||
| } | ||
|
|
||
| if (dryRun) { | ||
| console.log(nextVersion); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| packageJson.version = nextVersion; | ||
| writeFileSync(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}\n`); | ||
| console.log(`Updated apps/server/package.json to ${nextVersion}`); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟢 Low
scripts/version-alpha.mjs:15If
versionisundefined(missing frompackage.json), callingversion.match()throws aTypeError. Consider adding a guard at the start of the function to check for this.function nextAlphaVersion(version) { + if (typeof version !== "string") { + throw new Error("Missing or invalid version field in package.json."); + } const alphaMatch = version.match(alphaCounterPattern);🚀 Reply "fix it for me" or copy this AI Prompt for your agent: