Skip to content

Commit

Permalink
Use cross-env when scaffolding template on windows (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
szymmis authored Feb 4, 2024
1 parent 2671a05 commit 6cbc719
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 33 deletions.
37 changes: 4 additions & 33 deletions create-vite-express/src/cli.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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();
49 changes: 49 additions & 0 deletions create-vite-express/src/main.ts
Original file line number Diff line number Diff line change
@@ -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"));
}
16 changes: 16 additions & 0 deletions create-vite-express/src/patches.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const PATCHES: Record<string, (content: string) => 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;
}
},
};
53 changes: 53 additions & 0 deletions tests/patches.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
});

0 comments on commit 6cbc719

Please sign in to comment.