-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use cross-env when scaffolding template on windows (#117)
- Loading branch information
Showing
4 changed files
with
122 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |