@arcadia-eternity/content-sdk 4.6.0 → 4.7.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.
Files changed (72) hide show
  1. package/README.md +2 -2
  2. package/dist/author-api.d.ts +12 -2
  3. package/dist/author-api.js +11 -2
  4. package/dist/author-git.d.ts +63 -0
  5. package/dist/author-git.js +324 -0
  6. package/dist/author-reference.d.ts +15 -1
  7. package/dist/author-reference.js +20 -1
  8. package/dist/cli.js +5 -4
  9. package/dist/content-query.d.ts +15 -1
  10. package/dist/content-query.js +53 -11
  11. package/dist/content-workspace.d.ts +423 -3
  12. package/dist/content-workspace.js +194 -51
  13. package/dist/debug-environments.d.ts +393 -25
  14. package/dist/debug-environments.js +3 -42
  15. package/dist/demo.js +1 -1
  16. package/dist/effect-declarations.d.ts +14 -0
  17. package/dist/effect-declarations.js +103 -0
  18. package/dist/environment-host-base.d.ts +175 -0
  19. package/dist/environment-host-base.js +86 -0
  20. package/dist/environment-member-transfer.d.ts +5 -0
  21. package/dist/environment-member-transfer.js +95 -0
  22. package/dist/environment-rules.d.ts +728 -3
  23. package/dist/environment-rules.js +158 -74
  24. package/dist/environment-sharing.d.ts +51 -0
  25. package/dist/environment-sharing.js +321 -0
  26. package/dist/environment.d.ts +2 -0
  27. package/dist/environment.js +32 -9
  28. package/dist/jobs.d.ts +5 -0
  29. package/dist/jobs.js +59 -10
  30. package/dist/learnsets.d.ts +47 -0
  31. package/dist/learnsets.js +124 -0
  32. package/dist/lib/content.d.ts +1 -1
  33. package/dist/lib/content.js +26 -2
  34. package/dist/lib/presentation.js +5 -11
  35. package/dist/managed-projects.d.ts +6 -0
  36. package/dist/managed-projects.js +137 -45
  37. package/dist/managed-sources.d.ts +17 -0
  38. package/dist/managed-sources.js +28 -10
  39. package/dist/mcp-contracts.d.ts +405 -4
  40. package/dist/mcp-contracts.js +3 -1
  41. package/dist/mcp.js +23 -1
  42. package/dist/pack-project.d.ts +2 -1
  43. package/dist/pack-project.js +40 -5
  44. package/dist/prepared-document.d.ts +476 -0
  45. package/dist/prepared-document.js +47 -0
  46. package/dist/preview.js +5 -5
  47. package/dist/project.d.ts +3 -1
  48. package/dist/project.js +47 -4
  49. package/dist/rule-composition.d.ts +290 -0
  50. package/dist/rule-composition.js +267 -0
  51. package/dist/template/.github/scripts/release-payload.mjs +91 -0
  52. package/dist/template/.github/scripts/release-test-npm.mjs +27 -0
  53. package/dist/template/.github/scripts/verify-release.mjs +138 -0
  54. package/dist/template/.github/workflows/release-pack.yml +77 -0
  55. package/dist/template/README.md +15 -2
  56. package/dist/template/gitignore-template +7 -0
  57. package/dist/template/npm-lock-template.json +2319 -0
  58. package/dist/template/package.json +4 -2
  59. package/dist/template/skills/arcadia-content-authoring/references/tool-workflows.md +1 -1
  60. package/dist/template/skills/arcadia-content-git/SKILL.md +14 -0
  61. package/dist/template/skills/arcadia-content-release/SKILL.md +29 -0
  62. package/dist/template/test/pack.test.mjs +24 -5
  63. package/dist/testing.d.ts +1 -0
  64. package/dist/testing.js +26 -7
  65. package/dist/workspace-cli.js +2 -2
  66. package/dist/workspace-materials.d.ts +4 -1
  67. package/dist/workspace-materials.js +8 -2
  68. package/dist/workspace.d.ts +11 -0
  69. package/dist/workspace.js +191 -31
  70. package/package.json +16 -4
  71. package/release-payload.mjs +91 -0
  72. package/release-test-npm.mjs +27 -0
