forked from aisuda/components-playgroud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
129 lines (109 loc) · 2.8 KB
/
build.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
const fs = require('fs');
const path = require('path');
const JSZip = require('jszip');
const frameworkMap = {
react: 0,
vue: 1,
jquery: 2
};
const usageMap = {
renderer: 0,
formitem: 1,
options: 2
};
const basePath = 'src/components/';
const zip = new JSZip();
const items = fileDisplay(basePath);
for (const item of items) {
const source = fs.readFileSync(item).toString();
const parts = /src\/components\/([jquery|react|vue]+)\/([renderer|formitem|options]+)\/(.+)\/(.+)(?<!-plugin).(tsx|jsx|js)/.exec(
item
);
// 没有命中的就设置成其他的
if (!parts) {
continue;
}
const [_, framework, usage, folderName, name, ext] = parts;
let entry = [name]; // 资源入口
let files = [{ // 文件源码
name,
type: 'component',
source,
ext
}];
const cmptPath = `${basePath}${framework}/${usage}/${folderName}/`;
// 一个样式文件就可以了,scss格式
const styleFile = source.match(/import (\"|\')(\.\/.+\.scss)\1/);
if (styleFile) {
const style = fs.readFileSync(path.resolve(cmptPath, styleFile[0].match(/import \'(\S*)'/)[1])).toString();
files.push({
name: 'style',
type: 'style',
source: style,
ext: 'scss'
});
}
const pluginFile = fs.existsSync(`${cmptPath}plugin/`) && fileDisplay(`${cmptPath}plugin/`);
if (pluginFile) {
// 一个组件对应一个插件
const plugin = fs.readFileSync(`./${pluginFile[0]}`).toString();
entry.push(`${name}-plugin`)
files.push({
name: `${name}-plugin`,
type: 'plugin',
source: plugin,
ext: pluginFile[0].replace(/.+\./, '')
});
}
zip.file(
`component-${name}.json`,
JSON.stringify({
name,
framework: frameworkMap[framework],
usage: usageMap[usage],
type: name,
source,
entry,
files,
status: 1,
description: ''
})
);
}
const utils = fileDisplay('./src/util');
for (const util of utils) {
if (!/src\/util\/(.+)\.([js|jsx|tsx])+/.test(util)) {
continue;
}
const source = fs.readFileSync(util).toString();
const name = RegExp.$1.replace(/\//g, '-');
zip.file(
`util-${name}.json`,
JSON.stringify({
name,
framework: 0,
usage: 3,
type: `util-${name}`,
source,
status: 1,
description: ''
})
);
}
zip
.generateNodeStream({type: 'nodebuffer', streamFiles: true})
.pipe(fs.createWriteStream('components.zip'));
// 获取所有子文件
function fileDisplay(filePath, result = []) {
const files = fs.readdirSync(filePath);
files.forEach(filename => {
const filedir = path.join(filePath, filename);
const stats = fs.statSync(filedir);
if (stats.isFile()) {
result.push(filedir);
} else if (stats.isDirectory()) {
fileDisplay(filedir, result);
}
});
return result;
}