-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
118 lines (99 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
117
118
#!/usr/bin/env node
const fs = require("fs");
const { join } = require("path");
const { exec } = require("child_process");
// Unnecessary files
const unnecessaryFilesInSrc = [
"App.css",
"App.test.js",
"App.test.tsx",
"index.css",
"logo.svg",
"serviceWorker.js",
"serviceWorker.ts",
"setupTests.js",
"setupTests.ts",
];
const unnecessaryFilesInPublic = [
"favicon.ico",
"logo192.png",
"logo512.png",
"manifest.json",
];
// Content replacement
const appJsContent = `import React from 'react';
const App = () => {
return (
<div className="App">
</div>
);
}
export default App;`;
const indexJsContent = `import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById("root")
);`;
const indexHtmlContent = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>`;
// Args
const args = process.argv.slice(2);
// File paths
const folderName = args[0].startsWith("--") ? args[2] : args[0];
const appPath = join(process.cwd(), folderName);
const appJsPath = join(appPath, "src/App");
const indexJsPath = join(appPath, "src/index");
const indexHtmlPath = join(appPath, "public/index.html");
// fs util
const writeFileIfExists = async (file, content) =>
new Promise((rs, rj) => {
fs.readFile(file, (err) => {
if (err) {
rj();
} else {
rs(fs.writeFile(file, content, "utf-8", () => {}));
}
});
});
// Cleanup function
const cleanup = async () => {
console.log("");
console.log("Cleaning up...");
await Promise.allSettled([
...unnecessaryFilesInSrc.map((file) =>
fs.unlink(join(appPath, "src", file), () => {})
),
...unnecessaryFilesInPublic.map((file) =>
fs.unlink(join(appPath, "public", file), () => {})
),
fs.unlink(join(appPath, "README.md"), () => {}),
writeFileIfExists(appJsPath + ".js", appJsContent),
writeFileIfExists(appJsPath + ".tsx", appJsContent),
writeFileIfExists(indexJsPath + ".js", indexJsContent),
writeFileIfExists(indexJsPath + ".tsx", indexJsContent),
fs.writeFile(indexHtmlPath, indexHtmlContent, "utf-8", () => {}),
]);
console.log("Done!");
};
// Exec create-react-app and do cleanup
const composer = exec(`npx create-react-app ${args.join(" ")} --colors`);
composer.stdout.pipe(process.stdout);
composer.on("exit", (code, data) => {
if (code === 0) {
cleanup();
}
});