This repository has been archived by the owner on Jul 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
109 lines (97 loc) · 3.31 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var through = require("through"),
gutil = require("gulp-util"),
Buffer = require("buffer").Buffer,
PluginError = gutil.PluginError,
fs = require("fs"),
os = require("os"),
File = gutil.File,
closureTemplates = require("closure-templates"),
path = require("path"),
spawn = require("child_process").spawn,
md5 = require("MD5");
module.exports = function (options) {
if (typeof options !== "object") {
options = {};
}
var tmp = path.resolve(options.tmpDir || path.join(os.tmpdir(), "soy")),
addSoyUtils = options.hasOwnProperty("soyutils") ? options.soyutils : true,
compiler = path.resolve(closureTemplates["SoyToJsSrcCompiler.jar"]),
soyUtils = path.resolve(closureTemplates["soyutils.js"]),
files = [];
function write (file){
if (!file.isNull()) {
if (file.isStream()) {
this.emit("error", new PluginError("gulp-soy", "Streaming not supported"));
} else {
files.push(file);
}
}
}
function build(self, input, output, callback) {
var cp,
stderr = "",
args = [
"-classpath", compiler,
"com.google.template.soy.SoyToJsSrcCompiler",
"--codeStyle", "concat",
"--outputPathFormat", output,
input
];
cp = spawn("java", args);
cp.stderr.on("data", function (data) {
stderr += data
});
cp.on("exit", function (exitCode) {
if (exitCode) {
console.error("Compile error\n", stderr);
self.emit("compile", new Error("Error compiling templates"), false);
self.emit("end");
} else {
callback();
}
});
}
function end() {
var self = this,
count = 0,
compiled = [];
function newFile(file, contentPath) {
compiled.push(new File({
cwd: file.cwd,
base: file.base,
path: file.path.replace(/\.soy$/, ".js"),
contents: new Buffer(fs.readFileSync(contentPath, "utf8"))
}));
count += 1;
if (count === files.length) {
if (addSoyUtils) {
self.emit("data",
new File({
cwd: file.cwd,
base: file.base,
path: path.join(file.base, "soyutils.js"),
contents: new Buffer(fs.readFileSync(soyUtils, "utf8"))
})
);
}
compiled.forEach(function (file) {
self.emit("data", file);
});
self.emit("end");
}
}
files.forEach(function (file) {
var hash = md5(file.contents.toString()),
pathHash = path.join(tmp, hash),
callback = function () {
newFile(file, pathHash);
};
if (fs.existsSync(pathHash)) {
callback();
} else {
build(self, file.path, pathHash, callback);
}
});
}
return through(write, end);
};