-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
116 lines (105 loc) · 2.67 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
const inquirer = require("inquirer");
const fse = require("fs-extra");
const path = require("path");
const prettier = require("prettier");
const cwdpath = process.cwd();
const spawn = require("cross-spawn");
const _ = require("lodash");
function megePkg(rootPath, basePkg, pkgFile) {
const pkg = fse.readJSONSync(pkgFile, { encoding: "utf-8" });
const newPkg = _.merge(basePkg, pkg);
const formatNewPkg = prettier.format(JSON.stringify(newPkg), {
parser: "json"
});
fse.writeFileSync(path.join(rootPath, "package.json"), formatNewPkg, {
encoding: "utf-8"
});
return newPkg;
}
inquirer
.prompt([
{
type: "input",
message: "app name?",
name: "appName",
default: "react-app"
},
{
type: "confirm",
message: "react-router?",
name: "useReactRouter",
default: false
},
{
type: "list",
message: "state manager?",
name: "stateManager",
choices: ["none", "mobx", "redux"],
default: "mobx"
},
{
type: "confirm",
message: "husky?",
name: "useHusky",
default: false
},
{
type: "list",
message: "package manager?",
name: "packageManager",
choices: ["npm", "yarn"],
default: "npm"
}
])
.then(answers => {
const rootPath = path.join(cwdpath, answers.appName);
fse.mkdirSync(rootPath);
fse.copySync(path.join(__dirname, "src/base"), rootPath);
let basePkg = fse.readJSONSync(
path.join(__dirname, "src/base/package.json"),
{ encoding: "utf-8" }
);
if (answers.useReactRouter) {
fse.copySync(
path.join(__dirname, "src/router/src"),
path.join(rootPath, "src")
);
basePkg = megePkg(
rootPath,
basePkg,
path.join(__dirname, "src/router/package.json")
);
}
if (answers.stateManager !== "none") {
if (answers.stateManager === "mobx") {
basePkg = megePkg(
rootPath,
basePkg,
path.join(__dirname, "src/mobx/package.json")
);
}
if (answers.stateManager === "redux") {
fse.copySync(
path.join(__dirname, "src/redux/src"),
path.join(rootPath, "src")
);
basePkg = megePkg(
rootPath,
basePkg,
path.join(__dirname, "src/redux/package.json")
);
}
}
if (answers.useHusky) {
basePkg = megePkg(
rootPath,
basePkg,
path.join(__dirname, "src/husky/package.json")
);
}
console.log("build in " + rootPath);
// spawn.sync(answers.packageManager === "npm" ? "npm install" : "yarn", {
// cwd: rootPath,
// stdio: "inherit"
// });
});