forked from rejahrehim/gulp-hologram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (122 loc) · 3.51 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* gulp-hologram
*
*
* Copyright (c) 2014 Rejah Rehim
* Licensed under the MIT license.
*/
var which = require("which").sync,
gutil = require("gulp-util"),
path = require("path"),
spawn = require("child_process").spawn,
Stream = require("stream"),
Duplexer = require("plexer"),
PluginError = gutil.PluginError,
log = gutil.log;
// Consts
var PLUGIN_NAME = "gulp-hologram";
function gulpHologram(opts) {
"use strict";
opts = opts || {};
var args = [],
stream = new Stream.PassThrough({objectMode: true}),
hologramExecutable = "hologram";
if (opts.bundler) {
hologramExecutable = "bundle";
args = ["exec", "hologram"];
// check bundle command exist
try {
hologramExecutable = which(hologramExecutable);
} catch (err) {
throw new PluginError(PLUGIN_NAME,
"\nYou need to have Bundler installed in your PATH for this task to work.\n" +
"\nsudo gem install bundler\n"
);
}
}
// check Hologram command exist
try {
hologramExecutable = which(hologramExecutable);
} catch (err) {
throw new PluginError(PLUGIN_NAME,
"\nYou need to have Hologram installed in your PATH for this task to work.\n" +
"\nsudo gem install hologram\n"
);
}
// Run hologram
stream._transform = function (file, unused, cb) {
// Null check config file path from source
if (file.isNull()) {
stream.push(file);
return cb();
} else {
var ext = path.extname(file.path);
if (opts.logging) {
log("Config file extension:", ext);
}
//Passing config path of hologram to args
args.push(file.path);
if (ext !== ".yml" && ext !== ".yaml") {
throw new PluginError(PLUGIN_NAME,
"\nYou must provide a path to your yml hologram config file as source.\n"
);
}
}
// spawn program
if (opts.logging) {
log("Running command:", hologramExecutable, args);
}
var program = spawn(hologramExecutable, args);
// listen to stderr and emit errors if any
var errBuffer = new Buffer(0);
program.stderr.on("readable", function () {
var chunk;
while (chunk = program.stderr.read()) {
errBuffer = Buffer.concat([
errBuffer,
chunk
], errBuffer.length + chunk.length);
}
});
program.stderr.on("end", function () {
if (errBuffer.length) {
stream.emit("error", new PluginError(PLUGIN_NAME,
errBuffer.toString("utf-8")));
}
});
// check if we have a buffer or stream
if (file.contents instanceof Buffer) {
// create buffer
var newBuffer = new Buffer(0);
// when program receives data add it to buffer
program.stdout.on("readable", function () {
var chunk;
while (chunk = program.stdout.read()) {
newBuffer = Buffer.concat([
newBuffer,
chunk
], newBuffer.length + chunk.length);
}
});
// when program finishes call callback
program.stdout.on("end", function () {
file.contents = newBuffer;
stream.push(file);
cb();
});
// "execute"
// write file buffer to program
program.stdin.write(file.contents, function () {
program.stdin.end();
});
} else { // assume we have a stream.Readable
// stream away!
file.contents = file.contents
.pipe(new Duplexer(program.stdin, program.stdout));
stream.push(file);
cb();
}
};
return stream;
}
module.exports = gulpHologram;