package/dist/workspace.js CHANGED
@@ -1,26 +1,56 @@
1
- import { createHash } from 'node:crypto';
1
+ import { sha256Hex, sha256StreamHex } from '@arcadia-eternity/const';
2
+ import { isContentLocaleField, contentLocaleCollection } from '@arcadia-eternity/schema';
2
3
  import { createReadStream } from 'node:fs';
3
4
  import { lstat, mkdir, readdir, readFile, realpath, unlink } from 'node:fs/promises';
4
- import { dirname, extname, isAbsolute, join, relative, sep } from 'node:path';
5
+ import { dirname, extname, isAbsolute, join, relative, resolve, sep } from 'node:path';
5
6
  import { EventEmitter } from 'node:events';
6
7
  import { isDeepStrictEqual } from 'node:util';
7
8
  import YAML from 'yaml';
9
+ import { valid, validRange } from 'semver';
8
10
  import chokidar from 'chokidar';
9
11
  import writeFileAtomic from 'write-file-atomic';
10
12
  import { resourceReferences, bindingPresentation, validResourceId, validAssetPath } from './lib/presentation.js';
11
13
  import { yamlSharing, recordSharing, changedSharedField, sharingSignature } from './lib/yaml-sharing.js';
12
14
  import { withSourceLock } from './source-lock.js';
13
15
  import { errorCode, errorMessage, isRecord } from './types.js';
14
- const ignored = new Set(['node_modules', 'dist', '.git', '.arcadia', '.DS_Store', 'Thumbs.db']);
15
- const kinds = ['species', 'skills', 'marks', 'behaviors', 'presentations', 'resources'];
16
- const contentKinds = ['species', 'skills', 'marks', 'behaviors'];
17
- const contentFilePattern = /^data\/(species|skills|marks|behaviors|presentations|resources)\/.+\.(json|ya?ml)$/;
16
+ export const desktopMetadataNames = new Set(['.DS_Store', 'Thumbs.db']);
17
+ const ignored = new Set(['node_modules', 'dist', '.git', '.arcadia', ...desktopMetadataNames]);
18
+ const kinds = [
19
+ 'species',
20
+ 'skills',
21
+ 'marks',
22
+ 'behaviors',
23
+ 'natures',
24
+ 'enhancements',
25
+ 'elements',
26
+ 'parameterGroups',
27
+ 'parameterProfiles',
28
+ 'parameterBindings',
29
+ 'ruleSets',
30
+ 'matchupTables',
31
+ 'presentations',
32
+ 'resources',
33
+ ];
34
+ const contentKinds = [
35
+ 'species',
36
+ 'skills',
37
+ 'marks',
38
+ 'behaviors',
39
+ 'natures',
40
+ 'enhancements',
41
+ 'elements',
42
+ 'parameterGroups',
43
+ 'parameterProfiles',
44
+ 'parameterBindings',
45
+ 'ruleSets',
46
+ 'matchupTables',
47
+ ];
48
+ const contentFilePattern = /^data\/(species|skills|marks|behaviors|natures|enhancements|elements|parameterGroups|parameterProfiles|parameterBindings|ruleSets|matchupTables|presentations|resources)\/.+\.(json|ya?ml)$/;
18
49
  const localeFilePattern = /^locales\/([^/]+)\/([^/]+)\.(json|ya?ml)$/;
19
50
  const basePackageName = '@arcadia-eternity/data-pack-base';
20
51
  const baseProjectId = 'arcadia-eternity.base';
21
52
  const textLimit = 2 * 1024 * 1024;
22
53
  const object = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
23
- const hash = (value) => createHash('sha256').update(value).digest('hex');
24
54
  const isBaseManifest = (manifest) => manifest.name === basePackageName || manifest.arcadia?.projectId === baseProjectId;
25
55
  const isInstalledPackage = (root) => root.split(sep).includes('node_modules');
