Skip to content

Commit

Permalink
feat: add instruction creation template (#63)
Browse files Browse the repository at this point in the history
* feat: add instruction creation template

* 优化提示

* 对于终端输出格式进行优化,对于参数判断代码进行优化
  • Loading branch information
RSS1102 committed May 23, 2024
1 parent 63ea036 commit 1af052b
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/core/CoreGitDownloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export class CoreGitDownloader {
console.log(chalk.green('👉 开始构建,请稍侯...'));
console.log();
const spinner = ora('正在构建模板...').start();
console.log();

const { downloadUrl, fePermissionDownloadUrl, url } = templates[`${options.type || 'vue2'}`];
const executeDownloadUrl = Array.isArray(finalOptions.selectTypes) && options.type === 'vue3' ? (fePermissionDownloadUrl as string) : downloadUrl;
// 清除测试目录
Expand Down
83 changes: 78 additions & 5 deletions src/core/CoreIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@ import { CoreJsTransformInquirer } from './core-js-transform/CoreJsTransformInqu
import type { SupportedTemplateSize } from './CoreTemplate';
import { CoreLiteDownloader } from './core-lite/CoreLiteDownloader';


type CreatorOptions = {
name: string;
description: string;
type: 'vue2' | 'vue3' | 'react' | 'miniProgram' | 'mobileVue';
buildToolType: 'vite' | 'webpack';
template: 'lite' | 'all';
}

class Creator {
constructor() {
constructor(name: string, options: CreatorOptions, command: any) {
clear();
console.log('*****************************');
console.log(chalk.green(figlet.textSync('TDesign Starter', { horizontalLayout: 'full' })));
Expand All @@ -25,10 +34,74 @@ class Creator {

const spinner = ora('👉 检查构建环境...').start();


// 如果有name参数,直接下载模板
if (name) {
const answer: CreatorOptions = {
...options,
name,
};
let isValid = true;

outerLoop: for (const key of Object.keys(options)) {
switch (key) {
case 'description':
break;
case 'type':
if (!['vue2', 'vue3', 'react', 'miniProgram', 'mobileVue'].includes(options['type'])) {
spinner.fail(chalk.red('type 参数错误,请输入vue2 | vue3 | react | miniProgram | mobileVue'));
isValid = false;
break outerLoop;
}
break;
case 'template':
if (!['lite', 'all'].includes(options['template'])) {
spinner.fail(chalk.red('template 参数错误,请输入lite | all'));
isValid = false;
break outerLoop;
}
break;
case 'buildToolType':
if (!['vite', 'webpack'].includes(options['buildToolType'])) {
spinner.fail(chalk.red('buildToolType 参数错误,请输入vite | webpack'));
isValid = false;
break outerLoop;
}
break;
default:
ora().fail(chalk.red('命令无效'));
isValid = false;
break outerLoop;
}
}
if (!isValid) {
return
};

spinner.succeed(chalk.green('构建环境正常!'));
console.log();

if (['miniProgram', 'mobileVue'].includes(options?.type)) {
new CoreGitDownloader().syncDownload(options);
return;
}

switch (answer.template) {
case 'lite':
new CoreLiteDownloader().syncDownload(answer);
break;
default:
new CoreSelector().interactionsSelect(answer).then((contentAnswer) => {
new CoreGitDownloader().syncDownload(answer, contentAnswer);
});
}
return;
}

spinner.succeed(chalk.green('构建环境正常!'));
console.log();
this.init();

}

/**
Expand All @@ -41,11 +114,11 @@ class Creator {
// 1.基本配置数据获取
const answer = await new CoreInquirer().interactionsHandler();

if(['miniProgram', 'mobileVue'].includes(answer?.type)) {
if (['miniProgram', 'mobileVue'].includes(answer?.type)) {
await new CoreGitDownloader().syncDownload(answer);
return;
}

// 2.询问生成简化版还是自定义版本
const listOptions: { type: SupportedTemplateSize; name: string; description: string } = await new CoreLiteInquirer().interactionsHandler();

Expand All @@ -62,7 +135,7 @@ class Creator {
// 自定义版本处理逻辑
// 3-1.依据基本配置载下配置文件路由模板
const contentAnswer = await new CoreSelector().interactionsSelect(answer);

// 选择开发语言 javascript/typescript 【暂未发布】
// const languageAnswer = await new CoreJsTransformInquirer().interactionsHandler();
// const finalAnswer = { ...contentAnswer, ...languageAnswer };
Expand Down
2 changes: 2 additions & 0 deletions src/core/core-lite/CoreLiteDownloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class CoreLiteDownloader extends CoreGitDownloader {
console.log(chalk.green('👉 开始构建,请稍侯...'));
console.log();
const spinner = ora('正在构建模板...').start();
console.log();

// 清除测试目录
await this.clearTestFolder();
Expand Down Expand Up @@ -63,6 +64,7 @@ export class CoreLiteDownloader extends CoreGitDownloader {
const srcDir = pathResolve(path.posix.join('templates', options.buildToolType, copyFolderName));
try {
await fse.copy(srcDir, destDir);
console.log();
console.log(chalk.green('👉 生成代码完毕...'));
} catch (err) {
console.error(err);
Expand Down
19 changes: 15 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import chalk from 'chalk';
import Creater from './core/CoreIndex';
import Creator from './core/CoreIndex';
import { program } from 'commander';
import pkg from '../package.json';

program.version(chalk.green(`v${pkg.version}`), '-v, --version');

/**
* 初始化项目
* @description '--d', '--description', '项目描述'
* @description '--type', '--type', '代码版本 vue2 | vue3 | react | miniProgram | mobileVue'
* @description '--temp', '--template', '项目模版类型 lite | all'
* @description '--bt', '--buildToolType', '构建工具类型 vite | webpack'
*/
program
.command('init')
.command('init [templateName]')
.alias('i')
.description('欢迎使用 TDesign-Starter')
.action(() => {
new Creater();
.option('-d,--description <description>', 'description of a project', 'Base on tdesign-starter-cli')
.option('-type,--type <type>', 'Code version vue2 | vue3 | react | miniProgram | mobileVue ', 'vue2')
.option('-temp,--template <template>', 'Project template type: lite | all', 'lite')
.option('-bt,--buildToolType <buildToolType>', 'The construction tool for lite: vite | webpack', 'vite')
.action((name, options, command) => {
new Creator(name, options, command);
});

// 解析参数
Expand Down

0 comments on commit 1af052b

Please sign in to comment.