From f5cb5e71a551459854d15a678f72e992f44f4af0 Mon Sep 17 00:00:00 2001 From: Aman Varshney Date: Mon, 22 Dec 2025 21:52:26 +0530 Subject: [PATCH 1/8] phase-1 --- .github/workflows/pr-preview.yaml | 39 +++- .github/workflows/release.yaml | 74 ++++++-- apps/cli/bin/create-better-t-stack | 94 +++++++++ apps/cli/package.json | 46 +++-- apps/cli/scripts/build.ts | 158 ++++++++++++++++ apps/cli/scripts/postinstall.mjs | 129 +++++++++++++ apps/cli/scripts/publish.ts | 136 +++++++++++++ apps/cli/src/cli.ts | 178 +++++++++++++++++- apps/cli/src/constants.ts | 83 +++++++- apps/cli/src/helpers/addons/fumadocs-setup.ts | 8 +- apps/cli/src/helpers/addons/oxlint-setup.ts | 14 +- apps/cli/src/helpers/addons/ruler-setup.ts | 8 +- .../cli/src/helpers/addons/starlight-setup.ts | 10 +- apps/cli/src/helpers/addons/tauri-setup.ts | 10 +- apps/cli/src/helpers/addons/tui-setup.ts | 8 +- .../cli/src/helpers/addons/ultracite-setup.ts | 8 +- apps/cli/src/helpers/addons/wxt-setup.ts | 8 +- apps/cli/src/helpers/core/convex-codegen.ts | 4 +- apps/cli/src/helpers/core/git.ts | 20 +- .../src/helpers/core/install-dependencies.ts | 7 +- apps/cli/src/helpers/core/project-config.ts | 4 +- .../database-providers/mongodb-atlas-setup.ts | 8 +- .../helpers/database-providers/neon-setup.ts | 11 +- .../prisma-postgres-setup.ts | 7 +- .../database-providers/supabase-setup.ts | 38 +--- .../helpers/database-providers/turso-setup.ts | 20 +- apps/cli/src/utils/command-exists.ts | 6 +- apps/cli/src/utils/docker-utils.ts | 4 +- apps/cli/src/utils/file-formatter.ts | 32 +--- apps/cli/src/utils/get-latest-cli-version.ts | 24 ++- apps/cli/src/utils/open-url.ts | 23 +-- apps/cli/tsdown.config.ts | 17 -- bun.lock | 39 +--- 33 files changed, 980 insertions(+), 295 deletions(-) create mode 100644 apps/cli/bin/create-better-t-stack create mode 100644 apps/cli/scripts/build.ts create mode 100644 apps/cli/scripts/postinstall.mjs create mode 100644 apps/cli/scripts/publish.ts delete mode 100644 apps/cli/tsdown.config.ts diff --git a/.github/workflows/pr-preview.yaml b/.github/workflows/pr-preview.yaml index 8ac04cb09..322c365c4 100644 --- a/.github/workflows/pr-preview.yaml +++ b/.github/workflows/pr-preview.yaml @@ -18,6 +18,7 @@ jobs: name: Publish Preview runs-on: ubuntu-latest if: github.event.label.name == 'preview' + timeout-minutes: 30 steps: - name: Checkout PR Code uses: actions/checkout@v4 @@ -64,34 +65,52 @@ jobs: run: cd packages/types && bun publish --access public --tag ${{ steps.version.outputs.tag }} env: NPM_CONFIG_TOKEN: ${{ secrets.NPM_TOKEN }} - BTS_TELEMETRY: 0 - - name: Update CLI package version and types dependency + - name: Update CLI package version and dependencies run: | cd apps/cli - jq --arg v "${{ steps.version.outputs.version }}" '.version = $v | .dependencies["@better-t-stack/types"] = $v' package.json > tmp.json && mv tmp.json package.json + VERSION="${{ steps.version.outputs.version }}" + jq --arg v "$VERSION" ' + .version = $v | + .dependencies["@better-t-stack/types"] = $v | + .optionalDependencies["@better-t-stack/cli-darwin-arm64"] = $v | + .optionalDependencies["@better-t-stack/cli-darwin-x64"] = $v | + .optionalDependencies["@better-t-stack/cli-linux-arm64"] = $v | + .optionalDependencies["@better-t-stack/cli-linux-x64"] = $v | + .optionalDependencies["@better-t-stack/cli-windows-x64"] = $v + ' package.json > tmp.json && mv tmp.json package.json - name: Update create-bts alias package version run: | cd packages/create-bts jq --arg v "${{ steps.version.outputs.version }}" '.version = $v | .dependencies["create-better-t-stack"] = $v' package.json > tmp.json && mv tmp.json package.json - - name: Build CLI + - name: Build CLI binaries run: cd apps/cli && bun run build env: BTS_TELEMETRY: 0 + - name: Publish CLI platform packages to NPM + run: | + cd apps/cli/dist + for dir in better-t-stack-cli-*/; do + echo "Publishing $dir..." + cd "$dir" + bun publish --access public --tag ${{ steps.version.outputs.tag }} + cd .. + done + env: + NPM_CONFIG_TOKEN: ${{ secrets.NPM_TOKEN }} + - name: Publish CLI to NPM run: cd apps/cli && bun publish --access public --tag ${{ steps.version.outputs.tag }} env: NPM_CONFIG_TOKEN: ${{ secrets.NPM_TOKEN }} - BTS_TELEMETRY: 0 - name: Publish create-bts to NPM run: cd packages/create-bts && bun publish --access public --tag ${{ steps.version.outputs.tag }} env: NPM_CONFIG_TOKEN: ${{ secrets.NPM_TOKEN }} - BTS_TELEMETRY: 0 - name: Find existing preview comment uses: peter-evans/find-comment@v3 @@ -117,6 +136,11 @@ jobs: | `create-better-t-stack` | `${{ steps.version.outputs.version }}` | `pr${{ steps.version.outputs.pr }}` | | `create-bts` | `${{ steps.version.outputs.version }}` | `pr${{ steps.version.outputs.pr }}` | | `@better-t-stack/types` | `${{ steps.version.outputs.version }}` | `pr${{ steps.version.outputs.pr }}` | + | `@better-t-stack/cli-darwin-arm64` | `${{ steps.version.outputs.version }}` | `pr${{ steps.version.outputs.pr }}` | + | `@better-t-stack/cli-darwin-x64` | `${{ steps.version.outputs.version }}` | `pr${{ steps.version.outputs.pr }}` | + | `@better-t-stack/cli-linux-arm64` | `${{ steps.version.outputs.version }}` | `pr${{ steps.version.outputs.pr }}` | + | `@better-t-stack/cli-linux-x64` | `${{ steps.version.outputs.version }}` | `pr${{ steps.version.outputs.pr }}` | + | `@better-t-stack/cli-windows-x64` | `${{ steps.version.outputs.version }}` | `pr${{ steps.version.outputs.pr }}` | **Commit:** `${{ steps.version.outputs.commit }}` @@ -124,15 +148,12 @@ jobs: ```bash # Using the PR tag (always gets latest for this PR) - bunx create-better-t-stack@pr${{ steps.version.outputs.pr }} npx create-better-t-stack@pr${{ steps.version.outputs.pr }} # Using exact version - bunx create-better-t-stack@${{ steps.version.outputs.version }} npx create-better-t-stack@${{ steps.version.outputs.version }} # Using the alias - bunx create-bts@pr${{ steps.version.outputs.pr }} npx create-bts@pr${{ steps.version.outputs.pr }} ``` diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index a6d6067c6..288188c71 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -19,7 +19,7 @@ jobs: name: Release to NPM if: startsWith(github.event.head_commit.message, 'chore(release):') runs-on: ubuntu-latest - timeout-minutes: 15 + timeout-minutes: 30 steps: - name: Checkout Code uses: actions/checkout@v4 @@ -104,22 +104,49 @@ jobs: run: cd packages/types && bun publish --access public env: NPM_CONFIG_TOKEN: ${{ secrets.NPM_TOKEN }} - BTS_TELEMETRY: 1 - CONVEX_INGEST_URL: ${{ secrets.CONVEX_INGEST_URL }} - - name: Update CLI types dependency and version + - name: Update CLI version and types dependency if: steps.check-version.outputs.exists == 'false' run: | VERSION="${{ steps.version.outputs.version }}" cd apps/cli - jq --arg v "^$VERSION" '.dependencies["@better-t-stack/types"] = $v' package.json > tmp.json && mv tmp.json package.json + jq --arg v "$VERSION" --arg dep "^$VERSION" '.version = $v | .dependencies["@better-t-stack/types"] = $dep' package.json > tmp.json && mv tmp.json package.json - - name: Build CLI + - name: Update CLI optionalDependencies versions + if: steps.check-version.outputs.exists == 'false' + run: | + VERSION="${{ steps.version.outputs.version }}" + cd apps/cli + jq --arg v "$VERSION" ' + .optionalDependencies["@better-t-stack/cli-darwin-arm64"] = $v | + .optionalDependencies["@better-t-stack/cli-darwin-x64"] = $v | + .optionalDependencies["@better-t-stack/cli-linux-arm64"] = $v | + .optionalDependencies["@better-t-stack/cli-linux-x64"] = $v | + .optionalDependencies["@better-t-stack/cli-windows-x64"] = $v + ' package.json > tmp.json && mv tmp.json package.json + + - name: Build CLI binaries if: steps.check-version.outputs.exists == 'false' run: cd apps/cli && bun run build + + - name: Publish CLI platform packages to NPM + if: steps.check-version.outputs.exists == 'false' + run: | + cd apps/cli/dist + for dir in better-t-stack-cli-*/; do + echo "Publishing $dir..." + cd "$dir" + bun publish --access public + cd .. + done + env: + NPM_CONFIG_TOKEN: ${{ secrets.NPM_TOKEN }} + + - name: Publish CLI main package to NPM + if: steps.check-version.outputs.exists == 'false' + run: cd apps/cli && bun publish --access public env: - BTS_TELEMETRY: 1 - CONVEX_INGEST_URL: ${{ secrets.CONVEX_INGEST_URL }} + NPM_CONFIG_TOKEN: ${{ secrets.NPM_TOKEN }} - name: Update create-bts alias package version if: steps.check-version.outputs.exists == 'false' @@ -128,21 +155,29 @@ jobs: cd packages/create-bts jq --arg v "$VERSION" --arg dep "^$VERSION" '.version = $v | .dependencies["create-better-t-stack"] = $dep' package.json > tmp.json && mv tmp.json package.json - - name: Publish CLI to NPM + - name: Publish create-bts alias to NPM if: steps.check-version.outputs.exists == 'false' - run: cd apps/cli && bun publish --access public + run: cd packages/create-bts && bun publish --access public env: NPM_CONFIG_TOKEN: ${{ secrets.NPM_TOKEN }} - BTS_TELEMETRY: 1 - CONVEX_INGEST_URL: ${{ secrets.CONVEX_INGEST_URL }} - - name: Publish create-bts alias to NPM + - name: Upload binaries to GitHub Release if: steps.check-version.outputs.exists == 'false' - run: cd packages/create-bts && bun publish --access public + run: | + VERSION="${{ steps.version.outputs.version }}" + cd apps/cli/dist + for dir in better-t-stack-cli-*/; do + name="${dir%/}" + if [[ "$name" == *"linux"* ]]; then + tar -czf "${name}.tar.gz" -C "$dir/bin" . + gh release upload "v$VERSION" "${name}.tar.gz" --clobber + else + zip -j "${name}.zip" "$dir/bin/"* + gh release upload "v$VERSION" "${name}.zip" --clobber + fi + done env: - NPM_CONFIG_TOKEN: ${{ secrets.NPM_TOKEN }} - BTS_TELEMETRY: 1 - CONVEX_INGEST_URL: ${{ secrets.CONVEX_INGEST_URL }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Release Summary if: steps.check-version.outputs.exists == 'false' @@ -154,6 +189,11 @@ jobs: echo "- [create-better-t-stack@$VERSION](https://www.npmjs.com/package/create-better-t-stack/v/$VERSION)" >> $GITHUB_STEP_SUMMARY echo "- [create-bts@$VERSION](https://www.npmjs.com/package/create-bts/v/$VERSION)" >> $GITHUB_STEP_SUMMARY echo "- [@better-t-stack/types@$VERSION](https://www.npmjs.com/package/@better-t-stack/types/v/$VERSION)" >> $GITHUB_STEP_SUMMARY + echo "- @better-t-stack/cli-darwin-arm64@$VERSION" >> $GITHUB_STEP_SUMMARY + echo "- @better-t-stack/cli-darwin-x64@$VERSION" >> $GITHUB_STEP_SUMMARY + echo "- @better-t-stack/cli-linux-arm64@$VERSION" >> $GITHUB_STEP_SUMMARY + echo "- @better-t-stack/cli-linux-x64@$VERSION" >> $GITHUB_STEP_SUMMARY + echo "- @better-t-stack/cli-windows-x64@$VERSION" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY echo "### GitHub Release" >> $GITHUB_STEP_SUMMARY echo "[v$VERSION](https://github.com/${{ github.repository }}/releases/tag/v$VERSION)" >> $GITHUB_STEP_SUMMARY diff --git a/apps/cli/bin/create-better-t-stack b/apps/cli/bin/create-better-t-stack new file mode 100644 index 000000000..1dd181644 --- /dev/null +++ b/apps/cli/bin/create-better-t-stack @@ -0,0 +1,94 @@ +#!/usr/bin/env node + +/** + * Bin stub for create-better-t-stack. + * Finds and runs the platform-specific compiled binary. + * + * The binary is installed as an optional dependency: + * @better-t-stack/cli-{platform}-{arch} + * + * This stub searches node_modules for the matching binary package. + */ + +const childProcess = require("child_process"); +const fs = require("fs"); +const path = require("path"); +const os = require("os"); + +function run(target) { + const result = childProcess.spawnSync(target, process.argv.slice(2), { + stdio: "inherit", + }); + if (result.error) { + console.error(result.error.message); + process.exit(1); + } + const code = typeof result.status === "number" ? result.status : 0; + process.exit(code); +} + +// Allow override via environment variable +const envPath = process.env.CREATE_BETTER_T_STACK_BIN_PATH; +if (envPath) { + run(envPath); +} + +const scriptPath = fs.realpathSync(__filename); +const scriptDir = path.dirname(scriptPath); + +const platformMap = { + darwin: "darwin", + linux: "linux", + win32: "windows", +}; + +const archMap = { + x64: "x64", + arm64: "arm64", +}; + +let platform = platformMap[os.platform()]; +if (!platform) { + platform = os.platform(); +} + +let arch = archMap[os.arch()]; +if (!arch) { + arch = os.arch(); +} + +// Scoped package name: @better-t-stack/cli-{platform}-{arch} +const scopedPackage = "@better-t-stack/cli-" + platform + "-" + arch; +const binary = platform === "windows" ? "create-better-t-stack.exe" : "create-better-t-stack"; + +function findBinary(startDir) { + let current = startDir; + for (; ;) { + const modules = path.join(current, "node_modules"); + if (fs.existsSync(modules)) { + // Check for scoped package: node_modules/@better-t-stack/cli-{platform}-{arch} + const scopedPath = path.join(modules, "@better-t-stack", "cli-" + platform + "-" + arch); + const candidate = path.join(scopedPath, "bin", binary); + if (fs.existsSync(candidate)) { + return candidate; + } + } + const parent = path.dirname(current); + if (parent === current) { + return; + } + current = parent; + } +} + +const resolved = findBinary(scriptDir); + +if (!resolved) { + console.error( + 'No binary found for your platform (' + platform + '-' + arch + ').\n' + + 'You can try manually installing the "' + scopedPackage + '" package.', + ); + process.exit(1); +} + +run(resolved); diff --git a/apps/cli/package.json b/apps/cli/package.json index d9eccea25..80558d959 100644 --- a/apps/cli/package.json +++ b/apps/cli/package.json @@ -37,49 +37,40 @@ "directory": "apps/cli" }, "bin": { - "create-better-t-stack": "dist/cli.mjs" + "create-better-t-stack": "./bin/create-better-t-stack" }, "files": [ - "dist", - "templates" + "bin", + "templates", + "scripts/postinstall.mjs" ], "type": "module", - "exports": { - ".": { - "types": "./dist/index.d.mts", - "import": "./dist/index.mjs" - }, - "./cli": { - "import": "./dist/cli.mjs" - } - }, "publishConfig": { "access": "public" }, "scripts": { - "build": "tsdown --publint", - "dev": "tsdown --watch", + "build": "bun run scripts/build.ts", + "build:local": "bun run scripts/build.ts --single", + "dev": "bun run src/cli.ts", "check-types": "tsc --noEmit", - "test": "bun run build && bun test", - "test:watch": "bun run build && bun test --watch", - "test:coverage": "bun run build && bun test --coverage", - "test:ci": "bun run build && AGENT=1 bun test --bail=5", - "prepublishOnly": "npm run build" + "test": "bun test", + "test:watch": "bun test --watch", + "test:coverage": "bun test --coverage", + "test:ci": "AGENT=1 bun test --bail=5", + "prepublishOnly": "echo 'Build binaries separately before publishing'", + "postinstall": "node ./scripts/postinstall.mjs || true" }, "dependencies": { "@better-t-stack/types": "workspace:*", "@clack/prompts": "^1.0.0-alpha.8", - "@orpc/server": "^1.13.0", + "commander": "^14.0.2", "consola": "^3.4.2", - "execa": "^9.6.1", "fs-extra": "^11.3.3", "gradient-string": "^3.0.0", "handlebars": "^4.7.8", "jsonc-parser": "^3.3.1", - "oxfmt": "^0.19.0", "picocolors": "^1.1.1", "tinyglobby": "^0.2.15", - "trpc-cli": "^0.12.1", "ts-morph": "^27.0.2", "yaml": "^2.8.2", "zod": "^4.2.1" @@ -88,8 +79,13 @@ "@types/bun": "^1.3.5", "@types/fs-extra": "^11.0.4", "@types/node": "^25.0.3", - "publint": "^0.3.16", - "tsdown": "^0.18.2", "typescript": "^5.9.3" + }, + "optionalDependencies": { + "@better-t-stack/cli-darwin-arm64": "3.11.0", + "@better-t-stack/cli-darwin-x64": "3.11.0", + "@better-t-stack/cli-linux-arm64": "3.11.0", + "@better-t-stack/cli-linux-x64": "3.11.0", + "@better-t-stack/cli-windows-x64": "3.11.0" } } diff --git a/apps/cli/scripts/build.ts b/apps/cli/scripts/build.ts new file mode 100644 index 000000000..a74430244 --- /dev/null +++ b/apps/cli/scripts/build.ts @@ -0,0 +1,158 @@ +#!/usr/bin/env bun +/** + * Build script for compiling create-better-t-stack as native Bun binaries. + * Generates platform-specific npm packages ready for publishing. + * + * Usage: + * bun run scripts/build.ts # Build for all platforms + * bun run scripts/build.ts --single # Build only for current platform + * + * Output structure: + * dist/@better-t-stack-cli-darwin-arm64/ + * ├── bin/create-better-t-stack + * └── package.json + * dist/@better-t-stack-cli-darwin-x64/ + * ... etc + */ +import path from "node:path"; +import fs from "node:fs"; +import { fileURLToPath } from "node:url"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); +const projectDir = path.resolve(__dirname, ".."); + +process.chdir(projectDir); + +// Read package.json for version +const pkg = JSON.parse(fs.readFileSync("./package.json", "utf-8")); +const version = pkg.version; +const cliName = "create-better-t-stack"; +const scope = "@better-t-stack"; + +const singleFlag = process.argv.includes("--single"); + +interface Target { + os: "linux" | "darwin" | "win32"; + arch: "arm64" | "x64"; +} + +const allTargets: Target[] = [ + { os: "linux", arch: "arm64" }, + { os: "linux", arch: "x64" }, + { os: "darwin", arch: "arm64" }, + { os: "darwin", arch: "x64" }, + { os: "win32", arch: "x64" }, +]; + +const targets = singleFlag + ? allTargets.filter((item) => item.os === process.platform && item.arch === process.arch) + : allTargets; + +if (targets.length === 0) { + console.error(`No matching target for platform ${process.platform}-${process.arch}`); + process.exit(1); +} + +// Clean existing binary builds +for (const target of allTargets) { + const platformName = target.os === "win32" ? "windows" : target.os; + const dirName = `${scope.replace("@", "")}-cli-${platformName}-${target.arch}`; + const outDir = path.join(projectDir, "dist", dirName); + if (fs.existsSync(outDir)) { + fs.rmSync(outDir, { recursive: true }); + } +} + +console.log(`Building ${cliName} v${version}\n`); + +export interface BuiltPackage { + name: string; + version: string; + dirName: string; +} + +const builtPackages: BuiltPackage[] = []; + +for (const target of targets) { + const platformName = target.os === "win32" ? "windows" : target.os; + const packageName = `${scope}/cli-${platformName}-${target.arch}`; + const dirName = `${scope.replace("@", "")}-cli-${platformName}-${target.arch}`; + const binaryName = target.os === "win32" ? `${cliName}.exe` : cliName; + const outDir = path.join(projectDir, "dist", dirName); + const binDir = path.join(outDir, "bin"); + const bunTarget = `bun-${platformName}-${target.arch}`; + + console.log(`Building ${packageName}...`); + + // Create output directories + fs.mkdirSync(binDir, { recursive: true }); + + try { + // Compile the binary using bun build --compile + const compileResult = Bun.spawnSync({ + cmd: [ + "bun", + "build", + "./src/cli.ts", + "--compile", + "--minify", + `--target=${bunTarget}`, + `--define`, + `CLI_VERSION="${version}"`, + `--outfile=${path.join(binDir, binaryName)}`, + ], + cwd: projectDir, + stdout: "pipe", + stderr: "pipe", + }); + + if (compileResult.exitCode !== 0) { + const stderr = compileResult.stderr.toString(); + console.error(` ✗ Compile failed for ${packageName}`); + console.error(` ${stderr}`); + continue; + } + + // Create package.json for the platform package + const platformPkg = { + name: packageName, + version: version, + description: `${cliName} binary for ${platformName}-${target.arch}`, + os: [target.os === "win32" ? "win32" : target.os], + cpu: [target.arch], + bin: { + [cliName]: `./bin/${binaryName}`, + }, + publishConfig: { + access: "public", + }, + }; + + await Bun.write(path.join(outDir, "package.json"), JSON.stringify(platformPkg, null, 2)); + + // Get binary size + const binaryPath = path.join(binDir, binaryName); + const stats = fs.statSync(binaryPath); + const sizeMB = (stats.size / 1024 / 1024).toFixed(2); + + console.log(` ✓ Built ${packageName} (${sizeMB} MB)\n`); + builtPackages.push({ name: packageName, version, dirName }); + } catch (error) { + console.error(` ✗ Error building ${packageName}:`, error); + } +} + +console.log("─".repeat(50)); +console.log(`\nBuild complete! Built ${builtPackages.length}/${targets.length} packages.\n`); + +if (builtPackages.length > 0) { + console.log("Platform packages:"); + for (const pkg of builtPackages) { + console.log(` ${pkg.name}@${pkg.version}`); + } + console.log("\nTo publish, run: bun run scripts/publish.ts"); +} + +// Export for use in publish script +export { builtPackages, version }; diff --git a/apps/cli/scripts/postinstall.mjs b/apps/cli/scripts/postinstall.mjs new file mode 100644 index 000000000..013332855 --- /dev/null +++ b/apps/cli/scripts/postinstall.mjs @@ -0,0 +1,129 @@ +#!/usr/bin/env node + +/** + * Postinstall script for create-better-t-stack. + * Symlinks the platform-specific binary to the bin directory for convenience. + * + * This runs after npm install to set up the binary. + */ + +import fs from "fs"; +import path from "path"; +import os from "os"; +import { fileURLToPath } from "url"; +import { createRequire } from "module"; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); +const require = createRequire(import.meta.url); + +function detectPlatformAndArch() { + let platform; + switch (os.platform()) { + case "darwin": + platform = "darwin"; + break; + case "linux": + platform = "linux"; + break; + case "win32": + platform = "windows"; + break; + default: + platform = os.platform(); + break; + } + + let arch; + switch (os.arch()) { + case "x64": + arch = "x64"; + break; + case "arm64": + arch = "arm64"; + break; + default: + arch = os.arch(); + break; + } + + return { platform, arch }; +} + +function findBinary() { + const { platform, arch } = detectPlatformAndArch(); + // Scoped package: @better-t-stack/cli-{platform}-{arch} + const packageName = `@better-t-stack/cli-${platform}-${arch}`; + const binaryName = platform === "windows" ? "create-better-t-stack.exe" : "create-better-t-stack"; + + try { + // Use require.resolve to find the package + const packageJsonPath = require.resolve(`${packageName}/package.json`); + const packageDir = path.dirname(packageJsonPath); + const binaryPath = path.join(packageDir, "bin", binaryName); + + if (!fs.existsSync(binaryPath)) { + throw new Error(`Binary not found at ${binaryPath}`); + } + + return { binaryPath, binaryName }; + } catch (error) { + throw new Error(`Could not find package ${packageName}: ${error.message}`); + } +} + +function prepareBinDirectory(binaryName) { + const binDir = path.join(__dirname, "..", "bin"); + const targetPath = path.join(binDir, binaryName); + + // Ensure bin directory exists + if (!fs.existsSync(binDir)) { + fs.mkdirSync(binDir, { recursive: true }); + } + + // Remove existing binary/symlink if it exists + try { + if (fs.existsSync(targetPath) || fs.lstatSync(targetPath).isSymbolicLink()) { + fs.unlinkSync(targetPath); + } + } catch { + // File doesn't exist, that's fine + } + + return { binDir, targetPath }; +} + +function symlinkBinary(sourcePath, binaryName) { + const { targetPath } = prepareBinDirectory(binaryName); + + fs.symlinkSync(sourcePath, targetPath); + console.log(`create-better-t-stack binary symlinked: ${targetPath} -> ${sourcePath}`); + + // Verify the file exists after operation + if (!fs.existsSync(targetPath)) { + throw new Error(`Failed to symlink binary to ${targetPath}`); + } +} + +async function main() { + try { + if (os.platform() === "win32") { + // On Windows, symlinks require admin privileges + // The bin stub will find the binary directly + console.log("Windows detected: skipping symlink (bin stub will find binary directly)"); + return; + } + + const { binaryPath, binaryName } = findBinary(); + symlinkBinary(binaryPath, binaryName); + } catch (error) { + // Don't fail the install if we can't symlink - the bin stub will still work + console.log(`Note: Could not symlink binary (${error.message}). The CLI will still work.`); + } +} + +try { + main(); +} catch (error) { + // Silently continue - postinstall failures shouldn't break the install + console.log("Postinstall completed with warnings."); +} diff --git a/apps/cli/scripts/publish.ts b/apps/cli/scripts/publish.ts new file mode 100644 index 000000000..ec46344af --- /dev/null +++ b/apps/cli/scripts/publish.ts @@ -0,0 +1,136 @@ +#!/usr/bin/env bun +/** + * Publish script for create-better-t-stack. + * Builds all platform binaries and publishes them to npm. + * + * Usage: + * bun run scripts/publish.ts # Build and publish all + * bun run scripts/publish.ts --dry-run # Build only, don't publish + */ +import path from "node:path"; +import fs from "node:fs"; +import { $ } from "bun"; +import { fileURLToPath } from "node:url"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); +const projectDir = path.resolve(__dirname, ".."); + +process.chdir(projectDir); + +const dryRun = process.argv.includes("--dry-run"); +const tag = process.argv.includes("--tag") + ? process.argv[process.argv.indexOf("--tag") + 1] + : "latest"; + +// Read package.json +const pkg = JSON.parse(fs.readFileSync("./package.json", "utf-8")); +const version = pkg.version; + +console.log(`📦 Publishing create-better-t-stack v${version}\n`); + +// Step 1: Build all platform binaries +console.log("Step 1: Building binaries for all platforms...\n"); +const { builtPackages } = await import("./build.ts"); + +if (builtPackages.length === 0) { + console.error("No packages were built. Aborting publish."); + process.exit(1); +} + +// Step 2: Smoke test - run the binary for current platform +const currentPlatform = process.platform === "win32" ? "windows" : process.platform; +const currentArch = process.arch; +const currentPackage = builtPackages.find((p) => + p.name.includes(`${currentPlatform}-${currentArch}`), +); + +if (currentPackage) { + console.log(`\nStep 2: Smoke test - running ${currentPackage.name}...`); + const binaryName = + process.platform === "win32" ? "create-better-t-stack.exe" : "create-better-t-stack"; + const binaryPath = path.join(projectDir, "dist", currentPackage.dirName, "bin", binaryName); + + const result = Bun.spawnSync({ + cmd: [binaryPath, "--version"], + stdout: "pipe", + }); + + const output = result.stdout.toString().trim(); + if (output !== version) { + console.error(` ✗ Version mismatch: expected ${version}, got ${output}`); + process.exit(1); + } + console.log(` ✓ Binary reports version ${output}\n`); +} else { + console.log("\nStep 2: Smoke test skipped (no binary for current platform)\n"); +} + +// Step 3: Publish platform packages +console.log("Step 3: Publishing platform packages...\n"); + +for (const built of builtPackages) { + const pkgDir = path.join(projectDir, "dist", built.dirName); + + if (dryRun) { + console.log(` [dry-run] Would publish ${built.name}@${built.version}`); + // Pack to verify it works + await $`bun pm pack`.cwd(pkgDir).quiet(); + console.log(` ✓ Packed ${built.name}`); + } else { + console.log(` Publishing ${built.name}@${built.version}...`); + try { + await $`bun pm pack`.cwd(pkgDir).quiet(); + await $`npm publish *.tgz --access public --tag ${tag}`.cwd(pkgDir); + console.log(` ✓ Published ${built.name}`); + } catch (error) { + console.error(` ✗ Failed to publish ${built.name}:`, error); + process.exit(1); + } + } +} + +// Step 4: Update main package.json with correct versions +console.log("\nStep 4: Updating main package optionalDependencies..."); + +const optionalDeps: Record = {}; +for (const built of builtPackages) { + optionalDeps[built.name] = built.version; +} + +pkg.optionalDependencies = optionalDeps; +await Bun.write("./package.json", JSON.stringify(pkg, null, 2) + "\n"); +console.log(" ✓ Updated package.json\n"); + +// Step 5: Publish main package +console.log("Step 5: Publishing main package...\n"); + +if (dryRun) { + console.log(` [dry-run] Would publish create-better-t-stack@${version}`); + await $`bun pm pack`.quiet(); + console.log(` ✓ Packed create-better-t-stack`); +} else { + console.log(` Publishing create-better-t-stack@${version}...`); + try { + await $`bun pm pack`.quiet(); + await $`npm publish *.tgz --access public --tag ${tag}`; + console.log(` ✓ Published create-better-t-stack`); + } catch (error) { + console.error(` ✗ Failed to publish create-better-t-stack:`, error); + process.exit(1); + } +} + +// Cleanup tarballs +await $`rm -f *.tgz`.quiet(); +for (const built of builtPackages) { + await $`rm -f *.tgz`.cwd(path.join(projectDir, "dist", built.dirName)).quiet(); +} + +console.log("\n" + "─".repeat(50)); +console.log(`\n✅ ${dryRun ? "[DRY RUN] " : ""}Publishing complete!`); +console.log(`\nPublished packages:`); +for (const built of builtPackages) { + console.log(` - ${built.name}@${built.version}`); +} +console.log(` - create-better-t-stack@${version}`); diff --git a/apps/cli/src/cli.ts b/apps/cli/src/cli.ts index 18a2aa28b..a35c951e8 100644 --- a/apps/cli/src/cli.ts +++ b/apps/cli/src/cli.ts @@ -1,3 +1,177 @@ -import { createBtsCli } from "./index"; +#!/usr/bin/env bun +/** + * CLI entry point using commander directly + * Replaces trpc-cli for bun compile compatibility + */ +import { Command } from "commander"; +import { intro, log } from "@clack/prompts"; +import pc from "picocolors"; +import { addAddonsHandler, createProjectHandler } from "./helpers/core/command-handlers"; +import { handleError } from "./utils/errors"; +import { getLatestCLIVersion } from "./utils/get-latest-cli-version"; +import { openUrl } from "./utils/open-url"; +import { renderTitle } from "./utils/render-title"; +import { displaySponsors, fetchSponsors } from "./utils/sponsors"; +import type { + PackageManager, + Database, + ORM, + Backend, + Runtime, + Frontend, + Addons, + Examples, + DatabaseSetup, + API, + WebDeploy, + ServerDeploy, + DirectoryConflict, + Template, + Auth, + Payments, +} from "./types"; -createBtsCli().run(); +const program = new Command(); + +program + .name("create-better-t-stack") + .description("Create a new Better-T-Stack project") + .version(getLatestCLIVersion()) + .argument("[project-name]", "Name of the project") + .option("-t, --template