@playcraft/build 0.2.2 → 0.2.5

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.
@@ -16,10 +16,18 @@ export declare class BaseBuilder {
16
16
  private options;
17
17
  private playableScriptsConfig?;
18
18
  private usePlayableScripts?;
19
+ private onInjectChannelArtifact?;
19
20
  private _metadata?;
20
21
  constructor(projectDir: string, options: BaseBuildOptions, extraOptions?: {
21
22
  playableScriptsConfig?: PlayableScriptsConfig;
22
23
  usePlayableScripts?: boolean;
24
+ onInjectChannelArtifact?: (artifact: {
25
+ channel: string;
26
+ files: Array<{
27
+ filePath: string;
28
+ fileName: string;
29
+ }>;
30
+ }) => Promise<void>;
23
31
  });
24
32
  /**
25
33
  * 获取构建元数据(在 build() 完成后可用)
@@ -18,6 +18,7 @@ export class BaseBuilder {
18
18
  this.options = options;
19
19
  this.playableScriptsConfig = extraOptions?.playableScriptsConfig;
20
20
  this.usePlayableScripts = extraOptions?.usePlayableScripts;
21
+ this.onInjectChannelArtifact = extraOptions?.onInjectChannelArtifact;
21
22
  console.log(`[BaseBuilder] 初始化: projectDir=${projectDir}, outputDir=${options.outputDir}`);
22
23
  }
23
24
  /**
@@ -44,7 +45,7 @@ export class BaseBuilder {
44
45
  break;
45
46
  }
46
47
  case 'playable-scripts': {
47
- const adapter = new PlayableScriptsAdapter(this.projectDir, this.options, this.playableScriptsConfig);
48
+ const adapter = new PlayableScriptsAdapter(this.projectDir, this.options, this.playableScriptsConfig, { onInjectChannelArtifact: this.onInjectChannelArtifact });
48
49
  result = await adapter.baseBuild();
49
50
  break;
50
51
  }
@@ -11,13 +11,25 @@ import type { BaseBuildOptions, BaseBuildOutput, PlayableScriptsConfig } from '.
11
11
  *
12
12
  * 注意:产物已是最终格式(单 HTML),无需 ViteBuilder 二次处理
13
13
  */
14
+ export type InjectChannelArtifact = {
15
+ channel: string;
16
+ files: Array<{
17
+ filePath: string;
18
+ fileName: string;
19
+ }>;
20
+ };
14
21
  export declare class PlayableScriptsAdapter {
15
22
  private projectDir;
16
23
  private options;
17
24
  private config?;
18
25
  private projectBuildsConfig?;
26
+ private onInjectChannelArtifact?;
27
+ private readonly reportedInjectChannels;
28
+ private injectReportChain;
19
29
  private static readonly DEFAULTS;
20
- constructor(projectDir: string, options: BaseBuildOptions, config?: PlayableScriptsConfig);
30
+ constructor(projectDir: string, options: BaseBuildOptions, config?: PlayableScriptsConfig, hooks?: {
31
+ onInjectChannelArtifact?: (artifact: InjectChannelArtifact) => Promise<void>;
32
+ });
21
33
  private loadProjectBuildsConfig;
22
34
  private resolveObfuscateLevel;
23
35
  private resolveFflateCompression;
@@ -59,8 +71,9 @@ export declare class PlayableScriptsAdapter {
59
71
  */
60
72
  private mergeConfig;
61
73
  /**
62
- * 将 PlayCraft 配置转换为 playable-scripts CLI 参数
63
- * ★ 始终显式传递 --obfuscate-level 和 --fflate-compression
74
+ * 将 PlayCraft 配置转换为 playable-scripts CLI 参数。
75
+ * 非注入模式显式传递 --obfuscate-level 和 --fflate-compression。
76
+ * 注入模式不传,这两项留在项目 builds.config.js。
64
77
  */
65
78
  private buildCLIArgs;
66
79
  private logBuildParams;
@@ -218,6 +231,11 @@ export declare class PlayableScriptsAdapter {
218
231
  private pickArtifactForChannel;
219
232
  private reportPathForChannel;
220
233
  private saveBuildMetadata;
234
+ /**
235
+ * 注入模式在 builds 进行中扫描产物。文件名带上渠道标识且大小大于 0,即视为该渠道完成。
236
+ */
237
+ private reportInjectChannels;
238
+ private reportInjectChannelsOnce;
221
239
  /**
222
240
  * 执行命令并设置超时
223
241
  */
@@ -62,23 +62,14 @@ function artifactMatchesChannel(fileName, channel) {
62
62
  * htmlBackedZip 分支完成(还要额外注入 config.json 的 orientation),不能在这里简单打包。
63
63
  */
64
64
  const ZIP_FALLBACK_CHANNELS = new Set(['remerge']);
65
- /**
66
- * PlayableScripts 构建工具适配器
67
- *
68
- * 职责:作为 PlayCraft 和 @playcraft/devkit 之间的中间层
69
- * - @playcraft/devkit 是 PlayCraft 平台内置的构建工具(已集成到 @playcraft/cli)
70
- * - 用户项目只需依赖 @playcraft/adsdk(运行时 SDK),不需要安装构建工具
71
- * - 通过平台内置的 devkit 调用 playable-scripts builds 命令(单渠道与批量统一)
72
- * - 将 playcraft.config.json 的参数转换为 playable-scripts CLI 参数
73
- * - 收集构建产物并标记 skipChannelBuild
74
- *
75
- * 注意:产物已是最终格式(单 HTML),无需 ViteBuilder 二次处理
76
- */
77
65
  export class PlayableScriptsAdapter {
78
- constructor(projectDir, options, config) {
66
+ constructor(projectDir, options, config, hooks) {
67
+ this.reportedInjectChannels = new Set();
68
+ this.injectReportChain = Promise.resolve();
79
69
  this.projectDir = projectDir;
80
70
  this.options = options;
81
71
  this.config = config;
72
+ this.onInjectChannelArtifact = hooks?.onInjectChannelArtifact;
82
73
  console.log(`[PlayableScriptsAdapter] 初始化: projectDir=${projectDir}`);
83
74
  if (config?.channels) {
84
75
  console.log(`[PlayableScriptsAdapter] 目标渠道: ${config.channels.join(', ')}`);
@@ -273,10 +264,14 @@ export class PlayableScriptsAdapter {
273
264
  if (this.config?.naming) {
274
265
  injections.push(` naming: ${JSON.stringify(this.config.naming)}`);
275
266
  }
276
- // 主题配置一律显式注入:统一走 builds 后,devkit 会读取项目 builds.config.js
267
+ // 注入模式保留项目自己的 themes,脚本按主题注入 window.__MINI_GAME_THEME__。
268
+ // 其它模式一律显式注入:统一走 builds 后,devkit 会读取项目 builds.config.js
277
269
  // 的 themes.enabled;若在未指定主题时不显式关闭,单渠道构建会意外产出全部主题变体。
278
- const availableThemes = await this.scanThemes();
279
- if (this.config?.themes?.whitelist && this.config.themes.whitelist.length > 0) {
270
+ const availableThemes = this.config?.injectPack ? [] : await this.scanThemes();
271
+ if (this.config?.injectPack) {
272
+ console.log('[PlayableScriptsAdapter] 注入打包:保留项目 themes 配置');
273
+ }
274
+ else if (this.config?.themes?.whitelist && this.config.themes.whitelist.length > 0) {
280
275
  // 用户指定了主题白名单:启用主题切换
281
276
  const validThemes = this.config.themes.whitelist.filter(t => availableThemes.includes(t));
282
277
  if (validThemes.length > 0) {
@@ -321,18 +316,21 @@ export class PlayableScriptsAdapter {
321
316
  }
322
317
  // ==================== 4. 构建 CLI 参数 ====================
323
318
  /**
324
- * 将 PlayCraft 配置转换为 playable-scripts CLI 参数
325
- * ★ 始终显式传递 --obfuscate-level 和 --fflate-compression
319
+ * 将 PlayCraft 配置转换为 playable-scripts CLI 参数。
320
+ * 非注入模式显式传递 --obfuscate-level 和 --fflate-compression。
321
+ * 注入模式不传,这两项留在项目 builds.config.js。
326
322
  */
327
323
  buildCLIArgs() {
328
324
  const config = this.config ?? {};
329
325
  const args = [];
330
- // 混淆等级:config 指定 > 项目 builds.config.js > PlayCraft 默认 4
331
- const obfuscateLevel = this.resolveObfuscateLevel();
332
- args.push('--obfuscate-level', `${obfuscateLevel}`);
333
- // fflate 压缩:config 指定 > 项目 builds.config.js > PlayCraft 默认 true
334
- const fflate = this.resolveFflateCompression();
335
- args.push('--fflate-compression', `${fflate}`);
326
+ if (!config.injectPack) {
327
+ // 混淆等级:config 指定 > 项目 builds.config.js > PlayCraft 默认 4
328
+ const obfuscateLevel = this.resolveObfuscateLevel();
329
+ args.push('--obfuscate-level', `${obfuscateLevel}`);
330
+ // fflate 压缩:config 指定 > 项目 builds.config.js > PlayCraft 默认 true
331
+ const fflate = this.resolveFflateCompression();
332
+ args.push('--fflate-compression', `${fflate}`);
333
+ }
336
334
  // 渠道列表:单渠道与多渠道统一走 `builds --channels <...>`
337
335
  // 缺失时退回 validation.channel(沙箱 runner / worker / CLI 都会传本次渠道);
338
336
  // 仍为空说明调用方没给渠道 —— devkit 会按项目 builds.config.js 构建**全部**渠道,
@@ -360,8 +358,8 @@ export class PlayableScriptsAdapter {
360
358
  args.push('--google-play-url', config.storeUrls.android);
361
359
  }
362
360
  }
363
- // 主题配置
364
- if (config.themes?.enabled && config.themes.whitelist && config.themes.whitelist.length > 0) {
361
+ // 主题配置。注入模式不传 --themes,主题列表留在项目 builds.config.js。
362
+ if (!config.injectPack && config.themes?.enabled && config.themes.whitelist && config.themes.whitelist.length > 0) {
365
363
  args.push('--themes', config.themes.whitelist.join(','));
366
364
  }
367
365
  // 并发数
@@ -371,19 +369,21 @@ export class PlayableScriptsAdapter {
371
369
  // 输出目录 - 统一使用 dist,与 PlayCraft 标准一致
372
370
  const outputDir = config.outputDir ?? 'dist';
373
371
  args.push('--out-dir', outputDir);
374
- // ZIP 输出:显式 config.zip、Facebook(devkit 下恒为 ZIP)或构建请求 format=zip
375
- const isZipFormat = this.isZipOutput();
376
- if (isZipFormat) {
372
+ // ZIP 输出:显式 config.zip、Facebook(devkit 下恒为 ZIP)或构建请求 format=zip。
373
+ // 注入模式不传 --zip,各渠道格式由打包脚本决定,平台再按产物检查。
374
+ const isZipFormat = config.injectPack ? false : this.isZipOutput();
375
+ if (!config.injectPack && isZipFormat) {
377
376
  args.push('--zip');
378
377
  }
379
- // 自定义文件名格式 - 根据平台使用 PlayCraft 标准命名
378
+ // 自定义文件名格式 - 根据平台使用 PlayCraft 标准命名。
379
+ // 注入模式不再因为 zip 把名字收成 playable。
380
380
  const platformFilename = this.getPlatformFilename(isZipFormat);
381
381
  const filename = config.filename || platformFilename;
382
382
  if (filename) {
383
383
  args.push('--filename', filename);
384
384
  }
385
- // 所有渠道输出为 ZIP
386
- if (config.isChannelFold2zip) {
385
+ // 所有渠道输出为 ZIP。注入模式不传,避免一次调用把全部渠道收成同一种 zip。
386
+ if (!config.injectPack && config.isChannelFold2zip) {
387
387
  args.push('--is-channel-fold2zip');
388
388
  }
389
389
  return args;
@@ -514,7 +514,7 @@ export class PlayableScriptsAdapter {
514
514
  // 扫描可用主题,命中白名单时追加 -t(devkit 的主题过滤参数)
515
515
  const availableThemes = await this.scanThemes();
516
516
  let selectedThemes = [];
517
- if (config.themes?.whitelist && config.themes.whitelist.length > 0) {
517
+ if (!config.injectPack && config.themes?.whitelist && config.themes.whitelist.length > 0) {
518
518
  // 使用配置中指定的主题白名单,清理可能的引号
519
519
  const cleanWhitelist = config.themes.whitelist.map(t => t.replace(/^["']|["']$/g, ''));
520
520
  selectedThemes = cleanWhitelist.filter(t => availableThemes.includes(t));
@@ -526,6 +526,9 @@ export class PlayableScriptsAdapter {
526
526
  args.push('-t', selectedThemes.join(','));
527
527
  console.log(`[PlayableScriptsAdapter] 构建主题: ${selectedThemes.join(', ')}`);
528
528
  }
529
+ else if (config.injectPack) {
530
+ console.log('[PlayableScriptsAdapter] 注入打包:不传 -t,主题由项目 builds.config.js 决定');
531
+ }
529
532
  else {
530
533
  // 未指定主题:使用 devkit 默认行为(当前 index.ts 指向的主题)
531
534
  console.log(`[PlayableScriptsAdapter] 使用当前默认主题 (src/theme/index.ts)`);
@@ -549,14 +552,17 @@ export class PlayableScriptsAdapter {
549
552
  NODE_ENV: 'production',
550
553
  CI: 'true',
551
554
  },
555
+ onPoll: config.injectPack ? () => this.reportInjectChannels() : undefined,
552
556
  });
553
557
  console.log('[PlayableScriptsAdapter] playable-scripts 构建命令执行完成');
554
558
  }
555
559
  catch (error) {
560
+ await this.reportInjectChannels();
556
561
  console.error('[PlayableScriptsAdapter] ❌ playable-scripts 构建失败:');
557
562
  console.error(error.message || error);
558
563
  throw error;
559
564
  }
565
+ await this.reportInjectChannels();
560
566
  // 检查输出目录是否存在
561
567
  const outputDir = config.outputDir ?? 'dist';
562
568
  const fullOutputDir = path.isAbsolute(outputDir)
@@ -1537,6 +1543,55 @@ export class PlayableScriptsAdapter {
1537
1543
  console.log(`[PlayableScriptsAdapter] 构建完成(buildTool: playable-scripts, skipChannelBuild: true)`);
1538
1544
  }
1539
1545
  // ==================== 工具方法 ====================
1546
+ /**
1547
+ * 注入模式在 builds 进行中扫描产物。文件名带上渠道标识且大小大于 0,即视为该渠道完成。
1548
+ */
1549
+ async reportInjectChannels() {
1550
+ if (!this.config?.injectPack || !this.onInjectChannelArtifact)
1551
+ return;
1552
+ const run = this.injectReportChain.then(() => this.reportInjectChannelsOnce());
1553
+ this.injectReportChain = run.catch(() => undefined);
1554
+ await run;
1555
+ }
1556
+ async reportInjectChannelsOnce() {
1557
+ const hook = this.onInjectChannelArtifact;
1558
+ if (!hook)
1559
+ return;
1560
+ const outputDir = this.config?.outputDir ?? 'dist';
1561
+ const fullOutputDir = path.isAbsolute(outputDir) ? outputDir : path.join(this.projectDir, outputDir);
1562
+ const outputs = [];
1563
+ try {
1564
+ await this.scanOutputDir(fullOutputDir, fullOutputDir, outputs);
1565
+ }
1566
+ catch {
1567
+ return;
1568
+ }
1569
+ const byChannel = new Map();
1570
+ for (const output of outputs) {
1571
+ if (!output.channel || output.channel === 'unknown' || this.reportedInjectChannels.has(output.channel))
1572
+ continue;
1573
+ try {
1574
+ const stat = await fs.stat(output.filePath);
1575
+ if (stat.size <= 0)
1576
+ continue;
1577
+ }
1578
+ catch {
1579
+ continue;
1580
+ }
1581
+ const list = byChannel.get(output.channel) ?? [];
1582
+ list.push({ filePath: output.filePath, fileName: output.fileName });
1583
+ byChannel.set(output.channel, list);
1584
+ }
1585
+ for (const [channel, files] of byChannel) {
1586
+ this.reportedInjectChannels.add(channel);
1587
+ try {
1588
+ await hook({ channel, files });
1589
+ }
1590
+ catch (error) {
1591
+ console.warn(`[PlayableScriptsAdapter] 渠道 ${channel} 完成回调失败: ${error.message}`);
1592
+ }
1593
+ }
1594
+ }
1540
1595
  /**
1541
1596
  * 执行命令并设置超时
1542
1597
  */
@@ -1544,7 +1599,16 @@ export class PlayableScriptsAdapter {
1544
1599
  return new Promise((resolve, reject) => {
1545
1600
  const controller = new AbortController();
1546
1601
  const { signal } = controller;
1602
+ const pollId = options.onPoll
1603
+ ? setInterval(() => {
1604
+ void options.onPoll?.().catch((error) => {
1605
+ console.warn(`[PlayableScriptsAdapter] 注入渠道扫描失败: ${error.message}`);
1606
+ });
1607
+ }, 2000)
1608
+ : undefined;
1547
1609
  const timeoutId = setTimeout(() => {
1610
+ if (pollId)
1611
+ clearInterval(pollId);
1548
1612
  controller.abort();
1549
1613
  reject(new Error(`[PlayableScriptsAdapter] 命令超时(${options.timeout / 1000}秒)\n` +
1550
1614
  `命令: ${cmd} ${args.join(' ')}\n` +
@@ -1593,6 +1657,8 @@ export class PlayableScriptsAdapter {
1593
1657
  });
1594
1658
  child.on('error', (error) => {
1595
1659
  clearTimeout(timeoutId);
1660
+ if (pollId)
1661
+ clearInterval(pollId);
1596
1662
  if (error.name === 'AbortError') {
1597
1663
  reject(new Error(`命令被中止: ${cmd} ${args.join(' ')}`));
1598
1664
  }
@@ -1604,6 +1670,8 @@ export class PlayableScriptsAdapter {
1604
1670
  });
1605
1671
  child.on('close', (code) => {
1606
1672
  clearTimeout(timeoutId);
1673
+ if (pollId)
1674
+ clearInterval(pollId);
1607
1675
  if (code === 0) {
1608
1676
  resolve();
1609
1677
  }
package/dist/types.d.ts CHANGED
@@ -158,6 +158,11 @@ export interface DetectionResult {
158
158
  export interface PlayableScriptsConfig {
159
159
  /** 目标渠道列表(覆盖项目自带的 builds.config.js 中的 channels) */
160
160
  channels?: string[];
161
+ /**
162
+ * 注入打包:一次 builds 打完全部渠道。
163
+ * 不覆盖项目 themes,不传 --zip;文件名仍传,但不会因为 zip 收成 playable。
164
+ */
165
+ injectPack?: boolean;
161
166
  /** 主题配置 */
162
167
  themes?: {
163
168
  enabled?: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@playcraft/build",
3
- "version": "0.2.2",
3
+ "version": "0.2.5",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",