playcanvas-agent-bridge-cli 0.3.0 → 0.5.0
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 +182 -58
- package/README.zh-CN.md +175 -58
- package/dist/builder/manager.js +456 -0
- package/dist/cli.js +520 -87
- package/dist/config.js +1 -1
- package/dist/daemon/server.js +180 -6
- package/dist/daemon/target-registry.js +43 -3
- package/dist/shared/protocol.js +3 -1
- package/dist/snippets.js +726 -69
- package/dist/workspace/manager.js +1123 -0
- package/extension/icons/concepts/pcbridge-bridge.png +0 -0
- package/extension/icons/concepts/pcbridge-cube.png +0 -0
- package/extension/icons/concepts/pcbridge-p-active.png +0 -0
- package/extension/icons/concepts/pcbridge-p-arch.png +0 -0
- package/extension/icons/concepts/pcbridge-p-bridge.png +0 -0
- package/extension/icons/concepts/pcbridge-p-emblem.png +0 -0
- package/extension/icons/concepts/pcbridge-signal.png +0 -0
- package/extension/icons/icon-128.png +0 -0
- package/extension/icons/icon-16.png +0 -0
- package/extension/icons/icon-32.png +0 -0
- package/extension/icons/icon-48.png +0 -0
- package/extension/isolated.js +147 -15
- package/extension/main.js +757 -24
- package/extension/manifest.json +14 -2
- package/extension/popup.html +179 -56
- package/extension/popup.js +204 -62
- package/extension/service-worker.js +331 -17
- package/package.json +3 -1
- package/skills/claude/playcanvas-agent-bridge-cli/SKILL.md +93 -28
- package/skills/codex/playcanvas-agent-bridge-cli/SKILL.md +148 -57
- package/skills/cursor/playcanvas-agent-bridge-cli.mdc +56 -24
- package/skills/windsurf/playcanvas-agent-bridge-cli.md +55 -25
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { createReadStream, existsSync } from "node:fs";
|
|
3
|
+
import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
4
|
+
import { dirname, extname, join } from "node:path";
|
|
5
|
+
import { PutObjectCommand, S3Client, } from "@aws-sdk/client-s3";
|
|
6
|
+
import { parse as parseEnv } from "dotenv";
|
|
7
|
+
const OMITTED_ASSET_FIELDS = new Set([
|
|
8
|
+
"uniqueId",
|
|
9
|
+
"createdAt",
|
|
10
|
+
"modifiedAt",
|
|
11
|
+
"scope",
|
|
12
|
+
"user_id",
|
|
13
|
+
"source_asset_id",
|
|
14
|
+
"source",
|
|
15
|
+
"revision",
|
|
16
|
+
"region",
|
|
17
|
+
"task",
|
|
18
|
+
"path",
|
|
19
|
+
"has_thumbnail",
|
|
20
|
+
"thumbnail",
|
|
21
|
+
"thumbnails",
|
|
22
|
+
]);
|
|
23
|
+
const ENV_EXAMPLE = `# Copy to .env and fill in the values. A project .env overrides the workspace .env.\n\
|
|
24
|
+
PCBRIDGE_S3_ENDPOINT=https://s3.example.com\n\
|
|
25
|
+
PCBRIDGE_S3_REGION=us-east-1\n\
|
|
26
|
+
PCBRIDGE_S3_BUCKET=my-bucket\n\
|
|
27
|
+
PCBRIDGE_S3_ACCESS_KEY_ID=\n\
|
|
28
|
+
PCBRIDGE_S3_SECRET_ACCESS_KEY=\n\
|
|
29
|
+
# PCBRIDGE_S3_SESSION_TOKEN=\n\
|
|
30
|
+
PCBRIDGE_S3_PUBLIC_BASE_URL=https://cdn.example.com\n\
|
|
31
|
+
PCBRIDGE_S3_PREFIX=assets\n\
|
|
32
|
+
PCBRIDGE_S3_FORCE_PATH_STYLE=false\n`;
|
|
33
|
+
function md5(value) {
|
|
34
|
+
return createHash("md5").update(value).digest("hex");
|
|
35
|
+
}
|
|
36
|
+
function clone(value) {
|
|
37
|
+
return JSON.parse(JSON.stringify(value));
|
|
38
|
+
}
|
|
39
|
+
function envValue(env, ...names) {
|
|
40
|
+
for (const name of names) {
|
|
41
|
+
const value = env[name];
|
|
42
|
+
if (value && value.trim())
|
|
43
|
+
return value.trim();
|
|
44
|
+
}
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
function booleanValue(value, fallback = false) {
|
|
48
|
+
if (!value)
|
|
49
|
+
return fallback;
|
|
50
|
+
return ["1", "true", "yes", "on"].includes(value.toLowerCase());
|
|
51
|
+
}
|
|
52
|
+
function normalizePrefix(value) {
|
|
53
|
+
const parts = String(value || "assets")
|
|
54
|
+
.replaceAll("\\", "/")
|
|
55
|
+
.split("/")
|
|
56
|
+
.map((part) => part.trim())
|
|
57
|
+
.filter(Boolean);
|
|
58
|
+
if (parts.some((part) => part === "." || part === "..")) {
|
|
59
|
+
throw new Error("S3 upload prefix cannot contain . or .. path segments.");
|
|
60
|
+
}
|
|
61
|
+
return parts.join("/");
|
|
62
|
+
}
|
|
63
|
+
function safeSegment(value, fallback) {
|
|
64
|
+
const result = value
|
|
65
|
+
.normalize("NFKC")
|
|
66
|
+
.replace(/[<>:"/\\|?*\u0000-\u001f]/g, "-")
|
|
67
|
+
.replace(/[. ]+$/g, "")
|
|
68
|
+
.trim()
|
|
69
|
+
.slice(0, 120);
|
|
70
|
+
return result || fallback;
|
|
71
|
+
}
|
|
72
|
+
function timestamp() {
|
|
73
|
+
const date = new Date();
|
|
74
|
+
const part = (value) => String(value).padStart(2, "0");
|
|
75
|
+
return `${date.getFullYear()}_${part(date.getMonth() + 1)}_${part(date.getDate())}-` +
|
|
76
|
+
`${part(date.getHours())}_${part(date.getMinutes())}_${part(date.getSeconds())}`;
|
|
77
|
+
}
|
|
78
|
+
function contentType(path) {
|
|
79
|
+
const types = {
|
|
80
|
+
".bin": "application/octet-stream",
|
|
81
|
+
".css": "text/css",
|
|
82
|
+
".gif": "image/gif",
|
|
83
|
+
".glb": "model/gltf-binary",
|
|
84
|
+
".gltf": "model/gltf+json",
|
|
85
|
+
".jpeg": "image/jpeg",
|
|
86
|
+
".jpg": "image/jpeg",
|
|
87
|
+
".js": "text/javascript; charset=utf-8",
|
|
88
|
+
".json": "application/json; charset=utf-8",
|
|
89
|
+
".mp3": "audio/mpeg",
|
|
90
|
+
".mp4": "video/mp4",
|
|
91
|
+
".ogg": "audio/ogg",
|
|
92
|
+
".png": "image/png",
|
|
93
|
+
".wasm": "application/wasm",
|
|
94
|
+
".webm": "video/webm",
|
|
95
|
+
".webp": "image/webp",
|
|
96
|
+
};
|
|
97
|
+
return types[extname(path).toLowerCase()] || "application/octet-stream";
|
|
98
|
+
}
|
|
99
|
+
function publicObjectUrl(base, key) {
|
|
100
|
+
const encoded = key.split("/").map(encodeURIComponent).join("/");
|
|
101
|
+
return `${base.replace(/\/$/, "")}/${encoded}`;
|
|
102
|
+
}
|
|
103
|
+
function sanitizeAsset(asset) {
|
|
104
|
+
const result = clone(asset);
|
|
105
|
+
for (const field of OMITTED_ASSET_FIELDS)
|
|
106
|
+
delete result[field];
|
|
107
|
+
if (result.type === "material") {
|
|
108
|
+
result.data ||= {};
|
|
109
|
+
if (!result.data.shader)
|
|
110
|
+
result.data.shader = "blinn";
|
|
111
|
+
}
|
|
112
|
+
return result;
|
|
113
|
+
}
|
|
114
|
+
async function readEnvFile(path) {
|
|
115
|
+
if (!existsSync(path))
|
|
116
|
+
return {};
|
|
117
|
+
return parseEnv(await readFile(path, "utf8"));
|
|
118
|
+
}
|
|
119
|
+
async function loadS3Settings(rootDir, projectDirectory) {
|
|
120
|
+
const workspacePath = join(rootDir, ".env");
|
|
121
|
+
const projectPath = join(projectDirectory, ".env");
|
|
122
|
+
if (!existsSync(workspacePath) && !existsSync(projectPath)) {
|
|
123
|
+
await writeFile(join(rootDir, ".env.example"), ENV_EXAMPLE, { flag: "wx" }).catch(() => undefined);
|
|
124
|
+
}
|
|
125
|
+
const processValues = Object.fromEntries(Object.entries(process.env).filter((entry) => typeof entry[1] === "string"));
|
|
126
|
+
const workspaceValues = await readEnvFile(workspacePath);
|
|
127
|
+
const projectValues = await readEnvFile(projectPath);
|
|
128
|
+
const env = { ...processValues, ...workspaceValues, ...projectValues };
|
|
129
|
+
const source = existsSync(projectPath)
|
|
130
|
+
? "project"
|
|
131
|
+
: existsSync(workspacePath) ? "workspace" : "environment";
|
|
132
|
+
const endpoint = envValue(env, "PCBRIDGE_S3_ENDPOINT", "S3_ENDPOINT");
|
|
133
|
+
const region = envValue(env, "PCBRIDGE_S3_REGION", "AWS_REGION", "S3_REGION") || "us-east-1";
|
|
134
|
+
const bucket = envValue(env, "PCBRIDGE_S3_BUCKET", "S3_BUCKET");
|
|
135
|
+
const accessKeyId = envValue(env, "PCBRIDGE_S3_ACCESS_KEY_ID", "AWS_ACCESS_KEY_ID", "S3_ACCESS_KEY_ID");
|
|
136
|
+
const secretAccessKey = envValue(env, "PCBRIDGE_S3_SECRET_ACCESS_KEY", "AWS_SECRET_ACCESS_KEY", "S3_SECRET_ACCESS_KEY");
|
|
137
|
+
const sessionToken = envValue(env, "PCBRIDGE_S3_SESSION_TOKEN", "AWS_SESSION_TOKEN", "S3_SESSION_TOKEN");
|
|
138
|
+
if (!bucket)
|
|
139
|
+
throw new Error("Missing PCBRIDGE_S3_BUCKET in the project or workspace .env file.");
|
|
140
|
+
if (!accessKeyId || !secretAccessKey) {
|
|
141
|
+
throw new Error("Missing PCBRIDGE_S3_ACCESS_KEY_ID or PCBRIDGE_S3_SECRET_ACCESS_KEY in the project or workspace .env file.");
|
|
142
|
+
}
|
|
143
|
+
const configuredBase = envValue(env, "PCBRIDGE_S3_PUBLIC_BASE_URL", "S3_PUBLIC_BASE_URL");
|
|
144
|
+
let publicBaseUrl = configuredBase;
|
|
145
|
+
if (!publicBaseUrl && endpoint)
|
|
146
|
+
publicBaseUrl = `${endpoint.replace(/\/$/, "")}/${bucket}`;
|
|
147
|
+
if (!publicBaseUrl)
|
|
148
|
+
publicBaseUrl = `https://${bucket}.s3.${region}.amazonaws.com`;
|
|
149
|
+
return {
|
|
150
|
+
client: new S3Client({
|
|
151
|
+
region,
|
|
152
|
+
endpoint,
|
|
153
|
+
forcePathStyle: booleanValue(envValue(env, "PCBRIDGE_S3_FORCE_PATH_STYLE", "S3_FORCE_PATH_STYLE"), false),
|
|
154
|
+
credentials: { accessKeyId, secretAccessKey, sessionToken },
|
|
155
|
+
}),
|
|
156
|
+
bucket,
|
|
157
|
+
defaultPrefix: normalizePrefix(envValue(env, "PCBRIDGE_S3_PREFIX", "S3_PREFIX")),
|
|
158
|
+
publicBaseUrl,
|
|
159
|
+
source,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
function descriptorFrom(value) {
|
|
163
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
164
|
+
throw new Error("PlayCanvas returned an invalid Template dependency descriptor.");
|
|
165
|
+
}
|
|
166
|
+
const candidate = value;
|
|
167
|
+
if (!candidate.template || !Array.isArray(candidate.assets)) {
|
|
168
|
+
throw new Error("PlayCanvas returned an incomplete Template dependency descriptor.");
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
template: candidate.template,
|
|
172
|
+
assets: candidate.assets,
|
|
173
|
+
scripts: Array.isArray(candidate.scripts) ? candidate.scripts : [],
|
|
174
|
+
childTemplateIds: Array.isArray(candidate.childTemplateIds) ? candidate.childTemplateIds.map(String) : [],
|
|
175
|
+
projectUrl: typeof candidate.projectUrl === "string" ? candidate.projectUrl : null,
|
|
176
|
+
sceneUrl: typeof candidate.sceneUrl === "string" ? candidate.sceneUrl : null,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
export class BuilderManager {
|
|
180
|
+
rootDir;
|
|
181
|
+
#requestTarget;
|
|
182
|
+
#workspace;
|
|
183
|
+
#jobs = new Map();
|
|
184
|
+
constructor(options) {
|
|
185
|
+
this.rootDir = options.rootDir;
|
|
186
|
+
this.#requestTarget = options.requestTarget;
|
|
187
|
+
this.#workspace = options.workspace;
|
|
188
|
+
}
|
|
189
|
+
start(info, templateAssetId, options = {}) {
|
|
190
|
+
const now = new Date().toISOString();
|
|
191
|
+
const job = {
|
|
192
|
+
id: `build_${randomUUID()}`,
|
|
193
|
+
target: info.id,
|
|
194
|
+
templateAssetId,
|
|
195
|
+
state: "queued",
|
|
196
|
+
message: "Queued",
|
|
197
|
+
completedFiles: 0,
|
|
198
|
+
totalFiles: 0,
|
|
199
|
+
createdAt: now,
|
|
200
|
+
updatedAt: now,
|
|
201
|
+
outputPath: null,
|
|
202
|
+
publicUrl: null,
|
|
203
|
+
configSource: null,
|
|
204
|
+
error: null,
|
|
205
|
+
info,
|
|
206
|
+
options,
|
|
207
|
+
};
|
|
208
|
+
this.#jobs.set(job.id, job);
|
|
209
|
+
while (this.#jobs.size > 100)
|
|
210
|
+
this.#jobs.delete(this.#jobs.keys().next().value);
|
|
211
|
+
void this.#run(job);
|
|
212
|
+
return this.#publicJob(job);
|
|
213
|
+
}
|
|
214
|
+
get(jobId) {
|
|
215
|
+
const job = this.#jobs.get(jobId);
|
|
216
|
+
return job ? this.#publicJob(job) : null;
|
|
217
|
+
}
|
|
218
|
+
#update(job, values) {
|
|
219
|
+
Object.assign(job, values, { updatedAt: new Date().toISOString() });
|
|
220
|
+
}
|
|
221
|
+
#publicJob(job) {
|
|
222
|
+
const { info: _info, options: _options, ...status } = job;
|
|
223
|
+
return status;
|
|
224
|
+
}
|
|
225
|
+
async #descriptor(info, assetId) {
|
|
226
|
+
const response = await this.#requestTarget(info.id, "bridge:collectTemplateDependencies", { assetId }, 60000);
|
|
227
|
+
if (!response.ok)
|
|
228
|
+
throw new Error(response.error.message);
|
|
229
|
+
return descriptorFrom(response.data);
|
|
230
|
+
}
|
|
231
|
+
async #cacheRemoteFile(info, projectDirectory, assetId, filename, url, expectedHash) {
|
|
232
|
+
const path = join(projectDirectory, "tmp", "cache", "assets", safeSegment(assetId, "asset"), safeSegment(filename, "file.bin"));
|
|
233
|
+
if (existsSync(path)) {
|
|
234
|
+
const content = await readFile(path);
|
|
235
|
+
const hash = md5(content);
|
|
236
|
+
if (!expectedHash || hash === expectedHash)
|
|
237
|
+
return { path, hash };
|
|
238
|
+
}
|
|
239
|
+
const response = await this.#requestTarget(info.id, "bridge:readAssetResource", { assetId, url }, 120000);
|
|
240
|
+
if (!response.ok)
|
|
241
|
+
throw new Error(response.error.message);
|
|
242
|
+
const data = response.data;
|
|
243
|
+
const base64 = typeof data.base64 === "string" ? data.base64 : "";
|
|
244
|
+
if (!base64)
|
|
245
|
+
throw new Error(`Asset ${assetId} resource ${filename} returned no data.`);
|
|
246
|
+
const content = Buffer.from(base64, "base64");
|
|
247
|
+
const hash = md5(content);
|
|
248
|
+
if (expectedHash && hash !== expectedHash) {
|
|
249
|
+
throw new Error(`Asset ${assetId} resource ${filename} did not match its PlayCanvas MD5.`);
|
|
250
|
+
}
|
|
251
|
+
await mkdir(dirname(path), { recursive: true });
|
|
252
|
+
await writeFile(path, content);
|
|
253
|
+
return { path, hash };
|
|
254
|
+
}
|
|
255
|
+
async #run(job) {
|
|
256
|
+
let activeClient = null;
|
|
257
|
+
try {
|
|
258
|
+
this.#update(job, { state: "collecting", message: "Collecting Template dependencies" });
|
|
259
|
+
const descriptors = new Map();
|
|
260
|
+
const visit = async (assetId, stack) => {
|
|
261
|
+
if (descriptors.has(assetId))
|
|
262
|
+
return;
|
|
263
|
+
if (stack.has(assetId))
|
|
264
|
+
throw new Error(`Template dependency cycle detected at asset ${assetId}.`);
|
|
265
|
+
const nextStack = new Set(stack).add(assetId);
|
|
266
|
+
const descriptor = await this.#descriptor(job.info, assetId);
|
|
267
|
+
descriptors.set(assetId, descriptor);
|
|
268
|
+
for (const childId of descriptor.childTemplateIds)
|
|
269
|
+
await visit(childId, nextStack);
|
|
270
|
+
};
|
|
271
|
+
await visit(job.templateAssetId, new Set());
|
|
272
|
+
this.#update(job, { state: "syncing", message: "Synchronizing dependency files" });
|
|
273
|
+
const allAssetIds = new Set();
|
|
274
|
+
for (const descriptor of descriptors.values()) {
|
|
275
|
+
for (const asset of descriptor.assets)
|
|
276
|
+
allAssetIds.add(String(asset.id));
|
|
277
|
+
}
|
|
278
|
+
const prepared = await this.#workspace.prepareAssetFiles(job.info, [...allAssetIds]);
|
|
279
|
+
const preparedById = new Map(prepared.assets.map((asset) => [asset.id, asset]));
|
|
280
|
+
const settings = await loadS3Settings(this.rootDir, prepared.projectDirectory);
|
|
281
|
+
activeClient = settings.client;
|
|
282
|
+
job.configSource = settings.source;
|
|
283
|
+
const prefix = normalizePrefix(job.options.prefix || settings.defaultPrefix);
|
|
284
|
+
const uploads = new Map();
|
|
285
|
+
const built = new Map();
|
|
286
|
+
const buildTemplate = async (assetId) => {
|
|
287
|
+
const existing = built.get(assetId);
|
|
288
|
+
if (existing)
|
|
289
|
+
return existing;
|
|
290
|
+
const descriptor = descriptors.get(assetId);
|
|
291
|
+
if (!descriptor)
|
|
292
|
+
throw new Error(`Missing descriptor for Template ${assetId}.`);
|
|
293
|
+
for (const childId of descriptor.childTemplateIds)
|
|
294
|
+
await buildTemplate(childId);
|
|
295
|
+
const suffix = String(job.options.suffix || "").replaceAll("${time}", timestamp());
|
|
296
|
+
const buildName = safeSegment(`${descriptor.template.name}${suffix}`, `template-${assetId}`);
|
|
297
|
+
const buildRoot = join(prepared.tmpDirectory, "builds", buildName);
|
|
298
|
+
await mkdir(buildRoot, { recursive: true });
|
|
299
|
+
const rootData = {
|
|
300
|
+
projectUrl: descriptor.projectUrl,
|
|
301
|
+
sceneUrl: descriptor.sceneUrl,
|
|
302
|
+
template: clone(descriptor.template.data || {}),
|
|
303
|
+
assets: {},
|
|
304
|
+
};
|
|
305
|
+
const templateData = rootData.template;
|
|
306
|
+
const entities = templateData.entities && typeof templateData.entities === "object"
|
|
307
|
+
? templateData.entities
|
|
308
|
+
: {};
|
|
309
|
+
for (const entity of Object.values(entities)) {
|
|
310
|
+
const attributes = entity.components?.script;
|
|
311
|
+
const scripts = attributes?.scripts;
|
|
312
|
+
const handler = scripts?.sdsTinyRootHandler;
|
|
313
|
+
const values = handler?.attributes;
|
|
314
|
+
const childId = values && Number.isInteger(values.testTemplate)
|
|
315
|
+
? String(values.testTemplate)
|
|
316
|
+
: null;
|
|
317
|
+
if (!childId || !built.has(childId))
|
|
318
|
+
continue;
|
|
319
|
+
delete values.testTemplate;
|
|
320
|
+
values.type = "TinyApp";
|
|
321
|
+
values.tinyAppUrl = built.get(childId).publicUrl;
|
|
322
|
+
}
|
|
323
|
+
const assetOutput = rootData.assets;
|
|
324
|
+
const scriptIds = new Set(descriptor.scripts.map((script) => script.assetId));
|
|
325
|
+
for (const asset of descriptor.assets) {
|
|
326
|
+
const id = String(asset.id);
|
|
327
|
+
if (scriptIds.has(id) || asset.type === "script")
|
|
328
|
+
continue;
|
|
329
|
+
const output = sanitizeAsset(asset);
|
|
330
|
+
const originalFileUrl = asset.file?.url;
|
|
331
|
+
const originalFilename = asset.file?.filename;
|
|
332
|
+
const local = preparedById.get(id);
|
|
333
|
+
if (output.file && local?.path && local.filename) {
|
|
334
|
+
const key = `${prefix}/${buildName}/${id}/${safeSegment(local.filename, `asset-${id}`)}`;
|
|
335
|
+
output.file.url = publicObjectUrl(settings.publicBaseUrl, key);
|
|
336
|
+
uploads.set(key, {
|
|
337
|
+
key,
|
|
338
|
+
path: local.path,
|
|
339
|
+
contentType: contentType(local.path),
|
|
340
|
+
hash: local.hash || md5(await readFile(local.path)),
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
for (const variant of Object.values(output.file?.variants || {})) {
|
|
344
|
+
if (!variant.url || !variant.filename)
|
|
345
|
+
continue;
|
|
346
|
+
const cached = await this.#cacheRemoteFile(job.info, prepared.projectDirectory, id, variant.filename, variant.url, variant.hash);
|
|
347
|
+
const key = `${prefix}/${buildName}/${id}/${safeSegment(variant.filename, "variant.bin")}`;
|
|
348
|
+
variant.url = publicObjectUrl(settings.publicBaseUrl, key);
|
|
349
|
+
uploads.set(key, {
|
|
350
|
+
key,
|
|
351
|
+
path: cached.path,
|
|
352
|
+
contentType: contentType(cached.path),
|
|
353
|
+
hash: cached.hash,
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
const fontMaps = asset.type === "font" && Array.isArray(asset.data?.info &&
|
|
357
|
+
asset.data.info.maps)
|
|
358
|
+
? asset.data.info.maps
|
|
359
|
+
: [];
|
|
360
|
+
if (originalFileUrl && originalFilename && fontMaps.length > 1) {
|
|
361
|
+
for (let index = 1; index < fontMaps.length; index += 1) {
|
|
362
|
+
const filename = originalFilename.replace(/(\.[^.]*)$/, `${index}$1`);
|
|
363
|
+
const url = originalFileUrl.replace(/(\.[^.]*)$/, `${index}$1`);
|
|
364
|
+
const cached = await this.#cacheRemoteFile(job.info, prepared.projectDirectory, id, filename, url);
|
|
365
|
+
const key = `${prefix}/${buildName}/${id}/${safeSegment(filename, `font-map-${index}`)}`;
|
|
366
|
+
uploads.set(key, {
|
|
367
|
+
key,
|
|
368
|
+
path: cached.path,
|
|
369
|
+
contentType: contentType(cached.path),
|
|
370
|
+
hash: cached.hash,
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
assetOutput[id] = output;
|
|
375
|
+
}
|
|
376
|
+
let scriptText = "";
|
|
377
|
+
for (const script of descriptor.scripts) {
|
|
378
|
+
const local = preparedById.get(script.assetId);
|
|
379
|
+
if (!local?.path)
|
|
380
|
+
continue;
|
|
381
|
+
scriptText += `\n//-----script:${script.name}-----\n`;
|
|
382
|
+
scriptText += await readFile(local.path, "utf8");
|
|
383
|
+
scriptText += "\n";
|
|
384
|
+
}
|
|
385
|
+
if (scriptText.trim()) {
|
|
386
|
+
const scriptPath = join(buildRoot, "gamescript.js");
|
|
387
|
+
await writeFile(scriptPath, scriptText);
|
|
388
|
+
const key = `${prefix}/${buildName}/gamescript.js`;
|
|
389
|
+
rootData.scriptUrl = publicObjectUrl(settings.publicBaseUrl, key);
|
|
390
|
+
uploads.set(key, {
|
|
391
|
+
key,
|
|
392
|
+
path: scriptPath,
|
|
393
|
+
contentType: contentType(scriptPath),
|
|
394
|
+
hash: md5(scriptText),
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
const rootPath = join(buildRoot, "tinyapp.json");
|
|
398
|
+
const rootContent = `${JSON.stringify(rootData)}\n`;
|
|
399
|
+
await writeFile(rootPath, rootContent);
|
|
400
|
+
const key = `${prefix}/${buildName}/tinyapp.json`;
|
|
401
|
+
const result = { key, publicUrl: publicObjectUrl(settings.publicBaseUrl, key), buildName };
|
|
402
|
+
uploads.set(key, {
|
|
403
|
+
key,
|
|
404
|
+
path: rootPath,
|
|
405
|
+
contentType: contentType(rootPath),
|
|
406
|
+
hash: md5(rootContent),
|
|
407
|
+
});
|
|
408
|
+
built.set(assetId, result);
|
|
409
|
+
return result;
|
|
410
|
+
};
|
|
411
|
+
this.#update(job, { state: "preparing", message: "Preparing upload manifest" });
|
|
412
|
+
const root = await buildTemplate(job.templateAssetId);
|
|
413
|
+
job.totalFiles = uploads.size;
|
|
414
|
+
const uploadFiles = [...uploads.values()];
|
|
415
|
+
this.#update(job, { state: "uploading", message: `Uploading 0/${uploadFiles.length} files` });
|
|
416
|
+
const concurrency = Math.min(4, Math.max(1, uploadFiles.length));
|
|
417
|
+
let cursor = 0;
|
|
418
|
+
const worker = async () => {
|
|
419
|
+
while (cursor < uploadFiles.length) {
|
|
420
|
+
const file = uploadFiles[cursor++];
|
|
421
|
+
const info = await stat(file.path);
|
|
422
|
+
const input = {
|
|
423
|
+
Bucket: settings.bucket,
|
|
424
|
+
Key: file.key,
|
|
425
|
+
Body: createReadStream(file.path),
|
|
426
|
+
ContentLength: info.size,
|
|
427
|
+
ContentType: file.contentType,
|
|
428
|
+
Metadata: { "pcbridge-md5": file.hash },
|
|
429
|
+
};
|
|
430
|
+
await settings.client.send(new PutObjectCommand(input));
|
|
431
|
+
job.completedFiles += 1;
|
|
432
|
+
this.#update(job, {
|
|
433
|
+
message: `Uploading ${job.completedFiles}/${uploadFiles.length} files`,
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
};
|
|
437
|
+
await Promise.all(Array.from({ length: concurrency }, () => worker()));
|
|
438
|
+
this.#update(job, {
|
|
439
|
+
state: "completed",
|
|
440
|
+
message: `Uploaded ${uploadFiles.length} files`,
|
|
441
|
+
outputPath: join(prepared.tmpDirectory, "builds", root.buildName, "tinyapp.json"),
|
|
442
|
+
publicUrl: root.publicUrl,
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
catch (error) {
|
|
446
|
+
this.#update(job, {
|
|
447
|
+
state: "error",
|
|
448
|
+
message: "Build failed",
|
|
449
|
+
error: error instanceof Error ? error.message : String(error),
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
finally {
|
|
453
|
+
activeClient?.destroy();
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|