@playcraft/cli 0.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.
- package/README.md +12 -0
- package/dist/build-config.js +26 -0
- package/dist/commands/build.js +363 -0
- package/dist/commands/config.js +133 -0
- package/dist/commands/init.js +86 -0
- package/dist/commands/inspect.js +209 -0
- package/dist/commands/logs.js +121 -0
- package/dist/commands/start.js +284 -0
- package/dist/commands/status.js +106 -0
- package/dist/commands/stop.js +58 -0
- package/dist/config.js +31 -0
- package/dist/fs-handler.js +83 -0
- package/dist/index.js +200 -0
- package/dist/logger.js +122 -0
- package/dist/playable/base-builder.js +265 -0
- package/dist/playable/builder.js +1462 -0
- package/dist/playable/converter.js +150 -0
- package/dist/playable/index.js +3 -0
- package/dist/playable/platforms/base.js +12 -0
- package/dist/playable/platforms/facebook.js +37 -0
- package/dist/playable/platforms/index.js +24 -0
- package/dist/playable/platforms/snapchat.js +59 -0
- package/dist/playable/playable-builder.js +521 -0
- package/dist/playable/types.js +1 -0
- package/dist/playable/vite/config-builder.js +136 -0
- package/dist/playable/vite/platform-configs.js +102 -0
- package/dist/playable/vite/plugin-model-compression.js +63 -0
- package/dist/playable/vite/plugin-platform.js +65 -0
- package/dist/playable/vite/plugin-playcanvas.js +454 -0
- package/dist/playable/vite-builder.js +125 -0
- package/dist/port-utils.js +27 -0
- package/dist/process-manager.js +96 -0
- package/dist/server.js +128 -0
- package/dist/socket.js +117 -0
- package/dist/watcher.js +33 -0
- package/package.json +41 -0
- package/templates/playable-ad.html +59 -0
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import fs from 'fs/promises';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
5
|
+
const __dirname = path.dirname(__filename);
|
|
6
|
+
/**
|
|
7
|
+
* 基础构建器 - 生成可运行的多文件构建产物
|
|
8
|
+
*
|
|
9
|
+
* 职责:
|
|
10
|
+
* 1. 从源代码或构建产物加载项目
|
|
11
|
+
* 2. 确保所有必需文件存在且格式正确
|
|
12
|
+
* 3. 输出可直接运行的多文件版本
|
|
13
|
+
* 4. 不做任何内联或压缩
|
|
14
|
+
*/
|
|
15
|
+
export class BaseBuilder {
|
|
16
|
+
projectDir;
|
|
17
|
+
options;
|
|
18
|
+
constructor(projectDir, options) {
|
|
19
|
+
this.projectDir = projectDir;
|
|
20
|
+
this.options = options;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* 执行基础构建
|
|
24
|
+
*/
|
|
25
|
+
async build() {
|
|
26
|
+
// 1. 检测项目类型
|
|
27
|
+
const projectType = await this.detectProjectType();
|
|
28
|
+
if (projectType === 'official-build') {
|
|
29
|
+
// 官方构建产物 - 直接复制并验证
|
|
30
|
+
return await this.buildFromOfficial();
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
// 源代码 - 需要编译
|
|
34
|
+
throw new Error('源代码构建暂未实现,请使用官方构建产物。\n' +
|
|
35
|
+
'💡 推荐:先使用 PlayCanvas REST API 下载构建版本,然后再打包为 Playable Ad。');
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 检测项目类型
|
|
40
|
+
*/
|
|
41
|
+
async detectProjectType() {
|
|
42
|
+
// 检查是否是官方构建产物
|
|
43
|
+
const buildIndicators = [
|
|
44
|
+
path.join(this.projectDir, 'index.html'),
|
|
45
|
+
path.join(this.projectDir, 'config.json'),
|
|
46
|
+
];
|
|
47
|
+
try {
|
|
48
|
+
await fs.access(buildIndicators[0]);
|
|
49
|
+
await fs.access(buildIndicators[1]);
|
|
50
|
+
return 'official-build';
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
return 'source';
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* 从官方构建产物构建
|
|
58
|
+
*/
|
|
59
|
+
async buildFromOfficial() {
|
|
60
|
+
// 验证必需文件存在
|
|
61
|
+
await this.validateOfficialBuild();
|
|
62
|
+
// 创建输出目录
|
|
63
|
+
await fs.mkdir(this.options.outputDir, { recursive: true });
|
|
64
|
+
// 复制所有文件到输出目录
|
|
65
|
+
const files = await this.copyBuildFiles();
|
|
66
|
+
return {
|
|
67
|
+
outputDir: this.options.outputDir,
|
|
68
|
+
files,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* 验证官方构建产物
|
|
73
|
+
*/
|
|
74
|
+
async validateOfficialBuild() {
|
|
75
|
+
const requiredFiles = [
|
|
76
|
+
'index.html',
|
|
77
|
+
'config.json',
|
|
78
|
+
'__start__.js',
|
|
79
|
+
];
|
|
80
|
+
const missingFiles = [];
|
|
81
|
+
for (const file of requiredFiles) {
|
|
82
|
+
try {
|
|
83
|
+
await fs.access(path.join(this.projectDir, file));
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
missingFiles.push(file);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (missingFiles.length > 0) {
|
|
90
|
+
throw new Error(`官方构建产物缺少必需文件: ${missingFiles.join(', ')}\n` +
|
|
91
|
+
`请确保项目目录包含完整的构建产物。`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* 复制构建文件到输出目录
|
|
96
|
+
*/
|
|
97
|
+
async copyBuildFiles() {
|
|
98
|
+
const files = {
|
|
99
|
+
html: '',
|
|
100
|
+
engine: null,
|
|
101
|
+
config: '',
|
|
102
|
+
settings: null,
|
|
103
|
+
modules: null,
|
|
104
|
+
start: '',
|
|
105
|
+
scenes: [],
|
|
106
|
+
assets: [],
|
|
107
|
+
};
|
|
108
|
+
// 复制 index.html
|
|
109
|
+
const htmlPath = path.join(this.projectDir, 'index.html');
|
|
110
|
+
const outputHtmlPath = path.join(this.options.outputDir, 'index.html');
|
|
111
|
+
await fs.copyFile(htmlPath, outputHtmlPath);
|
|
112
|
+
files.html = outputHtmlPath;
|
|
113
|
+
// 复制 config.json
|
|
114
|
+
const configPath = path.join(this.projectDir, 'config.json');
|
|
115
|
+
const outputConfigPath = path.join(this.options.outputDir, 'config.json');
|
|
116
|
+
await fs.copyFile(configPath, outputConfigPath);
|
|
117
|
+
files.config = outputConfigPath;
|
|
118
|
+
// 读取 config.json 以获取场景信息
|
|
119
|
+
const configContent = await fs.readFile(configPath, 'utf-8');
|
|
120
|
+
const configJson = JSON.parse(configContent);
|
|
121
|
+
// 复制 __start__.js
|
|
122
|
+
const startPath = path.join(this.projectDir, '__start__.js');
|
|
123
|
+
const outputStartPath = path.join(this.options.outputDir, '__start__.js');
|
|
124
|
+
await fs.copyFile(startPath, outputStartPath);
|
|
125
|
+
files.start = outputStartPath;
|
|
126
|
+
// 复制 __settings__.js(如果存在)
|
|
127
|
+
const settingsPath = path.join(this.projectDir, '__settings__.js');
|
|
128
|
+
try {
|
|
129
|
+
await fs.access(settingsPath);
|
|
130
|
+
const outputSettingsPath = path.join(this.options.outputDir, '__settings__.js');
|
|
131
|
+
await fs.copyFile(settingsPath, outputSettingsPath);
|
|
132
|
+
files.settings = outputSettingsPath;
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
// __settings__.js 不是必需的
|
|
136
|
+
}
|
|
137
|
+
// 复制 __modules__.js(如果存在)
|
|
138
|
+
const modulesPath = path.join(this.projectDir, '__modules__.js');
|
|
139
|
+
try {
|
|
140
|
+
await fs.access(modulesPath);
|
|
141
|
+
const outputModulesPath = path.join(this.options.outputDir, '__modules__.js');
|
|
142
|
+
await fs.copyFile(modulesPath, outputModulesPath);
|
|
143
|
+
files.modules = outputModulesPath;
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
// __modules__.js 不是必需的
|
|
147
|
+
}
|
|
148
|
+
// 复制 PlayCanvas Engine(查找可能的文件名)
|
|
149
|
+
const engineNames = [
|
|
150
|
+
'playcanvas-stable.min.js',
|
|
151
|
+
'playcanvas.min.js',
|
|
152
|
+
'__lib__.js',
|
|
153
|
+
];
|
|
154
|
+
for (const engineName of engineNames) {
|
|
155
|
+
const enginePath = path.join(this.projectDir, engineName);
|
|
156
|
+
try {
|
|
157
|
+
await fs.access(enginePath);
|
|
158
|
+
const outputEnginePath = path.join(this.options.outputDir, engineName);
|
|
159
|
+
await fs.copyFile(enginePath, outputEnginePath);
|
|
160
|
+
files.engine = outputEnginePath;
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
// 继续尝试下一个
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
// 复制场景文件
|
|
168
|
+
if (configJson.scenes && Array.isArray(configJson.scenes)) {
|
|
169
|
+
for (const scene of configJson.scenes) {
|
|
170
|
+
if (scene.url && !scene.url.startsWith('data:')) {
|
|
171
|
+
const scenePath = path.join(this.projectDir, scene.url);
|
|
172
|
+
try {
|
|
173
|
+
await fs.access(scenePath);
|
|
174
|
+
const sceneDir = path.dirname(scene.url);
|
|
175
|
+
if (sceneDir && sceneDir !== '.') {
|
|
176
|
+
const outputSceneDir = path.join(this.options.outputDir, sceneDir);
|
|
177
|
+
await fs.mkdir(outputSceneDir, { recursive: true });
|
|
178
|
+
}
|
|
179
|
+
const outputScenePath = path.join(this.options.outputDir, scene.url);
|
|
180
|
+
await fs.copyFile(scenePath, outputScenePath);
|
|
181
|
+
files.scenes.push(outputScenePath);
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
console.warn(`警告: 场景文件不存在: ${scene.url}`);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
// 复制资产文件(从 config.json 中的 assets)
|
|
190
|
+
if (configJson.assets) {
|
|
191
|
+
const assetsDir = path.join(this.options.outputDir, 'files');
|
|
192
|
+
await fs.mkdir(assetsDir, { recursive: true });
|
|
193
|
+
for (const [assetId, assetData] of Object.entries(configJson.assets)) {
|
|
194
|
+
const asset = assetData;
|
|
195
|
+
if (asset.file && asset.file.url && !asset.file.url.startsWith('data:')) {
|
|
196
|
+
const assetPath = path.join(this.projectDir, asset.file.url);
|
|
197
|
+
try {
|
|
198
|
+
await fs.access(assetPath);
|
|
199
|
+
const assetDir = path.dirname(asset.file.url);
|
|
200
|
+
if (assetDir && assetDir !== '.') {
|
|
201
|
+
const outputAssetDir = path.join(this.options.outputDir, assetDir);
|
|
202
|
+
await fs.mkdir(outputAssetDir, { recursive: true });
|
|
203
|
+
}
|
|
204
|
+
const outputAssetPath = path.join(this.options.outputDir, asset.file.url);
|
|
205
|
+
await fs.copyFile(assetPath, outputAssetPath);
|
|
206
|
+
files.assets.push(outputAssetPath);
|
|
207
|
+
}
|
|
208
|
+
catch (error) {
|
|
209
|
+
// 资产文件可能不存在(可能是内联的)
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
// 复制 files/ 目录(如果存在)
|
|
215
|
+
const filesDir = path.join(this.projectDir, 'files');
|
|
216
|
+
try {
|
|
217
|
+
const filesDirStat = await fs.stat(filesDir);
|
|
218
|
+
if (filesDirStat.isDirectory()) {
|
|
219
|
+
const outputFilesDir = path.join(this.options.outputDir, 'files');
|
|
220
|
+
await this.copyDirectory(filesDir, outputFilesDir);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
catch (error) {
|
|
224
|
+
// files/ 目录可能不存在
|
|
225
|
+
}
|
|
226
|
+
// 复制 styles.css(如果存在)
|
|
227
|
+
const stylesPath = path.join(this.projectDir, 'styles.css');
|
|
228
|
+
try {
|
|
229
|
+
await fs.access(stylesPath);
|
|
230
|
+
const outputStylesPath = path.join(this.options.outputDir, 'styles.css');
|
|
231
|
+
await fs.copyFile(stylesPath, outputStylesPath);
|
|
232
|
+
}
|
|
233
|
+
catch (error) {
|
|
234
|
+
// styles.css 可能不存在
|
|
235
|
+
}
|
|
236
|
+
// 复制 manifest.json(如果存在)
|
|
237
|
+
const manifestPath = path.join(this.projectDir, 'manifest.json');
|
|
238
|
+
try {
|
|
239
|
+
await fs.access(manifestPath);
|
|
240
|
+
const outputManifestPath = path.join(this.options.outputDir, 'manifest.json');
|
|
241
|
+
await fs.copyFile(manifestPath, outputManifestPath);
|
|
242
|
+
}
|
|
243
|
+
catch (error) {
|
|
244
|
+
// manifest.json 可能不存在
|
|
245
|
+
}
|
|
246
|
+
return files;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* 递归复制目录
|
|
250
|
+
*/
|
|
251
|
+
async copyDirectory(src, dest) {
|
|
252
|
+
await fs.mkdir(dest, { recursive: true });
|
|
253
|
+
const entries = await fs.readdir(src, { withFileTypes: true });
|
|
254
|
+
for (const entry of entries) {
|
|
255
|
+
const srcPath = path.join(src, entry.name);
|
|
256
|
+
const destPath = path.join(dest, entry.name);
|
|
257
|
+
if (entry.isDirectory()) {
|
|
258
|
+
await this.copyDirectory(srcPath, destPath);
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
await fs.copyFile(srcPath, destPath);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|