-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
80 lines (61 loc) · 2.21 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
var path = require('path');
var through = require('through2');
var PluginError = require('gulp-util').PluginError;
var hspCompiler = require('hashspace').compiler;
var hspTranspiler = require('hashspace/hsp/transpiler').processString;
function compile(file, streamOfFiles) {
var compileResult = hspCompiler.compile(String(file.contents), file.path);
if (!compileResult.errors.length) {
file.contents = new Buffer(compileResult.code);
} else {
var err = compileResult.errors[0];
var errorMsg = 'Compilation error in "'+ file.path +'" at ' + err.line + ':' + err.column + ': ' + err.description;
streamOfFiles.emit('error', new PluginError('gulp-hsp', errorMsg));
}
return file;
}
function transpile(file, streamOfFiles) {
var contentAsString = String(file.contents);
var transpileResult = {changed: false};
try {
transpileResult = hspTranspiler(contentAsString, file.path);
} catch (e) {
streamOfFiles.emit('error', new PluginError('gulp-hsp',
'Transpilation error in "' + file.path + '" at '+ e.line + ':' + e.col + ': ' + e.message), {
fileName: file.path,
lineNumber: e.line,
stack: e.stack
});
}
if (transpileResult.changed) {
file.contents = new Buffer(transpileResult.code);
}
return file;
}
function gulpTaskFactory(taskToExecute) {
return function processHspFile(file, enc, cb) {
if(file.isStream()){
this.emit('error', new PluginError('gulp-hashspace', 'Streaming not supported'));
return cb();
}
if(file.isBuffer()){
try {
this.push(taskToExecute(file, this));
} catch(e) {
this.emit('error', e);
}
}
cb();
}
}
module.exports.compile = function() {
return through.obj(gulpTaskFactory(compile));
};
module.exports.transpile = function() {
return through.obj(gulpTaskFactory(transpile));
};
module.exports.process = function() {
return through.obj(gulpTaskFactory(function(file, streamOfFiles){
return path.extname(file.path) === '.hsp' ? compile(file, streamOfFiles) : transpile(file, streamOfFiles);
}));
};