@ui5/webcomponents-tools 0.0.0-d0bcf47c7 → 0.0.0-d4d43327a

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/CHANGELOG.md +418 -1
  2. package/README.md +5 -6
  3. package/assets-meta.js +153 -0
  4. package/bin/dev.js +12 -1
  5. package/components-package/eslint.js +28 -0
  6. package/components-package/nps.js +106 -44
  7. package/components-package/postcss.components.js +13 -13
  8. package/components-package/postcss.themes.js +19 -16
  9. package/components-package/vite.config.js +12 -0
  10. package/components-package/wdio.js +128 -62
  11. package/components-package/wdio.sync.js +360 -0
  12. package/icons-collection/nps.js +69 -28
  13. package/lib/copy-and-watch/index.js +145 -0
  14. package/lib/copy-list/index.js +28 -0
  15. package/lib/create-icons/index.js +124 -0
  16. package/lib/create-illustrations/index.js +161 -0
  17. package/lib/create-new-component/index.js +154 -0
  18. package/lib/dev-server/dev-server.js +66 -0
  19. package/lib/dev-server/virtual-index-html-plugin.js +53 -0
  20. package/lib/esm-abs-to-rel/index.js +58 -0
  21. package/lib/generate-custom-elements-manifest/index.js +327 -0
  22. package/lib/generate-js-imports/illustrations.js +72 -0
  23. package/lib/generate-json-imports/i18n.js +98 -0
  24. package/lib/generate-json-imports/themes.js +80 -0
  25. package/lib/hbs2lit/index.js +3 -0
  26. package/lib/hbs2lit/src/compiler.js +57 -0
  27. package/lib/hbs2lit/src/extendedAttributeMapping.js +12 -0
  28. package/lib/hbs2lit/src/includesReplacer.js +31 -0
  29. package/lib/hbs2lit/src/litVisitor2.js +253 -0
  30. package/lib/hbs2lit/src/partials2.js +51 -0
  31. package/lib/hbs2lit/src/partialsVisitor.js +187 -0
  32. package/lib/hbs2lit/src/svgProcessor.js +69 -0
  33. package/lib/hbs2ui5/RenderTemplates/LitRenderer.js +12 -0
  34. package/lib/hbs2ui5/index.js +102 -0
  35. package/lib/i18n/defaults.js +82 -0
  36. package/lib/i18n/toJSON.js +43 -0
  37. package/lib/jsdoc/config.json +29 -0
  38. package/lib/jsdoc/configTypescript.json +29 -0
  39. package/lib/jsdoc/plugin.js +2468 -0
  40. package/lib/jsdoc/preprocess.js +146 -0
  41. package/lib/jsdoc/template/publish.js +4120 -0
  42. package/lib/postcss-combine-duplicated-selectors/index.js +178 -0
  43. package/lib/postcss-css-to-esm/index.js +90 -0
  44. package/lib/postcss-css-to-json/index.js +47 -0
  45. package/lib/postcss-new-files/index.js +36 -0
  46. package/lib/postcss-p/postcss-p.mjs +14 -0
  47. package/lib/replace-global-core/index.js +25 -0
  48. package/lib/scoping/get-all-tags.js +37 -0
  49. package/lib/scoping/lint-src.js +31 -0
  50. package/lib/scoping/missing-dependencies.js +65 -0
  51. package/lib/scoping/report-tags-usage.js +28 -0
  52. package/lib/scoping/scope-test-pages.js +40 -0
  53. package/lib/test-runner/test-runner.js +63 -0
  54. package/package.json +48 -55
  55. package/bin/init-ui5-package.js +0 -3
  56. package/package-lock.json +0 -9994
