forked from qmlweb/gulp-qmlweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (35 loc) · 1.28 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
var es = require('event-stream');
var gutil = require('gulp-util');
var parser = require('qmlweb-parser');
module.exports = function (opt) {
opt = opt || {};
var pathFilter = opt.pathFilter || function(path) {
return path;
};
function modifyFile(file) {
if (file.isNull()) return this.emit('data', file);
if (file.isStream()) return this.emit('error', new Error("gulp-qml: Streaming not supported"));
var data;
var src;
var str = file.contents.toString('utf8');
var dest = gutil.replaceExtension(file.path, ".js");
var gulpPath = __dirname.split('/');
gulpPath = gulpPath.splice(0, gulpPath.length - 2).join('/') + '/';
var path = file.path.substr(gulpPath.length, file.path.length);
try {
if (file.path.match(/\.qml$/) != null)
data = parser.qmlweb_parse(str, parser.qmlweb_parse.QMLDocument);
else if (file.path.match(/\.js$/) != null)
data = parser.qmlweb_jsparse(str);
else
data = str;
} catch (err) {
return this.emit('error', new Error(file.path + ': ' + err));
}
src = "qrc['" + pathFilter(path) + "'] = " + JSON.stringify(data) + ';';
file.contents = new Buffer(src);
file.path = dest;
this.emit('data', file);
}
return es.through(modifyFile);
};