@easyv/react-component-builder 0.16.3 → 0.17.1

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/config/paths.js CHANGED
@@ -4,16 +4,21 @@ const path = require('path');
4
4
  const __cwd = process.cwd();
5
5
  const __root = path.resolve(__dirname, '..');
6
6
  const appPackage = require(path.join(__cwd, 'package.json'));
7
+ const userHome = require('os').homedir();
7
8
 
8
9
  module.exports = {
9
- appPackage,
10
- appBuild: path.resolve(__cwd, 'build'),
11
- appSrc: path.resolve(__cwd, appPackage.easyv.src || 'src'),
12
- appComponents: path.resolve(__cwd, (appPackage.easyv || {}).components || 'src/components'),
13
- webpackConfigExtend: path.resolve(__cwd, 'webpack.extend.js'),
14
- public: path.resolve(__root, 'public'),
15
- ownLoaders: path.resolve(__root, 'loaders'),
16
- templates: path.resolve(__root, 'templates'),
17
- cliNodeModules: path.join(__root, 'node_modules'),
18
- __root
19
- };
10
+ appPackage,
11
+ appBuild: path.resolve(__cwd, 'build'),
12
+ appSrc: path.resolve(__cwd, appPackage.easyv.src || 'src'),
13
+ appComponents: path.resolve(
14
+ __cwd,
15
+ (appPackage.easyv || {}).components || 'src/components',
16
+ ),
17
+ webpackConfigExtend: path.resolve(__cwd, 'webpack.extend.js'),
18
+ public: path.resolve(__root, 'public'),
19
+ ownLoaders: path.resolve(__root, 'loaders'),
20
+ templates: path.resolve(__root, 'templates'),
21
+ cliNodeModules: path.join(__root, 'node_modules'),
22
+ customTemplatesRoot: path.join(userHome, 'easyv_custom_templates'),
23
+ __root,
24
+ };
@@ -1,78 +1,99 @@
1
1
  var inquirer = require('inquirer');
2
2
  var path = require('path');
3
- const fs = require('fs')
3
+ const fs = require('fs');
4
4
  const fse = require('fs-extra');
5
5
  const chalk = require('chalk');
6
6
  const { appComponents, appPackage, templates } = require('../config/paths');
7
7
 
