-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
138 lines (113 loc) · 2.93 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
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env node
const fs = require("fs");
const { execSync } = require("child_process");
const fsExtra = require("fs-extra");
const path = require("path");
const { program } = require("commander");
const chalk = require("chalk");
const figlet = require("figlet");
const prompts = require("prompts");
const _ = require("underscore");
let packageJson = {};
let projectName = null;
let directoryPath = null;
try {
packageJson = JSON.parse(
fs.readFileSync(path.join(__dirname, "package.json"), "utf-8")
);
} catch (error) {
console.log(error);
process.exit(1);
}
const { name, description, version } = packageJson;
program.name(name).description(description).version(version);
program
.on("--help", () => {
console.log(
"\r\n" +
chalk.white.bgBlueBright.bold(
figlet.textSync("create-react-ie", {
font: "Standard",
horizontalLayout: "default",
verticalLayout: "default",
width: 80,
whitespaceBreak: true,
})
)
);
})
.arguments("<project-directory>")
.usage(`${chalk.green("<project-directory>")} [option]`)
.action((name) => {
projectName = name;
})
.option("-p, --port [port]", "webpack dev server port", 3000)
.parse(process.argv);
const { port } = program.opts();
if (projectName) {
directoryPath = path.join(process.cwd(), projectName);
console.log(chalk.bold("\nEnvironment Info:\n"));
console.log(` current version of ${name}: ${version}\n`);
console.log(` create path: ${directoryPath}\n`);
if (fs.existsSync(directoryPath)) {
(async () => {
console.log(chalk.bold("Configure:\n"));
const res = await prompts({
type: "select",
name: "isOverwrite",
message: "The folder already exists. Do you want to overwrite it?",
choices: [
{ title: "overwrite", value: true },
{ title: "exit", value: false },
],
initial: 0,
});
const { isOverwrite } = res;
if (isOverwrite) {
fs.rmSync(directoryPath, { recursive: true });
run();
} else {
process.exit(1);
}
})();
} else {
run();
}
} else {
process.exit(1);
}
function run() {
console.log(chalk.bold("\nInstall:\n"));
fs.mkdirSync(directoryPath, { recursive: true });
fsExtra.copySync(path.join(__dirname, "templates", "copy"), directoryPath);
fs.writeFileSync(
path.join(directoryPath, "package.json"),
_.template(
fs.readFileSync(
path.join(__dirname, "templates", "package.json.tmpl"),
"utf-8"
)
)({
projectName,
}),
{ flag: "w" }
);
fs.writeFileSync(
path.join(directoryPath, "webpack.config.js"),
_.template(
fs.readFileSync(
path.join(__dirname, "templates", "webpack.config.js.tmpl"),
"utf-8"
)
)({
port,
}),
{ flag: "w" }
);
console.log(` Start install dependencies\n`);
process.chdir(directoryPath);
execSync("npm install", { stdio: "inherit" });
console.log(`\n npm install completed successfully\n`);
console.log(` To start development. run ${chalk.green("npm start")}\n`);
process.exit(1);
}