-
Notifications
You must be signed in to change notification settings - Fork 0
/
facets2polymer.js
177 lines (177 loc) · 6.44 KB
/
facets2polymer.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
//<reference path="node_modules/reflect-metadata/reflect-metadata.d.ts"/>
///<reference path="Scripts/typings/cheerio/cheerio.d.ts"/>
///<reference path="Scripts/typings/node/node.d.ts"/>
"use strict";
require('reflect-metadata/Reflect');
const path = require('path');
const fs = require('fs');
const process = require('process');
//const filePath = './Tests/FlagIcon';
const filePath = process.argv[2];
const bt = require('./bt');
//import flagIcon = require(filePath);
//import flagIcon = require('./Tests/FlagIcon');
//var flagIcon = require(filePath);
function processFACETSFileTemplate(filePath) {
const facetsFile = require(filePath);
const fileName = path.basename(filePath);
const templateName = fileName + "Template";
const templateFnString = facetsFile[templateName].toString();
const astForView = bt.generateTemplateAbstractSyntaxTree(templateFnString);
const classReflection = bt.processFACETSFileClass(fileName, facetsFile);
const classDef = generatePolymerClassDefition(classReflection);
const templateString = performHTMLTransformToPolymerFormat(astForView, classDef);
// const test2 = astForView.html();
// console.log(test2);
}
const outputS = processFACETSFileTemplate(filePath);
const resolvedFilePath = path.resolve(filePath);
const outputFilePath = resolvedFilePath + '.html';
fs.writeFileSync(outputFilePath, outputS, { encoding: 'utf8' });
function performHTMLTransformToPolymerFormat($, classDef) {
const astForView = $.root();
const rootTemplate = astForView.children('xsl\\:template');
const matchParam = rootTemplate.attr('match').replace('_', '-');
const valueOfs = rootTemplate.find('xsl\\:value-of');
valueOfs.each((idx, valueOf) => {
const $valueOf = $(valueOf);
const select = $valueOf.attr('select');
$valueOf.replaceWith('{{' + select + '}}');
});
const forEachs = rootTemplate.find('xsl\\:for-each');
forEachs.each((idx, forEach) => {
const $forEach = $(forEach);
const select = $forEach.attr('select');
const $variableNameElement = $forEach.children('xsl\\:variable');
const name = $variableNameElement.attr('name');
//const $repeatingElement = $variableNameElement.next();
//$repeatingElement.attr('is', '');
$variableNameElement.remove();
const innerHTML = $forEach.html();
$forEach.replaceWith(`<template is="dom-repeat" items="{{${select}}}" as="${name}">
${innerHTML}
</template>
`);
});
const outputTemplate = `
<dom-module id='${matchParam}'>
<template>
${rootTemplate.html()}
</template>
<script>
Polymer({
is: '${matchParam}',
${classDef}
});
</script>
</dom-module>
`;
console.log(outputTemplate);
return outputTemplate;
}
//from http://blog.codeeffects.com/Article/String-Builder-In-Java-Script
function StringBuilder() {
var strings = [];
this.append = function (string) {
string = verify(string);
if (string.length > 0)
strings[strings.length] = string;
};
this.appendLine = function (string) {
string = verify(string);
if (this.isEmpty()) {
if (string.length > 0)
strings[strings.length] = string;
else
return;
}
else
strings[strings.length] = string.length > 0 ? "\r\n" + string : "\r\n";
};
this.clear = function () { strings = []; };
this.isEmpty = function () { return strings.length == 0; };
this.toString = function () { return strings.join(""); };
var verify = function (string) {
if (!defined(string))
return "";
if (getType(string) != getType(new String()))
return String(string);
return string;
};
var defined = function (el) {
// Changed per Ryan O'Hara's comment:
return el != null && typeof (el) != "undefined";
};
var getType = function (instance) {
if (!defined(instance.constructor))
throw Error("Unexpected object type");
var type = String(instance.constructor).match(/function\s+(\w+)/);
return defined(type) ? type[1] : "undefined";
};
}
;
function toJSObject(val) {
let JSVal;
switch (typeof (val)) {
case 'string':
JSVal = "'" + val + "'";
break;
default:
JSVal = val;
}
return JSVal;
}
function generatePolymerClassDefition(classReflection) {
let result = new StringBuilder();
result.appendLine(`properties: {`);
//let counter = 0;
let propLen = classReflection.properties.length;
classReflection.properties.forEach((property, idx) => {
result.appendLine(property.name + ':{');
if (property.metadata) {
let type = property.metadata['design:type'].toString();
if (type) {
const splitFunction = type.split(' ');
type = splitFunction[1].replace('(', '').replace(')', '');
result.appendLine('type: ' + type);
}
const wcp = property.metadata['WebComponentProps'];
if (wcp) {
if (typeof (wcp.defaultValue) !== 'undefined') {
result.appendLine('value: ' + toJSObject(wcp.defaultValue));
}
for (const key in wcp) {
if (key.startsWith('polymer_')) {
const polymerKey = key.substring(8);
result.appendLine(polymerKey + ':' + toJSObject(wcp[key]));
}
}
}
}
//counter++;
if (idx < propLen - 1) {
result.appendLine('},');
}
else {
result.appendLine('}');
}
//result.appendLine('');
});
result.appendLine('}');
if (classReflection.methods || classReflection.staticMethods) {
result.append(',');
if (classReflection.methods) {
const methodLength = classReflection.methods.length;
classReflection.methods.forEach((method, idx) => {
const paramNames = method.args.map(arg => arg.name);
result.appendLine(method.name + ': function(' + paramNames.join(',') + ')');
result.append(method.methodBody);
if (idx < methodLength - 1) {
result.append(',');
}
});
}
}
return result.toString();
}
//# sourceMappingURL=facets2polymer.js.map