forked from unconed/mathbox
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
46 lines (35 loc) · 1.3 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
var through = require('through');
var os = require('os');
var path = require('path');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var File = gutil.File;
module.exports = function(fileName, global){
if (!fileName) throw new PluginError('gulp-concat', 'Missing fileName option for gulp-jsify');
var buffer = [];
var firstFile = null;
function bufferContents(file){
if (file.isNull()) return; // ignore
if (file.isStream()) return this.emit('error', new PluginError('gulp-concat', 'Streaming not supported'));
if (!firstFile) firstFile = file;
var contents = file.contents.toString('utf8');
var json = JSON.stringify(contents);
var parts = path.basename(file.path).split(/\./g);
var name = JSON.stringify(parts.slice(0, -1).join('.'));
buffer.push(name + ': ' + json);
}
function endStream(){
if (buffer.length === 0) return this.emit('end');
var joinedContents = global + ' = {' + buffer.join(',\n') + '};\n';
var joinedPath = path.join(firstFile.base, fileName);
var joinedFile = new File({
cwd: firstFile.cwd,
base: firstFile.base,
path: joinedPath,
contents: new Buffer(joinedContents)
});
this.emit('data', joinedFile);
this.emit('end');
}
return through(bufferContents, endStream);
};