-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathinit.js
More file actions
208 lines (201 loc) · 4.86 KB
/
Copy pathinit.js
File metadata and controls
208 lines (201 loc) · 4.86 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const fs = require("fs.extra");
const path = require("path");
const clone = require("git-clone");
const init = function() {};
const ora = require("ora");
/**
* init command
* @param {string} projectName - project name
* @param {string} gitrepository - template repository url
* @param {string} options - additional options for the project example: eslint configuration
* @param {function} cb - callback for status return
*/
init.prototype.initialize = function(projectName, gitrepository, eslint, cb) {
if (gitrepository === undefined) {
if (eslint === undefined) {
let projectPath = projectName;
fs.mkdirp(projectPath, error => {
if (error) {
cb(error);
} else {
//resolve all promises async-ly
Promise.all([
this.copyCommonTemplates(projectName),
this.copyTemplate(
projectName,
"webpack.config.js",
false
),
this.copyTemplate(projectName, "reactclirc", true),
this.copyTemplate(projectName, "gitignore", true),
this.copyPackageJson(projectName)
])
.then(values => {
cb(true);
})
.catch(err => {
console.log(err);
cb(false);
});
}
});
} else {
if (eslint) {
Promise.all([
this.copyCommonTemplates(projectName),
this.copyTemplate(projectName, "webpack.config.js", false),
this.copyTemplate(projectName, "gitignore", true),
this.copyTemplate(projectName, "eslintrc.json", true),
this.copyPackageJson(projectName)
])
.then(values => {
cb(true);
})
.catch(err => {
console.log(err);
cb(false);
});
}
}
} else {
if (eslint === undefined) {
let projectPath = projectName;
fs.mkdirp(projectPath, error => {
if (error) {
cb(error);
} else {
// resolve all promises async-ly
Promise.all([
this.copyTemplate(
projectName,
"webpack.config.js",
false
),
this.copyTemplate(projectName, "reactclirc", true),
this.copyTemplate(projectName, "gitignore", true),
this.copyPackageJson(projectName)
])
.then(values => {
const spinner = ora("cloning template").start();
clone(
gitrepository,
projectName + "/src",
value => {
setTimeout(() => {
spinner.text =
"template cloned successfully";
spinner.succeed();
cb(true);
}, 1000);
}
);
})
.catch(err => {
console.log(err);
cb(false);
});
}
});
} else {
if (eslint) {
Promise.all([
this.copyTemplate(projectName, "webpack.config.js", false),
this.copyTemplate(projectName, "gitignore", true),
this.copyTemplate(projectName, "eslintrc.json", true),
this.copyPackageJson(projectName)
])
.then(values => {
const spinner = ora("cloning template").start();
clone(gitrepository, projectName + "/src", value => {
setTimeout(() => {
spinner.text = "template cloned successfully";
spinner.succeed();
cb(true);
}, 1000);
});
})
.catch(err => {
console.log(err);
cb(false);
});
}
}
}
};
/**
* copy common templates recursively
* @param {string} projectName - project name
*/
init.prototype.copyCommonTemplates = function(projectName) {
return new Promise(function(resolve, reject) {
fs.copyRecursive(
path.join(__dirname, "..", "templates/src"),
path.join(projectName, "src"),
function(err) {
if (err) {
console.log(err);
reject(err);
} else {
resolve(true);
}
}
);
});
};
/**
* prototype function for copying a special template file
* @param {string} projectName - project name
* @param {string} file - name of the file to be copied
*/
init.prototype.copyTemplate = function(projectName, file, dotFile) {
return new Promise(function(resolve, reject) {
let destFile = null;
if (dotFile) {
destFile = "." + file;
} else {
destFile = file;
}
fs.copy(
path.join(__dirname, "..", "templates", file),
path.join(projectName, destFile),
{ replace: false },
function(err) {
if (err) {
console.log(err);
reject(err);
} else {
resolve(true);
}
}
);
});
};
/**
* copy package.json file to project destination
* @param {string} projectName - project name
*/
init.prototype.copyPackageJson = function(projectName) {
return new Promise(function(resolve, reject) {
fs.readFile(
path.join(__dirname, "..", "templates/package.json"),
(err, buffer) => {
let jsonContent = JSON.parse(buffer.toString());
jsonContent.name = projectName;
fs.writeFile(
path.join(projectName, "package.json"),
JSON.stringify(jsonContent, null, 2),
"utf-8",
err => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(true);
}
}
);
}
);
});
};
module.exports = init;