-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenerator.js
More file actions
122 lines (115 loc) · 3.66 KB
/
Generator.js
File metadata and controls
122 lines (115 loc) · 3.66 KB
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
const { copyAllFiles, injectJson, mkdirSync, copyFile, mkfileSync } = require('./lib/copy');
const path = require('path');
const { MODE, projectTemplateMap, markdownTemplateMap } = require('./constant');
/**
* @description { 't1': { value: 'template1', desc: 'description1'} } => { 't1': 'template1' }
*/
const templateAdapter = (templateMap) => {
return Object.keys(templateMap).reduce(
(prev, cur) => ({
...prev,
[cur]: templateMap[cur].value,
}),
{}
);
};
/**
* @description 模板生成器
*/
class Generator {
constructor() {
/**
* @description 当前生成的模板格式
*/
this.mode = MODE.PROJECT;
/**
* @description 目标文件或目录的名称,用以组合路径
*/
this.targetName = '';
/**
* @description 项目模板文件映射
*/
this.projectTemplateMap = templateAdapter(projectTemplateMap);
/**
* @description markdown模板文件映射
*/
this.markdownTemplateMap = templateAdapter(markdownTemplateMap);
this.templateMap = { ...this.projectTemplateMap, ...this.markdownTemplateMap };
}
createProject(targetName) {
this.mode = MODE.PROJECT;
this.targetName = targetName;
}
createMarkdown(targetName) {
this.mode = MODE.MARKDOWN;
this.targetName = targetName;
}
generateTemplate(template) {
switch (this.mode) {
case MODE.PROJECT: {
if (!this.projectTemplateMap[template]) {
console.log('模板输入错误!');
console.log('可用project模板如下:', Object.keys(this.projectTemplateMap).join('/'));
return;
}
const srcPath = path.resolve(__dirname, './templates/', this.templateMap[template]);
const targetPath = path.resolve(process.cwd(), this.targetName);
// 生成目录
mkdirSync(targetPath);
// 拷贝生成模板
copyAllFiles(srcPath, targetPath);
// 写入package.json
this._handlePackageJSON({ targetPath, targetName: this.targetName, template });
break;
}
case MODE.MARKDOWN: {
if (!this.markdownTemplateMap[template]) {
console.log('模板输入错误!');
console.log('可用markdown模板如下:', Object.keys(this.markdownTemplateMap).join('/'));
return;
}
const srcPath = path.resolve(__dirname, './templates/markdowns', this.templateMap[template] + '.md');
const targetPath = path.resolve(process.cwd(), this.targetName + '.md');
// 生成文件
mkfileSync(targetPath);
// 拷贝内容
copyFile(srcPath, targetPath);
console.log('生成文件:', targetPath);
break;
}
default: {
console.log('生成错误,请检查当前选择的模式');
}
}
}
/**
* @description 处理生成项目模板时的package.json文件
*/
_handlePackageJSON({ targetPath, targetName, template }) {
switch (template) {
case 'rw-ts':
case 'rw-ts-w5': {
return injectJson(path.resolve(targetPath, './package.json'), { name: targetName });
}
case 'rc-ts': {
return injectJson(path.resolve(targetPath, './package.json'), {
name: targetName,
main: `dist/index.js`,
module: `dist/index.js`,
types: 'types/index.d.ts',
});
}
case 'lib-ts':
case 'lib-ts-w5': {
return injectJson(path.resolve(targetPath, './package.json'), {
name: targetName,
main: `lib/${targetName}.js`,
unpkg: `dist/${targetName}.js`,
module: `esm/${targetName}.js`,
types: 'types/index.d.ts',
});
}
}
}
}
module.exports = Generator;