-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.EXAMPLE.js
234 lines (213 loc) · 6.62 KB
/
build.EXAMPLE.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
/**
*
* THIS IS AN EXAMPLE BUILD SCRIPT, WHICH USES THE FUNCTIONS OF funcs.js
*
*/
(() => {
"use strict";
/* eslint-disable no-console */
/* global Func, path */
global.modulePath = __dirname + "/node_modules/";
try {
require("../node.js_Build/funcs");
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
console.error("Build script is missing. Please download from https://github.com/Kiuryy/node.js_Build");
process.exit(1);
}
/**
*
* @type {Object}
*/
let packageJson = {};
/**
* Starts building the application
*/
const Build = () => {
const start = +new Date();
console.log("Building release...\n");
loadPackageJson().then(() => {
return Func.cleanPre();
}).then(() => {
return eslintCheck();
}).then(() => {
return remoteJs();
}).then(() => {
return js();
}).then(() => {
return css();
}).then(() => {
return img();
}).then(() => {
return json();
}).then(() => {
return html();
}).then(() => {
return zip();
}).then(() => {
return Func.cleanPost();
}).then(() => {
console.log("\nRelease built successfully\t[" + (+new Date() - start) + " ms]");
});
};
/*
* ################################
* BUILD FUNCTIONS
* ################################
*/
/**
* Read the package.json of the project and parse its JSON content into an object
*
* @returns {*}
*/
const loadPackageJson = () => {
return Func.measureTime((resolve) => {
const fs = require("fs");
const rawData = fs.readFileSync("package.json");
const parsedData = JSON.parse(rawData);
if (parsedData) {
packageJson = parsedData;
packageJson.preamble = `(c) ${packageJson.author} under ${packageJson.license}`;
resolve();
} else {
console.error("Could not load package.json");
process.exit(1);
}
}, "Loaded package.json");
};
/**
* Copies the images to the dist directory
*
* @returns {Promise}
*/
const img = () => {
return Func.measureTime((resolve) => {
Func.copy([path.src + "img/**/*"], [path.src + "**/*.xcf"], path.dist, false).then(() => {
resolve();
});
}, "Moved image files to dist directory");
};
/**
* Parses the scss files and copies the css files to the dist directory
*
* @returns {Promise}
*/
const css = () => {
return Func.measureTime((resolve) => {
Func.minify([ // parse scss files
path.src + "scss/*.scss"
], path.dist + "css/").then(() => {
resolve();
});
}, "Moved css files to dist directory");
};
/**
* Parses the js files and copies them to the dist directory
*
* @returns {Promise}
*/
const js = () => {
return Func.measureTime((resolve) => {
Func.minify([
path.src + "js/lib/jsu.js",
], path.dist + "js/lib/").then(() => {
resolve();
});
}, "Moved js files to dist directory");
};
/**
* Retrieves the newest versions of the lib js files from Github
*
* @returns {Promise}
*/
const remoteJs = () => {
return new Promise((resolve) => {
let i = 0;
const files = [
{
file: "colorpicker.js",
urlPath: "colorpicker.js/master/src/js/colorpicker.js"
},
{
file: "jsu.js",
urlPath: "jsu.js/master/src/js/jsu.js"
}
];
const fetched = () => {
i++;
if (i === files.length) {
resolve();
}
};
files.forEach((obj) => {
Func.measureTime((rslv) => {
Func.getRemoteContent("https://raw.githubusercontent.com/Kiuryy/" + obj.urlPath).then((content) => {
Func.createFile(path.src + "js/lib/" + obj.file, content).then(() => {
rslv();
});
}, rslv);
}, "Fetched " + obj.file + " from Github").then(() => {
fetched();
});
});
});
};
/**
* Parses the html files and copies them to the dist directory
*
* @returns {Promise}
*/
const html = () => {
return Func.measureTime((resolve) => {
Func.minify([path.src + "html/**/*.html"], path.dist, false).then(() => {
resolve();
});
}, "Moved html files to dist directory");
};
/**
* Generate zip file from dist directory
*
* @returns {Promise}
*/
const zip = () => {
return Func.measureTime((resolve) => {
Func.zipDirectory(path.dist, packageJson.name + "_" + packageJson.versionName + ".zip").then(resolve);
}, "Created zip file from dist directory");
};
/**
* Parses the json files and copies them to the dist directory
*
* @returns {Promise}
*/
const json = () => {
return Func.measureTime((resolve) => {
Func.minify([path.src + "manifest.json"], path.dist, false).then(() => {
resolve();
});
}, "Moved json files to dist directory");
};
/**
* Performs eslint checks for the build and src/js directories
*
* @returns {Promise}
*/
const eslintCheck = async () => {
for (const files of ["build.js", path.src + "js/**/*.js"]) {
await Func.measureTime(async (resolve) => {
Func.cmd("eslint --fix " + files).then((obj) => {
if (obj.stdout && obj.stdout.trim().length > 0) {
console.error(obj.stdout);
process.exit(1);
}
resolve();
});
}, "Performed eslint check for " + files);
}
};
//
//
//
Build();
})();