@playcraft/cli 0.0.13 → 0.0.14

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,125 +0,0 @@
1
- import { build as viteBuild } from 'vite';
2
- import fs from 'fs/promises';
3
- import path from 'path';
4
- import { ViteConfigBuilder } from './vite/config-builder.js';
5
- import { PLATFORM_CONFIGS } from './vite/platform-configs.js';
6
- /**
7
- * Vite 构建器 - 使用 Vite 构建 Playable Ads
8
- *
9
- * 职责:
10
- * 1. 验证输入是有效的基础构建
11
- * 2. 创建 Vite 配置
12
- * 3. 执行 Vite 构建
13
- * 4. 验证输出大小
14
- * 5. 生成报告
15
- */
16
- export class ViteBuilder {
17
- baseBuildDir;
18
- options;
19
- sizeReport;
20
- constructor(baseBuildDir, options) {
21
- this.baseBuildDir = baseBuildDir;
22
- this.options = options;
23
- const platformConfig = PLATFORM_CONFIGS[options.platform];
24
- this.sizeReport = {
25
- engine: 0,
26
- assets: {},
27
- total: 0,
28
- limit: platformConfig.sizeLimit,
29
- };
30
- }
31
- /**
32
- * 执行构建
33
- */
34
- async build() {
35
- // 1. 验证输入
36
- await this.validateBaseBuild();
37
- // 2. 创建 Vite 配置
38
- const configBuilder = new ViteConfigBuilder(this.baseBuildDir, this.options.platform, this.options);
39
- const viteConfig = configBuilder.create();
40
- // 3. 执行 Vite 构建
41
- await viteBuild(viteConfig);
42
- // 4. 验证输出大小
43
- const outputPath = this.getOutputPath();
44
- await this.validateSize(outputPath);
45
- // 5. 生成报告
46
- this.generateReport(outputPath);
47
- return outputPath;
48
- }
49
- /**
50
- * 验证基础构建
51
- */
52
- async validateBaseBuild() {
53
- const requiredFiles = [
54
- 'index.html',
55
- 'config.json',
56
- '__start__.js',
57
- ];
58
- const missingFiles = [];
59
- for (const file of requiredFiles) {
60
- try {
61
- await fs.access(path.join(this.baseBuildDir, file));
62
- }
63
- catch (error) {
64
- missingFiles.push(file);
65
- }
66
- }
67
- if (missingFiles.length > 0) {
68
- throw new Error(`基础构建产物缺少必需文件: ${missingFiles.join(', ')}\n` +
69
- `请确保输入目录包含完整的多文件构建产物。`);
70
- }
71
- }
72
- /**
73
- * 获取输出路径
74
- */
75
- getOutputPath() {
76
- const platformConfig = PLATFORM_CONFIGS[this.options.platform];
77
- const outputDir = this.options.outputDir || './dist';
78
- if (platformConfig.outputFormat === 'zip') {
79
- return path.join(outputDir, 'playable.zip');
80
- }
81
- return path.join(outputDir, platformConfig.outputFileName);
82
- }
83
- /**
84
- * 验证输出大小
85
- */
86
- async validateSize(outputPath) {
87
- try {
88
- const stats = await fs.stat(outputPath);
89
- this.sizeReport.total = stats.size;
90
- this.sizeReport.assets[path.basename(outputPath)] = stats.size;
91
- if (stats.size > this.sizeReport.limit) {
92
- const sizeMB = (stats.size / 1024 / 1024).toFixed(2);
93
- const limitMB = (this.sizeReport.limit / 1024 / 1024).toFixed(2);
94
- console.warn(`⚠️ 警告: 文件大小 ${sizeMB} MB 超过限制 ${limitMB} MB`);
95
- }
96
- }
97
- catch (error) {
98
- console.warn(`警告: 无法读取输出文件: ${outputPath}`);
99
- }
100
- }
101
- /**
102
- * 生成报告
103
- */
104
- generateReport(outputPath) {
105
- // 报告已在 validateSize 中生成
106
- // 这里可以添加额外的报告逻辑
107
- }
108
- /**
109
- * 获取大小报告
110
- */
111
- getSizeReport() {
112
- return this.sizeReport;
113
- }
114
- /**
115
- * 格式化字节数
116
- */
117
- formatBytes(bytes) {
118
- if (bytes === 0)
119
- return '0 Bytes';
120
- const k = 1024;
121
- const sizes = ['Bytes', 'KB', 'MB', 'GB'];
122
- const i = Math.floor(Math.log(bytes) / Math.log(k));
123
- return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i];
124
- }
125
- }