generated from ranajahanzaib/typescript-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch.js
52 lines (41 loc) · 1.46 KB
/
switch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const fs = require("fs");
const { execSync } = require("child_process");
function switchToDevMode() {
console.log("Switching to dev mode...");
execSync("cd ./example/next && git checkout dev", { stdio: "inherit" });
execSync("rm -rf ./example/next/node_modules", { stdio: "inherit" });
execSync("yarn --cwd ./example/next", { stdio: "inherit" });
execSync("yarn dev:next", { stdio: "inherit" });
}
function switchToProdMode() {
console.log("Switching to prod mode...");
execSync("cd ./example/next && git checkout main", { stdio: "inherit" });
execSync("yarn --cwd ./example/next", { stdio: "inherit" });
execSync("yarn --cwd ./example/next dev", { stdio: "inherit" });
}
function checkErrors() {
const errors = [];
const arg = process.argv[2];
const isInvalidArg = !arg || (arg && arg !== "dev" && arg !== "prod");
if (isInvalidArg)
errors.push("Invalid argument. Please use 'dev' or 'prod'.");
const dirOrFileCheckList = ["./example/next/package.json"];
dirOrFileCheckList.forEach((dirOrFile) => {
if (!fs.existsSync(dirOrFile)) errors.push(`${dirOrFile} not found`);
});
if (errors.length) {
errors.forEach((error) => console.error(error));
process.exit(1);
}
}
function init() {
checkErrors();
const arg = process.argv[2];
if (arg === "dev") switchToDevMode();
else if (arg === "prod") switchToProdMode();
else {
console.log("No mode specified. Using default mode (dev).");
switchToDevMode();
}
}
init();