-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (54 loc) · 2.1 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
var gutil = require('gulp-util');
var File = gutil.File;
var PluginError = gutil.PluginError;
var through = require('through2');
var path = require('path');
var exec = require('child_process').exec;
var fs = require('fs');
var coffeeScriptConcatPath = require.resolve('coffeescript-concat');
var PLUGIN_NAME = require('./plugin-name');
function coffeeScriptConcat(file) {
var files = [];
if (!file || (typeof file !== 'string' && typeof file.path !== 'string'))
throw new PluginError(PLUGIN_NAME, 'Missing file option for ' + PLUGIN_NAME);
function bufferContents(file, encoding, callback) {
// Skipping empty files
if (file.isNull())
return callback();
if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streaming not supported'));
return callback();
}
// adding file to list of files that will be concatenated
// if it has no added yet
if (files.indexOf(file.path) === -1) {
// and change windows backslash directory separators
// to unix system slash separators, because
// coffeescript-concat understands only unix ones
files.push(file.path.replace(/\\/g, '/'));
}
callback();
}
function endStream(callback) {
var that = this;
exec('node ' + coffeeScriptConcatPath + ' ' + files.join(' '), {}, function (error, stdout) {
if (files.length < 1)
return callback();
if (error)
return callback(new PluginError(PLUGIN_NAME, error));
var concatFile;
if (typeof file === 'string') {
concatFile = new File();
concatFile.stat = {mode: 0666};
concatFile.path = file;
} else {
concatFile = new File(file);
}
concatFile.contents = new Buffer(stdout);
that.push(concatFile);
callback();
});
}
return through.obj(bufferContents, endStream);
}
module.exports = coffeeScriptConcat;