@jskit-ai/jskit-catalog 0.2.79 → 0.2.81

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.
@@ -28,6 +28,11 @@ const INSTALL_SCRIPT_PACKAGES = Object.freeze(["@jskit-ai/material-web"]);
28
28
  const SOURCE_FILE_PATTERN = /\.(?:[cm]?[jt]s|vue)$/u;
29
29
  const SKIPPED_DIRECTORIES = new Set(["node_modules", "dist", "build", "coverage"]);
30
30
  const CHECK_SCRIPT = "jskit check";
31
+ // Dependencies JSKIT's packages require at one exact version, of which an application must install one copy.
32
+ // json-rest-api knows its transactions by identity: beside a second copy (an app pinning another version than
33
+ // resource-runtime's), every write the app's api.transaction reaches inside JSKIT fails with "transaction must be an
34
+ // active transaction from api.transaction".
35
+ const SINGLE_COPY_DEPENDENCIES = Object.freeze(["json-rest-api"]);
31
36
 
32
37
  function normalizeCatalog(catalog = {}) {
33
38
  const releasePackages = catalog?.release?.packages;
@@ -48,7 +53,46 @@ function normalizeCatalog(catalog = {}) {
48
53
  .map((entry) => String(entry || "").trim())
49
54
  .filter((entry) => versions.has(entry))
50
55
  );
51
- return Object.freeze({ versions, singletonPackages });
56
+ return Object.freeze({ versions, singletonPackages, singleCopyDependencies: singleCopyDependencies(catalog?.packages) });
57
+ }
58
+
59
+ // Each single-copy dependency the catalog's packages require: its one version, and every catalog package that needs it,
60
+ // directly or through another JSKIT package it depends on.
61
+ function singleCopyDependencies(catalogPackages) {
62
+ const entries = Array.isArray(catalogPackages) ? catalogPackages : [];
63
+ const result = new Map();
64
+ for (const name of SINGLE_COPY_DEPENDENCIES) {
65
+ const versions = new Set();
66
+ const requiredBy = new Set();
67
+ for (const entry of entries) {
68
+ const version = entry?.packageJson?.dependencies?.[name];
69
+ if (!version) continue;
70
+ versions.add(version);
71
+ requiredBy.add(entry.packageId);
72
+ }
73
+ if (versions.size > 1) {
74
+ throw new Error(`JSKIT catalog requires ${name} at several versions: ${[...versions].sort().join(", ")}.`);
75
+ }
76
+ if (versions.size < 1) continue;
77
+ for (let grew = true; grew;) {
78
+ grew = false;
79
+ for (const entry of entries) {
80
+ const dependencies = Object.keys({ ...entry?.packageJson?.dependencies, ...entry?.packageJson?.peerDependencies });
81
+ if (!requiredBy.has(entry?.packageId) && dependencies.some((dependency) => requiredBy.has(dependency))) {
82
+ requiredBy.add(entry.packageId);
83
+ grew = true;
84
+ }
85
+ }
86
+ }
87
+ result.set(name, Object.freeze({ version: [...versions][0], requiredBy }));
88
+ }
89
+ return result;
90
+ }
91
+
92
+ // The single-copy dependencies that some of these JSKIT packages need.
93
+ function requiredSingleCopies(normalizedCatalog, packageIds) {
94
+ return [...normalizedCatalog.singleCopyDependencies]
95
+ .filter(([, { requiredBy }]) => [...requiredBy].some((packageId) => packageIds.has(packageId)));
52
96
  }
53
97
 
54
98
  function collectManifestDeclarations(manifests, projectRoot) {
@@ -176,10 +220,12 @@ async function checkProject({ projectRoot = process.cwd(), catalog, additionalMa
176
220
 
177
221
  const packageLockPath = path.join(root, "package-lock.json");
178
222
  let installations = [];
223
+ let lockPackages = {};
179
224
  if (!(await fileExists(packageLockPath))) {
180
225
  issues.push("package-lock.json is missing; run npm install after aligning the JSKIT release cohort.");
181
226
  } else {
182
227
  const packageLock = (await readJsonRecord(packageLockPath)).value;
228
+ lockPackages = packageLock.packages || {};
183
229
  installations = collectLockInstallations(packageLock);
184
230
  if (checkInstalled) {
185
231
  for (const [packagePath, record] of Object.entries(packageLock.packages || {})) {
@@ -253,6 +299,32 @@ async function checkProject({ projectRoot = process.cwd(), catalog, additionalMa
253
299
  }
254
300
  }
255
301
 
302
+ const appPackageIds = new Set([...declarations, ...installations].map((entry) => entry.packageId));
303
+ for (const [name, { version }] of requiredSingleCopies(normalizedCatalog, appPackageIds)) {
304
+ const fix = `Pin ${name} to ${version}`;
305
+ for (const manifest of manifests) {
306
+ const source = toPosixPath(path.relative(root, manifest.absolutePath)) || "package.json";
307
+ for (const field of DEPENDENCY_FIELDS) {
308
+ const declared = manifest.value?.[field]?.[name];
309
+ if (declared !== undefined && declared !== version) {
310
+ issues.push(
311
+ `${source}#${field}.${name} is ${declared}; JSKIT's packages require ${name} ${version}, and beside a second ` +
312
+ `copy every write an api.transaction reaches inside them fails. ${fix} (npm run jskit:update does it).`
313
+ );
314
+ }
315
+ }
316
+ }
317
+ const copies = Object.entries(lockPackages)
318
+ .filter(([packagePath, record]) => record && !record.link &&
319
+ (packagePath === `node_modules/${name}` || packagePath.endsWith(`/node_modules/${name}`)));
320
+ if (copies.length > 1) {
321
+ issues.push(
322
+ `${name} is installed ${copies.length} times (${copies.map(([packagePath, record]) => `${record.version} at ${packagePath}`)
323
+ .join(", ")}); JSKIT's packages need the one copy an app's api.transaction comes from. ${fix} and run npm install.`
324
+ );
325
+ }
326
+ }
327
+
256
328
  return Object.freeze({
257
329
  ok: issues.length < 1,
258
330
  issues: Object.freeze(issues),
@@ -543,6 +615,8 @@ async function updateProject({
543
615
  if (!catalogVersion) {
544
616
  throw new Error(`Coordinated catalog is missing ${CATALOG_PACKAGE_ID}.`);
545
617
  }
618
+ // A direct pin of a dependency the declared JSKIT packages need as one copy moves with the catalog.
619
+ const singleCopies = requiredSingleCopies(normalizedCatalog, declaredPackageIds);
546
620
 
547
621
  const updates = [];
548
622
  for (const [index, manifest] of manifests.entries()) {
@@ -564,6 +638,12 @@ async function updateProject({
564
638
  changed = true;
565
639
  }
566
640
  }
641
+ for (const [name, { version }] of singleCopies) {
642
+ if (Object.hasOwn(next?.[field] || {}, name) && next[field][name] !== version) {
643
+ next[field][name] = version;
644
+ changed = true;
645
+ }
646
+ }
567
647
  }
568
648
 
569
649
  if (index === 0) {