Skip to content

Commit 7182ce1

Browse files
authored
Merge pull request #30 from Tencent/feature/v0.2.4
feature(0.3.0): lite template
2 parents fd917c4 + f7f1bc4 commit 7182ce1

67 files changed

Lines changed: 1816 additions & 62 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.3.0 `2022-08-22`
2+
3+
### Features
4+
5+
- 新增 `Lite` 版本模板下载,只包含基本的 TDesign 引入及使用
6+
17
## 0.2.5 `2022-08-03`
28

39
### Bug Fixes

docs/20210627-114325.png

-177 KB
Binary file not shown.

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tdesign-starter-cli",
3-
"version": "0.2.5",
3+
"version": "0.3.0",
44
"description": "CLI tool for TDesign Starter project",
55
"main": "./src/main.ts",
66
"lib": "./bin/index.js",
@@ -12,6 +12,7 @@
1212
"watch": "rollup -w -c",
1313
"test": "npm run dev",
1414
"dev": "node ./bin/index.js",
15+
"prebuild": "rimraf bin/*",
1516
"build": "rollup -c ",
1617
"lint": "eslint 'src/**/*.{js,ts}'"
1718
},
@@ -45,7 +46,8 @@
4546
"rollup-plugin-terser": "7.0.2",
4647
"rollup-plugin-typescript": "1.0.1",
4748
"rollup-plugin-typescript2": "0.30.0",
48-
"rollup-plugin-uglify": "6.0.4"
49+
"rollup-plugin-uglify": "6.0.4",
50+
"rollup-plugin-copy": "3.4.0"
4951
},
5052
"dependencies": {
5153
"@babel/runtime": "7.14.0",
@@ -62,6 +64,7 @@
6264
"del": "6.0.0",
6365
"download-git-repo": "3.0.2",
6466
"figlet": "1.5.0",
67+
"fs-extra": "10.1.0",
6568
"husky": "6.0.0",
6669
"inquirer": "8.1.0",
6770
"is-ci": "3.0.0",
@@ -89,6 +92,7 @@
8992
"dist/*",
9093
"types/*",
9194
"package.json",
92-
"docs/*"
95+
"docs/*",
96+
"template/*"
9397
]
9498
}

rollup.config.js

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,68 @@ import { terser } from 'rollup-plugin-terser';
77
import typescript2 from 'rollup-plugin-typescript2';
88
import rollupResolve from 'rollup-plugin-node-resolve';
99
import pkg from './package.json';
10+
import copy from 'rollup-plugin-copy';
1011

1112
const extensions = ['.js', '.ts'];
1213

1314
const external = Object.keys(pkg.dependencies || '');
1415
const globals = external.reduce((prev, current) => {
15-
const newPrev = prev;
16-
newPrev[current] = current;
17-
return newPrev;
16+
const newPrev = prev;
17+
newPrev[current] = current;
18+
return newPrev;
1819
}, {});
1920

2021
const defaultConfig = {
21-
input: pkg.main,
22-
output: {
23-
file: path.resolve(__dirname, pkg.lib),
24-
format: 'cjs',
25-
banner: '#!/usr/bin/env node',
26-
globals
27-
},
28-
external,
29-
plugins: [
30-
typescript2({
31-
exclude: 'node_modules/**',
32-
useTsconfigDeclarationDir: true,
33-
typescript,
34-
tsconfig: './tsconfig.json'
35-
}),
36-
json(),
37-
terser(),
38-
rollupResolve({
39-
// 查找和打包node_modules中的第三方模块
40-
customResolveOptions: {
41-
moduleDirectory: 'src'
42-
},
43-
preferBuiltins: true,
44-
}),
45-
nodeResolve({
46-
extensions,
47-
modulesOnly: true
48-
}),
49-
babel({
50-
exclude: 'node_modules/**',
51-
extensions
52-
})
53-
]
22+
input: pkg.main,
23+
output: {
24+
file: path.resolve(__dirname, pkg.lib),
25+
format: 'cjs',
26+
banner: '#!/usr/bin/env node',
27+
globals
28+
},
29+
external,
30+
plugins: [
31+
typescript2({
32+
exclude: 'node_modules/**',
33+
useTsconfigDeclarationDir: true,
34+
typescript,
35+
tsconfig: './tsconfig.json'
36+
}),
37+
json(),
38+
terser(),
39+
rollupResolve({
40+
// 查找和打包node_modules中的第三方模块
41+
customResolveOptions: {
42+
moduleDirectory: 'src'
43+
},
44+
preferBuiltins: true
45+
}),
46+
nodeResolve({
47+
extensions,
48+
modulesOnly: true
49+
}),
50+
babel({
51+
exclude: 'node_modules/**',
52+
extensions
53+
}),
54+
copy({
55+
targets: [
56+
{
57+
src: ['templates/vue-lite/*', '!templates/vue-lite/node_modules'],
58+
dest: 'bin/templates/vue-lite'
59+
},
60+
{
61+
src: ['templates/vue-next-lite/*', '!templates/vue-next-lite/node_modules'],
62+
dest: 'bin/templates/vue-next-lite'
63+
},
64+
{
65+
src: ['templates/react-lite/*', '!templates/react-lite/node_modules'],
66+
dest: 'bin/templates/react-lite'
67+
}
68+
],
69+
verbose: true
70+
})
71+
]
5472
};
5573

