-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
263 lines (239 loc) · 6.99 KB
/
main.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
const child_process = require('child_process');
const dotenv = require('dotenv').config();
const fs = require('fs');
const path = require('path');
const rimraf = require('rimraf');
var mode = process.argv[2];
var config = require('./config.json');
function clean(cleanFiles, dir){
if(dir == null || cleanFiles == null){
logger('err', `No argument found on clean function !`);
}
for(c=0;c<cleanFiles.length;c++){
rimraf.sync(`${dir}/${cleanFiles[c]}`);
logger('info', `clean ${cleanFiles[c]}`);
}
}
function clone(repo, dest){
if(repo == null || dest == null){
logger('err', `No argument found on clone function !`);
return false;
}
try {
exec(`git clone ${repo} ${dest}`);
logger('info', `${repo} done`);
} catch (error) {
logger('err', `Fail to clone '${repo}' repo`);
}
}
function copyFileSync(source, target, remover){
var targetFile = target;
if(remover != null){
targetFile = targetFile.replace(`/${remover}`, '');
targetFile = targetFile.replace(`\\${remover}`, '');
}
if(fs.existsSync(target)){
if(fs.lstatSync(target).isDirectory()){
targetFile = path.join(target, path.basename(source));
}
}
fs.writeFileSync(targetFile, fs.readFileSync(source));
logger('info', targetFile);
}
function copyFolderRecursiveSync(source, target, remover){
var files = [];
var targetFolder = path.join(target, path.basename(source));
if(remover != null){
targetFolder = targetFolder.replace(`/${remover}`, '');
targetFolder = targetFolder.replace(`\\${remover}`, '');
}
if(!fs.existsSync(targetFolder)) {
fs.mkdirSync(targetFolder);
}
if(fs.lstatSync(source).isDirectory()){
files = fs.readdirSync(source);
files.forEach(function(file){
var curSource = path.join(source, file);
if(fs.lstatSync(curSource).isDirectory()){
copyFolderRecursiveSync(curSource, targetFolder, remover);
}
else{
copyFileSync(curSource, targetFolder, remover);
}
});
}
}
function create_dir(dir){
try {
if(fs.existsSync(dir) == false){
fs.mkdirSync(dir);
logger('info', `create '${dir}' dir`);
}
} catch (error) {
logger('err', `Fail to create '${dir}' dir`);
}
}
function create_dirs(dirs){
for(i=0;i<dirs.length;i++){
create_dir(dirs[i]);
if(i == dirs.length - 1){
return true;
}
}
}
async function exec(cmd){
child_process.exec(cmd, (err, stdout) => {
if(err){
logger('err', err);
}
});
sleep(7000);
}
function filter(key, value){
if(key == null || value == null){
logger('err', `No argument found on filter function !`);
return false;
}
switch(key){
case 'clean':
clean(config[value[0]], value[1]);
break;
case 'clone':
clone(value[0], value[1]);
break;
case 'exec':
exec(value[0]);
break;
case 'function':
for(fi=0;fi < Object.keys(config[value]).length; fi++){
filter(config[value].type, config[value][fi]);
}
break;
case 'keep':
keep(value[0], config[value[1]]);
break;
case 'mkdir':
create_dirs(value);
break;
case 'move':
move(value[0], value[1]);
break;
case 'moveFile':
moveFile(value[0], value[1]);
break;
case 'release':
if(mode != 'test'){
var GHTOKEN = process.env.GH_TOKEN;
exec(`bash ${__dirname.replace(/\\/g, '/')}/release.sh ${GHTOKEN}`);
}
break;
case 'wait':
sleep(value);
break;
default:
logger('err', `Not found key: ${key}`);
}
}
function getFiles(dir, files_){
files_ = files_ || [];
var files = fs.readdirSync(dir);
for(var i in files){
var name = dir + '/' + files[i];
if(fs.statSync(name).isDirectory()){
getFiles(name, files_);
}
}
return files_;
}
function init(){
logger('info', 'STARTING !');
for(ini=0;ini<Object.keys(config.order).length;){
let key = Object.keys(config.order)[ini];
let value = config.order[key];
if(key.indexOf('function') == 0){
key = "function";
logger('info', `FUNCTION ${value} ===============================`);
}
filter(key, value);
ini++;
}
}
function keep(dir, keep_files, files_){
if(fs.existsSync(dir) == true){
files_ = files_ || [];
var files = fs.readdirSync(dir);
for(var i in files){
var name = dir + '/' + files[i];
if(fs.statSync(name).isDirectory()){
for(k=0;k<keep_files.length;k++){
if(name != `${dir}/${keep_files[k]}`){
remove(name);
break;
}
else{
keep(name, keep_files, files_);
}
}
}
}
return files_;
}
}
function logger(lvl, content){
switch(lvl){
case 'err':
console.log(`\x1b[31m ERROR: ${content} \x1b[0m`);
break;
case 'info':
console.log(`\x1b[32m LOG: ${content} \x1b[0m`);
break;
default:
console.log(`\x1b[34m LOG: ${content} \x1b[0m`);
}
}
function move(target, dest){
var prefix = target.split('/');
prefix = prefix[1];
copyFolderRecursiveSync(target, dest, prefix);
}
function moveFile(target, dest){
fs.rename(target, dest, (err) => {
if(err){
logger('err', `RENAME: ${target} -> ${dest} ERROR !`);
}
else{
logger('info', `RENAME: ${target} -> ${dest} DONE !`);
}
});
}
function remove(target){
rimraf.sync(target);
logger('info', `Remove ${target} dir`);
}
const reset = new Promise(function(resolve, reject){
try {
logger('infoB', 'RESET ALL');
remove('build');
remove('dist');
remove('release');
setTimeout(() => {
logger('infoB', 'DONE');
resolve();
}, 1000);
} catch (error) {
logger('err', error);
resolve();
}
});
reset.then(function(value){
if(mode != 'clean'){
init();
}
});
function sleep(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}