@zairakai/js-dev-tools 3.14.0 → 3.15.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zairakai/js-dev-tools",
3
- "version": "3.14.0",
3
+ "version": "3.15.0",
4
4
  "description": "The unified and centralized development toolkit for JavaScript/TypeScript projects - ESLint, Prettier, Stylelint, Knip, Vitest, Git Hooks, and GitLab CI pipeline templates",
5
5
  "keywords": [
6
6
  "dev-tools",
@@ -11,7 +11,8 @@
11
11
  * - cascade: after the publication of this package, start the update of the projects that use it.
12
12
  * - audit: look for security advisories, open one issue per advisory and, with AUTO_FIX=true, start an update to fix them
13
13
  * (UPDATE_URGENT=true: the new versions are taken without waiting, and `npm audit fix` is run).
14
- * - policy: in the repository dependency-policy, lift the entries whose condition is met (merge request).
14
+ * - policy: in the repository dependency-policy, lift the entries of node.json and php.json whose condition is met (merge
15
+ * request).
15
16
  * - policy-validate: in the repository dependency-policy, check the shape of the files.
16
17
  *
17
18
  * The packages listed in the dependency policy (https://gitlab.com/zairakai/dependency-policy) are never updated, whatever
@@ -252,6 +253,79 @@ export function isLifted(entry, ranges) {
252
253
  })
253
254
  }
254
255
 
256
+ /** One token of a Composer constraint as semver reads it: only `~` differs (`~1.2` is `>=1.2.0 <2.0.0` for Composer). */
257
+ function composerToken(token) {
258
+ const tilde = /^~v?(\d+)(?:\.(\d+))?$/.exec(token)
259
+
260
+ return tilde ? `>=${tilde[1]}.${tilde[2] ?? 0}.0 <${Number(tilde[1]) + 1}.0.0` : token
261
+ }
262
+
263
+ /**
264
+ * A Composer constraint (`||` or `|`, comma or space, `~`, `^`, `*`, `@stable`) as a range that semver compares, or null
265
+ * when it cannot be expressed (branches, `!=`, aliases): a peer whose range is not understood never lifts an entry.
266
+ */
267
+ export function composerRange(constraint) {
268
+ if ('string' !== typeof constraint) return null
269
+
270
+ const text = constraint.replace(/@\w+/g, '').trim()
271
+ if ('' === text || /(^|[\s|,])dev-|\sas\s|!=/.test(text)) return null
272
+
273
+ const alternatives = text.split(/\s*\|\|?\s*/).map((alternative) => {
274
+ if (/\s-\s/.test(alternative)) return alternative.trim()
275
+
276
+ return alternative.replaceAll(',', ' ').split(/\s+/).filter(Boolean).map(composerToken).join(' ')
277
+ })
278
+
279
+ if (alternatives.includes('')) return null
280
+
281
+ const range = alternatives.join(' || ')
282
+
283
+ return null === semver.validRange(range) ? null : range
284
+ }
285
+
286
+ /** The releases of the metadata of Packagist (p2) in full: after the first one, each lists only what changed. */
287
+ export function expandReleases(releases) {
288
+ let previous = {}
289
+
290
+ return releases.map((release) => {
291
+ const full = { ...previous }
292
+
293
+ for (const [key, value] of Object.entries(release)) {
294
+ if ('__unset' === value) delete full[key]
295
+ else full[key] = value
296
+ }
297
+
298
+ previous = full
299
+
300
+ return full
301
+ })
302
+ }
303
+
304
+ /** The highest stable release, or null. */
305
+ export function latestStable(releases) {
306
+ let latest = null
307
+ let latestVersion = null
308
+
309
+ for (const release of releases) {
310
+ const version = semver.valid(String(release.version ?? '').replace(/^v/, ''))
311
+ if (null === version || null !== semver.prerelease(version)) continue
312
+
313
+ if (null === latestVersion || semver.gt(version, latestVersion)) {
314
+ latest = release
315
+ latestVersion = version
316
+ }
317
+ }
318
+
319
+ return latest
320
+ }
321
+
322
+ /** The range in which the latest stable release of a Composer package requires `name`, or null. */
323
+ export function composerPeerRange(releases, name) {
324
+ const constraint = latestStable(expandReleases(releases))?.require?.[name]
325
+
326
+ return composerRange(constraint)
327
+ }
328
+
255
329
  /**
256
330
  * The packages of a lock file that the refresh may update: the ones of the group are left out, because they are trusted
257
331
  * and not made to wait, and the cooling date would refuse a version that was just published.
@@ -973,6 +1047,20 @@ function peerRange(peer, name) {
973
1047
  }
974
1048
  }
975
1049
 
1050
+ /** The range in which the latest release of the Composer package `peer` requires `name`, or null. */
1051
+ export async function composerPeerRangeOnline(peer, name, fetchImpl = fetch) {
1052
+ try {
1053
+ const response = await fetchImpl(`https://repo.packagist.org/p2/${peer}.json`, {
1054
+ signal: AbortSignal.timeout(20000),
1055
+ })
1056
+ if (!response.ok) return null
1057
+
1058
+ return composerPeerRange((await response.json()).packages?.[peer] ?? [], name)
1059
+ } catch {
1060
+ return null
1061
+ }
1062
+ }
1063
+
976
1064
  /** In the repository dependency-policy: check the shape of the files. */