8
8
  const defaultValues = {
9
- module_name: 'text',
10
- name: '文本',
11
- }
12
- const templateValues = [
13
- {
14
- name: 'React演示',
15
- value: 'basic'
16
- }, {
17
- name: 'Three Component',
18
- value: 'three-component'
19
- }
20
- ]
21
- module.exports = (cb) => {
22
- inquirer
23
- .prompt([
24
- {
25
- type: 'input',
26
- name: 'moduleName',
27
- message: "你的组件名称是什么?",
28
- validate: function (value) {
29
- return /^[a-zA-Z][\w-]+$/.test(value) ? true : '名称只能是 英文开头 包含 英文、数字、_- 的字符串'
30
- },
31
- default: defaultValues.module_name
32
- },
33
- {
34
- type: 'input',
35
- name: 'name',
36
- message: "中文别名是什么?",
37
- default: defaultValues.name
38
- },
39
- { type: 'list', name: 'template', message: '要添加哪种模版?', choices: templateValues }
40
- ])
41
- .then(answers => {
42
- let { moduleName, name, template } = answers;
43
- const base = {
44
- name,
45
- module_name: moduleName,
46
- version: '1.0.0',
47
- show: 1,
48
- }
9
+ module_name: 'text',
10
+ name: '文本',
11
+ };
12
+ let templateValues = [
13
+ {
14
+ name: 'React演示',
15
+ value: 'basic',
16
+ },
17
+ {
18
+ name: 'Three Component',
19
+ value: 'three-component',
20
+ },
21
+ ];
49
22
 
50
- const copyPath = path.join(appComponents, moduleName)
51
- // 检查重名
52
- if (fs.existsSync(copyPath)) {
53
- console.log(chalk.red(`组件名称已存在`))
54
- process.exit()
55
- }
23
+ module.exports = (cb, addCustom, generateCustom, add) => {
24
+ if (!add) {
25
+ const custom = addCustom();
26
+ templateValues = [...templateValues, ...custom];
27
+ }
28
+
29
+ inquirer
30
+ .prompt([
31
+ {
32
+ type: 'input',
33
+ name: 'moduleName',
34
+ message: '你的组件名称是什么?',
35
+ validate: function (value) {
36
+ return /^[a-zA-Z][\w-]+$/.test(value)
37
+ ? true
38
+ : '名称只能是 英文开头 包含 英文、数字、_- 的字符串';
39
+ },
40
+ default: defaultValues.module_name,
41
+ },
42
+ {
43
+ type: 'input',
44
+ name: 'name',
45
+ message: '中文别名是什么?',
46
+ default: defaultValues.name,
47
+ },
48
+ {
49
+ type: 'list',
50
+ name: 'template',
51
+ message: '要添加哪种模版?',
52
+ choices: templateValues,
53
+ },
54
+ ])
55
+ .then((answers) => {
56
+ let { moduleName, name, template } = answers;
57
+ const base = {
58
+ name: !add ? name : '##name##',
59
+ module_name: !add ? moduleName : '##module_name##',
60
+ version: '1.0.0',
61
+ show: 1,
62
+ };
56
63
 
57
- // 重写配置
58
- const templatePath = path.join(templates, template);
59
- const configPath = 'config/main.json';
64
+ const copyPath = path.join(appComponents, moduleName);
65
+ // 检查重名
66
+ if (fs.existsSync(copyPath)) {
67
+ console.log(chalk.red(`组件名称已存在`));
68
+ process.exit();
69
+ }
60
70
 
61
- fse.copySync(templatePath, copyPath, {
62
- filter: (src, dest) => {
63
- if (src === path.join(templatePath, configPath)) {
64
- const config = require(src)
65
- config.base = base;
66
- fs.writeFileSync(path.join(copyPath, configPath), JSON.stringify(config, null, 2))
67
- return 0;
68
- }
69
- return 1;
70
- },
71
- });
71
+ if (template.includes('custom-')) {
72
+ generateCustom(name, moduleName, template.split('custom-')[1]);
73
+ } else {
74
+ // 重写配置
75
+ const templatePath = path.join(templates, template);
76
+ const configPath = 'config/main.json';
77
+
78
+ fse.copySync(templatePath, copyPath, {
79
+ filter: (src, dest) => {
80
+ if (src === path.join(templatePath, configPath)) {
81
+ const config = require(src);
82
+ config.base = base;
83
+ fs.writeFileSync(
84
+ path.join(copyPath, configPath),
85
+ JSON.stringify(config, null, 2),
86
+ );
87
+ return 0;
88
+ }
89
+ return 1;
90
+ },
91
+ });
92
+ }
72
93
 
73
- console.log()
74
- console.log(chalk.green(`模版添加成功`))
75
- cb && cb()
76
- })
77
- .catch((error) => cb && cb(error));
78
- }
94
+ console.log();
95
+ console.log(chalk.green(`模版添加成功`));
96
+ cb && cb();
97
+ })
98
+ .catch((error) => cb && cb(error));
99
+ };
@@ -1,49 +1,49 @@
1
1
  /**
2
2
  * 给导出的组件添加static方法获取配置文件
3
3
  */
4
- const path = require('path');
5
- const glob = require('glob');
6
- const fs = require('fs');
4
+ const path = require("path");
5
+ const glob = require("glob");
6
+ const fs = require("fs");
7
7
 
