@winner-fed/winner-deploy 3.3.6 → 3.4.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@winner-fed/winner-deploy",
3
- "version": "3.3.6",
3
+ "version": "3.4.2",
4
4
  "description": "Generate a publication deployed by the See platform",
5
5
  "keywords": [
6
6
  "winner",
package/src/see.js CHANGED
@@ -3,10 +3,73 @@ const path = require('path')
3
3
  const tpl = require('lodash.template')
4
4
  const xml = require('xml')
5
5
  const babel = require('@babel/core')
6
- const { unEscapeForXML } = require('./utils')
6
+ const { unEscapeForXML, formatComplexSelectXml, findTemplateVariableItem } = require('./utils')
7
7
 
8
+ // 支持的类型
9
+ // input - 普通文本输入框。
10
+ // select - 单选下拉框。
11
+ // switch - 开关组件。
12
+ // smallfile - 文件上传控件,仅支持小于 2M 的文件,文件内容以 base64 转码后的字符串存储。
13
+ // switchForm - 开关表单,通过控件 switch 控制下面表单的显示
14
+ // complexSelect - 复杂单选框,通过控件 select 控制下面显示哪个表单
15
+ // table - 表格控件,table 类型的模板配置目前只支持 Array<Object> 类型配置项,比如 APPS。
16
+ // division - 分栏折叠控件。 division分栏组件将界面上众多的组件分栏并给出每一栏目的标头该控件仅用于界面展示控制由于其不具有name属性故其子元素取值方式保持原有不变,举例division包含子控件b其取值变量为b),分区组件,用于界面控制,作为分栏使用,对参数使用没意义
17
+ const typeSupported = ['input', 'select', 'switch', 'smallfile', 'table', 'mselect', 'switchForm', 'complexSelect', 'division']
8
18
  const resolveFile = (file) => path.resolve(__dirname, `../templates/see/${file}`)
9
19
 
20
+ // 生成 filed 属性解析
21
+ const generateField = (attr) => {
22
+ const type = attr.type
23
+ if (!typeSupported.includes(type)) return ''
24
+ const {
25
+ label,
26
+ name,
27
+ tooltip = '',
28
+ // 配置界面开启是否默认显示该参数,用于隐藏不常修改的配置参数。可选值:true、false
29
+ visible = 'true',
30
+ required = 'false',
31
+ // 冻结表行,表格配置默认值,配置不允许用户增加或者修改行
32
+ fixed = 'false',
33
+ // 控件是否默认折叠,操控控件默认展示时是否占用最小空间,选填字段;可选值:true、false。
34
+ fold = 'false',
35
+ // 选择项,select、mselect、linkageform控件必填。示例:“true:是;false:否“
36
+ options = ''
37
+ } = attr
38
+ let defaultValue = attr.default
39
+
40
+ if (type === 'table') {
41
+ const keys = attr.keys || []
42
+ const columns = attr.columns || keys
43
+
44
+ defaultValue = columns.map(attr => generateField(attr)).join('\n ')
45
+ } else if (type === 'switchForm' || type === 'division') {
46
+ const children = attr.children
47
+ defaultValue = children.map(attr => generateField(attr)).join('\n ')
48
+ } else if (type === 'complexSelect') {
49
+ return formatComplexSelectXml(attr)
50
+ } else {
51
+ defaultValue = [defaultValue]
52
+ }
53
+
54
+ const xmlVariable = xml({
55
+ field: [{
56
+ _attr: {
57
+ type,
58
+ label,
59
+ name,
60
+ tooltip,
61
+ visible,
62
+ required,
63
+ fixed,
64
+ fold,
65
+ options
66
+ }
67
+ }, defaultValue].concat(defaultValue)
68
+ })
69
+
70
+ return unEscapeForXML(`${xmlVariable}`)
71
+ }
72
+
10
73
  exports.genDeployXml = function genDeployXml ({
11
74
  system,
12
75
  type,
@@ -17,7 +80,7 @@ exports.genDeployXml = function genDeployXml ({
17
80
  variables = [],
18
81
  description
19
82
  }) {
20
- const template = fs.readFileSync(resolveFile(`python/deploy.xml`))
83
+ const template = fs.readFileSync(resolveFile(`python/deploy.xml`)).toString()
21
84
 
22
85
  return tpl(template, {
23
86
  interpolate: /{{([\s\S]+?)}}/g
@@ -29,21 +92,16 @@ exports.genDeployXml = function genDeployXml ({
29
92
  version,
30
93
  configFileName,
31
94
  variables: (() => {
32
- return variables
33
- .map((attr) => {
34
- const defaultValue = attr.default
35
- delete attr.default
36
- const xmlVariable = xml({ field: [{ _attr: attr }, defaultValue] })
37
- return unEscapeForXML(`${xmlVariable}`)
38
- })
39
- .join('\n ')
95
+ return variables.map((attr) => {
96
+ return generateField(attr)
97
+ }).join('\n ')
40
98
  })(),
41
99
  description
42
100
  })
43
101
  }
44
102
 
45
103
  exports.genScripts = function genScripts (name, options) {
46
- const template = fs.readFileSync(resolveFile(`python/scripts/${name}.py`))
104
+ const template = fs.readFileSync(resolveFile(`python/scripts/${name}.py`)).toString()
47
105
  return tpl(template, {
48
106
  interpolate: /{{([\s\S]+?)}}/g
49
107
  })(options)
@@ -86,18 +144,96 @@ exports.genTemplate = function genTemplate (file, variables = []) {
86
144
  visitor: {
87
145
  Literal (path) {
88
146
  if (path.parentPath.isProperty()) {
89
- const _match = variables.find((variable) => path.parentPath.get('key').node.name === variable.name)
147
+ const currentNode = path.parentPath.get('key').node
148
+ let currentNodeName = currentNode.name
149
+ // 当 key 为字符串的时候,会转换失败
150
+ // 解决方法,重置为标识符字面量节点
151
+ // 如
152
+ // window.LOCAL_CONFIG_whaleTest = {
153
+ // "API_HOME": 'https://api.github.com/',
154
+ // 'API_USER': "https://www.baidu.com",
155
+ // API_UPLOAD: 'https://api.github.com/upload',
156
+ // IS_OPEN_VCONSOLE: false
157
+ // }
158
+ // 转换之后的效果是有问题的,API_HOME 没有转换
159
+ // window.LOCAL_CONFIG_whaleTest = {
160
+ // "API_HOME": 'https://api.github.com/',
161
+ // 'API_USER': "https://www.baidu.com",
162
+ // API_UPLOAD: 'https://api.github.com/upload',
163
+ // IS_OPEN_VCONSOLE: ${IS_OPEN_VCONSOLE}
164
+ // }
165
+ if (currentNode.type === 'StringLiteral') {
166
+ currentNode.type = 'Identifier'
167
+ currentNode.name = currentNode.value
168
+ currentNodeName = currentNode.value
169
+ }
170
+
171
+ const _match = findTemplateVariableItem(variables, (variable) => currentNodeName === variable.name)
90
172
  if (_match) {
173
+ if (_match.fullPath.includes('complexSelect')) {
174
+ _match.name = _match.fullName
175
+ }
176
+
177
+ // 是否为字符串字面量节点
91
178
  if (path.isStringLiteral()) {
92
- path.node.value = `$\{${_match.name}\}`
179
+ path.node.value = `$!\{${_match.name}\}`
93
180
  return
94
181
  }
182
+ // 是否为数字字面量节点
183
+ if (path.isNumericLiteral()) {
184
+ path.node.value = `$!\{${_match.name}\}`
185
+ return
186
+ }
187
+ // 是否为数布尔字面量节点
95
188
  if (path.isBooleanLiteral()) {
96
189
  path.node.type = 'StringLiteral'
190
+
191
+ if (_match.fullPath.includes('complexSelect')) {
192
+ path.node.value = `$!\{${_match.fullName}\}`
193
+ return
194
+ }
195
+
97
196
  path.node.value = `[NO_QUOTATION_MARKS]$\{${_match.name}\}[NO_QUOTATION_MARKS]`
98
197
  }
99
198
  }
100
199
  }
200
+ },
201
+
202
+ ArrayExpression (path) {
203
+ if (path.parentPath.isProperty()) {
204
+ const _match = findTemplateVariableItem(variables, variable => path.parentPath.get('key').node.name === variable.name)
205
+
206
+ if (_match) {
207
+ if (_match.fullPath.includes('complexSelect')) {
208
+ _match.name = _match.fullName
209
+ }
210
+
211
+ let value = ''
212
+
213
+ // 目前只支持 [{}, {}] 这种形式的 table
214
+ // keys 表示 Object 元素的 key,value 目前只支持 String 类型
215
+ const keys = _match.keys || []
216
+ // 原来这个字段是 keys,不太好理解,换成 columns,需要处理兼容问题
217
+ const columns = _match.columns || keys
218
+ const options = _match.options
219
+
220
+ if (options) {
221
+ value = `[NO_QUOTATION_MARKS][#foreach($row in $${_match.name}) '$row' , #end][NO_QUOTATION_MARKS]`
222
+ path.node.type = 'StringLiteral'
223
+ path.node.value = value
224
+ return
225
+ }
226
+
227
+ if (columns && columns.length) {
228
+ columns.forEach(item => {
229
+ value += `${item.name}: '$\!{row.${item.name}}',` // 双引号会被转义
230
+ })
231
+ }
232
+
233
+ path.node.type = 'StringLiteral'
234
+ path.node.value = `[NO_QUOTATION_MARKS][#foreach($row in $${_match.name}) {${value}}, #end][NO_QUOTATION_MARKS]`
235
+ }
236
+ }
101
237
  }
102
238
  }
103
239
  }
@@ -123,7 +259,7 @@ exports.genDockerDeployXml = function genDockerDeployXml ({
123
259
  web_app_web_service_name,
124
260
  dockerImage
125
261
  }) {
126
- const template = fs.readFileSync(resolveFile(`docker/deploy.xml`))
262
+ const template = fs.readFileSync(resolveFile(`docker/deploy.xml`)).toString()
127
263
 
128
264
  return tpl(template, {
129
265
  interpolate: /{{([\s\S]+?)}}/g
@@ -135,14 +271,9 @@ exports.genDockerDeployXml = function genDockerDeployXml ({
135
271
  version,
136
272
  configFileName,
137
273
  variables: (() => {
138
- return variables
139
- .map((attr) => {
140
- const defaultValue = attr.default
141
- delete attr.default
142
- const xmlVariable = xml({ field: [{ _attr: attr }, defaultValue] })
143
- return unEscapeForXML(`${xmlVariable}`)
144
- })
145
- .join('\n ')
274
+ return variables.map((attr) => {
275
+ return generateField(attr)
276
+ }).join('\n ')
146
277
  })(),
147
278
  description,
148
279
  web_app_web_service_name,
@@ -151,7 +282,7 @@ exports.genDockerDeployXml = function genDockerDeployXml ({
151
282
  }
152
283
 
153
284
  exports.genK8sDeployRes = function genK8sDeployResYaml (options) {
154
- const template = fs.readFileSync(resolveFile(`docker/k8s-deploy-res.yaml`));
285
+ const template = fs.readFileSync(resolveFile(`docker/k8s-deploy-res.yaml`)).toString()
155
286
 
156
287
  return tpl(template, {
157
288
  interpolate: /{{([\s\S]+?)}}/g
@@ -159,7 +290,7 @@ exports.genK8sDeployRes = function genK8sDeployResYaml (options) {
159
290
  }
160
291
 
161
292
  exports.genDockerfile = function genDockerfile (options) {
162
- const template = fs.readFileSync(resolveFile(`docker/Dockerfile`))
293
+ const template = fs.readFileSync(resolveFile(`docker/Dockerfile`)).toString()
163
294
 
164
295
  return tpl(template)(options)
165
296
  }
@@ -175,7 +306,7 @@ exports.genDeployXmlBySh = function genDeployXmlBySh ({
175
306
  variables = [],
176
307
  description
177
308
  }) {
178
- const template = fs.readFileSync(resolveFile(`bash/deploy.xml`));
309
+ const template = fs.readFileSync(resolveFile(`bash/deploy.xml`)).toString()
179
310
 
180
311
  return tpl(template, {
181
312
  interpolate: /{{([\s\S]+?)}}/g
@@ -187,21 +318,16 @@ exports.genDeployXmlBySh = function genDeployXmlBySh ({
187
318
  version,
188
319
  configFileName,
189
320
  variables: (() => {
190
- return variables
191
- .map((attr) => {
192
- const defaultValue = attr.default
193
- delete attr.default
194
- const xmlVariable = xml({ field: [{ _attr: attr }, defaultValue] })
195
- return unEscapeForXML(`${xmlVariable}`)
196
- })
197
- .join('\n ')
321
+ return variables.map((attr) => {
322
+ return generateField(attr)
323
+ }).join('\n ')
198
324
  })(),
199
325
  description
200
326
  })
201
327
  }
202
328
 
203
329
  exports.genScriptsBySh = function genScriptsBySh (name, options) {
204
- const template = fs.readFileSync(resolveFile(`bash/scripts/${name}.sh`))
330
+ const template = fs.readFileSync(resolveFile(`bash/scripts/${name}.sh`)).toString()
205
331
  return tpl(template, {
206
332
  interpolate: /{{([\s\S]+?)}}/g
207
333
  })(options)
package/src/utils.js CHANGED
@@ -3,6 +3,7 @@ const chalk = require('chalk')
3
3
  const ora = require('ora')
4
4
  const fs = require('fs-extra')
5
5
  const archiver = require('archiver')
6
+ const xml = require('xml')
6
7
 
7
8
  const context = process.cwd()
8
9
 
@@ -35,7 +36,7 @@ exports.unEscapeForXML = function unEscapeForXML (string) {
35
36
  */
36
37
  // 1,项目包名为 dist
37
38
  exports.isProProject = function isProProject (subPath) {
38
- const newSubPath = subPath || '';
39
+ const newSubPath = subPath || ''
39
40
  const sysconfigJS = path.resolve(context, 'dist', newSubPath, 'sysconfig.js')
40
41
  return fs.pathExistsSync(sysconfigJS)
41
42
  }
@@ -80,3 +81,102 @@ exports.genZipPackage = function genZipPackage (zipPackageName, tmp, cb) {
80
81
  archive.directory(tmp, false)
81
82
  archive.finalize()
82
83
  }
84
+
85
+ /**
86
+ * 生成通用的 field 字段
87
+ * @param attr 属性
88
+ * @param children 子集
89
+ * @param t 字段类型
90
+ * @return {*}
91
+ */
92
+ exports.formatCommonField = function formatCommonField (attr, children = [], t) {
93
+ const {
94
+ type,
95
+ label,
96
+ name,
97
+ tooltip = '',
98
+ visible = 'true',
99
+ required = 'false',
100
+ fixed = 'false',
101
+ fold = 'false',
102
+ options = ''
103
+ } = attr
104
+ const defaultValue = attr.default
105
+
106
+ if (t === 'form') {
107
+ const xmlVariable = xml({
108
+ form: [{
109
+ _attr: {
110
+ name,
111
+ tooltip,
112
+ visible,
113
+ required,
114
+ fixed,
115
+ fold,
116
+ options
117
+ }
118
+ }, children]
119
+ })
120
+ return exports.unEscapeForXML(xmlVariable)
121
+ }
122
+
123
+ const xmlVariable = xml({
124
+ field: [{
125
+ _attr: {
126
+ type,
127
+ label,
128
+ name,
129
+ tooltip,
130
+ visible,
131
+ required,
132
+ fixed,
133
+ fold,
134
+ options
135
+ }
136
+ }, defaultValue ? defaultValue : children]
137
+ })
138
+
139
+ return exports.unEscapeForXML(xmlVariable)
140
+ }
141
+
142
+ /**
143
+ * 复杂选择器解析
144
+ * @param attr
145
+ * @return {*}
146
+ */
147
+ exports.formatComplexSelectXml = function formatComplexSelectXml (attr) {
148
+ const children = attr.children
149
+ const formsXmlVariable = children.map((form) => {
150
+ const children = form.children
151
+ const formItemsValue = children.map(formItem => exports.formatCommonField(formItem)).join('')
152
+ return exports.formatCommonField(form, formItemsValue, 'form')
153
+ }).join('')
154
+
155
+ return exports.formatCommonField(attr, formsXmlVariable)
156
+ }
157
+
158
+ /**
159
+ * 解析模板变量
160
+ *
161
+ * @param {Array} variables 模板变量数组
162
+ * @param {Function} cb 回调函数
163
+ * @param prefix
164
+ * @param path
165
+ * @returns 查询对象,若没有找到则返回 null
166
+ */
167
+ exports.findTemplateVariableItem = function findTemplateVariableItem (variables, cb, prefix = '', path = '') {
168
+ for (let i = 0; i < variables.length; i++) {
169
+ let item = variables[i]
170
+ item.fullName = `${prefix === '' ? '' : prefix + '.'}${item.name}`
171
+ item.fullPath = `${path === '' ? '' : path + '-'}${item.type}`
172
+
173
+ if (cb(item)) {
174
+ return item
175
+ } else if (item.children && item.children.length > 0) {
176
+ let result = exports.findTemplateVariableItem(item.children, cb, item.fullName, item.fullPath)
177
+ if (result) return result
178
+ }
179
+ }
180
+
181
+ return null
182
+ }
@@ -34,7 +34,6 @@
34
34
  <% } else { %>
35
35
  <field name="webAppName" label="前端应用名" type="hidden" tooltip="前端部署完成后访问的站点名称;需要与IAR中的location前端目录名称保持一致">{{ name }}</field>
36
36
  <% } %>
37
-
38
37
  {{ variables }}
39
38
  </variables>
40
39
  <config path="./${webAppName}/{{ configFileName }}.js" encoding="UTF-8"/>
@@ -36,8 +36,8 @@
36
36
  </field>
37
37
  <field name="liveness_probe_enable" label="存活检查探针开启" type="switch" options="true:ON;false:OFF">false</field>
38
38
  </field>
39
-
40
39
  {{ variables }}
40
+
41
41
  </variables>
42
42
  <config path="./{{ name }}/{{ configFileName }}.js" encoding="UTF-8"/>
43
43
  <config reference="" encoding="utf-8" path="./k8s-deploy-res.yaml" />
@@ -34,8 +34,8 @@
34
34
  <% } else { %>
35
35
  <field name="webAppName" label="前端应用名" type="hidden" tooltip="前端部署完成后访问的站点名称;需要与IAR中的location前端目录名称保持一致">{{ name }}</field>
36
36
  <% } %>
37
-
38
37
  {{ variables }}
38
+
39
39
  </variables>
40
40
  <config path="./${webAppName}/{{ configFileName }}.js" encoding="UTF-8"/>
41
41
  <config path="./scripts/{{ name }}/install.py" encoding="UTF-8"/>
Binary file