diff --git a/cli.ts b/cli.ts index 5c07584..e13ae1d 100644 --- a/cli.ts +++ b/cli.ts @@ -12,7 +12,10 @@ const PKG_ROOT = path.join(distPath, "../"); const serverSpinner = ora("Launching UI...").start(); try { - const server = execaCommand("npm start", { cwd: PKG_ROOT }); + const server = execaCommand("npm start", { + cwd: PKG_ROOT, + env: { NODE_ENV: process.env.NODE_ENV, USER_DIR: process.cwd() }, + }); await new Promise((res, rej) => { server.stdout?.on("data", async (data: Buffer) => { diff --git a/package.json b/package.json index eb49690..8b963da 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "create-t3-ui", "description": "Simple to use UI for initializing your T3 app", "license": "MIT", - "version": "0.2.2", + "version": "0.2.3", "type": "module", "repository": { "type": "git", diff --git a/src/pages/turbo/index.tsx b/src/pages/turbo/index.tsx index b80d8d9..3e29af7 100644 --- a/src/pages/turbo/index.tsx +++ b/src/pages/turbo/index.tsx @@ -8,7 +8,7 @@ import { InputWithText } from "~/components/ui/input"; import { Button } from "~/components/ui/button"; import { api, RouterInputs } from "~/utils/api"; -type FormData = RouterInputs["project"]["create"]; +type FormData = RouterInputs["project"]["createTurbo"]; export const projectMetaSchema = z.object({ name: z @@ -21,7 +21,7 @@ export const projectMetaSchema = z.object({ const Index: NextPage = () => { const { data } = api.metadata.getCurrentDirectory.useQuery(); - const { mutateAsync } = api.project.create.useMutation(); + const { mutateAsync } = api.project.createTurbo.useMutation(); const { register, diff --git a/src/server/api/routers/metadata.ts b/src/server/api/routers/metadata.ts index 0feabf3..a0837ab 100644 --- a/src/server/api/routers/metadata.ts +++ b/src/server/api/routers/metadata.ts @@ -1,9 +1,8 @@ -import { z } from "zod"; - import { createTRPCRouter, publicProcedure } from "~/server/api/trpc"; +import { getWorkingDir } from "~/utils/get-user-data"; export const metadataRouter = createTRPCRouter({ getCurrentDirectory: publicProcedure.query(() => { - return process.cwd(); + return getWorkingDir(); }), }); diff --git a/src/server/api/routers/project.ts b/src/server/api/routers/project.ts index c7bc1dc..e28d7e1 100644 --- a/src/server/api/routers/project.ts +++ b/src/server/api/routers/project.ts @@ -2,20 +2,19 @@ import { projectMetaSchema } from "~/pages/turbo"; import { createTRPCRouter, publicProcedure } from "../trpc"; import path from "path"; import { execaCommand } from "execa"; +import { getWorkingDir } from "~/utils/get-user-data"; export const projectRouter = createTRPCRouter({ - create: publicProcedure + createTurbo: publicProcedure .input(projectMetaSchema) .mutation(async ({ input }) => { - const projectPath = path.join(process.cwd(), input.name); + const projectPath = path.join(getWorkingDir(), input.name); await execaCommand( `git clone https://github.com/t3-oss/create-t3-turbo.git ${projectPath}`, - { cwd: process.cwd() } + { cwd: getWorkingDir() } ); - const pkgManager = "pnpm"; - - await execaCommand(`${pkgManager} install`, { cwd: projectPath }); + await execaCommand(`pnpm install`, { cwd: projectPath }); }), }); diff --git a/src/utils/get-user-data.ts b/src/utils/get-user-data.ts new file mode 100644 index 0000000..63883b9 --- /dev/null +++ b/src/utils/get-user-data.ts @@ -0,0 +1,20 @@ +export const getWorkingDir = () => process.env.USER_DIR || process.cwd(); + +export type PackageManager = "npm" | "pnpm" | "yarn"; + +// Used from https://github.com/t3-oss/create-t3-app +export const getPkgManager = (): PackageManager => { + const userAgent = process.env.npm_config_user_agent; + + if (userAgent) { + if (userAgent.startsWith("yarn")) { + return "yarn"; + } else if (userAgent.startsWith("pnpm")) { + return "pnpm"; + } else { + return "npm"; + } + } else { + return "npm"; + } +};