26
56
  export class WorkspaceError extends Error {
@@ -35,6 +65,14 @@ export class WorkspaceError extends Error {
35
65
  function fail(code, message, file) {
36
66
  throw new WorkspaceError(code, message, file);
37
67
  }
68
+ export function normalizeArcadiaRange(value) {
69
+ if (typeof value !== 'string' || value.length > 256 || (value.trim() && !validRange(value.trim())))
70
+ fail('ARCADIA_VERSION_RANGE_INVALID', '请输入有效的 Arcadia 版本范围。');
71
+ return value.trim();
72
+ }
73
+ function manifestRange(manifest) {
74
+ return isRecord(manifest.engines) && typeof manifest.engines.arcadia === 'string' ? manifest.engines.arcadia : '';
75
+ }
38
76
  export function diagnostic(error) {
39
77
  return {
40
78
  code: errorCode(error) ?? 'WORKSPACE_ERROR',
@@ -117,6 +155,7 @@ export class AuthorWorkspace extends EventEmitter {
117
155
  baseProject;
118
156
  #watcher;
119
157
  #queues = new Map();
158
+ #effectDeclarations = new Map();
120
159
  #dirty = new Set();
121
160
  #closed = false;
122
161
  constructor(root) {
@@ -200,7 +239,7 @@ export class AuthorWorkspace extends EventEmitter {
200
239
  const bytes = await readFile(path);
201
240
  if (bytes.length > textLimit || bytes.includes(0))
202
241
  fail('SOURCE_INVALID', '文件不是受支持的源文本', file);
203
- return { file, text: bytes.toString('utf8'), version: hash(bytes) };
242
+ return { file, text: bytes.toString('utf8'), version: sha256Hex(bytes) };
204
243
  }
205
244
  async files() {
206
245
  const result = [];
@@ -233,15 +272,65 @@ export class AuthorWorkspace extends EventEmitter {
233
272
  continue;
234
273
  if (entry.error)
235
274
  fail('PATH_INVALID', entry.error, entry.file);
236
- const digest = createHash('sha256');
237
- for await (const chunk of createReadStream(await this.path(entry.file)))
238
- digest.update(chunk);
239
- hashes.push([entry.file, digest.digest('hex')]);
275
+ hashes.push([entry.file, await sha256StreamHex(createReadStream(await this.path(entry.file)))]);
276
+ }
277
+ return sha256Hex(JSON.stringify(hashes));
278
+ }
279
+ async #effectCatalog(project, files) {
280
+ if (typeof project.effectSources !== 'string' || !project.effectSources || this.published)
281
+ return;
282
+ const catalog = { definitions: [], issues: [] };
283
+ if (!project.effectArtifact || project.effects) {
284
+ catalog.issues.push({
285
+ file: 'package.json',
286
+ message: 'effectSources 需要指定 effectArtifact,并移除 effects 入口。',
287
+ });
288
+ return catalog;
289
+ }
290
+ const directory = relative(this.root, resolve(this.root, project.effectSources)).split(sep).join('/');
291
+ const sources = files.filter(entry => (!directory || entry.file.startsWith(directory + '/')) && entry.file.endsWith('.effect.ts'));
292
+ const current = new Set(sources.map(entry => entry.file));
293
+ for (const file of this.#effectDeclarations.keys())
294
+ if (!current.has(file))
295
+ this.#effectDeclarations.delete(file);
296
+ const namespace = project.namespace ?? '';
297
+ for (const { file } of sources) {
298
+ try {
299
+ const source = await this.read(file);
300
+ let cached = this.#effectDeclarations.get(file);
301
+ if (!cached || cached.version !== source.version || cached.namespace !== namespace) {
302
+ const { inspectEffectDeclarations } = await import('./effect-declarations.js');
303
+ cached = {
304
+ version: source.version,
305
+ namespace,
306
+ catalog: inspectEffectDeclarations(file, source.text, namespace),
307
+ };
308
+ this.#effectDeclarations.set(file, cached);
309
+ }
310
+ catalog.definitions.push(...cached.catalog.definitions);
311
+ catalog.issues.push(...cached.catalog.issues);
312
+ }
313
+ catch (error) {
314
+ catalog.issues.push({ file, message: errorMessage(error) });
315
+ }
240
316
  }
241
- return hash(JSON.stringify(hashes));
317
+ const seen = new Set(), duplicates = new Set();
318
+ for (const effect of catalog.definitions) {
319
+ if (seen.has(effect.id))
320
+ duplicates.add(effect.id);
321
+ seen.add(effect.id);
322
+ }
323
+ catalog.definitions = catalog.definitions.filter(effect => {
324
+ if (!duplicates.has(effect.id))
325
+ return true;
326
+ catalog.issues.push({ file: effect.file, message: `效果 ID 重复:${effect.id}` });
327
+ return false;
328
+ });
329
+ return catalog;
242
330
  }
243
331
  async snapshot() {
244
- const manifest = JSON.parse((await this.read('package.json')).text);
332
+ const source = await this.read('package.json');
333
+ const manifest = JSON.parse(source.text);
245
334
  const project = this.baseProject ?? manifest.arcadia ?? {};
246
335
  const readOnly = this.readOnly || isBaseManifest(manifest);
247
336
  const baseSources = new Map();
@@ -336,9 +425,7 @@ export class AuthorWorkspace extends EventEmitter {
336
425
  ? [entry.file, baseKind]
337
426
  : contentFilePattern.exec(entry.file);
338
427
  const locale = localeFilePattern.exec(entry.file);
339
- if (this.baseProject &&
340
- locale &&
341
- !['species', 'skill', 'mark', 'mark_ability', 'mark_emblem', 'mark_global'].includes(locale[2]))
428
+ if (this.baseProject && locale && !contentLocaleCollection(locale[2]))
342
429
  continue;
343
430
  if (!match && !locale)
344
431
  continue;
@@ -348,7 +435,7 @@ export class AuthorWorkspace extends EventEmitter {
348
435
  if (locale) {
349
436
  if (!object(value) ||
350
437
  Object.values(value).some(entry => !object(entry) ||
351
- Object.entries(entry).some(([key, text]) => !['name', 'description'].includes(key) || typeof text !== 'string')))
438
+ Object.entries(entry).some(([key, text]) => !isContentLocaleField(locale[2], key) || typeof text !== 'string')))
352
439
  fail('SOURCE_INVALID', '语言文件需要以对象 ID 映射名称与描述文本', entry.file);
353
440
  safeValue(value);
354
441
  locales[entry.file] = value;
@@ -467,9 +554,15 @@ export class AuthorWorkspace extends EventEmitter {
467
554
  root: this.root,
468
555
  project,
469
556
  name: this.baseProject ? '基础包' : manifest.name,
557
+ manifestVersion: source.version,
558
+ dependencies: isRecord(manifest.dependencies)
559
+ ? Object.fromEntries(Object.entries(manifest.dependencies).filter((entry) => typeof entry[1] === 'string'))
560
+ : {},
561
+ arcadiaRange: manifestRange(manifest),
470
562
  version: manifest.version,
471
563
  records,
472
564
  files,
565
+ effectCatalog: await this.#effectCatalog(project, files),
473
566
  assets,
474
567
  locales,
475
568
  localeSources,
@@ -491,6 +584,9 @@ export class AuthorWorkspace extends EventEmitter {
491
584
  else
492
585
  this.#dirty.delete(file);
493
586
  }
587
+ get hasDirtyFiles() {
588
+ return this.#dirty.size > 0;
589
+ }
494
590
  clearDirty() {
495
591
  this.#dirty.clear();
496
592
  }
@@ -501,7 +597,7 @@ export class AuthorWorkspace extends EventEmitter {
501
597
  if ((await lstat(path)).size > 64 * 1024 * 1024)
502
598
  fail('SOURCE_TOO_LARGE', '素材超过 64 MiB', file);
503
599
  const bytes = await readFile(path);
504
- return { file, bytes, version: hash(bytes) };
600
+ return { file, bytes, version: sha256Hex(bytes) };
505
601
  }
506
602
  async #serialize(file, action) {
507
603
  const pending = (this.#queues.get(file) ?? Promise.resolve())
@@ -548,7 +644,78 @@ export class AuthorWorkspace extends EventEmitter {
548
644
  if (fromForm)
549
645
  this.#dirty.delete(file);
550
646
  this.emit('change', { type: 'change', file });
551
- return { file, version: hash(text) };
647
+ return { file, version: sha256Hex(text) };
648
+ });
649
+ }
650
+ async editPackage(input) {
651
+ await this.assertWritable();
652
+ const source = await this.read('package.json');
653
+ if (input.expectedVersion !== source.version)
654
+ fail('REVISION_CONFLICT', '包声明已在外部修改,请核对后重试。', source.file);
655
+ const manifest = JSON.parse(source.text);
656
+ if (input.field === 'version') {
657
+ if (typeof input.value !== 'string' || valid(input.value) !== input.value)
658
+ fail('SOURCE_INVALID', '请输入完整的 npm 版本号。', source.file);
659
+ manifest.version = input.value;
660
+ }
661
+ else if (input.field === 'repository') {
662
+ if (typeof input.value !== 'string' ||
663
+ !/^https:\/\/github\.com\/[a-z0-9-]+\/[a-z0-9._-]+\.git$/i.test(input.value))
664
+ fail('SOURCE_INVALID', '请输入完整的 GitHub HTTPS 仓库地址。', source.file);
665
+ manifest.repository = { type: 'git', url: input.value };
666
+ }
667
+ else if (input.field === 'dependency') {
668
+ const name = input.name;
669
+ if (typeof name !== 'string' ||
670
+ name.length > 214 ||
671
+ !/^(?:@[a-z0-9][a-z0-9_.-]*\/)?[a-z0-9][a-z0-9_.-]*$/.test(name) ||
672
+ ['constructor', 'prototype', 'node_modules', 'favicon.ico', manifest.name].includes(name))
673
+ fail('SOURCE_INVALID', '依赖包名无效。', source.file);
674
+ if (manifest.dependencies !== undefined && !isRecord(manifest.dependencies))
675
+ fail('SOURCE_INVALID', 'dependencies 必须是对象。', source.file);
676
+ const dependencies = { ...manifest.dependencies };
677
+ if (input.value === null)
678
+ delete dependencies[name];
679
+ else {
680
+ if (typeof input.value !== 'string' || !input.value.trim() || !validRange(input.value))
681
+ fail('SOURCE_INVALID', '请输入 npm 版本或兼容范围。', source.file);
682
+ dependencies[name] = input.value.trim();
683
+ }
684
+ if (Object.keys(dependencies).length)
685
+ manifest.dependencies = dependencies;
686
+ else
687
+ delete manifest.dependencies;
688
+ }
689
+ else
690
+ fail('SOURCE_INVALID', '未知的包声明修改。', source.file);
691
+ await this.write({
692
+ file: source.file,
693
+ expectedVersion: source.version,
694
+ text: JSON.stringify(manifest, null, 2) + '\n',
695
+ });
696
+ }
697
+ async saveArcadiaRange(value, expectedRange) {
698
+ await this.assertWritable();
699
+ const range = normalizeArcadiaRange(value);
700
+ const source = await this.read('package.json');
701
+ const manifest = JSON.parse(source.text);
702
+ if (manifestRange(manifest) !== expectedRange)
703
+ fail('REVISION_CONFLICT', '兼容范围已在外部修改,请核对后再保存。', source.file);
704
+ if (manifest.engines !== undefined && !isRecord(manifest.engines))
705
+ fail('SOURCE_INVALID', 'package.json 的 engines 必须是对象。', source.file);
706
+ const engines = manifest.engines ?? {};
707
+ if (range)
708
+ engines.arcadia = range;
709
+ else
710
+ delete engines.arcadia;
711
+ if (Object.keys(engines).length)
712
+ manifest.engines = engines;
713
+ else
714
+ delete manifest.engines;
715
+ await this.write({
716
+ file: source.file,
717
+ expectedVersion: source.version,
718
+ text: JSON.stringify(manifest, null, 2) + '\n',
552
719
  });
553
720
  }
554
721
  async remove({ file, expectedVersion }) {
@@ -602,7 +769,7 @@ export class AuthorWorkspace extends EventEmitter {
602
769
  fail('SOURCE_INVALID', '不支持的修改类型', file);
603
770
  if (!(isLocale
604
771
  ? /^locales\/[^/]+\/[^/]+\.(json|ya?ml)$/
605
- : /^data\/(species|skills|marks|behaviors|presentations|resources)\/.+\.(json|ya?ml)$/).test(file))
772
+ : /^data\/(species|skills|marks|behaviors|natures|enhancements|elements|parameterGroups|parameterProfiles|parameterBindings|ruleSets|matchupTables|presentations|resources)\/.+\.(json|ya?ml)$/).test(file))
606
773
  fail('PATH_INVALID', '请选择已有的内容或语言源文件', file);
607
774
  const key = JSON.stringify([file, isLocale ? id : index]);
608
775
  if (targets.has(key))
@@ -650,22 +817,15 @@ export class AuthorWorkspace extends EventEmitter {
650
817
  fail('SOURCE_INVALID', '请将表现关联和资源定义分别保存到 data/presentations 与 data/resources', file);
651
818
  }
652
819
  if (isLocale) {
820
+ const namespace = /^locales\/[^/]+\/([^/]+)\.(json|ya?ml)$/.exec(file)?.[1] ?? '';
653
821
  const target = prepared.next;
654
822
  if (typeof id !== 'string' ||
655
823
  !id ||
656
824
  ['__proto__', 'constructor', 'prototype'].includes(id) ||
657
825
  !object(target) ||
658
- Object.entries(record).some(([key, value]) => !['name', 'description'].includes(key) || typeof value !== 'string'))
826
+ Object.entries(record).some(([key, value]) => !isContentLocaleField(namespace, key) || typeof value !== 'string'))
659
827
  fail('SOURCE_INVALID', '名称与描述需要文本值', file);
660
- const namespace = /^locales\/[^/]+\/([^/]+)\.(json|ya?ml)$/.exec(file)?.[1];
661
- const contentKind = {
662
- species: 'species',
663
- skill: 'skills',
664
- mark: 'marks',
665
- mark_ability: 'marks',
666
- mark_emblem: 'marks',
667
- mark_global: 'marks',
668
- }[namespace ?? ''];
828
+ const contentKind = contentLocaleCollection(namespace);
669
829
  if (!contentKind ||
670
830
  catalog.records.filter(row => row.kind === contentKind && row.record.id === id).length +
671
831
  Number(created.has(JSON.stringify([contentKind, id]))) !==
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcadia-eternity/content-sdk",
3
- "version": "4.6.0",
3
+ "version": "4.7.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "engines": {
@@ -17,6 +17,10 @@
17
17
  },
18
18
  "./testing": "./dist/testing.js",
19
19
  "./workspace": "./dist/author-api.js",
20
+ "./prepared-document": {
21
+ "types": "./dist/prepared-document.d.ts",
22
+ "import": "./dist/prepared-document.js"
23
+ },
20
24
  "./management-workspace": {
21
25
  "types": "./dist/content-workspace.d.ts",
22
26
  "import": "./dist/content-workspace.js",
@@ -39,13 +43,17 @@
39
43
  },
40
44
  "files": [
41
45
  "bin",
46
+ "release-payload.mjs",
47
+ "release-test-npm.mjs",
42
48
  "dist",
43
49
  "LICENSE"
44
50
  ],
45
51
  "dependencies": {
52
+ "@noble/hashes": "2.4.0",
46
53
  "@modelcontextprotocol/sdk": "^1.32.2",
47
54
  "chokidar": "^6.0.0",
48
55
  "mime-types": "^3.0.2",
56
+ "pacote": "^22.0.1",
49
57
  "semver": "7.8.6",
50
58
  "tar": "^7.5.22",
51
59
  "typescript": "npm:typescript-native-bridge@6.0.3-bridge.19.tsgo.7.0.2",
@@ -53,9 +61,12 @@
53
61
  "yaml": "2.9.1",
54
62
  "yauzl": "3.4.0",
55
63
  "zod": "4.6.7",
56
- "@arcadia-eternity/battle": "4.6.0",
57
- "@arcadia-eternity/pack-loader": "4.6.0",
58
- "@arcadia-eternity/schema": "4.6.0"
64
+ "fflate": "0.8.3",
65
+ "simple-git": "4.1.0",
66
+ "@arcadia-eternity/battle": "4.7.1",
67
+ "@arcadia-eternity/const": "4.7.1",
68
+ "@arcadia-eternity/pack-loader": "4.7.1",
69
+ "@arcadia-eternity/schema": "4.7.1"
59
70
  },
60
71
  "repository": {
61
72
  "type": "git",
@@ -68,6 +79,7 @@
68
79
  },
69
80
  "devDependencies": {
70
81
  "@types/mime-types": "^3.0.1",
82
+ "@types/pacote": "^11.1.8",
71
83
  "@types/semver": "7.8.0",
72
84
  "@types/write-file-atomic": "^4.0.3",
73
85
  "@types/yauzl": "^3.4.0"
@@ -0,0 +1,91 @@
1
+ // Shared by the desktop packer and the content template's Actions workflow.
2
+ import { createHash } from 'node:crypto'
3
+ import { readFile, writeFile } from 'node:fs/promises'
4
+ import { gunzipSync } from 'node:zlib'
5
+ import { gzipSync } from 'fflate'
6
+ import { createRequire } from 'node:module'
7
+ const require = createRequire(import.meta.url)
8
+ const sdkRequire = createRequire(require.resolve('@arcadia-eternity/content-sdk'))
9
+ const { valid, validRange } = sdkRequire('semver')
10
+
11
+ export function releasePayload(source, included, presentation) {
12
+ if (!source.arcadia?.projectId || !source.arcadia.namespace || !source.arcadia.content || !valid(source.version))
13
+ throw Error('发行包需要有效版本、projectId、namespace 和内容入口')
14
+ if (typeof source.license !== 'string' || !source.license.trim()) throw Error('请先设置 package.json 的 license')
15
+ const fields = [
16
+ 'name',
17
+ 'version',
18
+ 'description',
19
+ 'license',
20
+ 'type',
21
+ 'engines',
22
+ 'arcadia',
23
+ 'dependencies',
24
+ 'optionalDependencies',
25
+ 'peerDependencies',
26
+ 'peerDependenciesMeta',
27
+ ]
28
+ for (const key of ['dependencies', 'optionalDependencies', 'peerDependencies']) {
29
+ const ranges = source[key]
30
+ if (
31
+ ranges !== undefined &&
32
+ (!ranges ||
33
+ typeof ranges !== 'object' ||
34
+ Array.isArray(ranges) ||
35
+ Object.values(ranges).some(range => typeof range !== 'string' || !range.trim() || !validRange(range)))
36
+ )
37
+ throw Error('发行包的 ' + key + ' 必须使用 npm 版本或范围')
38
+ }
39
+ const manifest = Object.fromEntries(
40
+ fields.filter(key => Object.hasOwn(source, key)).map(key => [key, structuredClone(source[key])]),
41
+ )
42
+ const repository = typeof source.repository === 'object' ? source.repository?.url : source.repository
43
+ if (typeof repository === 'string' && /^(?:git\+)?https:\/\//.test(repository)) {
44
+ const url = new URL(repository.replace(/^git\+/, ''))
45
+ if (url.username || url.password || url.search || url.hash) throw Error('发行仓库地址不能包含凭据、查询参数或片段')
46
+ manifest.repository = { type: 'git', url: 'git+' + url.href }
47
+ }
48
+ const { arcadia } = manifest
49
+ if (arcadia.effects) arcadia.effectArtifact = 'dist/arcadia-effects.json'
50
+ delete arcadia.effects
51
+ delete arcadia.effectSources
52
+ const files = new Set(
53
+ [...included].filter(file => /^(?:package\.json|readme(?:\..+)?|licen[cs]e(?:\..+)?|notice(?:\..+)?)$/i.test(file)),
54
+ )
55
+ for (const file of [
56
+ arcadia.content,
57
+ arcadia.presentation,
58
+ arcadia.catalog,
59
+ arcadia.effectArtifact,
60
+ ...Object.values(arcadia.assets ?? {}),
61
+ ...(presentation?.resources ?? []).filter(resource => resource.kind === 'file').map(resource => resource.source),
62
+ ])
63
+ if (typeof file === 'string') files.add(file)
64
+ if (![...files].some(file => /^licen[cs]e(?:\.(?:md|txt))?$/i.test(file))) throw Error('发行包缺少 LICENSE 文件')
65
+ for (const file of files) {
66
+ if (
67
+ !file ||
68
+ /[\\:\p{Cc}]/u.test(file) ||
69
+ file
70
+ .split('/')
71
+ .some(part => !part || part.startsWith('.') || /^(?:src|tests?|scripts|debug|node_modules)$/i.test(part)) ||
72
+ /(?:\.(?:[cm]?[jt]sx?|map|pem|key|p12|pfx)|(?:^|\/)tsconfig[^/]*\.json)$/i.test(file)
73
+ )
74
+ throw Error('发行清单含开发文件或私密文件: ' + file)
75
+ }
76
+ manifest.files = [...files].sort()
77
+ return manifest
78
+ }
79
+
80
+ // npm creates a portable tar stream, but native zlib versions can encode it
81
+ // differently. Use the same pinned compressor for desktop and hosted builds.
82
+ export async function normalizeReleaseArchive(path) {
83
+ const tar = gunzipSync(await readFile(path), { maxOutputLength: 256 * 1024 * 1024 })
84
+ const bytes = gzipSync(tar, { level: 9, mtime: 0 })
85
+ await writeFile(path, bytes)
86
+ return {
87
+ integrity: 'sha512-' + createHash('sha512').update(bytes).digest('base64'),
88
+ shasum: createHash('sha1').update(bytes).digest('hex'),
89
+ size: bytes.length,
90
+ }
91
+ }
@@ -0,0 +1,27 @@
1
+ // SDK 4.6 requests "npm pack" in the scenario directory. Give npm the verified
2
+ // archive as its explicit input. All installs and dependency packs still use npm.
3
+ import { spawnSync } from 'node:child_process'
4
+ import { realpathSync } from 'node:fs'
5
+ import { createRequire } from 'node:module'
6
+ import { join } from 'node:path'
7
+ import { pathToFileURL } from 'node:url'
8
+
9
+ const args = process.argv.slice(2)
10
+ const filename = process.env.ARCADIA_TEST_RELEASE_ARCHIVE
11
+ if (
12
+ args[0] === 'pack' &&
13
+ args[1] === '--json' &&
14
+ filename &&
15
+ /^[a-z0-9][a-z0-9._-]*\.tgz$/.test(filename) &&
16
+ realpathSync(process.cwd()) === realpathSync(process.env.ARCADIA_TEST_RELEASE_ROOT)
17
+ )
18
+ args.splice(1, 0, join(process.cwd(), filename))
19
+ if (process.env.ARCADIA_TEST_BASE_NPM) process.env.ARCADIA_NPM_CLI = process.env.ARCADIA_TEST_BASE_NPM
20
+ else delete process.env.ARCADIA_NPM_CLI
21
+ const require = createRequire(import.meta.url)
22
+ const sdk = createRequire(require.resolve('@arcadia-eternity/content-sdk'))
23
+ const { resolveNpmInvocation } = await import(pathToFileURL(sdk.resolve('@arcadia-eternity/pack-loader/node')).href)
24
+ const command = await resolveNpmInvocation(args)
25
+ const result = spawnSync(command.file, command.args, { stdio: 'inherit', windowsHide: true })
26
+ if (result.error) throw result.error
27
+ process.exitCode = result.status ?? 1