Skip to content

Commit

Permalink
feat: working dir corrected and turbo creation
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrekPKP committed Mar 11, 2023
1 parent 76134d4 commit 18607c1
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
5 changes: 4 additions & 1 deletion cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>((res, rej) => {
server.stdout?.on("data", async (data: Buffer) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/pages/turbo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions src/server/api/routers/metadata.ts
Original file line number Diff line number Diff line change
@@ -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();
}),
});
11 changes: 5 additions & 6 deletions src/server/api/routers/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}),
});
20 changes: 20 additions & 0 deletions src/utils/get-user-data.ts
Original file line number Diff line number Diff line change
@@ -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";
}
};

0 comments on commit 18607c1

Please sign in to comment.