This repository has been archived by the owner on Feb 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (53 loc) · 2.16 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
'use strict';
var through = require('through2');
var gutil = require('gulp-util');
var path = require('path');
var typeScriptGenerator = require('onejs-compiler').TypeScriptGenerator;
var typeScriptViewModelGenerator = require('onejs-compiler').TypeScriptViewModelGenerator;
var extend = require('extend');
module.exports = function(userOptions) {
var options = extend(true, {
typeScriptViewFileFormat: '{{templateName}}.ts',
typeScriptViewModelFileFormat: '',
paths: {
onejs: '../onejs/',
defaultView: '../{{viewType}}/{{viewType}}'
}
}, userOptions);
function getFileName(nameMatch, templateName) {
return nameMatch.replace('{{templateName}}', templateName);
}
function generate(generatorType, templateContent, fileNameFormat, file) {
var generator = new generatorType();
var outputFile = file.clone();
try {
outputFile.contents = new Buffer(generator.generate(templateContent, options));
outputFile.path = file.path.replace(path.basename(file.path), getFileName(fileNameFormat, generator.template.name));
} catch (e) {
throw new gutil.PluginError({
plugin: 'gulp-onejs-compiler',
message: 'Error parsing template: ' + file.path + '\n' + e
});
}
return outputFile;
}
var stream = through.obj(function(file, enc, callback) {
if (file.isNull() || !file.contents || path.extname(file.path).toLowerCase() !== '.html') {
this.push(file);
callback();
return;
}
var fileContent = file.contents.toString('utf8');
var outputFile;
// Build typescript view class.
if (options.typeScriptViewFileFormat) {
this.push(generate(typeScriptGenerator, fileContent, options.typeScriptViewFileFormat, file));
}
// Build typescript view model interface.
if (options.typeScriptViewModelFileFormat) {
this.push(generate(typeScriptViewModelGenerator, fileContent, options.typeScriptViewModelFileFormat, file));
}
callback();
});
return stream;
};