From d99e161e40d9fe909049a8cad53591998c58923c Mon Sep 17 00:00:00 2001 From: Aman Varshney Date: Thu, 13 Feb 2025 11:34:49 +0530 Subject: [PATCH] refractor: use script syntax in execa --- .changeset/clean-pandas-double.md | 5 +++++ apps/cli/src/create-project.ts | 18 +++++++----------- apps/cli/src/helpers/db-setup.ts | 30 +++++++++--------------------- apps/cli/src/utils/turso-cli.ts | 6 +++--- bun.lock | 2 +- 5 files changed, 25 insertions(+), 36 deletions(-) create mode 100644 .changeset/clean-pandas-double.md diff --git a/.changeset/clean-pandas-double.md b/.changeset/clean-pandas-double.md new file mode 100644 index 0000000..597c730 --- /dev/null +++ b/.changeset/clean-pandas-double.md @@ -0,0 +1,5 @@ +--- +"create-better-t-stack": patch +--- + +use script syntax in execa diff --git a/apps/cli/src/create-project.ts b/apps/cli/src/create-project.ts index 6908476..17cba48 100644 --- a/apps/cli/src/create-project.ts +++ b/apps/cli/src/create-project.ts @@ -1,6 +1,6 @@ import path from "node:path"; import { confirm, select } from "@inquirer/prompts"; -import { execa } from "execa"; +import { $ } from "execa"; import fs from "fs-extra"; import ora from "ora"; import { setupTurso } from "./helpers/db-setup"; @@ -17,11 +17,7 @@ export async function createProject(options: ProjectConfig) { spinner.succeed(); spinner.start("Cloning template repository..."); - await execa("npx", [ - "degit", - "https://github.com/AmanVarshney01/Better-T-Stack.git", - projectDir, - ]); + await $`npx degit https://github.com/AmanVarshney01/Better-T-Stack.git ${projectDir}`; spinner.succeed(); const initGit = await confirm({ @@ -31,7 +27,7 @@ export async function createProject(options: ProjectConfig) { if (initGit) { spinner.start("Initializing git repository..."); - await execa("git", ["init"], { cwd: projectDir }); + await $`git init ${projectDir}`; spinner.succeed(); } @@ -69,16 +65,16 @@ export async function createProject(options: ProjectConfig) { spinner.start(`Installing dependencies using ${packageManager}...`); switch (packageManager) { case "npm": - await execa("npm", ["install"], { cwd: projectDir }); + await $`npm install ${projectDir}`; break; case "yarn": - await execa("yarn", ["install"], { cwd: projectDir }); + await $`yarn install ${projectDir}`; break; case "pnpm": - await execa("pnpm", ["install"], { cwd: projectDir }); + await $`pnpm install ${projectDir}`; break; case "bun": - await execa("bun", ["install"], { cwd: projectDir }); + await $`bun install ${projectDir}`; break; default: throw new Error("Unsupported package manager"); diff --git a/apps/cli/src/helpers/db-setup.ts b/apps/cli/src/helpers/db-setup.ts index 1d8ae4c..3e5ad5f 100644 --- a/apps/cli/src/helpers/db-setup.ts +++ b/apps/cli/src/helpers/db-setup.ts @@ -1,7 +1,7 @@ import os from "node:os"; import path from "node:path"; import { confirm, input } from "@inquirer/prompts"; -import { execa } from "execa"; +import { $ } from "execa"; import fs from "fs-extra"; import ora, { type Ora } from "ora"; import { logger } from "../utils/logger"; @@ -15,7 +15,7 @@ interface TursoConfig { async function loginToTurso(spinner: Ora) { try { spinner.start("Logging in to Turso..."); - await execa("turso", ["auth", "login"]); + await $`turso auth login`; spinner.succeed("Logged in to Turso successfully!"); } catch (error) { spinner.fail("Failed to log in to Turso"); @@ -28,13 +28,11 @@ async function installTursoCLI(isMac: boolean, spinner: Ora) { spinner.start("Installing Turso CLI..."); if (isMac) { - await execa("brew", ["install", "tursodatabase/tap/turso"]); + await $`brew install tursodatabase/tap/turso`; } else { - const { stdout: installScript } = await execa("curl", [ - "-sSfL", - "https://get.tur.so/install.sh", - ]); - await execa("bash", [], { input: installScript }); + const { stdout: installScript } = + await $`curl -sSfL https://get.tur.so/install.sh`; + await $`bash -c '${installScript}'`; } spinner.succeed("Turso CLI installed successfully!"); @@ -51,7 +49,7 @@ async function installTursoCLI(isMac: boolean, spinner: Ora) { async function createTursoDatabase(dbName: string): Promise { try { - await execa("turso", ["db", "create", dbName]); + await $`turso db create ${dbName}`; } catch (error) { if (error instanceof Error && error.message.includes("already exists")) { throw new Error("DATABASE_EXISTS"); @@ -59,19 +57,9 @@ async function createTursoDatabase(dbName: string): Promise { throw error; } - const { stdout: dbUrl } = await execa("turso", [ - "db", - "show", - dbName, - "--url", - ]); + const { stdout: dbUrl } = await $`turso db show ${dbName} --url`; - const { stdout: authToken } = await execa("turso", [ - "db", - "tokens", - "create", - dbName, - ]); + const { stdout: authToken } = await $`turso db tokens create ${dbName}`; return { dbUrl: dbUrl.trim(), diff --git a/apps/cli/src/utils/turso-cli.ts b/apps/cli/src/utils/turso-cli.ts index 7881ae5..0d5986b 100644 --- a/apps/cli/src/utils/turso-cli.ts +++ b/apps/cli/src/utils/turso-cli.ts @@ -1,8 +1,8 @@ -import { execa } from "execa"; +import { $ } from "execa"; export async function isTursoInstalled() { try { - await execa("turso", ["--version"]); + await $`turso --version`; return true; } catch { return false; @@ -11,7 +11,7 @@ export async function isTursoInstalled() { export async function isTursoLoggedIn() { try { - const output = await execa("turso", ["auth", "whoami"]); + const output = await $`turso auth whoami`; return !output.stdout.includes("You are not logged in"); } catch { return false; diff --git a/bun.lock b/bun.lock index 35ac1e7..1bdb7b6 100644 --- a/bun.lock +++ b/bun.lock @@ -14,7 +14,7 @@ }, "apps/cli": { "name": "create-better-t-stack", - "version": "0.3.0", + "version": "0.3.1", "bin": { "create-better-t-stack": "dist/index.js" },