This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
forked from sprity/sprity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (106 loc) · 2.43 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
'use strict';
var _ = require('lodash');
var vfs = require('vinyl-fs');
var ifStream = require('ternary-stream');
var through2 = require('through2').obj;
var tile = require('./lib/tile');
var layout = require('./lib/layout');
var sprite = require('./lib/sprite');
var style = require('./lib/style');
var toVinyl = require('./lib/to-vinyl');
var noop = function () {};
var error = null;
var defaults = {
'src': null,
'out': '',
'name': 'sprite',
'style': null,
'dimension': [{ratio: 1, dpi: 72}],
'engine': 'lwip',
'cssPath': '../images',
'processor': 'css',
'template': null,
'orientation': 'vertical',
'background': '#FFFFFF',
'margin': 4,
'opacity': 0,
'sort': true,
'split': false,
'style-indent-char': 'space',
'style-indent-size': 2,
'logger': {
log: noop,
warn: noop,
debug: noop,
error: noop,
success: noop
}
};
var handleError = function () {
return function (err) {
error = true;
this.push(err);
};
};
var handleCallbackError = function (cb) {
return function (err) {
error = true;
if (_.isFunction(cb)) {
cb(err);
}
};
};
module.exports = {
/*
* creates sprite and style file and save them to disk
*/
create: function (o, cb) {
if (!o.out) {
throw new Error('output dir missing');
}
this.src(o)
.on('error', handleCallbackError(cb))
.pipe(vfs.dest(function (file) {
return file.base;
}))
.on('error', handleCallbackError(cb))
.on('end', function () {
if (_.isFunction(cb) && !error) {
cb();
}
});
},
/*
* returns a Readable/Writable stream of vinyl objects with sprite and style files
*/
src: function (o) {
if (!o.src) {
throw new Error('src dir missing');
}
var opts = _.extend({}, defaults, o);
var hasStyle = function () {
return !!opts.style;
};
style.init();
var stream = vfs.src(opts.src)
.pipe(tile(opts))
.on('error', handleError())
.pipe(layout(opts))
.on('error', handleError())
.pipe(sprite(opts))
.on('error', handleError())
.pipe(ifStream(hasStyle, style.style(opts)))
.on('error', handleError())
.pipe(toVinyl(opts))
.on('error', handleError())
.pipe(through2(function (obj, enc, cb) {
if (obj instanceof Error) {
cb(obj, null);
}
else {
cb(null, obj);
}
}));
return stream;
}
};