-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
128 lines (116 loc) · 3.09 KB
/
index.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env node
const inquirer = require("inquirer");
const degit = require("degit");
const ora = require("ora");
const { exec } = require("child_process");
const cwd = process.cwd();
const spinner = ora({ text: "Installing packages", color: "green" });
const choiceMap = {
vanilla: "vanilla chext",
cms: "with CMS (yarn only)",
typescript: "typescript",
typescriptCms: "typescript with CMS (yarn only)",
};
const choices = Object.values(choiceMap);
inquirer
.prompt([
{
type: "input",
message: "Please give your project a name:",
name: "projectName",
default: "chext-starter",
},
{
type: "list",
message: "Please select a starter variant:",
name: "starterBranch",
choices,
default: "vanilla",
},
{
type: "list",
message: "Select your package manager",
name: "pkgManager",
choices: ({ starterBranch }) => {
switch (starterBranch) {
case choiceMap.cms:
case choiceMap.typescriptCms:
return ["yarn"];
default:
return ["npm", "yarn"];
}
},
default: "npm",
},
])
.then(({ projectName, starterBranch, pkgManager }) => {
let path = cwd + "/" + projectName;
let branch = "";
switch (starterBranch) {
case "vanilla chext":
branch = "main";
break;
case choiceMap.cms:
branch = "sanity";
break;
case choiceMap.typescript:
branch = "typescript";
break;
case choiceMap.typescriptCms:
branch = "typescript-sanity";
break;
default:
branch = "main";
}
const emitter = degit(`XanderJL/chext-starter#${branch}`, {
verbose: true,
});
emitter.on("info", (info) => {
console.log(info.message);
});
emitter.clone(path).then(() => {
console.log("done");
let installer = "";
switch (pkgManager) {
case "yarn":
switch (starterBranch) {
case choiceMap.cms:
case choiceMap.typescriptCms:
installer = "yarn install && cd studio && yarn install && cd ..";
break;
default:
installer = `yarn install`;
break;
}
break;
default:
installer = "npm install";
}
spinner.start();
exec(`cd ${projectName} && ${installer}`, (error, stdout, stderr) => {
spinner.stop();
console.log(stdout);
console.error(stderr);
if (error) {
console.error(error);
} else {
console.log(
"\x1b[32m",
`You're all set! run cd ${projectName}${
starterBranch === choiceMap.cms ||
starterBranch === choiceMap.typescriptCms
? " && yarn studio:init"
: ""
} && ${pkgManager === "yarn" ? "yarn" : "npm run"} dev`
);
}
});
});
})
.catch((error) => {
if (error.isTtyError) {
console.error("Oops. Something went wrong.");
} else {
console.error(`¯\\_(ツ)_/¯`);
}
});