-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmwc_compiler.js
256 lines (207 loc) · 6.68 KB
/
mwc_compiler.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
var chokidar = Npm.require("chokidar"),
fs = Npm.require("fs"),
mkdirp = Npm.require("mkdirp"),
path = Npm.require("path"),
echo = Npm.require("node-echo"),
vulcanize = Npm.require("vulcanize");
var watcher = null;
function append(file, html) {
var body = html.match(/<body[^>]*>((.|[\n\r])*)<\/body>/im),
head = html.match(/<head[^>]*>((.|[\n\r])*)<\/head>/im);
if (body) {
file.addHtml({
data: body[1],
section: "body"
});
}
if (head) {
file.addHtml({
data: head[1],
section: "head"
});
}
}
function canProceed() {
var unAcceptableCommands = {'add':1,'test-packages': 1, 'publish': 1};
if(process.argv.length > 2) {
var command = process.argv[2];
if(unAcceptableCommands[command]) {
return false;
}
}
return true;
}
MWC_Compiler = function() {
this.compilerFileName = "compiler.mwc.json";
this.mwcFile = "mwc_compiler.html";
this.watcherFolder = null;
this.publicFolder = path.resolve("./public");
this.extensions = {};
}
MWC_Compiler.prototype.processFilesForTarget = function(files) {
var _this = this;
if (-1 < ["web.browser", "web.cordova"].indexOf(files[0].getArch())) {
var restartOnEdit = [],
watcherFolder = null;
files.forEach(function(file) {
if (file.getBasename() == _this.compilerFileName) {
try {
var json = JSON.parse(file.getContentsAsString());
} catch (error) {
file.error({
message: "failed to parse, " + error.message
});
return;
}
if (json.hasOwnProperty("root")) {
var mwcRootPath = path.resolve(json.root);
if (fs.existsSync(mwcRootPath)) {
if (watcherFolder) {
file.error({
message: "more than one " + _this.compilerFileName + " found"
});
return;
} else {
watcherFolder = mwcRootPath;
}
if(json.hasOwnProperty("extensions")){
_this.extensions = json.extensions;
}
if (json.hasOwnProperty("append")) {
json.append.forEach(function(item) {
var itemPath = path.resolve(mwcRootPath, item);
if (fs.existsSync(itemPath)) {
vulcanizer(itemPath, file, _this.extensions);
if (restartOnEdit.indexOf(itemPath) == -1) {
restartOnEdit.push(itemPath);
}
} else {
file.error({
message: "@append " + itemPath + " notFound"
});
}
});
}
if (json.hasOwnProperty("import")) {
var data = "";
json.import.forEach(function(item) {
var itemPath = path.resolve(mwcRootPath, item);
if (fs.existsSync(itemPath)) {
data += '<link href="' + item + '" rel="import">';
} else {
file.addHtml({
data: '<link href="' + item + '" rel="import">',
section: "head"
});
console.log("can't find " + itemPath + ", added @head");
}
});
if (data != "") {
data = "<head>" + data + "</head>";
if (!fs.existsSync(_this.publicFolder)) {
mkdirp.sync(_this.publicFolder);
}
var mwcPath = path.resolve(mwcRootPath, _this.mwcFile),
mwcPathPublic = path.resolve(_this.publicFolder, _this.mwcFile);
if (!fs.existsSync(mwcPath) || !fs.existsSync(mwcPathPublic) || fs.readFileSync(mwcPath).toString("utf-8") != data) {
fs.writeFileSync(mwcPath, data);
vulcanizer(mwcPath, mwcPathPublic, _this.extensions);
}
file.addHtml({
data: '<link href="/' + _this.mwcFile + '" rel="import">',
section: "head"
});
}
}
} else {
file.error({
message: "can't find root, " + mwcRootPath
});
}
} else {
file.error({
message: "property root[polymer project absolute/relative path] required"
});
}
}
});
if (_this.watcherFolder != watcherFolder) {
_this.watcherFolder = watcherFolder;
if (watcher) {
watcher.close();
}
//if(process.env.MWC_WATCHING == undefined){
//process.env.MWC_WATCHING = "true";
watcher = chokidar.watch(_this.watcherFolder, {
// ignored: /[\/\\]\./,
ignoreInitial: true
});
watcher.on("all", function(event,A, stats) {
//console.log(this);
var d = new Date();
var prevRun = process.env.MWC_LAST_RUN || 0;
if( (d.valueOf()-prevRun) > 300 ){
if (_this.mwcFile != path.basename(A)) {
if (!fs.existsSync(_this.publicFolder)) {
mkdirp.sync(_this.publicFolder);
}
var mwcPathPublic = path.resolve(_this.publicFolder, _this.mwcFile);
if (-1 < restartOnEdit.indexOf(A)) {
fs.appendFileSync(mwcPathPublic, " ");
} else {
var mwcPath = path.resolve(_this.watcherFolder, _this.mwcFile);
if (fs.existsSync(mwcPath)) {
setTimeout(function(){
vulcanizer(mwcPath, mwcPathPublic, _this.extensions);
},300)
} else {
fs.appendFileSync(mwcPathPublic, " ");
}
}
}
}
process.env.MWC_LAST_RUN = d.valueOf();
});
//}
}
}
};
vulcanizer = function(target, destination, extensions) {
// var wait = new future();
vulcanize.setOptions({
implicitStrip: true,
inlineCss: true,
inlineScripts: true,
stripComments: true
});
vulcanize.process(target, function(error, html) {
if (typeof(destination) == "string") {
if (error) {
console.log(error);
} else {
var extended = MWC_extend(html,extensions);
fs.writeFile(destination, extended);
}
} else {
if (error) {
destination.error({
message: error.message
});
} else {
var extended = MWC_extend(html,extensions);
append(destination,extended);
}
}
// wait.return(true);
});
// return wait.wait();
}
function MWC_extend(html,extensions){
if(!!Package["mwc:extensions"]){
if(!!Package["mwc:extensions"]["MWCExtend"]){
return Package["mwc:extensions"]["MWCExtend"](html,extensions);
}
}else{
}
return html;
}