977
1065
  async function policyValidate() {
978
1066
  let failed = false
@@ -995,24 +1083,42 @@ async function policyValidate() {
995
1083
  log('The dependency policy is valid.')
996
1084
  }
997
1085
 
1086
+ /** The range in which the latest release of `peer` requires `name`, in the registry of the ecosystem. */
1087
+ function peerRangeOf(ecosystem, peer, name) {
1088
+ return 'npm' === ecosystem ? peerRange(peer, name) : composerPeerRangeOnline(peer, name)
1089
+ }
1090
+
998
1091
  /** In the repository dependency-policy: lift the entries whose condition is met, with a merge request into main. */
999
1092
  async function policyWatch() {
1000
1093
  assertEnvironment()
1001
1094
 
1002
- const file = POLICY_FILES.npm
1003
- const doc = JSON.parse(readFileSync(file, 'utf8'))
1004
- const errors = validatePolicy(doc)
1005
- if (0 < errors.length) throw new Error(`${file} is not valid: ${errors.join(' ')}`)
1095
+ const changes = []
1006
1096
 
1007
- const lifted = doc.entries.filter((entry) => {
1008
- if (!entry.unblock) return false
1097
+ for (const [ecosystem, file] of Object.entries(POLICY_FILES)) {
1098
+ if (!existsSync(file)) continue
1009
1099
 
1010
- const ranges = Object.fromEntries(entry.unblock.peers.map((peer) => [peer, peerRange(peer, entry.package)]))
1100
+ const doc = JSON.parse(readFileSync(file, 'utf8'))
1101
+ const errors = validatePolicy(doc)
1102
+ if (0 < errors.length) throw new Error(`${file} is not valid: ${errors.join(' ')}`)
1011
1103
 
1012
- return isLifted(entry, ranges)
1013
- })
1104
+ const lifted = []
1105
+
1106
+ for (const entry of doc.entries) {
1107
+ if (!entry.unblock) continue
1108
+
1109
+ const ranges = Object.fromEntries(
1110
+ await Promise.all(
1111
+ entry.unblock.peers.map(async (peer) => [peer, await peerRangeOf(ecosystem, peer, entry.package)])
1112
+ )
1113
+ )
1114
+
1115
+ if (isLifted(entry, ranges)) lifted.push(entry)
1116
+ }
1014
1117
 
1015
- if (0 === lifted.length) {
1118
+ if (0 < lifted.length) changes.push({ file, doc, lifted })
1119
+ }
1120
+
1121
+ if (0 === changes.length) {
1016
1122
  log('No entry can be lifted.')
1017
1123
  return
1018
1124
  }
@@ -1022,17 +1128,22 @@ async function policyWatch() {
1022
1128
  return
1023
1129
  }
1024
1130
 
1025
- const names = lifted.map((entry) => entry.package).join(', ')
1131
+ const all = changes.flatMap(({ lifted }) => lifted)
1132
+ const names = all.map((entry) => entry.package).join(', ')
1026
1133
  const branch = `${POLICY_BRANCH_PREFIX}-${new Date().toISOString().slice(0, 10).replaceAll('-', '')}`
1027
1134
  const subject = `chore(policy): unblock ${names}`
1028
1135
 
1029
1136
  configureSigning()
1030
1137
  run('git', ['switch', '-C', branch])
1031
- writeFileSync(
1032
- file,
1033
- `${JSON.stringify({ ...doc, entries: doc.entries.filter((entry) => !lifted.includes(entry)) }, null, 2)}\n`
1034
- )
1035
- run('git', ['add', file])
1138
+
1139
+ for (const { file, doc, lifted } of changes) {
1140
+ writeFileSync(
1141
+ file,
1142
+ `${JSON.stringify({ ...doc, entries: doc.entries.filter((entry) => !lifted.includes(entry)) }, null, 2)}\n`
1143
+ )
1144
+ run('git', ['add', file])
1145
+ }
1146
+
1036
1147
  run('git', ['commit', '-m', subject, '-m', 'The peer ranges of the packages that held it back now accept it.'])
1037
1148
  run('git', ['push', remoteUrl(), `HEAD:refs/heads/${branch}`])
1038
1149
 
@@ -1040,7 +1151,7 @@ async function policyWatch() {
1040
1151
  source_branch: branch,
1041
1152
  target_branch: 'main',
1042
1153
  title: subject,
1043
- description: `The condition of the entry is met: ${lifted.map((entry) => `${entry.package} ${entry.unblock.version} (${entry.unblock.peers.join(', ')})`).join('; ')}. It is merged when its pipeline succeeds, and the update tools take the package at their next run.`,
1154
+ description: `The condition of the entry is met: ${all.map((entry) => `${entry.package} ${entry.unblock.version} (${entry.unblock.peers.join(', ')})`).join('; ')}. It is merged when its pipeline succeeds, and the update tools take the package at their next run.`,
1044
1155
  remove_source_branch: true,
1045
1156
  ...(await mergeRequestOptions()),
1046
1157
  })