8
- const parser = require('@babel/parser').parse;
9
- const traverse = require('@babel/traverse').default;
10
- const generate = require('@babel/generator').default;
11
- const t = require('@babel/types');
8
+ const parser = require("@babel/parser").parse;
9
+ const traverse = require("@babel/traverse").default;
10
+ const generate = require("@babel/generator").default;
11
+ const t = require("@babel/types");
12
12
 
13
13
  function getOldDimension(width, height, hasUndeline) {
14
14
  if (hasUndeline) {
15
15
  return {
16
- _name: 'dimension', // 定义位置宽高
17
- _displayName: '位置尺寸',
16
+ _name: "dimension", // 定义位置宽高
17
+ _displayName: "位置尺寸",
18
18
  _value: [
19
19
  {
20
- _name: 'chartPosition',
21
- _displayName: '图表位置',
20
+ _name: "chartPosition",
21
+ _displayName: "图表位置",
22
22
  _value: [
23
23
  {
24
- _name: 'left',
25
- _displayName: 'X轴坐标',
24
+ _name: "left",
25
+ _displayName: "X轴坐标",
26
26
  _value: 100, // 开发环境默认显示位置
27
27
  },
28
28
  {
29
- _name: 'top',
30
- _displayName: 'Y轴坐标',
29
+ _name: "top",
30
+ _displayName: "Y轴坐标",
31
31
  _value: 100, // 开发环境默认显示位置
32
32
  },
33
33
  ],
34
34
  },
35
35
  {
36
- _name: 'chartDimension',
37
- _displayName: '图表尺寸',
36
+ _name: "chartDimension",
37
+ _displayName: "图表尺寸",
38
38
  _value: [
39
39
  {
40
- _name: 'width',
41
- _displayName: '宽度',
40
+ _name: "width",
41
+ _displayName: "宽度",
42
42
  _value: width, // 组件默认宽
43
43
  },
44
44
  {
45
- _name: 'height',
46
- _displayName: '高度',
45
+ _name: "height",
46
+ _displayName: "高度",
47
47
  _value: height, // 组件默认高
48
48
  },
49
49
  ],
@@ -52,37 +52,37 @@ function getOldDimension(width, height, hasUndeline) {
52
52
  };
53
53
  }
54
54
  return {
55
- name: 'dimension', // 定义位置宽高
56
- displayName: '位置尺寸',
55
+ name: "dimension", // 定义位置宽高
56
+ displayName: "位置尺寸",
57
57
  value: [
58
58
  {
59
- name: 'chartPosition',
60
- displayName: '图表位置',
59
+ name: "chartPosition",
60
+ displayName: "图表位置",
61
61
  value: [
62
62
  {
63
- name: 'left',
64
- displayName: 'X轴坐标',
63
+ name: "left",
64
+ displayName: "X轴坐标",
65
65
  value: 100, // 开发环境默认显示位置
66
66
  },
67
67
  {
68
- name: 'top',
69
- displayName: 'Y轴坐标',
68
+ name: "top",
69
+ displayName: "Y轴坐标",
70
70
  value: 100, // 开发环境默认显示位置
71
71
  },
72
72
  ],
73
73
  },
74
74
  {
75
- name: 'chartDimension',
76
- displayName: '图表尺寸',
75
+ name: "chartDimension",
76
+ displayName: "图表尺寸",
77
77
  value: [
78
78
  {
79
- name: 'width',
80
- displayName: '宽度',
79
+ name: "width",
80
+ displayName: "宽度",
81
81
  value: width, // 组件默认宽
82
82
  },
83
83
  {
84
- name: 'height',
85
- displayName: '高度',
84
+ name: "height",
85
+ displayName: "高度",
86
86
  value: height, // 组件默认高
87
87
  },
88
88
  ],
@@ -99,7 +99,7 @@ module.exports = function (content, map, meta) {
99
99
  let file = content;
100
100
 
101
101
  // 兼容旧的写法
102
- if (~file.indexOf('getDefaultConfig')) {
102
+ if (~file.indexOf("getDefaultConfig")) {
103
103
  return callback(null, file);
104
104
  }
105
105
  const configPath = `${modulePath}/config`;
@@ -131,26 +131,26 @@ module.exports = function (content, map, meta) {
131
131
  const names = new Set();
132
132
  if (
133
133
  matches
134
- .map((filename) => filename.split('.')[0])
134
+ .map((filename) => filename.split(".")[0])
135
135
  .some((name, i) => {
136
136
  let config;
137
137
  try {
138
138
  let jsonFile = fs.existsSync(
139
- path.join(configPath, name + '.json')
139
+ path.join(configPath, name + ".json")
140
140
  );
141
141
 
142
142
  if (jsonFile) {
143
143
  config = JSON.parse(
144
- fs.readFileSync(path.resolve(configPath, name + '.json')),
145
- { encoding: 'utf8' }
144
+ fs.readFileSync(path.resolve(configPath, name + ".json")),
145
+ { encoding: "utf8" }
146
146
  );
147
147
  } else {
148
148
  delete require.cache[
149
- require.resolve(path.resolve(configPath, name + '.js'))
149
+ require.resolve(path.resolve(configPath, name + ".js"))
150
150
  ];
151
151
  config = require(path.resolve(
152
152
  configPath,
153
- name + '.js'
153
+ name + ".js"
154
154
  )).default;
155
155
  }
156
156
  } catch (error) {
@@ -162,23 +162,23 @@ module.exports = function (content, map, meta) {
162
162
  let data;
163
163
  try {
164
164
  let dataJsonFile = fs.existsSync(
165
- path.join(configPath, name + '.data.json')
165
+ path.join(configPath, name + ".data.json")
166
166
  );
167
167
 
168
168
  if (dataJsonFile) {
169
169
  data = JSON.parse(
170
170
  fs.readFileSync(
171
- path.resolve(configPath, name + '.data.json'),
172
- { encoding: 'utf8' }
171
+ path.resolve(configPath, name + ".data.json"),
172
+ { encoding: "utf8" }
173
173
  )
174
174
  );
175
175
  } else {
176
176
  delete require.cache[
177
- require.resolve(path.resolve(configPath, name + '.data.js'))
177
+ require.resolve(path.resolve(configPath, name + ".data.js"))
178
178
  ];
179
179
  data = require(path.resolve(
180
180
  configPath,
181
- name + '.data.js'
181
+ name + ".data.js"
182
182
  )).default;
183
183
  }
184
184
  } catch (error) {
@@ -197,7 +197,7 @@ module.exports = function (content, map, meta) {
197
197
  } else {
198
198
  names.add(config.base.module_name);
199
199
  }
200
- if (name === 'main') {
200
+ if (name === "main") {
201
201
  main.config = config;
202
202
  main.data = data;
203
203
  return;
@@ -212,17 +212,17 @@ module.exports = function (content, map, meta) {
212
212
  return;
213
213
 
214
214
  // 解析导出变量名
215
- let vname = '',
215
+ let vname = "",
216
216
  exportName = `__EASYV_COMPONENT__`;
217
217
  const ast = parser(content, {
218
- sourceType: 'module',
219
- plugins: ['jsx', 'typescript', 'classProperties'],
218
+ sourceType: "module",
219
+ plugins: ["jsx", "typescript", "classProperties"],
220
220
  });
221
221
  traverse(ast, {
222
222
  ExportDefaultDeclaration(path) {
223
223
  const child = path.node.declaration;
224
224
 
225
- if (child.type === 'ClassDeclaration') {
225
+ if (child.type === "ClassDeclaration") {
226
226
  // e.g: export default class a {}
227
227
  if (child.id) {
228
228
  vname = child.id.name;
@@ -235,26 +235,26 @@ module.exports = function (content, map, meta) {
235
235
  // e.g: export default () => {}
236
236
  } else if (
237
237
  !!~[
238
- 'CallExpression',
239
- 'ArrowFunctionExpression',
240
- 'FunctionExpression',
238
+ "CallExpression",
239
+ "ArrowFunctionExpression",
240
+ "FunctionExpression",
241
241
  ].indexOf(child.type)
242
242
  ) {
243
243
  const exportIdentifier = t.identifier(exportName);
244
244
  path.insertBefore(
245
- t.variableDeclaration('const', [
245
+ t.variableDeclaration("const", [
246
246
  t.variableDeclarator(exportIdentifier, child),
247
247
  ])
248
248
  );
249
249
  path.node.declaration = exportIdentifier;
250
250
  vname = exportName;
251
251
  // e.g: export default function(){}
252
- } else if (!!~['FunctionDeclaration'].indexOf(child.type)) {
252
+ } else if (!!~["FunctionDeclaration"].indexOf(child.type)) {
253
253
  const exportIdentifier = t.identifier(exportName);
254
254
  child.id = exportIdentifier;
255
255
  vname = exportName;
256
256
  // e.g: export default a;
257
- } else if (child.type === 'Identifier') {
257
+ } else if (child.type === "Identifier") {
258
258
  vname = child.name;
259
259
  }
260
260
  path.stop();
@@ -262,13 +262,13 @@ module.exports = function (content, map, meta) {
262
262
  ExportNamedDeclaration(path) {
263
263
  const child = path.node.declaration;
264
264
  // e.g: export const a = {};
265
- if (child.type === 'VariableDeclaration') {
265
+ if (child.type === "VariableDeclaration") {
266
266
  const exportIdentifier = child.declarations[0].id;
267
267
  vname = exportIdentifier.name;
268
268
  path.replaceWith(child);
269
269
  path.insertAfter(t.exportDefaultDeclaration(exportIdentifier));
270
270
  // e.g: export function a(){};
271
- } else if (child.type === 'FunctionDeclaration') {
271
+ } else if (child.type === "FunctionDeclaration") {
272
272
  const exportIdentifier = child.id;
273
273
  path.replaceWith(child);
274
274
  path.insertAfter(t.exportDefaultDeclaration(exportIdentifier));
@@ -276,12 +276,12 @@ module.exports = function (content, map, meta) {
276
276
  path.stop();
277
277
  },
278
278
  });
279
- file = generate(ast, { quotes: 'single', retainLines: false }).code;
279
+ file = generate(ast, { quotes: "single", retainLines: false }).code;
280
280
  if (!vname) {
281
281
  return callback(
282
282
  `cannot found default export object in ${path.join(
283
283
  modulePath,
284
- 'index'
284
+ "index"
285
285
  )}`
286
286
  );
287
287
  }
@@ -294,20 +294,20 @@ module.exports = function (content, map, meta) {
294
294
  // 添加width/height属性兼容老版本
295
295
  let updateChart = false;
296
296
  const hasUnderline = mainConfig.configuration[0]
297
- ? '_value' in mainConfig.configuration[0]
297
+ ? "_value" in mainConfig.configuration[0]
298
298
  : false;
299
299
  const keys = {
300
- name: hasUnderline ? '_name' : 'name',
301
- displayName: hasUnderline ? '_displayName' : 'displayName',
302
- value: hasUnderline ? '_value' : 'value',
303
- type: hasUnderline ? '_type' : 'type',
304
- children: hasUnderline ? '_children' : 'children',
300
+ name: hasUnderline ? "_name" : "name",
301
+ displayName: hasUnderline ? "_displayName" : "displayName",
302
+ value: hasUnderline ? "_value" : "value",
303
+ type: hasUnderline ? "_type" : "type",
304
+ children: hasUnderline ? "_children" : "children",
305
305
  };
306
306
  mainConfig.configuration = mainConfig.configuration.map((item) => {
307
- if (item[keys.name] === 'chart') {
307
+ if (item[keys.name] === "chart") {
308
308
  let values = item._value || item.value;
309
309
  values = values.map((item) => {
310
- if (item[keys.name] === 'dimension') {
310
+ if (item[keys.name] === "dimension") {
311
311
  updateChart = true;
312
312
  return item;
313
313
  // return getOldDimension(mainConfig.width, mainConfig.height, hasUnderline)
@@ -331,8 +331,8 @@ module.exports = function (content, map, meta) {
331
331
  });
332
332
  if (!updateChart) {
333
333
  const chart = {
334
- [keys.name]: 'chart',
335
- [keys.displayName]: '组件',
334
+ [keys.name]: "chart",
335
+ [keys.displayName]: "组件",
336
336
  [keys.value]: [
337
337
  getOldDimension(
338
338
  mainConfig.width,
@@ -367,23 +367,23 @@ module.exports = function (content, map, meta) {
367
367
  const presetChidlren = mainConfig.presetChildren || [];
368
368
 
369
369
  const children = {
370
- [keys.name]: 'children',
371
- [keys.displayName]: '子组件',
370
+ [keys.name]: "children",
371
+ [keys.displayName]: "子组件",
372
372
  [keys.value]: [
373
373
  {
374
- [keys.name]: 'component',
375
- [keys.displayName]: '管理',
374
+ [keys.name]: "component",
375
+ [keys.displayName]: "管理",
376
376
  [keys.value]: [
377
377
  {
378
- [keys.name]: 'list',
379
- [keys.displayName]: '',
378
+ [keys.name]: "list",
379
+ [keys.displayName]: "",
380
380
  [keys.value]: presetChidlren
381
381
  .filter((id) => defineMap[id])
382
382
  .map((id) => {
383
383
  const { base } = defineMap[id];
384
384
  return { label: base.name, name: base.module_name };
385
385
  }),
386
- [keys.type]: 'children',
386
+ [keys.type]: "children",
387
387
  [keys.children]: defineChildren
388
388
  .filter((id) => defineMap[id])
389
389
  .map((id) => {
@@ -399,14 +399,14 @@ module.exports = function (content, map, meta) {
399
399
  }
400
400
  // 替换modulename
401
401
  if (query.uniqueId) {
402
- mainConfig.base.module_name += '.' + query.uniqueId;
402
+ mainConfig.base.module_name += "." + query.uniqueId;
403
403
  }
404
404
  // 删除平台无法识别的参数
405
405
  delete mainConfig.children;
406
406
  delete mainConfig.presetChildren;
407
407
  }
408
408
 
409
- file += '\n';
409
+ file += "\n";
410
410
  file += `${vname}.getDefaultConfig=function(){return ${JSON.stringify(
411
411
  mainConfig
412
412
  )}};`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easyv/react-component-builder",
3
- "version": "0.16.3",
3
+ "version": "0.17.1",
4
4
  "files": [
5
5
  "launcher",
6
6
  "templates",
@@ -18,14 +18,14 @@
18
18
  "@babel/core": "7.9.0",
19
19
  "@babel/register": "^7.10.4",
20
20
  "@easyv/react-gui": "^1.6.6",
21
- "@easyv/simulator": "^0.3.1",
21
+ "@easyv/simulator": "^0.4.0",
22
22
  "@svgr/webpack": "4.3.3",
23
23
  "acorn": "^7.3.1",
24
24
  "acorn-jsx": "^5.2.0",
25
25
  "antd": "^4.0.3",
26
26
  "babel-loader": "8.1.0",
27
27
  "babel-plugin-named-asset-import": "^0.3.6",
28
- "babel-preset-react-app": "^10.0.1",
28
+ "babel-preset-react-app": "^9.1.2",
29
29
  "classnames": "^2.2.6",
30
30
  "css-loader": "3.4.2",
31
31
  "d3": "^5.16.0",