@plugin-light/cli 1.0.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/LICENSE +9 -0
- package/README.md +58 -0
- package/bin/cli.js +34 -0
- package/lib/config.d.ts +5 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +31 -0
- package/lib/loader.d.ts +1 -0
- package/lib/loader.js +41 -0
- package/lib/loader.prod.js +1 -0
- package/lib/publish-inner.d.ts +7 -0
- package/package.json +33 -0
- package/script/docs-demo/helper.js +180 -0
- package/script/docs-demo/watch.js +10 -0
- package/script/generate-component-config/1.docs-sidebar.js +48 -0
- package/script/generate-component-config/2.demo-index.js +56 -0
- package/script/generate-component-config/3.demo-pages.js +78 -0
- package/script/generate-component-config/4.demo-title.js +41 -0
- package/script/generate-component-config/5.build-config.js +34 -0
- package/script/generate-component-config/6.docs-url.js +67 -0
- package/script/generate-component-config/core.js +43 -0
- package/script/generate-component-config/generate-entry.js +215 -0
- package/script/generate-component-config/helper.js +29 -0
- package/script/generate-new-component/copy-dir.js +158 -0
- package/script/generate-new-component/generate-new-component.js +128 -0
- package/script/generate-new-component/sort.js +23 -0
- package/script/post-install/index.js +100 -0
- package/script/release/prepare.js +59 -0
- package/script/release/release.js +31 -0
- package/script/release/version.js +39 -0
- package/script/utils/utils.js +114 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
const { readFileSync, writeFileSync, hyphenate } = require('t-comm');
|
|
5
|
+
|
|
6
|
+
const {
|
|
7
|
+
DEMO_DIR_MAP,
|
|
8
|
+
} = require('../utils/utils');
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
function updateDocsData(config, targetDir) {
|
|
12
|
+
const compList = Object.values(config);
|
|
13
|
+
compList.forEach((item) => {
|
|
14
|
+
const { list } = item;
|
|
15
|
+
list.forEach((comp) => {
|
|
16
|
+
updateDocsUrl({ targetDir, comp });
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
function updateDocsUrl({ targetDir, comp }) {
|
|
23
|
+
const demoUrl = getDemoUrl(comp);
|
|
24
|
+
const { name } = comp;
|
|
25
|
+
const hyphenatedName = hyphenate(name);
|
|
26
|
+
|
|
27
|
+
// 这里组件都是放在 `targetDir/packages/press-xxx` 中
|
|
28
|
+
const compDir = path.resolve(targetDir, 'packages', `press-${hyphenatedName}`);
|
|
29
|
+
|
|
30
|
+
const readme = path.resolve(compDir, 'README.md');
|
|
31
|
+
const readmeEn = path.resolve(compDir, 'README.en-US.md');
|
|
32
|
+
|
|
33
|
+
realUpdateDocsUrl({
|
|
34
|
+
readme,
|
|
35
|
+
demoUrl,
|
|
36
|
+
});
|
|
37
|
+
realUpdateDocsUrl({
|
|
38
|
+
readme: readmeEn,
|
|
39
|
+
demoUrl,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
function realUpdateDocsUrl({ readme, demoUrl }) {
|
|
45
|
+
if (fs.existsSync(readme)) {
|
|
46
|
+
const content = readFileSync(readme);
|
|
47
|
+
const newContent = content.replace(/(url\s*:\s*)pages\/[\w-]+\/[\w/\-?=]+\n/, `$1${demoUrl}\n`);
|
|
48
|
+
writeFileSync(readme, newContent);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
function getDemoUrl(compConfig) {
|
|
54
|
+
const { name, demoRedirect, subPackage = DEMO_DIR_MAP.PRESS } = compConfig;
|
|
55
|
+
const hyphenatedName = hyphenate(name);
|
|
56
|
+
if (!demoRedirect) {
|
|
57
|
+
return `pages/${subPackage}/${hyphenatedName}/${hyphenatedName}`;
|
|
58
|
+
}
|
|
59
|
+
const hyphenatedRedirect = hyphenate(demoRedirect);
|
|
60
|
+
|
|
61
|
+
return `pages/${subPackage}/${hyphenatedRedirect}/${hyphenatedRedirect}?comp=${hyphenatedName}`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
module.exports = {
|
|
66
|
+
updateDocsData,
|
|
67
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
|
|
2
|
+
const { writeDocSidebar } = require('./1.docs-sidebar');
|
|
3
|
+
const { writeDemoIndexConfig } = require('./2.demo-index');
|
|
4
|
+
const { writeDemoPagesJson } = require('./3.demo-pages');
|
|
5
|
+
|
|
6
|
+
const { writeDemoTitleI18n } = require('./4.demo-title');
|
|
7
|
+
const { writeBuildConfig } = require('./5.build-config');
|
|
8
|
+
const { updateDocsData } = require('./6.docs-url');
|
|
9
|
+
const {
|
|
10
|
+
writeSrcIndexJs,
|
|
11
|
+
} = require('./generate-entry');
|
|
12
|
+
|
|
13
|
+
const { PATH_MAP } = require('./helper');
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
function generateComponentConfig({
|
|
17
|
+
componentConfig,
|
|
18
|
+
scssPath,
|
|
19
|
+
globMatch,
|
|
20
|
+
targetDir,
|
|
21
|
+
needAct = false,
|
|
22
|
+
}) {
|
|
23
|
+
writeDocSidebar(componentConfig);
|
|
24
|
+
writeDemoIndexConfig(componentConfig);
|
|
25
|
+
writeDemoPagesJson(componentConfig, needAct);
|
|
26
|
+
|
|
27
|
+
writeDemoTitleI18n(componentConfig);
|
|
28
|
+
writeBuildConfig(componentConfig);
|
|
29
|
+
updateDocsData(componentConfig, targetDir);
|
|
30
|
+
|
|
31
|
+
writeSrcIndexJs({
|
|
32
|
+
componentConfig,
|
|
33
|
+
filePath: PATH_MAP.SRC_INDEX,
|
|
34
|
+
allComponent: true,
|
|
35
|
+
scssPath,
|
|
36
|
+
globMatch,
|
|
37
|
+
targetDir,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
module.exports = {
|
|
42
|
+
generateComponentConfig,
|
|
43
|
+
};
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
const { replaceAllPolyfill, hyphenate } = require('t-comm');
|
|
5
|
+
// eslint-disable-next-line import/order
|
|
6
|
+
const glob = require('glob');
|
|
7
|
+
|
|
8
|
+
replaceAllPolyfill();
|
|
9
|
+
const PACKAGE_ENTRY_JS = './src/packages/index.js';
|
|
10
|
+
|
|
11
|
+
const MAIN_TEMPLATE = `/* eslint-disable import/order */
|
|
12
|
+
/* Automatically generated by 'press-ui/script/generate-component-config/generate-entry.js' */
|
|
13
|
+
{{install}}
|
|
14
|
+
|
|
15
|
+
const components = {
|
|
16
|
+
{{components}}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const install = function (Vue) {
|
|
20
|
+
Object.values(components).forEach((component) => {
|
|
21
|
+
Vue.component(component.name, component);
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
if (typeof window !== 'undefined' && window.Vue) {
|
|
26
|
+
install(window.Vue);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export default {
|
|
30
|
+
install,
|
|
31
|
+
...components,
|
|
32
|
+
};
|
|
33
|
+
export * from './packages/common/public/public';
|
|
34
|
+
`;
|
|
35
|
+
const CSS_BASE = `/* 基础样式 */
|
|
36
|
+
@import './packages/common/style/press/index.scss';
|
|
37
|
+
@import './packages/common/style/press/var.scss';
|
|
38
|
+
|
|
39
|
+
/* 组件样式 */
|
|
40
|
+
`;
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
function getAllPressComponents(globMatch) {
|
|
44
|
+
const list = glob.sync(globMatch);
|
|
45
|
+
const preNames = {};
|
|
46
|
+
|
|
47
|
+
const compList = list.map((item) => {
|
|
48
|
+
const list = item.split('/');
|
|
49
|
+
const file = list[list.length - 1];
|
|
50
|
+
const componentName = file.replace(/^press-/, '').replace(/\.vue$/, '');
|
|
51
|
+
|
|
52
|
+
const parsedComponentName = componentName.replace(/(?:^|-)(\w)/g, (_, c) => (c ? c.toUpperCase() : ''));
|
|
53
|
+
preNames[parsedComponentName] = (preNames[parsedComponentName] || 0) + 1;
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
dir: path.dirname(item),
|
|
57
|
+
name: componentName,
|
|
58
|
+
folder: list[list.length - 2],
|
|
59
|
+
componentName: preNames[parsedComponentName] > 1
|
|
60
|
+
? parsedComponentName + preNames[parsedComponentName]
|
|
61
|
+
: parsedComponentName,
|
|
62
|
+
};
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const componentNameList = compList.map(item => item.componentName);
|
|
66
|
+
const importList = compList.map(item => `import ${item.componentName} from './packages/${item.folder}/press-${item.name}.vue';`);
|
|
67
|
+
|
|
68
|
+
sortByStr(compList, 'name');
|
|
69
|
+
sortByStr(importList);
|
|
70
|
+
sortByStr(componentNameList);
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
compList,
|
|
74
|
+
importList,
|
|
75
|
+
componentNameList,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
function generateIndexScss({ scssPath, globMatch, targetDir }) {
|
|
81
|
+
const { compList } = getAllPressComponents(globMatch);
|
|
82
|
+
let result = [];
|
|
83
|
+
|
|
84
|
+
for (const comp of compList) {
|
|
85
|
+
const globMatch = `${comp.dir}/css/*`;
|
|
86
|
+
const list = glob.sync(globMatch);
|
|
87
|
+
|
|
88
|
+
sortByStr(list);
|
|
89
|
+
|
|
90
|
+
result.push(...list.map((item) => {
|
|
91
|
+
let relativePath = path.relative(targetDir, item);
|
|
92
|
+
if (process.platform === 'win32') {
|
|
93
|
+
relativePath = relativePath.replace(/\\/g, '/');
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const str = `@import './${relativePath}';`;
|
|
97
|
+
return str;
|
|
98
|
+
}));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
result = Array.from(new Set(result));
|
|
102
|
+
const resultStr = `${CSS_BASE}${result.join('\n')}`;
|
|
103
|
+
|
|
104
|
+
fs.writeFileSync(scssPath, resultStr, {
|
|
105
|
+
encoding: 'utf-8',
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
function getComponentPath(component) {
|
|
111
|
+
const SPECIAL_COMPONENT_MAP = {
|
|
112
|
+
'message-board': 'message-board-list',
|
|
113
|
+
};
|
|
114
|
+
return SPECIAL_COMPONENT_MAP[component] || component;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function sortByStr(list, key) {
|
|
118
|
+
list.sort((a, b) => {
|
|
119
|
+
const valueA = key ? a[key] : a;
|
|
120
|
+
const valueB = key ? b[key] : b;
|
|
121
|
+
return valueA.localeCompare(valueB);
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
// if (valueA - valueB > 0) {
|
|
125
|
+
// return 1;
|
|
126
|
+
// }
|
|
127
|
+
// if (valueA - valueB < 0) {
|
|
128
|
+
// return -1;
|
|
129
|
+
// }
|
|
130
|
+
// return 0;
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function getCompList(componentConfig) {
|
|
135
|
+
const importList = [];
|
|
136
|
+
const componentNameList = [];
|
|
137
|
+
|
|
138
|
+
Object.keys(componentConfig)
|
|
139
|
+
.forEach((key) => {
|
|
140
|
+
const value = componentConfig[key];
|
|
141
|
+
const { list } = value;
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
list.forEach((item) => {
|
|
145
|
+
const hyphenatedName = hyphenate(item.name);
|
|
146
|
+
const shortName = item.name.replace(/$Press/, '');
|
|
147
|
+
const compPath = getComponentPath(hyphenatedName);
|
|
148
|
+
importList.push(`import ${shortName} from './packages/press-${hyphenatedName}/press-${compPath}.vue';`);
|
|
149
|
+
|
|
150
|
+
componentNameList.push(shortName);
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
sortByStr(importList);
|
|
154
|
+
sortByStr(componentNameList);
|
|
155
|
+
|
|
156
|
+
return {
|
|
157
|
+
importList,
|
|
158
|
+
componentNameList,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function getSrcIndexJs({ componentConfig, allComponent = false, globMatch }) {
|
|
163
|
+
let {
|
|
164
|
+
importList,
|
|
165
|
+
componentNameList,
|
|
166
|
+
} = getCompList(componentConfig);
|
|
167
|
+
|
|
168
|
+
if (allComponent) {
|
|
169
|
+
const info = getAllPressComponents(globMatch);
|
|
170
|
+
importList = info.importList;
|
|
171
|
+
componentNameList = info.componentNameList;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const res = MAIN_TEMPLATE
|
|
175
|
+
.replace('{{install}}', importList.join('\n'))
|
|
176
|
+
.replaceAll('{{components}}', componentNameList.map(item => `${item},`).join('\n '));
|
|
177
|
+
|
|
178
|
+
return res;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
function writeSrcIndexJs({
|
|
183
|
+
componentConfig,
|
|
184
|
+
filePath,
|
|
185
|
+
allComponent = false,
|
|
186
|
+
scssPath,
|
|
187
|
+
globMatch,
|
|
188
|
+
targetDir,
|
|
189
|
+
}) {
|
|
190
|
+
const js = getSrcIndexJs({ componentConfig, allComponent, globMatch });
|
|
191
|
+
fs.writeFileSync(filePath, js, {
|
|
192
|
+
encoding: 'utf-8',
|
|
193
|
+
});
|
|
194
|
+
const packageEntryJsContent = getAllPressComponents(globMatch)
|
|
195
|
+
.compList
|
|
196
|
+
.map((item) => {
|
|
197
|
+
const result = (`export { default as ${item.componentName} } from './${item.folder}/press-${item.name}.vue';`);
|
|
198
|
+
return result;
|
|
199
|
+
})
|
|
200
|
+
.join('\n');
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
generateIndexScss({ scssPath, globMatch, targetDir });
|
|
204
|
+
|
|
205
|
+
fs.writeFileSync(PACKAGE_ENTRY_JS, `/* eslint-disable no-duplicate-imports */\n${packageEntryJsContent}\n`, {
|
|
206
|
+
encoding: 'utf-8',
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
module.exports = {
|
|
212
|
+
hyphenate,
|
|
213
|
+
getComponentPath,
|
|
214
|
+
writeSrcIndexJs,
|
|
215
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const { writeFileSync } = require('t-comm');
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
IS_INNER_DOCS,
|
|
5
|
+
} = require('../utils/utils');
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
const PATH_MAP = {
|
|
9
|
+
DOC_SIDE_BAR_CONFIG: `./${IS_INNER_DOCS ? 'docs/' : ''}docs/.vuepress/sidebar/sidebar.json`,
|
|
10
|
+
DOC_SIDE_BAR_EN_CONFIG: `./${IS_INNER_DOCS ? 'docs/' : ''}docs/.vuepress/sidebar/sidebar-en.json`,
|
|
11
|
+
|
|
12
|
+
DEMO_INDEX_CONFIG: 'src/pages/index/page-config.json',
|
|
13
|
+
DEMO_PAGES_JSON: './src/pages.json',
|
|
14
|
+
DEMO_I18N: 'src/utils/i18n/title-i18n.json',
|
|
15
|
+
|
|
16
|
+
BUILD_CONFIG: 'config/components.json',
|
|
17
|
+
SRC_INDEX: 'src/index.js',
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
function saveJsonToFile(filePath, data) {
|
|
22
|
+
writeFileSync(filePath, data, true);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
module.exports = {
|
|
27
|
+
PATH_MAP,
|
|
28
|
+
saveJsonToFile,
|
|
29
|
+
};
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
const { copyDir, execCommand, getPureCompName, getFullCompName } = require('t-comm');
|
|
5
|
+
|
|
6
|
+
const { isActComponent, COMP_PREFIX } = require('../utils/utils');
|
|
7
|
+
|
|
8
|
+
const TEMPLATE_PATH = './script/new/template';
|
|
9
|
+
const COMP_TARGET_PATH = './src/packages';
|
|
10
|
+
const DEFAULT_COMP_NAME = 'press.vue';
|
|
11
|
+
const DEFAULT_README_NAME = 'README.md';
|
|
12
|
+
const DEFAULT_README_EN_NAME = 'README.en-US.md';
|
|
13
|
+
const DEFAULT_DEMO_NAME = 'demo.vue';
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
function copyComponentDir(config) {
|
|
17
|
+
const { name, title } = config;
|
|
18
|
+
if (!name) {
|
|
19
|
+
throw new Error('请输入组件名称');
|
|
20
|
+
}
|
|
21
|
+
console.log(`[NEW] 正在创建 ${name} 组件...`);
|
|
22
|
+
|
|
23
|
+
const fullName = getFullCompName(name, COMP_PREFIX);
|
|
24
|
+
const pureName = getPureCompName(name, COMP_PREFIX);
|
|
25
|
+
const targetDir = path.resolve(COMP_TARGET_PATH, fullName);
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
if (fs.existsSync(targetDir)) {
|
|
29
|
+
throw new Error(`${name} 已经存在,请移除后重试`);
|
|
30
|
+
}
|
|
31
|
+
copyDir(TEMPLATE_PATH, targetDir, () => {
|
|
32
|
+
console.log(`[NEW] ${fullName} 拷贝成功`);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
if (isActComponent(fullName)) {
|
|
36
|
+
execCommand('rm -rf demo.vue && mv demo-act.vue demo.vue', targetDir, 'inherit');
|
|
37
|
+
} else {
|
|
38
|
+
execCommand('rm -rf demo-act.vue', targetDir, 'inherit');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const compVue = path.resolve(COMP_TARGET_PATH, fullName, DEFAULT_COMP_NAME);
|
|
42
|
+
const newCompName = path.resolve(COMP_TARGET_PATH, fullName, `${fullName}.vue`);
|
|
43
|
+
|
|
44
|
+
fs.renameSync(compVue, newCompName);
|
|
45
|
+
console.log(`[NEW] ${fullName}.vue 重命名成功`);
|
|
46
|
+
|
|
47
|
+
changeReadme({
|
|
48
|
+
fullName,
|
|
49
|
+
pureName,
|
|
50
|
+
title,
|
|
51
|
+
name,
|
|
52
|
+
});
|
|
53
|
+
changeReadme({
|
|
54
|
+
fullName,
|
|
55
|
+
pureName,
|
|
56
|
+
title,
|
|
57
|
+
name,
|
|
58
|
+
isEn: true,
|
|
59
|
+
});
|
|
60
|
+
changeDemo({
|
|
61
|
+
fullName,
|
|
62
|
+
pureName,
|
|
63
|
+
title,
|
|
64
|
+
name,
|
|
65
|
+
newCompName,
|
|
66
|
+
});
|
|
67
|
+
changeComponent({
|
|
68
|
+
fullName,
|
|
69
|
+
pureName,
|
|
70
|
+
title,
|
|
71
|
+
name,
|
|
72
|
+
newCompName,
|
|
73
|
+
});
|
|
74
|
+
console.log(`[NEW] ${fullName} 文档变量替换成功`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
function changeReadme({
|
|
79
|
+
fullName,
|
|
80
|
+
pureName,
|
|
81
|
+
title,
|
|
82
|
+
name,
|
|
83
|
+
isEn,
|
|
84
|
+
}) {
|
|
85
|
+
const docPath = path.resolve(COMP_TARGET_PATH, fullName, isEn ? DEFAULT_README_EN_NAME : DEFAULT_README_NAME);
|
|
86
|
+
const data = fs.readFileSync(docPath, {
|
|
87
|
+
encoding: 'utf-8',
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const newData = data
|
|
91
|
+
.replace(/COMP/g, pureName)
|
|
92
|
+
.replace(/SUBTITLE/g, name)
|
|
93
|
+
.replace(/TITLE/g, title);
|
|
94
|
+
|
|
95
|
+
fs.writeFileSync(docPath, newData, {
|
|
96
|
+
encoding: 'utf-8',
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
fs.writeFileSync(docPath, newData, {
|
|
100
|
+
encoding: 'utf-8',
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
function changeDemo({
|
|
106
|
+
fullName,
|
|
107
|
+
pureName,
|
|
108
|
+
name,
|
|
109
|
+
title,
|
|
110
|
+
newCompName,
|
|
111
|
+
}) {
|
|
112
|
+
const filePath = path.resolve(COMP_TARGET_PATH, fullName, newCompName);
|
|
113
|
+
const data = fs.readFileSync(filePath, {
|
|
114
|
+
encoding: 'utf-8',
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
const newData = data
|
|
118
|
+
.replace(/COMP/g, pureName)
|
|
119
|
+
.replace(/SUBTITLE/g, name)
|
|
120
|
+
.replace(/TITLE/g, title);
|
|
121
|
+
|
|
122
|
+
fs.writeFileSync(filePath, newData, {
|
|
123
|
+
encoding: 'utf-8',
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
fs.writeFileSync(filePath, newData, {
|
|
127
|
+
encoding: 'utf-8',
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
function changeComponent({
|
|
133
|
+
fullName,
|
|
134
|
+
pureName,
|
|
135
|
+
name,
|
|
136
|
+
title,
|
|
137
|
+
}) {
|
|
138
|
+
const filePath = path.resolve(COMP_TARGET_PATH, fullName, DEFAULT_DEMO_NAME);
|
|
139
|
+
const data = fs.readFileSync(filePath, {
|
|
140
|
+
encoding: 'utf-8',
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
const newData = data
|
|
144
|
+
.replace(/COMP/g, pureName)
|
|
145
|
+
.replace(/SUBTITLE/g, name)
|
|
146
|
+
.replace(/TITLE/g, title);
|
|
147
|
+
|
|
148
|
+
fs.writeFileSync(filePath, newData, {
|
|
149
|
+
encoding: 'utf-8',
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
fs.writeFileSync(filePath, newData, {
|
|
153
|
+
encoding: 'utf-8',
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
module.exports = {
|
|
157
|
+
copyComponentDir,
|
|
158
|
+
};
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
const { execSync } = require('child_process');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
|
|
4
|
+
const { pascalCase, getPureCompName } = require('t-comm');
|
|
5
|
+
const inquirer = require('inquirer');
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
const { COMP_PREFIX } = require('../utils/utils');
|
|
9
|
+
|
|
10
|
+
const { copyComponentDir } = require('./copy-dir');
|
|
11
|
+
const { sortComponentConfig } = require('./sort');
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
function getTypeMap(componentConfigPath) {
|
|
15
|
+
const json = require(componentConfigPath);
|
|
16
|
+
|
|
17
|
+
const typeMap = Object.keys(json).reduce((cAcc, key) => {
|
|
18
|
+
const value = json[key];
|
|
19
|
+
const { title, list } = value;
|
|
20
|
+
|
|
21
|
+
let desc = `${title},如 `;
|
|
22
|
+
|
|
23
|
+
desc += list.slice(0, 2)
|
|
24
|
+
.reduce((acc, item) => {
|
|
25
|
+
acc.push(item.name);
|
|
26
|
+
return acc;
|
|
27
|
+
}, [])
|
|
28
|
+
.join('、');
|
|
29
|
+
|
|
30
|
+
cAcc[desc] = key;
|
|
31
|
+
|
|
32
|
+
return cAcc;
|
|
33
|
+
}, {});
|
|
34
|
+
|
|
35
|
+
return typeMap;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
async function getComponentConfig(componentConfigPath) {
|
|
40
|
+
const typeMap = getTypeMap(componentConfigPath);
|
|
41
|
+
const typeList = Object.keys(typeMap);
|
|
42
|
+
|
|
43
|
+
const result = await inquirer
|
|
44
|
+
.prompt([
|
|
45
|
+
{
|
|
46
|
+
type: 'input',
|
|
47
|
+
name: 'name',
|
|
48
|
+
message: '请输入组件英文名称,格式为大驼峰,如 MessageList、Cell?',
|
|
49
|
+
validate: (val) => {
|
|
50
|
+
if (val && val.trim()) {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
return '请输入组件英文名称';
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
type: 'input',
|
|
58
|
+
name: 'title',
|
|
59
|
+
message: '请输入组件中文名称?',
|
|
60
|
+
validate: (val) => {
|
|
61
|
+
if (val && val.trim()) {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
return '请输入组件中文名称';
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
type: 'list',
|
|
69
|
+
name: 'type',
|
|
70
|
+
message: '请选择组件分类?',
|
|
71
|
+
choices: typeList,
|
|
72
|
+
},
|
|
73
|
+
|
|
74
|
+
]);
|
|
75
|
+
|
|
76
|
+
const { name, title, type } = result;
|
|
77
|
+
const typeDesc = type.split('组件')[0];
|
|
78
|
+
const confirm = await inquirer.prompt([
|
|
79
|
+
{
|
|
80
|
+
type: 'confirm',
|
|
81
|
+
name: 'confirm',
|
|
82
|
+
message: `确认创建英文名为 ${name},中文名为 ${title},类型为 ${typeDesc} 的组件吗?`,
|
|
83
|
+
},
|
|
84
|
+
]);
|
|
85
|
+
|
|
86
|
+
if (!confirm.confirm) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
name: pascalCase(getPureCompName(name.trim(), COMP_PREFIX)),
|
|
92
|
+
title: title.trim(),
|
|
93
|
+
type: typeMap[type],
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function writeCompConfig(config, componentConfigPath) {
|
|
98
|
+
const json = require(componentConfigPath);
|
|
99
|
+
const { type, name, title } = config;
|
|
100
|
+
json[type].list.push({
|
|
101
|
+
name,
|
|
102
|
+
title,
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const sortedJson = sortComponentConfig(json);
|
|
106
|
+
fs.writeFileSync(componentConfigPath, JSON.stringify(sortedJson, null, 2));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function genDocsAndDemoConfig() {
|
|
110
|
+
execSync('npm run gen:config', {
|
|
111
|
+
stdio: 'inherit',
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
async function generateNewComponent(componentConfigPath) {
|
|
117
|
+
const config = await getComponentConfig(componentConfigPath);
|
|
118
|
+
if (!config) return;
|
|
119
|
+
|
|
120
|
+
writeCompConfig(config, componentConfigPath);
|
|
121
|
+
genDocsAndDemoConfig();
|
|
122
|
+
copyComponentDir(config);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
module.exports = {
|
|
127
|
+
generateNewComponent,
|
|
128
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
function sortComponentConfig(config) {
|
|
2
|
+
return Object.keys(config).reduce((acc, key) => {
|
|
3
|
+
const value = config[key];
|
|
4
|
+
|
|
5
|
+
value.list.sort((a, b) => {
|
|
6
|
+
if (a.name > b.name) return 1;
|
|
7
|
+
if (b.name > a.name) return -1;
|
|
8
|
+
return 0;
|
|
9
|
+
});
|
|
10
|
+
acc = {
|
|
11
|
+
...acc,
|
|
12
|
+
[key]: {
|
|
13
|
+
...value,
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
return acc;
|
|
17
|
+
}, {});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = {
|
|
21
|
+
sortComponentConfig,
|
|
22
|
+
};
|
|
23
|
+
|