@aiot-toolkit/shared-utils 1.0.17-binary-beta.1 → 1.0.17-binary-beta.2

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.
@@ -1,2 +1,284 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.extractFunctions=extractFunctions,exports.createBin=createBin;const fs=require("fs"),vdom_pb=require("./vdom_pb"),Path=require("path");class AIOT{constructor(t){this.funcArr=[],this.chld=[],this.idCount=-1,this.expCell=new vdom_pb.ExportCell,this.aiotf=t}init(t){this.expCell.setTagname(t)}parseObj(t){let e=new vdom_pb.StyleValueArr;for(let r in t){let s=new vdom_pb.StyleKey,i=new vdom_pb.OneStyleValue;console.log(`---parseobj-- key:${r}`),s.setValue(r),i.setKey(s);let n=Object.prototype.toString.call(t[r]);if("[object Number]"===n)i.setInt32vl(t[r]);else if("[object String]"===n)i.setStringvl(t[r]);else if("[object Function]"===n)null==this.aiotf?(this.funcArr.push(t[r]),i.setFuncid(this.funcArr.length-1)):(this.aiotf.funcArr.push(t[r]),i.setFuncid(this.aiotf.funcArr.length-1));else if("[object Object]"===n){let e=new vdom_pb.StyleValueArr;e=this.parseObj(t[r]),i.setStylevlarr(e)}else if("[object Array]"===n){let e=new vdom_pb.StringArr;e.setStringvlarrList(t[r]),i.setStringarr(e)}e.addVlarr(i)}return e}__ce__(t,e,r){let s=new vdom_pb.TemplateOne,i=new vdom_pb.TemplateCE;if(i.setTag(t),Array.isArray(r)&&r.length>0){let t=[];for(let e in r)t.push(r[e].id_);s.setChldsidList(t)}s.setId(++this.idCount);let n=this.parseObj(e.__opts__);return i.setOptsarrList(n.getVlarrList()),s.setCe(i),this.expCell.addTemplatearr(s),{id_:this.idCount,aiot_:this}}__ci__(t,e){let r=new vdom_pb.TemplateOne,s=new vdom_pb.TemplateCI;r.setId(++this.idCount),"[object Function]"===Object.prototype.toString.call(t.__opts__.shown)&&(null==this.aiotf?(this.funcArr.push(t.__opts__.shown),s.setShownid(this.funcArr.length-1)):(this.aiotf.funcArr.push(t.__opts__.shown),s.setShownid(this.aiotf.funcArr.length-1)));let i=new AIOT(this),n=e(i),l=Object.prototype.toString.call(n);if("[object Object]"===l)s.setTemplaterenderList(n.aiot_.expCell.getTemplatearrList());else if("[object Array]"===l)for(let t in n){s.setTemplaterenderList(n[t].aiot_.expCell.getTemplatearrList());break}return this.funcArr=i.aiotf.funcArr,r.setCi(s),this.expCell.addTemplatearr(r),{id_:this.idCount,aiot_:this}}__cf__(t,e){let r=new vdom_pb.TemplateOne,s=new vdom_pb.TemplateCF;r.setId(++this.idCount);for(let e in t.__opts__){if("exp"===e&&"[object Function]"===Object.prototype.toString.call(t.__opts__[e])&&(null==this.aiotf?(this.funcArr.push(t.__opts__[e]),s.setExpid(this.funcArr.length-1)):(this.aiotf.funcArr.push(t.__opts__[e]),s.setExpid(this.aiotf.funcArr.length-1))),"key"===e&&"[object String]"===Object.prototype.toString.call(t.__opts__[e])){let r=new vdom_pb.OneStyleValue;r.setStringvl(t.__opts__[e]),s.setKey(r)}if("value"===e&&"[object String]"===Object.prototype.toString.call(t.__opts__[e])){let r=new vdom_pb.OneStyleValue;r.setStringvl(t.__opts__[e]),s.setValue(r)}}let i=new AIOT(this),n=e(undefined,undefined,i),l=Object.prototype.toString.call(n);if("[object Object]"===l)s.setTemplaterenderList(n.aiot_.expCell.getTemplatearrList());else if("[object Array]"===l)for(let t in n){s.setTemplaterenderList(n[t].aiot_.expCell.getTemplatearrList());break}return this.funcArr=i.aiotf.funcArr,r.setCf(s),this.expCell.addTemplatearr(r),{id_:this.idCount,aiot_:this}}__cc__(t,e){let r=new vdom_pb.TemplateOne,s=new vdom_pb.TemplateCC;s.setTag(t),r.setId(++this.idCount);let i=this.parseObj(e.__opts__);return s.setOptsarrList(i.getVlarrList()),s.setExportarrid(-1),r.setCc(s),this.expCell.addTemplatearr(r),{id_:this.idCount,aiot_:this}}}function translateStyle(t,e){const r=e.expCell;return t.forEach((t=>{const e=t[0][0][0],s=t[0][0][1],i=t[1],n=r.addCssarr();n.setId(e),n.setName(s),Object.keys(i).forEach((t=>{const e=i[t],r=n.addStylearr(),s=new vdom_pb.StyleKey;switch(s.setValue(t),r.setKey(s),typeof e){case"string":r.setStringvl(e);break;case"number":Number.isInteger(e)?r.setInt32vl(e):r.setDoublevl(e)}}))})),r}function translateTemplate(template,aiot){const vm={};template&&eval(`(${template})`)(vm)}function createItem(t,e,r){const s=Path.parse(t).base;let i=new AIOT;return i.init(s),translateTemplate(r,i),translateStyle(e,i),i}function extractFunctions(t){let e=new AIOT;return e.init("temp"),translateTemplate(t,e),e.funcArr}function createBin(t,e,r){const s=e.map(((s,i)=>createItem(t,JSON.parse(e[i]),r[i])));let i=new vdom_pb.ExportArr;i.clearValuearrList(),s.forEach((t=>{i.addValuearr(t.expCell)}));let n=i.serializeBinary();fs.writeFileSync(t,n)}
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.extractFunctions = extractFunctions;
7
+ exports.createBin = createBin;
8
+ const fs = require('fs');
9
+ const vdom_pb = require('./vdom_pb');
10
+ const Path = require('path');
11
+ /* eslint-disable */
12
+
13
+ class AIOT {
14
+ constructor(aiotf) {
15
+ this.funcArr = [];
16
+ this.chld = [];
17
+ this.idCount = -1;
18
+ this.expCell = new vdom_pb.ExportCell();
19
+ this.aiotf = aiotf;
20
+ }
21
+ init(expName) {
22
+ this.expCell.setTagname(expName);
23
+ }
24
+ parseObj(opts) {
25
+ let styVaA = new vdom_pb.StyleValueArr();
26
+ for (let key in opts) {
27
+ let sKey = new vdom_pb.StyleKey();
28
+ let onesvl = new vdom_pb.OneStyleValue();
29
+ console.log(`---parseobj-- key:${key}`);
30
+ sKey.setValue(key);
31
+ onesvl.setKey(sKey);
32
+ let typ = Object.prototype.toString.call(opts[key]);
33
+ if (typ === '[object Number]') {
34
+ // TODO:double
35
+ onesvl.setInt32vl(opts[key]);
36
+ } else if (typ === '[object String]') {
37
+ // console.log(`String ${opts[key]}`)
38
+ onesvl.setStringvl(opts[key]);
39
+ } else if (typ === '[object Function]') {
40
+ if (this.aiotf == undefined) {
41
+ this.funcArr.push(opts[key]);
42
+ onesvl.setFuncid(this.funcArr.length - 1);
43
+ } else {
44
+ this.aiotf.funcArr.push(opts[key]);
45
+ onesvl.setFuncid(this.aiotf.funcArr.length - 1);
46
+ }
47
+ } else if (typ === '[object Object]') {
48
+ // console.log(`object ${opts[key]}`)
49
+ let styVaA_ = new vdom_pb.StyleValueArr();
50
+ styVaA_ = this.parseObj(opts[key]);
51
+ onesvl.setStylevlarr(styVaA_);
52
+ } else if (typ === '[object Array]') {
53
+ //TODO:default string arr
54
+ // console.log(`Array ${opts[key]}`)
55
+ let strarr = new vdom_pb.StringArr();
56
+ strarr.setStringvlarrList(opts[key]);
57
+ onesvl.setStringarr(strarr);
58
+ }
59
+ styVaA.addVlarr(onesvl);
60
+ }
61
+ return styVaA;
62
+ }
63
+ __ce__(tagName, opts, childsArr) {
64
+ let tempOne = new vdom_pb.TemplateOne();
65
+ let ce = new vdom_pb.TemplateCE();
66
+ ce.setTag(tagName);
67
+ if (Array.isArray(childsArr) && childsArr.length > 0) {
68
+ let arr = [];
69
+ for (let key in childsArr) {
70
+ arr.push(childsArr[key].id_);
71
+ }
72
+ tempOne.setChldsidList(arr);
73
+ }
74
+ tempOne.setId(++this.idCount);
75
+ let styVaA_ = this.parseObj(opts.__opts__);
76
+ ce.setOptsarrList(styVaA_.getVlarrList());
77
+ tempOne.setCe(ce);
78
+ this.expCell.addTemplatearr(tempOne);
79
+ return {
80
+ id_: this.idCount,
81
+ aiot_: this
82
+ };
83
+ }
84
+ __ci__(opts, renderFunc) {
85
+ let tempOne = new vdom_pb.TemplateOne();
86
+ let ci = new vdom_pb.TemplateCI();
87
+ tempOne.setId(++this.idCount);
88
+ if (Object.prototype.toString.call(opts.__opts__['shown']) === '[object Function]') {
89
+ if (this.aiotf == undefined) {
90
+ this.funcArr.push(opts.__opts__['shown']);
91
+ ci.setShownid(this.funcArr.length - 1);
92
+ } else {
93
+ this.aiotf.funcArr.push(opts.__opts__['shown']);
94
+ ci.setShownid(this.aiotf.funcArr.length - 1);
95
+ }
96
+ }
97
+
98
+ // function(aiot) {
99
+
100
+ let aiotif = new AIOT(this);
101
+ aiotif.funcArr = this.funcArr;
102
+ let renderArr = renderFunc(aiotif);
103
+ let typ = Object.prototype.toString.call(renderArr);
104
+ if (typ === '[object Object]') {
105
+ ci.setTemplaterenderList(renderArr.aiot_.expCell.getTemplatearrList());
106
+ } else if (typ === '[object Array]') {
107
+ for (let key in renderArr) {
108
+ ci.setTemplaterenderList(renderArr[key].aiot_.expCell.getTemplatearrList());
109
+ break;
110
+ }
111
+ }
112
+ this.funcArr = aiotif.aiotf.funcArr;
113
+ tempOne.setCi(ci);
114
+ this.expCell.addTemplatearr(tempOne);
115
+ return {
116
+ id_: this.idCount,
117
+ aiot_: this
118
+ };
119
+ }
120
+ __cf__(opts, renderFunc) {
121
+ let tempOne = new vdom_pb.TemplateOne();
122
+ let cf = new vdom_pb.TemplateCF();
123
+ tempOne.setId(++this.idCount);
124
+ for (let key in opts.__opts__) {
125
+ if (key === 'exp' && Object.prototype.toString.call(opts.__opts__[key]) === '[object Function]') {
126
+ if (this.aiotf == undefined) {
127
+ this.funcArr.push(opts.__opts__[key]);
128
+ cf.setExpid(this.funcArr.length - 1);
129
+ } else {
130
+ this.aiotf.funcArr.push(opts.__opts__[key]);
131
+ cf.setExpid(this.aiotf.funcArr.length - 1);
132
+ }
133
+ }
134
+ if (key === 'key' && Object.prototype.toString.call(opts.__opts__[key]) === '[object String]') {
135
+ let vl = new vdom_pb.OneStyleValue();
136
+ vl.setStringvl(opts.__opts__[key]);
137
+ cf.setKey(vl);
138
+ }
139
+ if (key === 'value' && Object.prototype.toString.call(opts.__opts__[key]) === '[object String]') {
140
+ let vl = new vdom_pb.OneStyleValue();
141
+ vl.setStringvl(opts.__opts__[key]);
142
+ cf.setValue(vl);
143
+ }
144
+ }
145
+
146
+ //function($idx, $item, aiot) {
147
+ // function($idx, $item)
148
+
149
+ let aiotfor = new AIOT(this);
150
+ aiotfor.funcArr = this.funcArr;
151
+ let key_, value_;
152
+ let renderArr = renderFunc(key_, value_, aiotfor);
153
+ let typ = Object.prototype.toString.call(renderArr);
154
+ if (typ === '[object Object]') {
155
+ cf.setTemplaterenderList(renderArr.aiot_.expCell.getTemplatearrList());
156
+ } else if (typ === '[object Array]') {
157
+ for (let key in renderArr) {
158
+ cf.setTemplaterenderList(renderArr[key].aiot_.expCell.getTemplatearrList());
159
+ break;
160
+ }
161
+ }
162
+ this.funcArr = aiotfor.aiotf.funcArr;
163
+ tempOne.setCf(cf);
164
+ this.expCell.addTemplatearr(tempOne);
165
+ return {
166
+ id_: this.idCount,
167
+ aiot_: this
168
+ };
169
+ }
170
+ __cc__(tagName, opts) {
171
+ let tempOne = new vdom_pb.TemplateOne();
172
+ let cc = new vdom_pb.TemplateCC();
173
+ cc.setTag(tagName);
174
+ tempOne.setId(++this.idCount);
175
+ let styVaA_ = this.parseObj(opts.__opts__);
176
+ cc.setOptsarrList(styVaA_.getVlarrList());
177
+
178
+ // 1.解决方法先生成子组件,
179
+ // 2.删除这项,建树使用tagName查找(遍历一遍数组)
180
+ cc.setExportarrid(-1);
181
+ tempOne.setCc(cc);
182
+ this.expCell.addTemplatearr(tempOne);
183
+ return {
184
+ id_: this.idCount,
185
+ aiot_: this
186
+ };
187
+ }
188
+ }
189
+
190
+ /**
191
+ *
192
+ * @param {[[[number, string]], {[key:string]:string|number}][]} style
193
+ */
194
+ function translateStyle(style, aiot) {
195
+ const exportCell = aiot.expCell;
196
+ style.forEach(item => {
197
+ const id = item[0][0][0];
198
+ const name = item[0][0][1];
199
+ const styleValue = item[1];
200
+ const cssCell = exportCell.addCssarr();
201
+ cssCell.setId(id);
202
+ cssCell.setName(name);
203
+ Object.keys(styleValue).forEach(key => {
204
+ const value = styleValue[key];
205
+ const styleVl = cssCell.addStylearr();
206
+
207
+ // 设置css属性名称
208
+ const pk = new vdom_pb.StyleKey();
209
+ pk.setValue(key);
210
+ styleVl.setKey(pk);
211
+
212
+ // 设置css值
213
+ switch (typeof value) {
214
+ case 'string':
215
+ styleVl.setStringvl(value);
216
+ break;
217
+ case 'number':
218
+ if (Number.isInteger(value)) {
219
+ styleVl.setInt32vl(value);
220
+ } else {
221
+ styleVl.setDoublevl(value);
222
+ }
223
+ break;
224
+ }
225
+ });
226
+ });
227
+ return exportCell;
228
+ }
229
+ function translateTemplate(template, aiot) {
230
+ const vm = {};
231
+ if (template) {
232
+ // eslint-disable-next-line no-eval
233
+ eval(`(${template})`)(vm);
234
+ }
235
+ }
236
+ function createItem(path, style, template) {
237
+ const name = Path.parse(path).base;
238
+ // 创建aiot对象
239
+ let aiot = new AIOT();
240
+ aiot.init(name);
241
+ // 写入template
242
+ translateTemplate(template, aiot);
243
+ // 写入style
244
+ translateStyle(style, aiot);
245
+ return aiot;
246
+ }
247
+
248
+ /**
249
+ *
250
+ * @param {*} template
251
+ */
252
+ function extractFunctions(template) {
253
+ // 创建aiot对象
254
+ let aiot = new AIOT();
255
+ aiot.init('temp');
256
+
257
+ // 写入template
258
+ translateTemplate(template, aiot);
259
+ return aiot.funcArr;
260
+ }
261
+
262
+ /**
263
+ * 创建bin文件
264
+ * @param {string} fileName
265
+ * @param {string[]} styleList
266
+ * @param {string[]} templateList
267
+ */
268
+ function createBin(fileName, styleList, templateList) {
269
+ const dataList = styleList.map((item, index) => {
270
+ return createItem(fileName, JSON.parse(styleList[index]), templateList[index]);
271
+ });
272
+
273
+ // 写入数据
274
+ let indexExport = new vdom_pb.ExportArr();
275
+ indexExport.clearValuearrList();
276
+ dataList.forEach(item => {
277
+ indexExport.addValuearr(item.expCell);
278
+ });
279
+
280
+ // 写入文件
281
+ let outbuff = indexExport.serializeBinary();
282
+ fs.writeFileSync(fileName, outbuff);
283
+ }
2
284
  //# sourceMappingURL=protobuf.js.map