-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
61 lines (48 loc) · 1.73 KB
/
index.js
File metadata and controls
61 lines (48 loc) · 1.73 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
var cp = require('child_process');
var Stream = require('stream');
var gutil = require('gulp-util');
var Duplexer = require('plexer');
var pypugjs = function(options) {
options = options || {};
var stream = new Stream.PassThrough({ objectMode: true });
stream._transform = function(file, _, cb) {
if (file.isNull()) {
stream.push(file);
return cb();
}
var spawnOpts = ['-c'].concat(options.engine ? options.engine.split(' ') : ['django']);
var program = cp.spawn('pypugjs', spawnOpts, { cwd: '.' });
if (file.contents instanceof Buffer) {
var newBuffer = new Buffer(0);
program.stdout.on('readable', function () {
var chunk;
while ((chunk = program.stdout.read())) {
newBuffer = Buffer.concat(
[newBuffer, chunk],
newBuffer.length + chunk.length
);
}
});
program.stdout.on('end', function () {
if (options.newline) {
newBuffer = Buffer.concat([newBuffer, new Buffer('\n')]);
}
file.path = gutil.replaceExtension(file.path, (options.extension || '.html'));
file.contents = newBuffer;
stream.push(file);
cb();
});
program.stdin.write(file.contents, function () {
program.stdin.end();
});
} else {
file.contents = file.contents.pipe(
new Duplexer(program.stdin, program.stdout)
);
stream.push(file);
cb();
}
}
return stream;
}
module.exports = pypugjs;