From 6cbc7190f281029348ef9aea198f0cbb8de7f415 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Mi=C5=9Btal?= <37044072+szymmis@users.noreply.github.com> Date: Sun, 4 Feb 2024 14:06:33 +0100 Subject: [PATCH] Use cross-env when scaffolding template on windows (#117) --- create-vite-express/src/cli.ts | 37 +++------------------ create-vite-express/src/main.ts | 49 +++++++++++++++++++++++++++ create-vite-express/src/patches.ts | 16 +++++++++ tests/patches.test.ts | 53 ++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 33 deletions(-) create mode 100644 create-vite-express/src/main.ts create mode 100644 create-vite-express/src/patches.ts create mode 100644 tests/patches.test.ts diff --git a/create-vite-express/src/cli.ts b/create-vite-express/src/cli.ts index 24ffcd8..2535cbd 100644 --- a/create-vite-express/src/cli.ts +++ b/create-vite-express/src/cli.ts @@ -1,11 +1,9 @@ -import fs from "fs-extra"; -import * as kolorist from "kolorist"; -import path from "path"; import prompts from "prompts"; +import { main } from "./main"; import { TEMPLATES } from "./templates"; -async function main() { +async function bin() { const answers = await prompts([ { name: "projectName", @@ -29,34 +27,7 @@ async function main() { }, ]); - const { projectName, framework, ts } = answers; - - const templateName = `${framework}${ts ? "-ts" : ""}`; - const templatePath = path.join(__dirname, "..", "templates", templateName); - if (!fs.existsSync(templatePath)) { - console.error( - kolorist.red(`Template ${templateName} at ${templatePath} not found!`), - ); - process.exit(1); - } - - const projectPath = path.resolve(process.cwd(), projectName); - - console.log(); - console.log(`Scaffolding app at ${kolorist.gray(projectPath)}`); - console.log(); - - fs.copySync(templatePath, projectPath); - fs.moveSync( - path.join(projectPath, "_gitignore"), - path.join(projectPath, ".gitignore"), - ); - - console.log(`${kolorist.green("✔")} Done! You can start with:`); - console.log(` ${kolorist.gray("1.")} cd ${projectName}`); - console.log(` ${kolorist.gray("2.")} npm install`); - console.log(` ${kolorist.gray("3.")} npm run dev`); - console.log(kolorist.green("\nHappy hacking! 🎉\n")); + main(answers); } -main(); +bin(); diff --git a/create-vite-express/src/main.ts b/create-vite-express/src/main.ts new file mode 100644 index 0000000..ac8662a --- /dev/null +++ b/create-vite-express/src/main.ts @@ -0,0 +1,49 @@ +import fs from "fs-extra"; +import * as kolorist from "kolorist"; +import path from "path"; + +import { PATCHES } from "./patches"; + +export async function main({ + projectName, + framework, + ts, +}: { + projectName: string; + framework: string; + ts: boolean; +}) { + const templateName = `${framework}${ts ? "-ts" : ""}`; + const templatePath = path.join(__dirname, "..", "templates", templateName); + if (!fs.existsSync(templatePath)) { + console.error( + kolorist.red(`Template ${templateName} at ${templatePath} not found!`), + ); + process.exit(1); + } + + const projectPath = path.resolve(process.cwd(), projectName); + + console.log(); + console.log(`Scaffolding app at ${kolorist.gray(projectPath)}`); + console.log(); + + fs.copySync(templatePath, projectPath); + fs.moveSync( + path.join(projectPath, "_gitignore"), + path.join(projectPath, ".gitignore"), + ); + for (const patch in PATCHES) { + const filePath = path.join(projectPath, patch); + if (fs.existsSync(filePath)) { + const content = fs.readFileSync(filePath, "utf-8"); + fs.writeFileSync(filePath, PATCHES[patch](content)); + } + } + + console.log(`${kolorist.green("✔")} Done! You can start with:`); + console.log(` ${kolorist.gray("1.")} cd ${projectName}`); + console.log(` ${kolorist.gray("2.")} npm install`); + console.log(` ${kolorist.gray("3.")} npm run dev`); + console.log(kolorist.green("\nHappy hacking! 🎉\n")); +} diff --git a/create-vite-express/src/patches.ts b/create-vite-express/src/patches.ts new file mode 100644 index 0000000..5662c69 --- /dev/null +++ b/create-vite-express/src/patches.ts @@ -0,0 +1,16 @@ +export const PATCHES: Record string> = { + "package.json": (content: string) => { + console.log(process.platform); + if (process.platform === "win32") { + const json = JSON.parse(content); + json.scripts["start"] = json.scripts["start"].replace( + "NODE_ENV=production", + "cross-env NODE_ENV=production", + ); + json.dependencies["cross-env"] = "^7.0.3"; + return JSON.stringify(json, null, 2); + } else { + return content; + } + }, +}; diff --git a/tests/patches.test.ts b/tests/patches.test.ts new file mode 100644 index 0000000..7171f7b --- /dev/null +++ b/tests/patches.test.ts @@ -0,0 +1,53 @@ +import fs from "fs"; +import os from "os"; +import path from "path"; +import { beforeAll, describe, expect, test } from "vitest"; + +import { main } from "../create-vite-express/src/main"; + +describe("Templates patches", () => { + describe("When the platform is win32", () => { + const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), "/")); + + beforeAll(() => { + process.chdir(tmpdir); + Object.defineProperty(process, "platform", { value: "win32" }); + }); + + test("install template", async () => { + main({ projectName: "test", framework: "react", ts: false }); + }); + + test("cross-env was added to package.json", () => { + const pkg = JSON.parse( + fs.readFileSync(path.join(tmpdir, "test", "package.json"), "utf-8"), + ); + + expect(pkg.scripts.start).toContain("cross-env NODE_ENV=production"); + expect(pkg.dependencies["cross-env"]).toBeDefined(); + expect(pkg.dependencies["cross-env"]).toBe("^7.0.3"); + }); + }); + + describe("When the platform is linux", () => { + const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), "/")); + + beforeAll(() => { + process.chdir(tmpdir); + Object.defineProperty(process, "platform", { value: "linux" }); + }); + + test("install template", async () => { + main({ projectName: "test", framework: "react", ts: false }); + }); + + test("cross-env was added to package.json", () => { + const pkg = JSON.parse( + fs.readFileSync(path.join(tmpdir, "test", "package.json"), "utf-8"), + ); + + expect(pkg.scripts.start).not.toContain("cross-env NODE_ENV=production"); + expect(pkg.dependencies["cross-env"]).toBeUndefined(); + }); + }); +});