create-unibest 3.0.0 → 3.0.1

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.
@@ -1,133 +0,0 @@
1
- /* eslint-disable style/brace-style */
2
- import path from 'node:path'
3
- import process from 'node:process'
4
- import { fileURLToPath } from 'node:url'
5
- import fs from 'fs-extra'
6
- import { TemplateHandler } from './template'
7
-
8
- const __filename = fileURLToPath(import.meta.url)
9
- const __dirname = path.dirname(__filename)
10
-
11
- /**
12
- * 文件生成器类
13
- */
14
- export class ProjectGenerator {
15
- private templateHandler: TemplateHandler
16
- private projectName: string
17
- private projectPath: string
18
- private uiLibrary: string
19
- private useJs: boolean
20
- private useI18n: boolean
21
-
22
- constructor(
23
- projectName: string,
24
- options: {
25
- uiLibrary: string
26
- useJs: boolean
27
- useI18n: boolean
28
- },
29
- ) {
30
- this.templateHandler = new TemplateHandler()
31
- this.projectName = projectName
32
- this.projectPath = path.resolve(process.cwd(), projectName)
33
- this.uiLibrary = options.uiLibrary
34
- this.useJs = options.useJs
35
- this.useI18n = options.useI18n
36
- }
37
-
38
- /**
39
- * 生成项目
40
- */
41
- public async generate(): Promise<void> {
42
- // 检查项目目录是否已存在
43
- if (await fs.pathExists(this.projectPath)) {
44
- throw new Error(`项目目录 ${this.projectName} 已存在`)
45
- }
46
-
47
- try {
48
- // 1. 复制基础模板
49
- await this.copyBaseTemplate()
50
-
51
- // 2. 复制UI库特定模板
52
- await this.copyUiTemplate()
53
-
54
- // 3. 如果需要i18n,复制i18n模板
55
- if (this.useI18n) {
56
- await this.copyI18nTemplate()
57
- }
58
-
59
- // 4. 生成项目配置文件
60
- await this.generateConfigFiles()
61
-
62
- console.log(`✅ 项目 ${this.projectName} 创建成功!`)
63
- console.log(`📁 目录: ${this.projectPath}`)
64
- console.log(`💡 下一步: cd ${this.projectName} && pnpm install`)
65
- } catch (error) {
66
- // 出错时清理已创建的目录
67
- if (await fs.pathExists(this.projectPath)) {
68
- await fs.remove(this.projectPath)
69
- }
70
- throw error
71
- }
72
- }
73
-
74
- /**
75
- * 复制基础模板
76
- */
77
- private async copyBaseTemplate(): Promise<void> {
78
- const baseTemplatePath = this.templateHandler.getBaseTemplatePath()
79
- await this.templateHandler.copyTemplate(baseTemplatePath, this.projectPath, {
80
- projectName: this.projectName,
81
- useJs: this.useJs,
82
- useI18n: this.useI18n,
83
- uiLibrary: this.uiLibrary,
84
- })
85
- }
86
-
87
- /**
88
- * 复制UI库特定模板
89
- */
90
- private async copyUiTemplate(): Promise<void> {
91
- const uiTemplatePath = this.templateHandler.getUiTemplatePath(this.uiLibrary)
92
-
93
- // 检查UI库模板是否存在
94
- if (!(await fs.pathExists(uiTemplatePath))) {
95
- throw new Error(`不支持的UI库: ${this.uiLibrary}`)
96
- }
97
-
98
- // 复制TS或JS模板
99
- const languageDir = this.useJs ? 'js' : 'ts'
100
- const specificUiTemplatePath = path.join(uiTemplatePath, languageDir)
101
-
102
- if (await fs.pathExists(specificUiTemplatePath)) {
103
- await this.templateHandler.copyTemplate(specificUiTemplatePath, this.projectPath, {
104
- projectName: this.projectName,
105
- useJs: this.useJs,
106
- useI18n: this.useI18n,
107
- uiLibrary: this.uiLibrary,
108
- })
109
- }
110
- }
111
-
112
- /**
113
- * 复制i18n模板
114
- */
115
- private async copyI18nTemplate(): Promise<void> {
116
- const i18nTemplatePath = this.templateHandler.getI18nTemplatePath()
117
- await this.templateHandler.copyTemplate(i18nTemplatePath, this.projectPath, {
118
- projectName: this.projectName,
119
- useJs: this.useJs,
120
- useI18n: this.useI18n,
121
- uiLibrary: this.uiLibrary,
122
- })
123
- }
124
-
125
- /**
126
- * 生成项目配置文件
127
- */
128
- private async generateConfigFiles(): Promise<void> {
129
- // 这里可以根据需要生成或修改项目配置文件
130
- // 例如 package.json, tsconfig.json, vite.config.ts 等
131
- // 这个方法可以根据项目需求进行扩展
132
- }
133
- }
package/src/utils/help.ts DELETED
@@ -1,27 +0,0 @@
1
- import { color } from './color'
2
-
3
- /**
4
- * 打印帮助信息
5
- */
6
- export function printHelp(): void {
7
- console.log(color.green('\nunibest - 跨平台开发框架脚手架'))
8
- console.log('')
9
- console.log(color.blue('用法:'))
10
- console.log(' unibest <command> [options]')
11
- console.log('')
12
- console.log(color.blue('命令:'))
13
- console.log(' create <project-name> 创建新的unibest项目')
14
- console.log(' help 显示帮助信息')
15
- console.log(' version 显示版本信息')
16
- console.log('')
17
- console.log(color.blue('选项:'))
18
- console.log(' --ui, --ui-library 指定UI库')
19
- console.log(' --ts, --typescript 使用TypeScript(默认)')
20
- console.log(' --js, --javascript 使用JavaScript')
21
- console.log(' --i18n 启用i18n')
22
- console.log('')
23
- console.log(color.blue('示例:'))
24
- console.log(' unibest create my-project')
25
- console.log(' unibest create my-project --ui uv-ui --ts --i18n')
26
- console.log(' unibest create my-project --ui wot-ui --js')
27
- }
@@ -1,31 +0,0 @@
1
- import { bold, green, red, yellow, cyan } from 'kolorist'
2
-
3
- /**
4
- * 日志工具类,提供不同级别的日志输出
5
- */
6
- export const logger = {
7
- /** 普通信息日志 */
8
- info: (message: string) => {
9
- console.log(`[${cyan('INFO')}] ${message}`)
10
- },
11
-
12
- /** 成功日志 */
13
- success: (message: string) => {
14
- console.log(`[${green('SUCCESS')}] ${bold(message)}`)
15
- },
16
-
17
- /** 错误日志 */
18
- error: (message: string) => {
19
- console.error(`[${red('ERROR')}] ${bold(message)}`)
20
- },
21
-
22
- /** 警告日志 */
23
- warn: (message: string) => {
24
- console.log(`[${yellow('WARN')}] ${message}`)
25
- },
26
-
27
- /** 提示日志 */
28
- tip: (message: string) => {
29
- console.log(`[${cyan('TIP')}] ${message}`)
30
- },
31
- }
@@ -1,16 +0,0 @@
1
- import { readFileSync, writeFileSync } from 'node:fs'
2
- import { join } from 'node:path'
3
-
4
- function replaceContent(filePath: string, projectName: string, version: string) {
5
- const fileContent = JSON.parse(readFileSync(filePath, 'utf8'))
6
- fileContent.name = projectName
7
- fileContent.version = version
8
- writeFileSync(filePath, JSON.stringify(fileContent, null, 2))
9
- }
10
-
11
- export function replacePackageJson(root: string, name: string, version: string) {
12
- const projectName = name.toLocaleLowerCase().replace(/\s/g, '-')
13
- const pkgPath = join(root, 'package.json')
14
-
15
- replaceContent(pkgPath, projectName, version)
16
- }
@@ -1,96 +0,0 @@
1
- /* eslint-disable style/arrow-parens */
2
- /* eslint-disable antfu/if-newline */
3
- /* eslint-disable style/brace-style */
4
- import path from 'node:path'
5
- import { fileURLToPath } from 'node:url'
6
- import ejs from 'ejs'
7
- import fs from 'fs-extra'
8
-
9
- const __filename = fileURLToPath(import.meta.url)
10
- const __dirname = path.dirname(__filename)
11
-
12
- /**
13
- * 模板处理工具类
14
- */
15
- export class TemplateHandler {
16
- private templateRoot: string
17
-
18
- constructor() {
19
- this.templateRoot = path.resolve(__dirname, '../../templates')
20
- }
21
-
22
- /**
23
- * 获取基础模板路径
24
- */
25
- public getBaseTemplatePath(): string {
26
- return path.join(this.templateRoot, 'base')
27
- }
28
- /**
29
- * 获取UI库模板路径
30
- */
31
- public getUiTemplatePath(uiLibrary: string): string {
32
- return path.join(this.templateRoot, 'ui-templates', uiLibrary)
33
- }
34
-
35
- /**
36
- * 获取i18n模板路径
37
- */
38
- public getI18nTemplatePath(): string {
39
- return path.join(this.templateRoot, 'i18n')
40
- }
41
-
42
- /**
43
- * 渲染EJS模板
44
- */
45
- public async renderTemplate(templatePath: string, data: Record<string, any>): Promise<string> {
46
- const templateContent = await fs.readFile(templatePath, 'utf-8')
47
- return ejs.render(templateContent, data, {}) // 添加空对象作为第三个参数
48
- }
49
-
50
- /**
51
- * 复制模板文件到目标路径
52
- */
53
- public async copyTemplate(sourceDir: string, targetDir: string, data?: Record<string, any>): Promise<void> {
54
- // 确保目标目录存在
55
- await fs.ensureDir(targetDir)
56
-
57
- // 读取源目录中的所有文件
58
- const files = await fs.readdir(sourceDir)
59
-
60
- for (const file of files) {
61
- const sourcePath = path.join(sourceDir, file)
62
- const targetPath = path.join(targetDir, file)
63
- const stats = await fs.stat(sourcePath)
64
-
65
- if (stats.isDirectory()) {
66
- // 递归复制子目录
67
- await this.copyTemplate(sourcePath, targetPath, data)
68
- } else if (file.endsWith('.ejs')) {
69
- // 渲染EJS模板
70
- const renderedContent = await this.renderTemplate(sourcePath, data || {})
71
- // 移除.ejs扩展名
72
- const actualTargetPath = targetPath.replace('.ejs', '')
73
- await fs.writeFile(actualTargetPath, renderedContent, 'utf-8')
74
- } else {
75
- // 直接复制文件
76
- await fs.copyFile(sourcePath, targetPath)
77
- }
78
- }
79
- }
80
-
81
- /**
82
- * 获取所有支持的UI库列表
83
- */
84
- public async getSupportedUiLibraries(): Promise<string[]> {
85
- const uiTemplatesDir = path.join(this.templateRoot, 'ui-templates')
86
- const exists = await fs.pathExists(uiTemplatesDir)
87
- if (!exists) return []
88
-
89
- const dirs = await fs.readdir(uiTemplatesDir)
90
- return dirs.filter(async dir => {
91
- const dirPath = path.join(uiTemplatesDir, dir)
92
- const stats = await fs.stat(dirPath)
93
- return stats.isDirectory()
94
- })
95
- }
96
- }