1 | var through = require('through2');
|
2 | var gutil = require('gulp-util');
|
3 | var PluginError = gutil.PluginError;
|
4 | var cp = require('child_process');
|
5 | var path = require('path');
|
6 |
|
7 | var PLUGIN_NAME = 'gulp-old-compile';
|
8 |
|
9 |
|
10 | function gulpOldCompile() {
|
11 | var s = through.obj(function(file, enc, cb) {
|
12 | if (file.isStream()) {
|
13 | this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported!'));
|
14 | return cb();
|
15 | }
|
16 |
|
17 | if (file.isBuffer()) {
|
18 | var oldCompilePath = path.resolve(__dirname, "compile.py");
|
19 |
|
20 | var args = [oldCompilePath, file.path];
|
21 | var cpObj = cp.spawn("python", args);
|
22 |
|
23 | var stdoutData = [];
|
24 | cpObj.stdout.on('data', function(chunk) { stdoutData.push(chunk); });
|
25 |
|
26 | var stderrData = [];
|
27 | cpObj.stderr.on('data', function(chunk) { stderrData.push(chunk); });
|
28 |
|
29 | cpObj.on('error', function(err) {
|
30 | s.emit('error', new PluginError(PLUGIN_NAME, "Error while compiling " + file.path + ":\n" + err));
|
31 | cb();
|
32 | });
|
33 |
|
34 | cpObj.on('close', function(code) {
|
35 | var stdout = Buffer.concat(stdoutData);
|
36 | var lines = stdout.toString().split('\n');
|
37 | for(var i=0; i<lines.length; i++) {
|
38 | if(lines[i].match(/^\[Errno/)) {
|
39 | s.emit('error', new PluginError(PLUGIN_NAME, "Error while compiling " + file.path + ":\n" + stdout.toString()));
|
40 | }
|
41 | }
|
42 |
|
43 | if(stderrData.length > 0) {
|
44 | var stderr = Buffer.concat(stderrData);
|
45 | s.emit('error', new PluginError(PLUGIN_NAME, "Error while compiling " + file.path + ":\n" + stderr.toString() + "\n\n" + stdout.toString()));
|
46 | cb();
|
47 | }
|
48 |
|
49 | if(!gutil.isBuffer(stdout)) stdout = new Buffer(String(stdout));
|
50 | file.contents = stdout;
|
51 | s.push(file);
|
52 | cb();
|
53 | });
|
54 | }
|
55 | });
|
56 |
|
57 |
|
58 | return s;
|
59 | };
|
60 |
|
61 |
|
62 | module.exports = gulpOldCompile;
|