-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
106 lines (83 loc) · 2.68 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
#!/usr/bin/env node
/**
* Copyright 2017, Yahoo Holdings.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
const Util = require('./lib/util.js');
//Catch uncaught exceptions
/* istanbul ignore next */
process.on('uncaughtException', (err) => {
// handle the error safely
Util.showErrorMsg(err);
});
/** @ignore */
const Files = require('./lib/files.js');
const ParallelSprites = require('./lib/parallel-sprites.js');
const ParallelCss = require('./lib/parallel-css.js');
const UnusedImages = require('./lib/unused-images.js');
/**
* This function show error message in the console and forward the error callback
* @private
* @param {Object} err The error Object
* @param {Function} callback function
* @returns {void}
*/
const errorCallback = (err, callback) => {
// show error message in the console
Util.showErrorMsg(err);
callback(err);
};
/**
* This function create image sprite(s) from images and update css file(s) with new image sprite(s) details.
* @memberof Spritify
* @param {Object} params object
* @param {Function} callback function
* @returns {void}
*/
const build = (params, callback) => {
// set and normalize options
Util.setBuildOptions(params);
// show start message in the console
Util.startMsg();
// get all images files paths that will be used for the sprites
Files.getSpriteImagesFiles(params.sprites, (err, spritesData) => {
if (err) {
// error callback
errorCallback(err, callback);
return;
}
// build sprite images from images info
ParallelSprites.buildSprites(spritesData, (err, sprites) => {
if (err) {
// error callback
errorCallback(err, callback);
return;
}
if (params.css === undefined) {
// show end message in the console
Util.endMsg();
// no need to update css, call success callback
callback();
return;
}
// update css files with the new sprite images
ParallelCss.updateCss(params.css, sprites, (err) => {
// check and show the unused images
UnusedImages.checkImages(sprites);
// show end message in the console
Util.endMsg();
if (err) {
// error callback
errorCallback(err, callback);
return;
}
// success callback
callback();
});
});
});
};
/** @namespace Spritify */
module.exports = {
build
};