-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (52 loc) · 1.43 KB
/
Copy pathindex.js
File metadata and controls
66 lines (52 loc) · 1.43 KB
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
'use strict';
var gutil = require('gulp-util');
var through = require('through2');
var PluginError = gutil.PluginError;
var File = gutil.File;
var path = require('path');
var PLUGIN_NAME = 'gulp-cssmyicons';
// file can be a vinyl file object or a string
module.exports = function(file, opts) {
if (!file) {
throw new PluginError(PLUGIN_NAME, 'Missing file option for ' + PLUGIN_NAME);
}
// Opts
opts = opts || {};
var fileName,
iconsCSS = '';
//
if (typeof file === 'string') {
fileName = file;
} else if (typeof file.path === 'string') {
fileName = path.basename(file.path);
} else {
throw new PluginError(PLUGIN_NAME, 'Missing path file for ' + PLUGIN_NAME);
}
var getCSS = function(el, prefix) {
// Path
var path = el.split('/'),
file = path[path.length-1].split('.'),
filenameNoExt = file[0],
relativePath = prefix + el;
return '.icon-' + filenameNoExt + '{background-image:url("' + relativePath + '");background-repeat:no-repeat;}';
};
function bufferFile(file, enc, cb) {
// Empty files
if (file.isNull()) {
cb();
return;
}
iconsCSS += getCSS(file.relative, opts.prefix || '');
cb();
}
function endStream(cb) {
// If not icons
if (iconsCSS.length === 0) {
cb();
return;
}
this.push(new File({ path: fileName, contents: new Buffer(iconsCSS) }));
cb();
}
return through.obj(bufferFile, endStream);
};