5674
export default defaultConfig;

src/core/CoreGitDownloader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class CoreGitDownloader {
1414
* 下载工程目录,依据配置选择是否需要筛选不需要目录
1515
* @returns 命令行数组
1616
*/
17-
public async syncDownload(options: { type: SupportedTemplate; name: string; description: string }, finalOptions: any) {
17+
public async syncDownload(options: { type: SupportedTemplate; name: string; description: string }, finalOptions: any = {}) {
1818
console.log();
1919
console.log(chalk.green('👉 开始构建,请稍侯...'));
2020
console.log();
@@ -111,7 +111,7 @@ export class CoreGitDownloader {
111111
*
112112
* @memberOf CoreGitDownloader
113113
*/
114-
private executeBuildSuccess(spinner: any, options: any) {
114+
protected executeBuildSuccess(spinner: any, options: any) {
115115
console.log();
116116
spinner.succeed(chalk.green('构建成功!'));
117117
const packagePath = path.join(options.name, 'package.json');

src/core/CoreIndex.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import { CoreSelector } from './CoreSelector';
66
import { CoreInquirer } from './CoreInquirer';
77
import { CoreGitDownloader } from './CoreGitDownloader';
88
import fs from 'fs';
9+
import { CoreLiteInquirer } from './core-lite/CoreLiteInquirer';
10+
import { SupportedTemplate } from './CoreTemplate';
11+
import { CoreLiteDownloader } from './core-lite/CoreLiteDownloader';
912

1013
class Creator {
1114
constructor() {
@@ -42,11 +45,25 @@ class Creator {
4245
// 1.基本配置数据获取
4346
const answer = await new CoreInquirer().interactionsHandler();
4447

45-
// 2.依据基本配置载下配置文件路由模板
46-
const finalAnswer = await new CoreSelector().interactionsSelect(answer);
48+
// 2.询问生成简化版还是自定义版本
49+
const listOptions: { type: SupportedTemplate; name: string; description: string } = await new CoreLiteInquirer().interactionsHandler();
4750

48-
// 3.构建配置保存
49-
await new CoreGitDownloader().syncDownload(answer, finalAnswer);
51+
// 3.执行生成动作
52+
switch (listOptions.type) {
53+
case 'lite':
54+
// 极简版本处理逻辑
55+
await new CoreLiteDownloader().syncDownload(answer);
56+
break;
57+
58+
default:
59+
// 自定义版本处理逻辑
60+
// 3-1.依据基本配置载下配置文件路由模板
61+
const finalAnswer = await new CoreSelector().interactionsSelect(answer);
62+
63+
// 3-2.构建配置保存
64+
await new CoreGitDownloader().syncDownload(answer, finalAnswer);
65+
break;
66+
}
5067
}
5168

5269
/**
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import chalk from 'chalk';
2+
import ora from 'ora';
3+
import path from 'path';
4+
import { CoreGitDownloader } from '../CoreGitDownloader';
5+
import { SupportedTemplate } from '../CoreTemplate';
6+
import fs from 'fs';
7+
import fse from 'fs-extra';
8+
9+
/**
10+
* 极简板本生成器
11+
*
12+
* @export
13+
* @class CoreLiteDownloader
14+
* @extends {CoreGitDownloader}
15+
*/
16+
export class CoreLiteDownloader extends CoreGitDownloader {
17+
/**
18+
* 下载工程目录,依据配置选择是否需要筛选不需要目录
19+
* @returns 命令行数组
20+
*/
21+
public async syncDownload(options: { type: SupportedTemplate; name: string; description: string }, finalOptions: any = {}) {
22+
// console.log('options==>', options);
23+
console.log();
24+
console.log(chalk.green('👉 开始构建,请稍侯...'));
25+
console.log();
26+
const spinner = ora('正在构建模板...').start();
27+
28+
// 清除测试目录
29+
await this.clearTestFolder();
30+
31+
// console.log(options.type, options);
32+
await this.copyTemplate(options);
33+
34+
// 执行成功相关操作
35+
this.executeBuildSuccess(spinner, options);
36+
}
37+
38+
/**
39+
* 复制模板
40+
*
41+
* @protected
42+
* @param {{ type: SupportedTemplate; name: string; description: string; }} options
43+
*
44+
* @memberOf CoreLiteDownloader
45+
*/
46+
protected async copyTemplate(options: { type: SupportedTemplate; name: string; description: string }): Promise<any> {
47+
let copyFolderName = 'vue-lite';
48+
const destDir = path.join(`${process.env.PWD}`, options.name);
49+
// console.log('options.type==>', options.type);
50+
switch (options.type) {
51+
case 'vue2':
52+
copyFolderName = 'vue-lite';
53+
break;
54+
case 'vue3':
55+
copyFolderName = 'vue-next-lite';
56+
break;
57+
case 'react':
58+
copyFolderName = 'react-lite';
59+
break;
60+
}
61+
const srcDir = path.join(__dirname, `templates/${copyFolderName}`);
62+
63+
try {
64+
await fse.copy(srcDir, destDir);
65+
console.log(chalk.green('👉 生成代码完毕...'));
66+
} catch (err) {
67+
console.error(err);
68+
}
69+
}
70+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import inquirer from 'inquirer';
2+
import { CoreInquirer } from '../CoreInquirer';
3+
4+
/**
5+
* 简化板询问器
6+
*
7+
* @export
8+
* @class CoreLiteInquirer
9+
*/
10+
export class CoreLiteInquirer extends CoreInquirer {
11+
/**
12+
* override 交互命令
13+
* @returns 命令行数组
14+
*/
15+
public interactionsHandler() {
16+
const questions: Array<{ type: string; name: string; message: string; choices: Array<Record<string, string>>; default: string }> = [
17+
{
18+
type: 'list',
19+
name: 'type',
20+
message: '生成代码版本:',
21+
choices: [
22+
{ name: ' Lite版本 (只包含TDesign的基本使用和项目工程)', value: 'lite' },
23+
{ name: ' 通用模板版本', value: 'custom' }
24+
],
25+
default: 'lite'
26+
}
27+
];
28+
29+
return inquirer.prompt(questions);
30+
}
31+
}

src/core/core-template/CoreTemplateReactConfig.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
1-
import { IParsedSourceData } from "../CoreParsedConfig";
2-
import { ICoreTemplate } from "./CoreTemplateVue2Config";
1+
import { IParsedSourceData } from '../CoreParsedConfig';
2+
import { ICoreTemplate } from './CoreTemplateVue2Config';
33

44
/**
55
* React config
66
*
77
* @class CoreTemplateReactConfig
88
*/
99
class CoreTemplateReactConfig implements ICoreTemplate {
10-
1110
private configData!: any;
1211

1312
/** 解析的原始数据结构配置 */
1413
private parsedConfigData: Array<IParsedSourceData> = [];
1514

1615
/**
17-
* 配置服务单例
18-
*
19-
* @static
20-
*/
21-
public static getInstance(): CoreTemplateReactConfig {
22-
if (!CoreTemplateReactConfig.instance) {
23-
CoreTemplateReactConfig.instance = new CoreTemplateReactConfig();
24-
}
25-
26-
return CoreTemplateReactConfig.instance;
27-
}
16+
* 配置服务单例
17+
*
18+
* @static
19+
*/
20+
public static getInstance(): CoreTemplateReactConfig {
21+
if (!CoreTemplateReactConfig.instance) {
22+
CoreTemplateReactConfig.instance = new CoreTemplateReactConfig();
23+
}
24+
25+
return CoreTemplateReactConfig.instance;
26+
}
2827

2928
/**
3029
* 获取解析的原始数据结构配置
@@ -68,8 +67,7 @@ class CoreTemplateReactConfig implements ICoreTemplate {
6867
*/
6968
public getConfig(): any {
7069
if (!this.configData) {
71-
this.configData =
72-
`import React, { lazy } from 'react';
70+
this.configData = `import React, { lazy } from 'react';
7371
import { BrowserRouterProps } from 'react-router-dom';
7472
import dashboard from './modules/dashboard';
7573
import list from './modules/list';
@@ -135,7 +133,7 @@ class CoreTemplateReactConfig implements ICoreTemplate {
135133
return this.configData;
136134
}
137135

138-
private static instance: CoreTemplateReactConfig;
136+
private static instance: CoreTemplateReactConfig;
139137
}
140138

141139
export default CoreTemplateReactConfig.getInstance();

templates/react-lite/.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# indicate this is the root of the project
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[*.md]
13+
trim_trailing_whitespace = false

0 commit comments

Comments
 (0)