-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
101 lines (77 loc) · 2.37 KB
/
index.js
File metadata and controls
101 lines (77 loc) · 2.37 KB
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
#!/usr/bin/env node
const path = require("path");
const fs = require("fs-extra");
const templateConfig = require("./lib/config");
const argv = require("minimist")(process.argv.slice(2));
const {
cleanUp,
commitGit,
addTemplates,
createReactApp,
installPackages,
updatePackageDotJson
} = require("./lib/scripts");
const supportedTemplates = ["template-react", "template-react-ts"];
async function init() {
const targetDir = argv._[0] || ".";
const cwd = process.cwd();
const root = path.join(cwd, targetDir);
console.log(` Scaffolding project in ${root}...`);
await fs.ensureDir(root);
const existing = await fs.readdir(root);
if (existing.length) {
const validFiles = [".", "..", ".git"];
const checkFiles = existing
.filter((file) => file.charAt(0) === ".")
.filter((file) => !validFiles.includes(file));
if (checkFiles.length) {
console.error(
`Error: ${path.relative(cwd, root)} directory is not empty.`
);
process.exit(0);
}
}
const appName = `${path.relative(cwd, root)}` || ".";
const appType = `template-${argv.t || argv.template || "react"}`;
if (!appName.length) {
console.log("Please enter a name for your new app.".red);
return process.exit(0);
}
const app = supportedTemplates.filter((name) => name === appType);
if (app.length === 0) {
console.log(
`App type: ${appType} is not yet supported by this CLI tool.`.red
);
return process.exit(0);
}
const res = await create(appName, appType);
if (!res) {
console.log("There was an error generating your app.".red);
return process.exit(0);
}
return process.exit(0);
}
async function create(appName, template) {
const preferredConfig = [];
templateConfig.forEach((config) => {
if (template === config.name) preferredConfig.push(config);
});
await createReactApp(appName, template);
await installPackages(preferredConfig);
await updatePackageDotJson(preferredConfig);
await addTemplates(preferredConfig);
await cleanUp(template);
await commitGit();
console.log();
console.log(
` Created your new React app with: ${preferredConfig
.map((_) => _.name)
.join(", ")}.`.green
);
console.log(" Run your code: yarn dev (or `npm run dev`)");
console.log();
console.log(" Happy hacking!".blue);
console.log();
return true;
}
init().catch((e) => console.error(e));