-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (88 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
#!/usr/bin/env node
let shell = require('shelljs')
let colors = require('colors')
let fs = require('fs') //fs already comes included with node.
let appName = process.argv[2]
let appDirectory = `${process.cwd()}/${appName}`
let configTemplates = require('./templates/templates.js')
let publicTemplates = require('./templates/public/templates.js');
let srcTemplates = require('./templates/src/templates.js');
const run = async () => {
try {
log("creating react-simple-boilerplate");
await createDirectory(appName);
await cdIntoNewApp(appName);
await gitInit();
await addConfigFiles();
await createDirectory("public");
await addPublicFiles();
await createDirectory("src");
await addSrcFiles();
log("react-simple-boilerplate created... Enjoy!!!")
} catch (exception) {
log(exception);
}
}
const createDirectory = (name) => {
return new Promise(resolve => {
if (name) {
shell.exec(`mkdir ${name}`, () => {
log("Directory created");
resolve(true);
});
} else {
log("\nNo app name was provided.".red);
log("\nProvide an app name in the following format: ");
log("\ninit-react-app ", "app-name\n".cyan);
resolve(false);
}
});
}
const cdIntoNewApp = (appName) => {
return new Promise(resolve => {
shell.cd(appName);
resolve();
});
}
const gitInit = () => {
return new Promise(resolve => {
shell.exec(`git init`, () => {
log("git initialized");
resolve();
});
});
}
const addFiles = (directory, templates) => {
return new Promise(resolve => {
log("started copying files...");
let promises = [];
Object.keys(templates).forEach((fileName, i) => {
promises[i] = new Promise(resolve => {
fs.writeFile(`${directory}/${fileName}`, templates[fileName], (err) => {
if (err) {
return log(err);
}
resolve();
});
});
});
Promise.all(promises).then(() => { resolve() })
log("copied files...");
})
};
const addConfigFiles = () => {
log("adding config files...");
return addFiles(appDirectory, configTemplates);
};
const addPublicFiles = () => {
log("adding config files...");
return addFiles(`${appDirectory}/public`, publicTemplates);
};
const addSrcFiles = () => {
log("adding source files...");
return addFiles(`${appDirectory}/src`, srcTemplates);
};
const log = (...msg) => {
console.log(...msg);
}
run();