@@ -0,0 +1,253 @@
1
+ const Handlebars = require("handlebars/dist/handlebars.min.js");
2
+ const path = require("path");
3
+ const Visitor = Handlebars.Visitor;
4
+
5
+ // skip ifDefined for event handlers and boolean attrs
6
+ let skipIfDefined = false;
7
+
8
+ // when true => an HTML node value, when false => an attribute value
9
+ let isNodeValue = false;
10
+
11
+ // when true => the current attribute is "style"
12
+ let isStyleAttribute = false;
13
+
14
+ // matches event handlers @click= and boolean attrs ?disabled=
15
+ const dynamicAttributeRgx = /\s(\?|@)([a-zA-Z|-]+)="?\s*$/;
16
+
17
+ if (!String.prototype.replaceAll) {
18
+ String.prototype.replaceAll = function(str, newStr){
19
+
20
+ // If a regex pattern
21
+ if (Object.prototype.toString.call(str).toLowerCase() === '[object regexp]') {
22
+ return this.replace(str, newStr);
23
+ }
24
+
25
+ // If a string
26
+ return this.replace(new RegExp(str, 'g'), newStr);
27
+
28
+ };
29
+ }
30
+
31
+ function HTMLLitVisitor(debug) {
32
+ this.blockCounter = 0;
33
+ this.keys = [];
34
+ this.blocks = {};
35
+ this.result = "";
36
+ this.mainBlock = "";
37
+ this.blockLevel = 0;
38
+ this.blockParameters = ["context", "tags", "suffix"];
39
+ this.paths = []; //contains all normalized relative paths
40
+ this.debug = debug;
41
+ if (this.debug) {
42
+ this.blockByNumber = [];
43
+ }
44
+ }
45
+
46
+ HTMLLitVisitor.prototype = new Visitor();
47
+
48
+ HTMLLitVisitor.prototype.Program = function(program) {
49
+ let key = `block${this.blockCounter++}`;
50
+
51
+ this.keys.push(key);
52
+ this.debug && this.blockByNumber.push(key);
53
+
54
+ this.blocks[this.currentKey()] = "const " + this.currentKey() + " = (" + this.blockParameters.join(", ") + ") => ";
55
+
56
+ if (this.keys.length > 1) { //it's a nested block
57
+ this.blocks[this.prevKey()] += this.currentKey() + "(" + this.blockParameters.join(", ") + ")";
58
+ } else {
59
+ this.mainBlock = this.currentKey();
60
+ }
61
+
62
+ this.blocks[this.currentKey()] += "html`";
63
+ Visitor.prototype.Program.call(this, program);
64
+ this.blocks[this.currentKey()] += "`;";
65
+
66
+ this.keys.pop(key);
67
+ };
68
+
69
+ HTMLLitVisitor.prototype.ContentStatement = function(content) {
70
+ Visitor.prototype.ContentStatement.call(this, content);
71
+ // let content = content.orgiinal; // attribute="__ attribute = "__ attribute ="__
72
+
73
+ let contentStatement = content.original;
74
+ skipIfDefined = !!dynamicAttributeRgx.exec(contentStatement);
75
+
76
+ const closingIndex = contentStatement.lastIndexOf(">");
77
+ const openingIndex = contentStatement.lastIndexOf("<");
78
+ if (closingIndex !== -1 || openingIndex !== -1) { // Only change isNodeValue whenever < or > is found in the content statement
79
+ isNodeValue = closingIndex > openingIndex;
80
+ }
81
+
82
+ isStyleAttribute = !isNodeValue && contentStatement.match(/style *= *["']? *$/);
83
+
84
+ if (!isStyleAttribute && contentStatement.match(/style=/)) {
85
+ console.log("WARNING: style hard-coded", contentStatement);
86
+ }
87
+
88
+ // Scope custom element tags
89
+ contentStatement = contentStatement.replaceAll(/(<\/?\s*)([a-zA-Z0-9_]+-[a-zA-Z0-9_-]+)/g, "$1\${scopeTag(\"$2\", tags, suffix)}");
90
+
91
+ this.blocks[this.currentKey()] += contentStatement;
92
+ };
93
+
94
+ HTMLLitVisitor.prototype.MustacheStatement = function(mustache) {
95
+ Visitor.prototype.MustacheStatement.call(this, mustache);
96
+
97
+ if (mustache.path.original === "@index") {
98
+ this.blocks[this.currentKey()] += "${index}";
99
+ } else {
100
+ const path = normalizePath.call(this, mustache.path.original);
101
+ const hasCalculatingClasses = path.includes("context.classes");
102
+
103
+ let parsedCode = "";
104
+
105
+ if (isNodeValue && !mustache.escaped) {
106
+ parsedCode = `\${unsafeHTML(${path})}`;
107
+ } else if (hasCalculatingClasses) {
108
+ parsedCode = `\${classMap(${path})}`;
109
+ } else if (isStyleAttribute) {
110
+ parsedCode = `\${styleMap(${path})}`;
111
+ } else if (skipIfDefined){
112
+ parsedCode = `\${${path}}`;
113
+ } else {
114
+ parsedCode = `\${ifDefined(${path})}`;
115
+ }
116
+
117
+ this.blocks[this.currentKey()] += parsedCode;
118
+ }
119
+ };
120
+
121
+ HTMLLitVisitor.prototype.BlockStatement = function(block) {
122
+ if (block.path.original === "if") {
123
+ visitIfBlock.call(this, block);
124
+ } else if (block.path.original === "unless") {
125
+ visitUnlessBlock.call(this, block);
126
+ } else if (block.path.original === "each") {
127
+ visitEachBlock.call(this, block);
128
+ }
129
+ };
130
+
131
+ HTMLLitVisitor.prototype.currentKey = function() {
132
+ return this.keys[this.keys.length - 1];
133
+ };
134
+
135
+ HTMLLitVisitor.prototype.prevKey = function() {
136
+ return this.keys[this.keys.length - 2];
137
+ };
138
+
139
+ function visitSubExpression(mustache) {
140
+ this.acceptRequired(mustache, "path");
141
+ this.acceptArray(mustache.params);
142
+ this.acceptKey(mustache, "hash");
143
+ }
144
+
145
+ function visitIfBlock(block) {
146
+ visitSubExpression.call(this, block);
147
+
148
+ let params = normalizePath.call(this, block.params[0].original);
149
+ this.blocks[this.currentKey()] += "${ " + params + " ? ";
150
+ this.acceptKey(block, "program");
151
+ this.blocks[this.currentKey()] += " : ";
152
+ if (block.inverse) {
153
+ this.acceptKey(block, "inverse");
154
+ } else {
155
+ this.blocks[this.currentKey()] += "undefined";
156
+ }
157
+ this.blocks[this.currentKey()] += " }";
158
+ }
159
+
160
+ function visitUnlessBlock(block) {
161
+ visitSubExpression.call(this, block);
162
+
163
+ let params = normalizePath.call(this, block.params[0].original);
164
+ this.blocks[this.currentKey()] += "${ !" + params + " ? ";
165
+ this.acceptKey(block, "program");
166
+ this.blocks[this.currentKey()] += " : undefined }";
167
+ }
168
+
169
+ function visitEachBlock(block) {
170
+ var bParamAdded = false;
171
+ visitSubExpression.call(this, block);
172
+
173
+ this.blocks[this.currentKey()] += "${ repeat(" + normalizePath.call(this, block.params[0].original) + ", (item, index) => item._id || index, (item, index) => ";
174
+ this.paths.push(normalizePath.call(this, block.params[0].original));
175
+ this.blockLevel++;
176
+
177
+ if (this.blockParameters.indexOf("item") === -1) {
178
+ bParamAdded = true;
179
+ this.blockParameters.unshift("index");
180
+ this.blockParameters.unshift("item");
181
+ }
182
+ this.acceptKey(block, "program");
183
+ if (bParamAdded) {
184
+ this.blockParameters.shift("item");
185
+ this.blockParameters.shift("index");
186
+ }
187
+ this.blockLevel--;
188
+ this.blocks[this.currentKey()] += ") }";
189
+ }
190
+
191
+ function normalizePath(sPath) {
192
+ let result = replaceAll(replaceAll(replaceAll(sPath, ".this", ""), "this.", ""), "this", "");
193
+
194
+ //read carefully - https://github.com/wycats/handlebars.js/issues/1028
195
+ //kpdecker commented on May 20, 2015
196
+
197
+ if (result.indexOf("@root") === 0) {
198
+ // Trying to access root context via the HBS "@root" variable.
199
+ // Example: {{@root.property}} compiles to "context.property" - called from anywhere within the template.
200
+ result = result.replace("@root", "context");
201
+
202
+ } else if (result.indexOf("../") === 0) {
203
+ let absolutePath;
204
+ const levelsUp = (result.match(/..\//g) || []).length;
205
+
206
+ if (this.blockLevel <= levelsUp) {
207
+ // Trying to access root context from nested loops.
208
+ // Example: {{../../property}} compiles to "context.property" - when currently in a nested level loop.
209
+ // Example: {{../../../property}} compile to "context.property" - when requested levels are not present. fallback to root context.
210
+ absolutePath = `context.${replaceAll(result,"../", "")}`;
211
+ } else {
212
+ // Trying to access upper context (one-level-up) and based on the current lelev, that could be "context" or "item".
213
+ // Example: {{../property}} compiles to "context.property" - when called in a top level loop.
214
+ // Example: {{../property}} compiles to "item.property" - when called in a nested level loop.
215
+ // TODO: the second example, although correctly generated to "item.property", "item" will point to the current object within the nested loop,
216
+ // not the upper level loop as intended. So accessing the upper loop from nested loop is currently not working.
217
+ absolutePath = replaceAll(this.paths[this.paths.length - 1 - levelsUp], ".", "/") + "/" + result;
218
+ }
219
+
220
+ result = replaceAll(path.normalize(absolutePath), path.sep, ".");
221
+
222
+ } else {
223
+ // When neither "@root", nor "../" are used, use the following contexts:
224
+ // - use "context" - for the top level of execution, e.g "this.blockLevel = 0".
225
+ // - use "item" - for any nested level, e.g "this.blockLevel > 0".
226
+ // Example:
227
+ //
228
+ // {{text}} -> compiles to "context.text"
229
+ // {{#each items}}
230
+ // Item text: {{text}}</div> -> compiles to "item.text"
231
+ // {{#each words}}
232
+ // Word text: {{text}}</div> -> compiles to "item.text"
233
+ // {{/each}}
234
+ // Item text: {{text}}</div> -> compiles to "item.text"
235
+ // {{/each}}
236
+ // {{text}} -> compiles to "context.text"
237
+
238
+ const blockPath = this.blockLevel > 0 ? "item" : "context";
239
+ result = result ? replaceAll(blockPath + "/" + result, "/", ".") : blockPath;
240
+ }
241
+
242
+ return result;
243
+ }
244
+
245
+ function replaceAll(str, find, repl) {
246
+ let sResult = str;
247
+ while (sResult.indexOf(find) !== -1) {
248
+ sResult = sResult.replace(find, repl);
249
+ }
250
+ return sResult;
251
+ }
252
+
253
+ module.exports = HTMLLitVisitor;
@@ -0,0 +1,51 @@
1
+ //collects the partials defined with the inline mustache syntax
2
+ //also cleans the handlebars template from the partials definitions
3
+ function collectPartials(hbs) {
4
+ let oResult = {};
5
+
6
+ const re = RegExp("{{#\\*inline\\s* \"(\\w+)\"\\s*}}((.|\\s)*?){{\\/inline}}");
7
+ const regexGroups = {partialKey: 1, partialDefinition: 2};
8
+
9
+ let m;
10
+
11
+ do {
12
+ m = re.exec(hbs);
13
+ if (m) {
14
+ // This is necessary to avoid infinite loops with zero-width matches
15
+ if (m.index === re.lastIndex) {
16
+ re.lastIndex++;
17
+ }
18
+
19
+ // The result can be accessed through the `m`-variable.
20
+ oResult[m[regexGroups.partialKey]] = m[regexGroups.partialDefinition];
21
+
22
+ //remove the partial from the hbs
23
+ hbs = hbs.replace(m[0], "");
24
+ }
25
+ } while (m);
26
+
27
+ return {partials: oResult, hbs: hbs};
28
+ }
29
+
30
+ //replaces the partials usages with their actual definitions
31
+ function replacePartials(hbs, mPartials) {
32
+ let sResultHbs = hbs;
33
+ for (let key in mPartials) {
34
+ if (mPartials.hasOwnProperty(key)) {
35
+ let val = mPartials[key];
36
+ let re = new RegExp("{{\\s*>\\s*" + key + "\\s*}}", "g"); //ex match: {{ > controlData }}
37
+ sResultHbs = sResultHbs.replace(re, val);
38
+ }
39
+ }
40
+
41
+ return sResultHbs;
42
+ }
43
+
44
+ module.exports = {
45
+ resolve: function(hbs) {
46
+ let oResult = collectPartials(hbs);
47
+ let sResultHbs = replacePartials(oResult.hbs, oResult.partials);
48
+
49
+ return sResultHbs;
50
+ }
51
+ };
@@ -0,0 +1,187 @@
1
+ function Visitor() {
2
+ this.parents = [];
3
+ this.paths = [];
4
+ }
5
+
6
+ Visitor.prototype = {
7
+ constructor: Visitor,
8
+ mutating: true,
9
+
10
+ // Visits a given value. If mutating, will replace the value if necessary.
11
+ acceptKey: function(node, name) {
12
+ this.paths.push(name);
13
+ let value = this.accept(node[name]);
14
+ if (this.mutating) {
15
+ // Hacky sanity check: This may have a few false positives for type for the helper
16
+ // methods but will generally do the right thing without a lot of overhead.
17
+ if (value && !Visitor.prototype[value.type]) {
18
+ throw new Error("Unexpected node type \"" + value.type + "\" found when accepting " + name + " on " + node.type);
19
+ }
20
+ node[name] = value;
21
+ }
22
+ this.paths.pop();
23
+ },
24
+
25
+ // Performs an accept operation with added sanity check to ensure
26
+ // required keys are not removed.
27
+ acceptRequired: function(node, name) {
28
+ this.acceptKey(node, name);
29
+
30
+ if (!node[name]) {
31
+ throw new Error(node.type + " requires " + name);
32
+ }
33
+ },
34
+
35
+ // Traverses a given array. If mutating, empty respnses will be removed
36
+ // for child elements.
37
+ acceptArray: function(array) {
38
+ for (let i = 0, l = array.length; i < l; i++) {
39
+ this.acceptKey(array, i);
40
+
41
+ if (!array[i]) {
42
+ array.splice(i, 1);
43
+ i--;
44
+ l--;
45
+ }
46
+ }
47
+ },
48
+
49
+ accept: function(object) {
50
+ if (!object) {
51
+ return;
52
+ }
53
+
54
+ /* istanbul ignore next: Sanity code */
55
+ if (!this[object.type]) {
56
+ throw new Error("Unknown type: " + object.type, object);
57
+ }
58
+
59
+ if (this.current) {
60
+ this.parents.unshift(this.current);
61
+ }
62
+ this.current = object;
63
+
64
+ let ret = this[object.type](object);
65
+
66
+ this.current = this.parents.shift();
67
+
68
+ if (!this.mutating || ret) {
69
+ return ret;
70
+ } else if (ret !== false) {
71
+ return object;
72
+ }
73
+ },
74
+
75
+ Program: function(program) {
76
+ this.paths.push("body");
77
+ this.acceptArray(program.body);
78
+ this.paths.pop();
79
+ },
80
+
81
+ MustacheStatement: visitSubExpression,
82
+ Decorator: visitSubExpression,
83
+
84
+ BlockStatement: visitBlock,
85
+ DecoratorBlock: visitBlock,
86
+
87
+ PartialStatement: visitPartial,
88
+ PartialBlockStatement: function(partial) {
89
+ visitPartial.call(this, partial);
90
+
91
+ this.acceptKey(partial, "program");
92
+ },
93
+
94
+ ContentStatement: function(/* content */) {},
95
+ CommentStatement: function(/* comment */) {},
96
+
97
+ SubExpression: visitSubExpression,
98
+
99
+ PathExpression: function(/* path */) {},
100
+
101
+ StringLiteral: function(/* string */) {},
102
+ NumberLiteral: function(/* number */) {},
103
+ BooleanLiteral: function(/* bool */) {},
104
+ UndefinedLiteral: function(/* literal */) {},
105
+ NullLiteral: function(/* literal */) {},
106
+
107
+ Hash: function(hash) {
108
+ this.paths.push("pairs");
109
+ this.acceptArray(hash.pairs);
110
+ this.paths.pop();
111
+ },
112
+ HashPair: function(pair) {
113
+ this.acceptRequired(pair, "value");
114
+ }
115
+ };
116
+
117
+ function visitSubExpression(mustache) {
118
+ this.acceptRequired(mustache, "path");
119
+ this.paths.push("params");
120
+ this.acceptArray(mustache.params);
121
+ this.paths.pop();
122
+ this.acceptKey(mustache, "hash");
123
+ }
124
+ function visitBlock(block) {
125
+ visitSubExpression.call(this, block);
126
+
127
+ this.acceptKey(block, "program");
128
+ this.acceptKey(block, "inverse");
129
+ }
130
+ function visitPartial(partial) {
131
+ this.acceptRequired(partial, "name");
132
+ this.paths.push("params");
133
+ this.acceptArray(partial.params);
134
+ this.paths.pop();
135
+ this.acceptKey(partial, "hash");
136
+ }
137
+
138
+ ////////////
139
+
140
+ function PartialsVisitor() {
141
+ this.partialDefinitions = {};
142
+ this.partials = [];
143
+ }
144
+
145
+ PartialsVisitor.prototype = new Visitor();
146
+
147
+ PartialsVisitor.prototype.PartialStatement = function(node) {
148
+ this.partials.push({
149
+ nodes: this.paths.slice(0),
150
+ name: node.name.original
151
+ });
152
+ Visitor.prototype.PartialStatement.call(this, node);
153
+ };
154
+
155
+ PartialsVisitor.prototype.DecoratorBlock = function(node) {
156
+ if (node.path.original === "ui5.inline" || node.path.original === "inline") {
157
+ let name = node.params[0].original;
158
+ this.partialDefinitions[name] = Object.assign({}, node);
159
+ }
160
+
161
+ Visitor.prototype.DecoratorBlock.call(this, node);
162
+
163
+ return false;
164
+ };
165
+
166
+ PartialsVisitor.prototype.collect = function(node) {
167
+ return Visitor.prototype.accept(node);
168
+ };
169
+
170
+ PartialsVisitor.prototype.modify = function(node) {
171
+ for (let i = this.partials.length - 1; i >= 0; i--) {
172
+ let partial = this.partials[i];
173
+ let parentNode = node;
174
+
175
+ //find the parent node - it's always inside the body of some Program node
176
+ while (partial.nodes.length > 1) {
177
+ parentNode = parentNode[partial.nodes.shift()];
178
+ }
179
+
180
+ let nodeName = partial.nodes.shift();
181
+ if (Array.isArray(parentNode) && typeof (nodeName) === "number") {
182
+ parentNode.splice(nodeName, 1, ...this.partialDefinitions[partial.name].program.body);
183
+ }
184
+ }
185
+ };
186
+
187
+ module.exports = PartialsVisitor;
@@ -0,0 +1,69 @@
1
+
2
+ const svgrx = new RegExp(/<svg[\s\S]*?>([\s\S]*?)<\/svg>/, 'g');
3
+ const blockrx = /block[0-9]+/g;
4
+
5
+ function process(input) {
6
+ let matches;
7
+ let template = input;
8
+ let blockCounter = 0;
9
+
10
+ matches = getSVGMatches(template);
11
+
12
+ if (!matches.length) {
13
+ return template;
14
+ }
15
+
16
+ matches.forEach(match => {
17
+ //(1) extract the SVG content as a separate block
18
+ const svgContentGroup = match[1];
19
+ const block = getSVGBlock(svgContentGroup, ++blockCounter);
20
+
21
+ // (2) replace the SVG content with its block called, e.g ${blockSVG(context)}
22
+ template = template.replace(svgContentGroup, block.usage);
23
+
24
+ // (3) look for internal blocks in the SVG content and replace their `html with `svg
25
+ template = replaceInternalBlocks(template, svgContentGroup);
26
+
27
+ // (4) append the SVG block definiton (built in step 1), e.g const blockSVG = (context) => {return svg`.*`}
28
+ template += block.definition;
29
+ });
30
+
31
+ return template;
32
+ }
33
+
34
+ function getSVGMatches(template) {
35
+ let matches = [];
36
+
37
+ while (svgMatch = svgrx.exec(template)) {
38
+ matches.push(svgMatch);
39
+ if (svgrx.lastIndex === svgMatch.index) {
40
+ svgrx.lastIndex++;
41
+ }
42
+ }
43
+
44
+ return matches;
45
+ }
46
+
47
+ function getSVGBlock(input, blockCounter) {
48
+ return {
49
+ usage: `\${blockSVG${blockCounter}(context, tags, suffix)}`,
50
+ definition: `\nconst blockSVG${blockCounter} = (context, tags, suffix) => svg\`${input}\`;`,
51
+ };
52
+ }
53
+
54
+ function replaceInternalBlocks(template, svgContent) {
55
+ const internalBlocks = svgContent.match(blockrx) || [];
56
+
57
+ internalBlocks.forEach(blockName => {
58
+ const rx = new RegExp(`const ${blockName}.*(html\`).*;`);
59
+ template = template.replace(rx, (match, p1) => {
60
+ return match.replace(p1, "svg\`");
61
+ });
62
+ });
63
+
64
+ return template;
65
+ }
66
+
67
+ module.exports = {
68
+ process: process
69
+ };
@@ -0,0 +1,12 @@
1
+ const buildRenderer = (controlName, litTemplate) => {
2
+ return `/* eslint no-unused-vars: 0 */
3
+ import { html, svg, repeat, classMap, styleMap, ifDefined, unsafeHTML, scopeTag } from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
4
+
5
+ ${litTemplate}
6
+
7
+ export default block0;`;
8
+ };
9
+
10
+ module.exports = {
11
+ generateTemplate: buildRenderer
12
+ };
@@ -0,0 +1,102 @@
1
+ const fs = require('fs').promises;
2
+ const getopts = require('getopts');
3
+ const hbs2lit = require('../hbs2lit');
4
+ const path = require('path');
5
+ const litRenderer = require('./RenderTemplates/LitRenderer');
6
+ const recursiveReadDir = require("recursive-readdir");
7
+
8
+ const args = getopts(process.argv.slice(2), {
9
+ alias: {
10
+ o: 'output',
11
+ d: 'directory',
12
+ f: 'file',
13
+ t: 'type'
14
+ },
15
+ default: {
16
+ t: 'lit-html'
17
+ }
18
+ });
19
+
20
+ const onError = (place) => {
21
+ console.log(`A problem occoured when reading ${place}. Please recheck passed parameters.`);
22
+ };
23
+
24
+ const isHandlebars = (fileName) => fileName.indexOf('.hbs') !== -1;
25
+
26
+ const processFile = async (file, outputDir) => {
27
+ const litCode = await hbs2lit(file);
28
+ const absoluteOutputDir = composeAbsoluteOutputDir(file, outputDir);
29
+ const componentNameMatcher = /(\w+)(\.hbs)/gim;
30
+ const componentName = componentNameMatcher.exec(file)[1];
31
+
32
+ return writeRenderers(absoluteOutputDir, componentName, litRenderer.generateTemplate(componentName, litCode));
33
+ };
34
+
35
+ const composeAbsoluteOutputDir = (file, outputDir) => {
36
+ // (1) Extract the dir structure from the source file path - "src/lvl1/lvl2/MyCompBadge.hbs"
37
+ // - remove the filename - "src/lvl1/lvl2"
38
+ // - remove the leading dir - "lvl1/lvl2"
39
+ const fileDir = file.split(path.sep).slice(1, -1).join(path.sep);
40
+
41
+ // (2) Compose full output dir - "dist/generated/templates/lvl1/lvl2"
42
+ return `${outputDir}${path.sep}${fileDir}`;
43
+ };
44
+
45
+ const wrapDirectory = (directory, outputDir) => {
46
+ directory = path.normalize(directory);
47
+ outputDir = path.normalize(outputDir);
48
+
49
+ return new Promise((resolve, reject) => {
50
+ recursiveReadDir(directory, (err, files) => {
51
+
52
+ if (err) {
53
+ onError('directory');
54
+ reject();
55
+ }
56
+
57
+ const promises = files.map(fileName => {
58
+ if (isHandlebars(fileName)) {
59
+ return processFile(fileName, outputDir);
60
+ }
61
+ }).filter(x => !!x);
62
+
63
+ resolve(Promise.all(promises));
64
+ });
65
+ });
66
+ };
67
+
68
+ const writeRenderers = async (outputDir, controlName, fileContent) => {
69
+ try {
70
+
71
+ await fs.mkdir(outputDir, { recursive: true });
72
+
73
+ const compiledFilePath = `${outputDir}${path.sep}${controlName}Template.lit.js`;
74
+
75
+ // strip DOS line endings because the break the source maps
76
+ let fileContentUnix = fileContent.replace(/\r\n/g, "\n");
77
+ fileContentUnix = fileContentUnix.replace(/\r/g, "\n");
78
+
79
+ // Only write to the file system actual changes - each updated file, no matter if the same or not, triggers an expensive operation for rollup
80
+ // Note: .hbs files that include a changed .hbs file will also be recompiled as their content will be updated too
81
+
82
+ let existingFileContent = "";
83
+ try {
84
+ existingFileContent = await fs.readFile(compiledFilePath);
85
+ } catch (e) {}
86
+
87
+ if (existingFileContent !== fileContentUnix) {
88
+ return fs.writeFile(compiledFilePath, fileContentUnix);
89
+ }
90
+
91
+ } catch (e) {
92
+ console.log(e);
93
+ }
94
+ };
95
+
96
+ if (!args['d'] || !args['o']) {
97
+ console.log('Please provide an input and output directory (-d and -o)');
98
+ } else {
99
+ wrapDirectory(args['d'], args['o']).then(() => {
100
+ console.log("Templates generated");
101
+ });
102
+ }