-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
120 lines (111 loc) · 3.4 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
var {spawn} = require('child_process')
var fs = require('fs')
var path = require('path')
const {reduxSnippet}= require('./snippets')
const {gitClone,reduxPlugins} = require('./values')
const CloneRepo = () =>{
return new Promise((resolve,reject) => {
var child = spawn('git', ["clone", gitClone])
child.stdout.on("close",() => {
resolve()
})
})
}
const addRedux = () => {
return new Promise((resolve,reject) => {
var child = spawn("npm",["install",...reduxPlugins])
child.stdout.on("error" ,(err) => {
reject(err)
})
child.stdout.on("close",() => {
resolve()
})
})
}
const deleteFolderRecursive = function(path) {
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) {
deleteFolderRecursive(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
const moveFile = (file, dir2,callback)=>{
var f = path.basename(file);
var dest = path.resolve(dir2, f);
fs.rename(file, dest, callback);
};
const updateRepoForRedux = () => {
return new Promise((resolve,reject) => {
process.chdir(path.join(__dirname , process.argv[3]))
var actionFile = fs.readFileSync('./redux-files/actions.js').toString()
var reducerFile = fs.readFileSync('./redux-files/reducers.js').toString()
process.chdir(path.join(process.cwd(),'/src'))
fs.mkdir("store",()=>{
fs.writeFileSync(path.resolve(process.cwd(),"./store/actions.js"),actionFile)
fs.writeFileSync(path.resolve(process.cwd(),'./store/reducers.js'),reducerFile)
let filename = "index.js"
try {
let content = fs.readFileSync(process.cwd() + "/" + filename).toString()
content = content.split("\n")
content.splice(6,0,reduxSnippet)
fs.writeFileSync(process.cwd() + "/" + filename,content.join("\n"))
deleteFolderRecursive(path.join(process.cwd(),"../redux-files"))
resolve()
}catch(e) {
console.log(e)
reject()
}
})
})
}
const ReadFiles = () => {
return new Promise((resolve,reject) => {
fs.readdir(__dirname,(err,filenames)=>{
if(err || filenames.indexOf('react-starter-boilerplate')===-1) {
reject()
}else {
resolve()
}
})
})
}
const RenameFolder = () =>{
return ReadFiles()
.then(() => {
return new Promise((resolve,reject) => {
fs.rename('react-starter-boilerplate/',process.argv[3],(err) => {
if(err) {
reject()
}else {
resolve()
}
})
})
})
.catch((err) => {
console.error(err)
process.exit(1)
})
}
const InstallDepencies = () => {
return new Promise((resolve,reject) => {
process.chdir(path.join(__dirname , process.argv[3]))
var child = spawn('npm', ["install"])
child.stdout.on("close",() =>{
resolve()
})
})
}
module.exports = {
CloneRepo,
RenameFolder,
InstallDepencies,
addRedux,
updateRepoForRedux
}