-
Notifications
You must be signed in to change notification settings - Fork 55
/
index.js
74 lines (69 loc) · 1.67 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
'use strict';
var sprite = require('./lib/css-sprite');
var through2 = require('through2');
var vfs = require('vinyl-fs');
var fs = require('graceful-fs');
var mkdirp = require('mkdirp');
var path = require('path');
var replaceExtension = require('./lib/replace-extension');
var _ = require('lodash');
var noop = function () {};
var writeFile = function (file, enc, cb) {
var stream = this;
mkdirp(file.base, function () {
fs.writeFile(file.path, file.contents, function () {
stream.push(file);
cb();
});
});
};
var defaults = {
src: null,
out: '',
name: 'sprite',
style: null,
format: 'png',
cssPath: '../images',
processor: 'css',
template: null,
orientation: 'vertical',
retina: false,
background: '#FFFFFF',
margin: 4,
opacity: 0,
sort: true
};
module.exports = {
/*
* Creates sprite and css file
*/
create: function (o, cb) {
if (!o.src) {
throw new Error('glob missing');
}
if (!o.out) {
throw new Error('output dir missing');
}
var opts = _.extend({}, defaults, o);
if (opts.style && path.basename(opts.style).indexOf('.') === -1) {
opts.style = path.join(opts.style, replaceExtension(opts.name, '.' + opts.processor));
}
vfs.src(opts.src)
.pipe(sprite(opts))
.pipe(through2.obj(writeFile))
.on('data', noop)
.on('end', function () {
if (_.isFunction(cb)) {
cb();
}
});
},
/*
* Takes a vinyl-fs Readable/Writable stream of images
* returns a Readable/Writable stream of vinyl files of the sprite and css file
*/
stream: function (o) {
var opts = _.extend({}, defaults, o);
return sprite(opts);
}
};