-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
89 lines (65 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
// gulp-wrapper
//
// A plugin used to wrap files with custom strings
var through2 = require('through2'),
gutil = require('gulp-util');
var PluginError = gutil.PluginError;
module.exports = function(opt) {
'use strict';
if(typeof opt !== 'object'){
opt = {};
}
return through2.obj(function (file, enc, callback) {
// check if file is there
if (file.isNull()) {
this.push(file);
return callback();
}
if (file.isStream()){
return this.emit('error', new PluginError('gulp-wrapper', 'Streaming not supported'));
}
// get the file's name
var fileName = file.path.replace(file.base, ''), // replace front slash from windowsLand
// set the new contents
newContentString = file.contents.toString(),
header,
footer;
// normalize windows platform slashes
if(process.platform.match(/^win/)){
fileName = fileName.replace(/\\/g, '/');
}
// set now the header and footer values according to the
// options passed...
// ------------------------------- if header given is a function...
if(typeof opt.header === 'function'){
// execute the function with `file` as an argument
header = opt.header(file);
// ------------------------------- if header given is a string...
} else if(typeof opt.header === 'string'){
// inject the file name if needed
header = opt.header.replace(/\${filename}/g, fileName);
// ------------------------------- if header given is anything else...
} else if(typeof opt.header !== 'string'){
header = '';
}
// ------------------------------- if footer given is a function...
if(typeof opt.footer === 'function'){
// execute the function with `file` as an argument
footer = opt.footer(file);
// ------------------------------- if footer given is a string...
} else if(typeof opt.footer === 'string'){
// inject the file name if needed
footer = opt.footer.replace(/\${filename}/g, fileName);
// ------------------------------- if footer given is anything else...
} else if(typeof opt.footer !== 'string'){
footer = '';
}
// wrap the contents
newContentString = header + newContentString + footer;
// change the file contents
file.contents = new Buffer(newContentString);
// push the file into the output
this.push(file);
callback();
});
};