@zairakai/js-dev-tools 3.20.0 → 4.0.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.
Files changed (52) hide show
  1. package/.editorconfig +11 -72
  2. package/.gitlab/ci/parts/base.yml +95 -0
  3. package/.gitlab/ci/parts/publish.yml +156 -0
  4. package/.gitlab/ci/parts/quality.yml +161 -0
  5. package/.gitlab/ci/parts/security.yml +40 -0
  6. package/.gitlab/ci/parts/updates.yml +60 -0
  7. package/.gitlab/ci/pipeline-js-app.yml +19 -38
  8. package/.gitlab/ci/pipeline-js-package.yml +23 -528
  9. package/README.md +92 -11
  10. package/config/.markdownlintignore +8 -1
  11. package/config/.prettierignore +13 -0
  12. package/config/.stylelintignore +13 -3
  13. package/config/eslint.config.js +7 -1
  14. package/config/knip.config.js +7 -3
  15. package/config/stylelint.config.js +1 -0
  16. package/package.json +45 -28
  17. package/scripts/build.sh +28 -28
  18. package/scripts/ci-quality.sh +6 -6
  19. package/scripts/config.sh +164 -148
  20. package/scripts/doctor.sh +12 -12
  21. package/scripts/eslint-fix.sh +8 -8
  22. package/scripts/eslint.sh +15 -15
  23. package/scripts/install-bats.sh +143 -146
  24. package/scripts/install-hooks.sh +108 -111
  25. package/scripts/install-markdownlint.sh +4 -4
  26. package/scripts/install-shellcheck.sh +135 -138
  27. package/scripts/knip.sh +7 -7
  28. package/scripts/markdownlint.sh +38 -38
  29. package/scripts/outdated.sh +30 -29
  30. package/scripts/prettier-fix.sh +13 -13
  31. package/scripts/prettier.sh +13 -13
  32. package/scripts/setup-project.sh +691 -629
  33. package/scripts/stylelint-fix.sh +17 -17
  34. package/scripts/stylelint.sh +18 -18
  35. package/scripts/test.sh +13 -11
  36. package/scripts/typecheck.sh +11 -11
  37. package/scripts/update-dependencies.mjs +552 -181
  38. package/scripts/validate-shellcheck.sh +18 -18
  39. package/stubs/githooks/commit-msg +24 -21
  40. package/stubs/githooks/pre-commit +19 -19
  41. package/stubs/githooks/pre-push +18 -18
  42. package/stubs/githooks/prepare-commit-msg +6 -6
  43. package/stubs/gitlab-ci/gitlab-ci.yml.stub +1 -1
  44. package/stubs/governance/CONTRIBUTING.md.stub +5 -3
  45. package/stubs/quality/eslint.config.js.stub +1 -1
  46. package/stubs/quality/knip.config.js.stub +1 -1
  47. package/stubs/style/prettier.config.js.stub +3 -3
  48. package/stubs/style/stylelint.config.js.stub +8 -1
  49. package/stubs/testing/vitest.config.js.stub +1 -1
  50. package/tools/make/bats.mk +3 -3
  51. package/tools/make/core.mk +3 -3
  52. package/tools/make/variables.mk +1 -1
@@ -58,13 +58,26 @@ const DEFAULTS = {
58
58
  cascade: null,
59
59
  }
60
60
 
61
- const RUNTIME_SECTIONS = ['dependencies', 'peerDependencies', 'optionalDependencies']
62
- const ALL_SECTIONS = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']
61
+ const RUNTIME_SECTIONS = [
62
+ 'dependencies',
63
+ 'peerDependencies',
64
+ 'optionalDependencies',
65
+ ]
66
+ const ALL_SECTIONS = [
67
+ 'dependencies',
68
+ 'devDependencies',
69
+ 'peerDependencies',
70
+ 'optionalDependencies',
71
+ ]
63
72
 
64
73
  /** The manifest of each ecosystem, with the sections that are published and all of them. */
65
74
  const ECOSYSTEMS = {
66
75
  npm: { file: 'package.json', runtime: RUNTIME_SECTIONS, all: ALL_SECTIONS },
67
- composer: { file: 'composer.json', runtime: ['require'], all: ['require', 'require-dev'] },
76
+ composer: {
77
+ file: 'composer.json',
78
+ runtime: ['require'],
79
+ all: ['require', 'require-dev'],
80
+ },
68
81
  }
69
82
 
70
83
  /** The ecosystem of the project: a package.json at its root, otherwise a composer.json. */
@@ -100,19 +113,23 @@ export function latestTag(tags) {
100
113
  export function nextVersion(last, level) {
101
114
  const [major, minor, patch] = last.split('.').map(Number)
102
115
 
103
- if ('major' === level) return `${major + 1}.0.0`
104
- if ('minor' === level) return `${major}.${minor + 1}.0`
116
+ if (level === 'major') return `${major + 1}.0.0`
117
+ if (level === 'minor') return `${major}.${minor + 1}.0`
105
118
 
106
119
  return `${major}.${minor}.${patch + 1}`
107
120
  }
108
121
 
109
122
  function sorted(section) {
110
- return JSON.stringify(Object.entries(section ?? {}).sort(([a], [b]) => a.localeCompare(b)))
123
+ return JSON.stringify(
124
+ Object.entries(section ?? {}).sort(([a], [b]) => a.localeCompare(b)),
125
+ )
111
126
  }
112
127
 
113
128
  /** True when a range that is published (dependencies, peerDependencies, optionalDependencies) is not the same. */
114
129
  export function runtimeChanged(before, after, sections = RUNTIME_SECTIONS) {
115
- return sections.some((section) => sorted(before?.[section]) !== sorted(after?.[section]))
130
+ return sections.some(
131
+ (section) => sorted(before?.[section]) !== sorted(after?.[section]),
132
+ )
116
133
  }
117
134
 
118
135
  /** The dependencies whose range changed, with the section, the old range and the new one. */
@@ -123,22 +140,38 @@ export function diffDependencies(before, after, sections = ALL_SECTIONS) {
123
140
  const old = before?.[section] ?? {}
124
141
  const current = after?.[section] ?? {}
125
142
 
126
- for (const name of new Set([...Object.keys(old), ...Object.keys(current)])) {
143
+ for (const name of new Set([
144
+ ...Object.keys(old),
145
+ ...Object.keys(current),
146
+ ])) {
127
147
  if (old[name] !== current[name]) {
128
- rows.push({ section, name, from: old[name] ?? null, to: current[name] ?? null })
148
+ rows.push({
149
+ section,
150
+ name,
151
+ from: old[name] ?? null,
152
+ to: current[name] ?? null,
153
+ })
129
154
  }
130
155
  }
131
156
  }
132
157
 
133
- return rows.sort((a, b) => a.section.localeCompare(b.section) || a.name.localeCompare(b.name))
158
+ return rows.sort(
159
+ (a, b) =>
160
+ a.section.localeCompare(b.section) || a.name.localeCompare(b.name),
161
+ )
134
162
  }
135
163
 
136
164
  /** The changes as a Markdown table, for the merge request and the commit. */
137
165
  export function formatChanges(rows) {
138
- const lines = ['| Section | Package | From | To |', '| --- | --- | --- | --- |']
166
+ const lines = [
167
+ '| Section | Package | From | To |',
168
+ '| --- | --- | --- | --- |',
169
+ ]
139
170
 
140
171
  for (const row of rows) {
141
- lines.push(`| ${row.section} | ${row.name} | ${row.from ?? '-'} | ${row.to ?? '-'} |`)
172
+ lines.push(
173
+ `| ${row.section} | ${row.name} | ${row.from ?? '-'} | ${row.to ?? '-'} |`,
174
+ )
142
175
  }
143
176
 
144
177
  return lines.join('\n')
@@ -146,11 +179,15 @@ export function formatChanges(rows) {
146
179
 
147
180
  /** The identity and the key used to sign, from the group variables. It fails when one is missing: no unsigned tag. */
148
181
  export function signerIdentity(env) {
149
- const missing = ['GPG_PRIVATE_KEY_B64', 'RELEASE_SIGNER_NAME', 'RELEASE_SIGNER_EMAIL'].filter((name) => !env[name])
182
+ const missing = [
183
+ 'GPG_PRIVATE_KEY_B64',
184
+ 'RELEASE_SIGNER_NAME',
185
+ 'RELEASE_SIGNER_EMAIL',
186
+ ].filter((name) => !env[name])
150
187
 
151
- if (0 < missing.length) {
188
+ if (missing.length > 0) {
152
189
  throw new Error(
153
- `${missing.join(', ')} not set: the commits and the tags are signed, see the handbook (versioning).`
190
+ `${missing.join(', ')} not set: the commits and the tags are signed, see the handbook (versioning).`,
154
191
  )
155
192
  }
156
193
 
@@ -162,16 +199,38 @@ export function signerIdentity(env) {
162
199
  }
163
200
 
164
201
  /** What `tag` has to do: the version to create, or null (and why). */
165
- export function planTag({ config, last, commits, before, after, sections = RUNTIME_SECTIONS }) {
166
- if ('never' === config.tag.when) return { version: null, reason: 'the project is never tagged by the update' }
202
+ export function planTag({
203
+ config,
204
+ last,
205
+ commits,
206
+ before,
207
+ after,
208
+ sections = RUNTIME_SECTIONS,
209
+ }) {
210
+ if (config.tag.when === 'never')
211
+ return {
212
+ version: null,
213
+ reason: 'the project is never tagged by the update',
214
+ }
167
215
  if (!last) return { version: null, reason: 'there is no tag yet' }
168
- if (0 === commits.length) return { version: null, reason: 'no update since the last tag' }
169
-
170
- if ('runtime' === config.tag.when && !runtimeChanged(before, after, sections)) {
171
- return { version: null, reason: 'only the development dependencies changed: the published archive is the same' }
216
+ if (commits.length === 0)
217
+ return { version: null, reason: 'no update since the last tag' }
218
+
219
+ if (
220
+ config.tag.when === 'runtime' &&
221
+ !runtimeChanged(before, after, sections)
222
+ ) {
223
+ return {
224
+ version: null,
225
+ reason:
226
+ 'only the development dependencies changed: the published archive is the same',
227
+ }
172
228
  }
173
229
 
174
- return { version: nextVersion(last, config.tag.level), reason: 'a published range changed' }
230
+ return {
231
+ version: nextVersion(last, config.tag.level),
232
+ reason: 'a published range changed',
233
+ }
175
234
  }
176
235
 
177
236
  /**
@@ -179,9 +238,14 @@ export function planTag({ config, last, commits, before, after, sections = RUNTI
179
238
  * pipeline to the installed version: that is the only change of the pipeline file an update may commit.
180
239
  */
181
240
  export function isRefOnlyChange(diff) {
182
- const changed = diff.split('\n').filter((line) => /^[+-]/.test(line) && !/^(\+\+\+|---)/.test(line))
241
+ const changed = diff
242
+ .split('\n')
243
+ .filter((line) => /^[+-]/.test(line) && !/^(\+\+\+|---)/.test(line))
183
244
 
184
- return 2 === changed.length && changed.every((line) => /^[+-]\s*ref:\s*\S+\s*$/.test(line))
245
+ return (
246
+ changed.length === 2 &&
247
+ changed.every((line) => /^[+-]\s*ref:\s*\S+\s*$/.test(line))
248
+ )
185
249
  }
186
250
 
187
251
  /** The problems of a policy document, as a list of sentences (empty when it is valid). */
@@ -193,20 +257,27 @@ export function validatePolicy(doc) {
193
257
 
194
258
  doc.entries.forEach((entry, index) => {
195
259
  const where = `entries[${index}]`
196
- const name = 'string' === typeof entry?.package ? entry.package : ''
260
+ const name = typeof entry?.package === 'string' ? entry.package : ''
197
261
 
198
- if ('' === name) errors.push(`${where}: "package" is missing.`)
262
+ if (name === '') errors.push(`${where}: "package" is missing.`)
199
263
  else if (seen.has(name)) errors.push(`${where}: "${name}" is listed twice.`)
200
264
  seen.add(name)
201
265
 
202
- if ('string' !== typeof entry?.reason || '' === entry.reason.trim()) errors.push(`${where}: "reason" is missing.`)
203
- if (!/^\d{4}-\d{2}-\d{2}$/.test(entry?.since ?? '')) errors.push(`${where}: "since" must be a date (YYYY-MM-DD).`)
266
+ if (typeof entry?.reason !== 'string' || entry.reason.trim() === '')
267
+ errors.push(`${where}: "reason" is missing.`)
268
+ if (!/^\d{4}-\d{2}-\d{2}$/.test(entry?.since ?? ''))
269
+ errors.push(`${where}: "since" must be a date (YYYY-MM-DD).`)
204
270
 
205
271
  if (undefined !== entry?.unblock) {
206
272
  const { version, peers } = entry.unblock ?? {}
207
273
 
208
- if (null === semver.valid(version)) errors.push(`${where}: "unblock.version" must be a version (X.Y.Z).`)
209
- if (!Array.isArray(peers) || 0 === peers.length || !peers.every((peer) => 'string' === typeof peer && peer)) {
274
+ if (semver.valid(version) === null)
275
+ errors.push(`${where}: "unblock.version" must be a version (X.Y.Z).`)
276
+ if (
277
+ !Array.isArray(peers) ||
278
+ peers.length === 0 ||
279
+ !peers.every((peer) => typeof peer === 'string' && peer)
280
+ ) {
210
281
  errors.push(`${where}: "unblock.peers" must be a list of packages.`)
211
282
  }
212
283
  }
@@ -219,28 +290,41 @@ export function validatePolicy(doc) {
219
290
  export async function fetchPolicy(
220
291
  ecosystem,
221
292
  fetchImpl = fetch,
222
- base = process.env.DEPENDENCY_POLICY_URL || POLICY_URL
293
+ base = process.env.DEPENDENCY_POLICY_URL || POLICY_URL,
223
294
  ) {
224
295
  const url = `${base.replace(/\/$/, '')}/${POLICY_FILES[ecosystem]}`
225
296
  let doc
226
297
 
227
298
  try {
228
- const response = await fetchImpl(url, { signal: AbortSignal.timeout(20000) })
299
+ const response = await fetchImpl(url, {
300
+ signal: AbortSignal.timeout(20000),
301
+ })
229
302
  if (!response.ok) throw new Error(`HTTP ${response.status}`)
230
303
  doc = await response.json()
231
304
  } catch (error) {
232
- throw new Error(`The dependency policy could not be read (${url}): ${error.message}`, { cause: error })
305
+ throw new Error(
306
+ `The dependency policy could not be read (${url}): ${error.message}`,
307
+ { cause: error },
308
+ )
233
309
  }
234
310
 
235
311
  const errors = validatePolicy(doc)
236
- if (0 < errors.length) throw new Error(`The dependency policy is not valid (${url}): ${errors.join(' ')}`)
312
+ if (errors.length > 0)
313
+ throw new Error(
314
+ `The dependency policy is not valid (${url}): ${errors.join(' ')}`,
315
+ )
237
316
 
238
317
  return doc
239
318
  }
240
319
 
241
320
  /** The packages that the project never updates: its own `reject` and the packages of the policy. */
242
321
  export function rejectedPackages(config, policy) {
243
- return [...new Set([...config.reject, ...policy.entries.map((entry) => entry.package)])]
322
+ return [
323
+ ...new Set([
324
+ ...config.reject,
325
+ ...policy.entries.map((entry) => entry.package),
326
+ ]),
327
+ ]
244
328
  }
245
329
 
246
330
  /** Whether the condition of an entry is met: every package of `peers` accepts the version in its peer range. */
@@ -251,7 +335,9 @@ export function isLifted(entry, ranges) {
251
335
  const range = ranges[peer]
252
336
 
253
337
  return (
254
- 'string' === typeof range && null !== semver.validRange(range) && semver.satisfies(entry.unblock.version, range)
338
+ typeof range === 'string' &&
339
+ semver.validRange(range) !== null &&
340
+ semver.satisfies(entry.unblock.version, range)
255
341
  )
256
342
  })
257
343
  }
@@ -261,10 +347,14 @@ export function lockedVersions(lock) {
261
347
  const versions = {}
262
348
 
263
349
  for (const [path, info] of Object.entries(lock?.packages ?? {})) {
264
- if ('' === path || !info?.version) continue
350
+ if (path === '' || !info?.version) continue
265
351
 
266
- const name = path.slice(path.lastIndexOf('node_modules/') + 'node_modules/'.length)
267
- versions[name] = [...new Set([...(versions[name] ?? []), info.version])].sort()
352
+ const name = path.slice(
353
+ path.lastIndexOf('node_modules/') + 'node_modules/'.length,
354
+ )
355
+ versions[name] = [
356
+ ...new Set([...(versions[name] ?? []), info.version]),
357
+ ].sort()
268
358
  }
269
359
 
270
360
  return versions
@@ -274,12 +364,20 @@ export function lockedVersions(lock) {
274
364
  * The packages of the policy whose version changes: a range of package.json or a locked version. A package that is
275
365
  * added or removed is not an update.
276
366
  */
277
- export function policyViolations({ blocked, beforeManifest, afterManifest, beforeLock, afterLock }) {
367
+ export function policyViolations({
368
+ blocked,
369
+ beforeManifest,
370
+ afterManifest,
371
+ beforeLock,
372
+ afterLock,
373
+ }) {
278
374
  const problems = []
279
375
 
280
376
  for (const row of diffDependencies(beforeManifest, afterManifest)) {
281
- if (blocked.includes(row.name) && null !== row.from && null !== row.to) {
282
- problems.push(`package.json: ${row.name} moves from ${row.from} to ${row.to}.`)
377
+ if (blocked.includes(row.name) && row.from !== null && row.to !== null) {
378
+ problems.push(
379
+ `package.json: ${row.name} moves from ${row.from} to ${row.to}.`,
380
+ )
283
381
  }
284
382
  }
285
383
 
@@ -287,8 +385,14 @@ export function policyViolations({ blocked, beforeManifest, afterManifest, befor
287
385
  const after = lockedVersions(afterLock)
288
386
 
289
387
  for (const name of blocked) {
290
- if (before[name] && after[name] && before[name].join(', ') !== after[name].join(', ')) {
291
- problems.push(`package-lock.json: ${name} moves from ${before[name].join(', ')} to ${after[name].join(', ')}.`)
388
+ if (
389
+ before[name] &&
390
+ after[name] &&
391
+ before[name].join(', ') !== after[name].join(', ')
392
+ ) {
393
+ problems.push(
394
+ `package-lock.json: ${name} moves from ${before[name].join(', ')} to ${after[name].join(', ')}.`,
395
+ )
292
396
  }
293
397
  }
294
398
 
@@ -304,17 +408,24 @@ export function syncDependabot(text, names, ecosystem) {
304
408
  const end = `# END dependency-policy (${ecosystem})`
305
409
  const pattern = new RegExp(
306
410
  `^([ \\t]*)${begin.replaceAll(/[()]/g, '\\$&')}\\n[\\s\\S]*?^[ \\t]*${end.replaceAll(/[()]/g, '\\$&')}$`,
307
- 'm'
411
+ 'm',
308
412
  )
309
413
  const match = pattern.exec(text)
310
414
 
311
- if (null === match) return null
415
+ if (match === null) return null
312
416
 
313
417
  const indent = match[1]
314
418
  const rules =
315
- 0 === names.length ? [] : [`${indent}ignore:`, ...names.map((name) => `${indent} - dependency-name: "${name}"`)]
316
-
317
- return text.replace(pattern, () => [`${indent}${begin}`, ...rules, `${indent}${end}`].join('\n'))
419
+ names.length === 0
420
+ ? []
421
+ : [
422
+ `${indent}ignore:`,
423
+ ...names.map((name) => `${indent} - dependency-name: '${name}'`),
424
+ ]
425
+
426
+ return text.replace(pattern, () =>
427
+ [`${indent}${begin}`, ...rules, `${indent}${end}`].join('\n'),
428
+ )
318
429
  }
319
430
 
320
431
  /**
@@ -325,18 +436,23 @@ export function fixedOverrides(overrides, vulnerabilities, versionsOf) {
325
436
  const fixes = {}
326
437
 
327
438
  for (const [name, pin] of Object.entries(overrides ?? {})) {
328
- const pinned = 'string' === typeof pin ? pin : pin?.['.']
439
+ const pinned = typeof pin === 'string' ? pin : pin?.['.']
329
440
  const range = vulnerabilities?.[name]?.range
330
441
 
331
- if (null === semver.valid(pinned) || 'string' !== typeof range || null === semver.validRange(range)) continue
442
+ if (
443
+ semver.valid(pinned) === null ||
444
+ typeof range !== 'string' ||
445
+ semver.validRange(range) === null
446
+ )
447
+ continue
332
448
  if (!semver.satisfies(pinned, range)) continue
333
449
 
334
450
  const candidates = (versionsOf(name) ?? []).filter(
335
451
  (version) =>
336
- null !== semver.valid(version) &&
337
- null === semver.prerelease(version) &&
452
+ semver.valid(version) !== null &&
453
+ semver.prerelease(version) === null &&
338
454
  semver.major(version) === semver.major(pinned) &&
339
- !semver.satisfies(version, range)
455
+ !semver.satisfies(version, range),
340
456
  )
341
457
  const best = semver.maxSatisfying(candidates, '*')
342
458
 
@@ -352,8 +468,10 @@ export function fixedOverrides(overrides, vulnerabilities, versionsOf) {
352
468
  */
353
469
  export function refreshableNames(lock, scopes = TRUSTED_SCOPES) {
354
470
  const names = Object.keys(lock?.packages ?? {})
355
- .filter((path) => '' !== path)
356
- .map((path) => path.slice(path.lastIndexOf('node_modules/') + 'node_modules/'.length))
471
+ .filter((path) => path !== '')
472
+ .map((path) =>
473
+ path.slice(path.lastIndexOf('node_modules/') + 'node_modules/'.length),
474
+ )
357
475
  .filter((name) => !scopes.some((scope) => name.startsWith(scope)))
358
476
 
359
477
  return [...new Set(names)].sort()
@@ -379,26 +497,37 @@ export function isInherited(lock, name, scope = GROUP_SCOPE) {
379
497
  const edges = new Map()
380
498
 
381
499
  for (const [path, node] of Object.entries(packages)) {
382
- if ('' === path) continue
500
+ if (path === '') continue
383
501
 
384
- const key = path.slice(path.lastIndexOf('node_modules/') + 'node_modules/'.length)
502
+ const key = path.slice(
503
+ path.lastIndexOf('node_modules/') + 'node_modules/'.length,
504
+ )
385
505
  const next = edges.get(key) ?? new Set()
386
506
 
387
- for (const section of ['dependencies', 'optionalDependencies', 'peerDependencies']) {
388
- Object.keys(node[section] ?? {}).forEach((dependency) => next.add(dependency))
507
+ for (const section of [
508
+ 'dependencies',
509
+ 'optionalDependencies',
510
+ 'peerDependencies',
511
+ ]) {
512
+ Object.keys(node[section] ?? {}).forEach((dependency) =>
513
+ next.add(dependency),
514
+ )
389
515
  }
390
516
  edges.set(key, next)
391
517
  }
392
518
 
393
519
  const root = packages['']
394
- const direct = ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies'].flatMap((section) =>
395
- Object.keys(root?.[section] ?? {})
396
- )
520
+ const direct = [
521
+ 'dependencies',
522
+ 'devDependencies',
523
+ 'optionalDependencies',
524
+ 'peerDependencies',
525
+ ].flatMap((section) => Object.keys(root?.[section] ?? {}))
397
526
  const reaches = (starts) => {
398
527
  const seen = new Set(starts)
399
528
  const queue = [...starts]
400
529
 
401
- while (0 < queue.length) {
530
+ while (queue.length > 0) {
402
531
  for (const next of edges.get(queue.shift()) ?? []) {
403
532
  if (!seen.has(next)) {
404
533
  seen.add(next)
@@ -410,15 +539,20 @@ export function isInherited(lock, name, scope = GROUP_SCOPE) {
410
539
  return seen
411
540
  }
412
541
 
413
- const ours = reaches(direct.filter((dependency) => !dependency.startsWith(scope)))
414
- const theirs = reaches(direct.filter((dependency) => dependency.startsWith(scope)))
542
+ const ours = reaches(
543
+ direct.filter((dependency) => !dependency.startsWith(scope)),
544
+ )
545
+ const theirs = reaches(
546
+ direct.filter((dependency) => dependency.startsWith(scope)),
547
+ )
415
548
 
416
549
  return theirs.has(name) && !ours.has(name)
417
550
  }
418
551
 
419
552
  /** The id of an advisory: the last part of its URL (GHSA-xxxx-xxxx-xxxx), else the number npm gives it. */
420
553
  function advisoryId(via) {
421
- const fromUrl = 'string' === typeof via.url ? via.url.split('/').filter(Boolean).pop() : ''
554
+ const fromUrl =
555
+ typeof via.url === 'string' ? via.url.split('/').filter(Boolean).pop() : ''
422
556
 
423
557
  return fromUrl || String(via.source ?? '')
424
558
  }
@@ -430,7 +564,7 @@ export function parseNpmAudit(report) {
430
564
  for (const entry of Object.values(report?.vulnerabilities ?? {})) {
431
565
  for (const via of entry.via ?? []) {
432
566
  // A string only names another vulnerable package: the advisory itself is on that one.
433
- if ('object' !== typeof via || null === via) continue
567
+ if (typeof via !== 'object' || via === null) continue
434
568
 
435
569
  const row = {
436
570
  id: advisoryId(via),
@@ -445,7 +579,9 @@ export function parseNpmAudit(report) {
445
579
  }
446
580
  }
447
581
 
448
- return [...rows.values()].sort((a, b) => `${a.package}|${a.id}`.localeCompare(`${b.package}|${b.id}`))
582
+ return [...rows.values()].sort((a, b) =>
583
+ `${a.package}|${a.id}`.localeCompare(`${b.package}|${b.id}`),
584
+ )
449
585
  }
450
586
 
451
587
  /** The title of the issue of an advisory. It is also the key that keeps the issue from being opened twice. */
@@ -469,18 +605,26 @@ export function isNotable(severity) {
469
605
  */
470
606
  export function auditAlert(opened, project) {
471
607
  const notable = opened.filter(({ row }) => isNotable(row.severity))
472
- if (0 === notable.length) return null
608
+ if (notable.length === 0) return null
473
609
 
474
610
  const silent = opened.length - notable.length
475
- const lines = notable.map(({ row, url }) => `${row.package} (${row.id}, ${row.severity || 'unknown'}): ${url}`)
476
- if (0 < silent) lines.push(`${silent} more of a low severity, issue only.`)
611
+ const lines = notable.map(
612
+ ({ row, url }) =>
613
+ `${row.package} (${row.id}, ${row.severity || 'unknown'}): ${url}`,
614
+ )
615
+ if (silent > 0) lines.push(`${silent} more of a low severity, issue only.`)
477
616
 
478
- return { title: `Security advisories: ${notable.length} new in ${project}`, message: lines.join('\n') }
617
+ return {
618
+ title: `Security advisories: ${notable.length} new in ${project}`,
619
+ message: lines.join('\n'),
620
+ }
479
621
  }
480
622
 
481
623
  /** The labels of the issue of an advisory: the kind and the area, the priority from the severity, and Status::Ready. */
482
624
  export function advisoryLabels(row) {
483
- const priority = { critical: 'Priority::Critical', high: 'Priority::High' }[row.severity] ?? 'Priority::Low'
625
+ const priority =
626
+ { critical: 'Priority::Critical', high: 'Priority::High' }[row.severity] ??
627
+ 'Priority::Low'
484
628
 
485
629
  return [...ISSUE_LABELS, priority, 'Status::Ready'].join(',')
486
630
  }
@@ -502,7 +646,7 @@ export function advisoryIssueBody(row, pipelineUrl, autoFix) {
502
646
  ].join('\n')
503
647
  }
504
648
 
505
- // ---------------------------------------------------------------------------------------------------------------------
649
+ // ================
506
650
 
507
651
  const log = (message) => console.info(message)
508
652
 
@@ -516,9 +660,18 @@ function secret(text) {
516
660
 
517
661
  function run(command, args = [], options = {}) {
518
662
  try {
519
- return execFileSync(command, args, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], ...options }).trim()
663
+ return execFileSync(command, args, {
664
+ encoding: 'utf8',
665
+ stdio: ['ignore', 'pipe', 'pipe'],
666
+ ...options,
667
+ }).trim()
520
668
  } catch (error) {
521
- throw new Error(secret(`${command} ${args.join(' ')} failed: ${error.stderr || error.message}`), { cause: error })
669
+ throw new Error(
670
+ secret(
671
+ `${command} ${args.join(' ')} failed: ${error.stderr || error.message}`,
672
+ ),
673
+ { cause: error },
674
+ )
522
675
  }
523
676
  }
524
677
 
@@ -533,16 +686,27 @@ function capture(command, args = []) {
533
686
  } catch (error) {
534
687
  if (error.stdout) return error.stdout
535
688
 
536
- throw new Error(secret(`${command} ${args.join(' ')} failed: ${error.stderr || error.message}`), { cause: error })
689
+ throw new Error(
690
+ secret(
691
+ `${command} ${args.join(' ')} failed: ${error.stderr || error.message}`,
692
+ ),
693
+ { cause: error },
694
+ )
537
695
  }
538
696
  }
539
697
 
540
698
  async function api(method, path, body, token = process.env.GITLAB_TOKEN) {
541
- const response = await fetch(`${process.env.CI_API_V4_URL ?? 'https://gitlab.com/api/v4'}${path}`, {
542
- method,
543
- headers: { 'PRIVATE-TOKEN': token ?? '', 'Content-Type': 'application/json' },
544
- body: body === undefined ? undefined : JSON.stringify(body),
545
- })
699
+ const response = await fetch(
700
+ `${process.env.CI_API_V4_URL ?? 'https://gitlab.com/api/v4'}${path}`,
701
+ {
702
+ method,
703
+ headers: {
704
+ 'PRIVATE-TOKEN': token ?? '',
705
+ 'Content-Type': 'application/json',
706
+ },
707
+ body: body === undefined ? undefined : JSON.stringify(body),
708
+ },
709
+ )
546
710
  const text = await response.text()
547
711
 
548
712
  return { status: response.status, ok: response.ok, data: parseBody(text) }
@@ -558,16 +722,24 @@ function parseBody(text) {
558
722
  }
559
723
  }
560
724
 
561
- const sleep = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds))
725
+ const sleep = (milliseconds) =>
726
+ new Promise((resolve) => setTimeout(resolve, milliseconds))
562
727
 
563
728
  function projectId() {
564
729
  return process.env.CI_PROJECT_ID
565
730
  }
566
731
 
567
732
  function assertEnvironment() {
568
- for (const name of ['GITLAB_TOKEN', 'CI_PROJECT_ID', 'CI_PROJECT_PATH', 'CI_SERVER_HOST']) {
733
+ for (const name of [
734
+ 'GITLAB_TOKEN',
735
+ 'CI_PROJECT_ID',
736
+ 'CI_PROJECT_PATH',
737
+ 'CI_SERVER_HOST',
738
+ ]) {
569
739
  if (!process.env[name])
570
- throw new Error(`${name} is not set: run this in GitLab CI, with the group variable GITLAB_TOKEN.`)
740
+ throw new Error(
741
+ `${name} is not set: run this in GitLab CI, with the group variable GITLAB_TOKEN.`,
742
+ )
571
743
  }
572
744
 
573
745
  // The runner clones the project with another user than the one of the job: Git refuses it otherwise.
@@ -600,7 +772,9 @@ function ensureGpg() {
600
772
  if (available()) return
601
773
  }
602
774
 
603
- throw new Error('gpg is not installed and could not be added: add gnupg to the image of the job.')
775
+ throw new Error(
776
+ 'gpg is not installed and could not be added: add gnupg to the image of the job.',
777
+ )
604
778
  }
605
779
 
606
780
  /** Import the signing key and make Git sign the commits and the tags with it. */
@@ -609,7 +783,10 @@ function configureSigning() {
609
783
 
610
784
  ensureGpg()
611
785
 
612
- run('gpg', ['--batch', '--import'], { input: signer.key, stdio: ['pipe', 'pipe', 'pipe'] })
786
+ run('gpg', ['--batch', '--import'], {
787
+ input: signer.key,
788
+ stdio: ['pipe', 'pipe', 'pipe'],
789
+ })
613
790
  const fingerprint = run('gpg', ['--list-secret-keys', '--with-colons'])
614
791
  .split('\n')
615
792
  .find((line) => line.startsWith('fpr'))
@@ -628,9 +805,13 @@ function remoteUrl() {
628
805
 
629
806
  async function mergeRequestOptions() {
630
807
  const user = await api('GET', '/user')
631
- const milestones = await api('GET', `/projects/${projectId()}/milestones?state=active`)
808
+ const milestones = await api(
809
+ 'GET',
810
+ `/projects/${projectId()}/milestones?state=active`,
811
+ )
632
812
  const milestone = Array.isArray(milestones.data)
633
- ? (milestones.data.find((item) => 'Maintenance' === item.title) ?? milestones.data[0])
813
+ ? (milestones.data.find((item) => item.title === 'Maintenance') ??
814
+ milestones.data[0])
634
815
  : null
635
816
 
636
817
  return {
@@ -643,11 +824,15 @@ async function mergeRequestOptions() {
643
824
 
644
825
  /** Merge the request when its pipeline succeeds. GitLab needs a moment to create the pipeline of a new request. */
645
826
  async function mergeWhenGreen(iid) {
646
- for (let attempt = 0; 18 > attempt; attempt++) {
647
- const result = await api('PUT', `/projects/${projectId()}/merge_requests/${iid}/merge`, {
648
- merge_when_pipeline_succeeds: true,
649
- should_remove_source_branch: true,
650
- })
827
+ for (let attempt = 0; attempt < 18; attempt++) {
828
+ const result = await api(
829
+ 'PUT',
830
+ `/projects/${projectId()}/merge_requests/${iid}/merge`,
831
+ {
832
+ merge_when_pipeline_succeeds: true,
833
+ should_remove_source_branch: true,
834
+ },
835
+ )
651
836
 
652
837
  if (result.ok) {
653
838
  log(`Merge request !${iid}: it is merged when its pipeline succeeds.`)
@@ -657,22 +842,34 @@ async function mergeWhenGreen(iid) {
657
842
  await sleep(10_000)
658
843
  }
659
844
 
660
- log(`Merge request !${iid}: it could not be set to merge by itself, it stays open.`)
845
+ log(
846
+ `Merge request !${iid}: it could not be set to merge by itself, it stays open.`,
847
+ )
661
848
 
662
849
  return false
663
850
  }
664
851
 
665
852
  async function openMergeRequests(query = '') {
666
- const result = await api('GET', `/projects/${projectId()}/merge_requests?state=opened&per_page=100${query}`)
853
+ const result = await api(
854
+ 'GET',
855
+ `/projects/${projectId()}/merge_requests?state=opened&per_page=100${query}`,
856
+ )
667
857
 
668
858
  return Array.isArray(result.data) ? result.data : []
669
859
  }
670
860
 
671
861
  function ncu(args) {
672
- return run('npx', ['--yes', 'npm-check-updates', '--target', 'minor', ...args, '-u'])
862
+ return run('npx', [
863
+ '--yes',
864
+ 'npm-check-updates',
865
+ '--target',
866
+ 'minor',
867
+ ...args,
868
+ '-u',
869
+ ])
673
870
  }
674
871
 
675
- // ---------------------------------------------------------------------------------------------------------------------
872
+ // ================
676
873
 
677
874
  /** Raise the overrides that pin a vulnerable version, then install. Returns the raised ones. */
678
875
  function raiseVulnerableOverrides() {
@@ -680,16 +877,20 @@ function raiseVulnerableOverrides() {
680
877
  if (!manifest.overrides) return {}
681
878
 
682
879
  const report = JSON.parse(capture('npm', ['audit', '--json']) || '{}')
683
- const fixes = fixedOverrides(manifest.overrides, report.vulnerabilities, (name) =>
684
- JSON.parse(capture('npm', ['view', name, 'versions', '--json']) || '[]')
880
+ const fixes = fixedOverrides(
881
+ manifest.overrides,
882
+ report.vulnerabilities,
883
+ (name) =>
884
+ JSON.parse(capture('npm', ['view', name, 'versions', '--json']) || '[]'),
685
885
  )
686
886
 
687
887
  for (const [name, version] of Object.entries(fixes)) {
688
- if ('string' === typeof manifest.overrides[name]) manifest.overrides[name] = version
888
+ if (typeof manifest.overrides[name] === 'string')
889
+ manifest.overrides[name] = version
689
890
  else manifest.overrides[name]['.'] = version
690
891
  }
691
892
 
692
- if (0 < Object.keys(fixes).length) {
893
+ if (Object.keys(fixes).length > 0) {
693
894
  writeFileSync('package.json', `${JSON.stringify(manifest, null, 2)}\n`)
694
895
  run('npm', ['install', '--no-audit', '--no-fund'])
695
896
  }
@@ -700,7 +901,11 @@ function raiseVulnerableOverrides() {
700
901
  async function update(config) {
701
902
  assertEnvironment()
702
903
 
703
- if ((await openMergeRequests()).some((request) => request.source_branch.startsWith(BRANCH_PREFIX))) {
904
+ if (
905
+ (await openMergeRequests()).some((request) =>
906
+ request.source_branch.startsWith(BRANCH_PREFIX),
907
+ )
908
+ ) {
704
909
  log('An update is already open: nothing to do.')
705
910
  return
706
911
  }
@@ -712,12 +917,12 @@ async function update(config) {
712
917
  run('git', ['switch', '-C', branch])
713
918
 
714
919
  // A PHP project was prepared by the job before this one, in its own image: only npm is updated here.
715
- if ('package.json' === ecosystem.file) {
920
+ if (ecosystem.file === 'package.json') {
716
921
  // The new versions wait for the cooling period. The packages of the group are trusted: no waiting for them.
717
922
  const rejected = rejectedPackages(config, await readPolicy('npm'))
718
- const reject = 0 < rejected.length ? ['--reject', rejected.join(',')] : []
923
+ const reject = rejected.length > 0 ? ['--reject', rejected.join(',')] : []
719
924
  // An update started by the audit (UPDATE_URGENT=true) fixes an advisory: the fix is not made to wait.
720
- const urgent = 'true' === process.env.UPDATE_URGENT
925
+ const urgent = process.env.UPDATE_URGENT === 'true'
721
926
  ncu([...(urgent ? [] : ['--cooldown', config.cooldown]), ...reject])
722
927
  ncu(['--filter', '@zairakai/*', ...reject])
723
928
  run('npm', ['install', '--no-audit', '--no-fund'])
@@ -725,33 +930,48 @@ async function update(config) {
725
930
  // The lock file is refreshed inside the ranges, so that a fix of a package that this project only inherits arrives
726
931
  // (overrides of a dependency do not apply here). The new versions wait for the cooling period, except for a fix.
727
932
  const before = cooldownDate(config.cooldown)
728
- const names = refreshableNames(JSON.parse(readFileSync('package-lock.json', 'utf8')))
933
+ const names = refreshableNames(
934
+ JSON.parse(readFileSync('package-lock.json', 'utf8')),
935
+ )
729
936
 
730
937
  // The refresh is a bonus: when npm cannot respect the cooling date (a range that starts at a version younger than
731
938
  // the period, after an urgent update), it waits for the next run and the update goes on.
732
939
  try {
733
- if (urgent || null === before) {
940
+ if (urgent || before === null) {
734
941
  run('npm', ['update', '--no-audit', '--no-fund'])
735
- } else if (0 < names.length) {
736
- run('npm', ['update', '--no-audit', '--no-fund', `--before=${before.toISOString()}`, ...names])
942
+ } else if (names.length > 0) {
943
+ run('npm', [
944
+ 'update',
945
+ '--no-audit',
946
+ '--no-fund',
947
+ `--before=${before.toISOString()}`,
948
+ ...names,
949
+ ])
737
950
  }
738
951
  } catch (error) {
739
- log(`The lock file is not refreshed this time: ${secret(error.message).split('\n')[0]}`)
952
+ log(
953
+ `The lock file is not refreshed this time: ${secret(error.message).split('\n')[0]}`,
954
+ )
740
955
  }
741
956
 
742
957
  if (urgent) {
743
958
  try {
744
959
  run('npm', ['audit', 'fix', '--no-fund'])
745
960
  } catch (error) {
746
- log(`npm audit fix did not apply: ${secret(error.message).split('\n')[0]}`)
961
+ log(
962
+ `npm audit fix did not apply: ${secret(error.message).split('\n')[0]}`,
963
+ )
747
964
  }
748
965
 
749
966
  // npm audit fix leaves the overrides alone: one that pins a vulnerable version is raised here.
750
967
  try {
751
968
  const raised = raiseVulnerableOverrides()
752
- for (const [name, version] of Object.entries(raised)) log(`The override of ${name} is raised to ${version}.`)
969
+ for (const [name, version] of Object.entries(raised))
970
+ log(`The override of ${name} is raised to ${version}.`)
753
971
  } catch (error) {
754
- log(`The overrides are not raised this time: ${secret(error.message).split('\n')[0]}`)
972
+ log(
973
+ `The overrides are not raised this time: ${secret(error.message).split('\n')[0]}`,
974
+ )
755
975
  }
756
976
  }
757
977
  }
@@ -760,14 +980,22 @@ async function update(config) {
760
980
  const rows = diffDependencies(before, after, ecosystem.all)
761
981
 
762
982
  // Only the manifest and the lock file: the postinstall of the tools can rewrite other files of the project in CI.
763
- const files = [ecosystem.file, ...['package-lock.json', 'composer.lock'].filter((file) => existsSync(file))]
983
+ const files = [
984
+ ecosystem.file,
985
+ ...['package-lock.json', 'composer.lock'].filter((file) =>
986
+ existsSync(file),
987
+ ),
988
+ ]
764
989
 
765
990
  // The one exception: the `ref:` of the shared pipeline follows the installed version of the tools.
766
- if (existsSync(PIPELINE_FILE) && isRefOnlyChange(run('git', ['diff', '-U0', '--', PIPELINE_FILE]))) {
991
+ if (
992
+ existsSync(PIPELINE_FILE) &&
993
+ isRefOnlyChange(run('git', ['diff', '-U0', '--', PIPELINE_FILE]))
994
+ ) {
767
995
  files.push(PIPELINE_FILE)
768
996
  }
769
997
 
770
- if ('' === run('git', ['status', '--porcelain', '--', ...files])) {
998
+ if (run('git', ['status', '--porcelain', '--', ...files]) === '') {
771
999
  log('Everything is up to date.')
772
1000
  return
773
1001
  }
@@ -792,7 +1020,10 @@ async function update(config) {
792
1020
  ...(await mergeRequestOptions()),
793
1021
  })
794
1022
 
795
- if (!request.ok) throw new Error(`The merge request could not be created: ${secret(JSON.stringify(request.data))}`)
1023
+ if (!request.ok)
1024
+ throw new Error(
1025
+ `The merge request could not be created: ${secret(JSON.stringify(request.data))}`,
1026
+ )
796
1027
 
797
1028
  log(`Merge request !${request.data.iid} created.`)
798
1029
  await mergeWhenGreen(request.data.iid)
@@ -803,9 +1034,14 @@ async function closeObsolete() {
803
1034
  try {
804
1035
  const found = parseNpmAudit(JSON.parse(capture('npm', ['audit', '--json'])))
805
1036
 
806
- await closeResolved(found.map(advisoryTitle), process.env.FRESHNESS_TOKEN || process.env.GITLAB_TOKEN)
1037
+ await closeResolved(
1038
+ found.map(advisoryTitle),
1039
+ process.env.FRESHNESS_TOKEN || process.env.GITLAB_TOKEN,
1040
+ )
807
1041
  } catch (error) {
808
- log(`The obsolete issues could not be checked: ${secret(error.message).split('\n')[0]}`)
1042
+ log(
1043
+ `The obsolete issues could not be checked: ${secret(error.message).split('\n')[0]}`,
1044
+ )
809
1045
  }
810
1046
  }
811
1047
 
@@ -815,9 +1051,11 @@ async function release() {
815
1051
  // The update that was just merged into develop may fix advisories: their issues have no reason to stay open.
816
1052
  await closeObsolete()
817
1053
 
818
- const existing = await openMergeRequests('&source_branch=develop&target_branch=main')
1054
+ const existing = await openMergeRequests(
1055
+ '&source_branch=develop&target_branch=main',
1056
+ )
819
1057
 
820
- if (0 < existing.length) {
1058
+ if (existing.length > 0) {
821
1059
  await mergeWhenGreen(existing[0].iid)
822
1060
  return
823
1061
  }
@@ -826,11 +1064,15 @@ async function release() {
826
1064
  source_branch: 'develop',
827
1065
  target_branch: 'main',
828
1066
  title: COMMIT_SUBJECT,
829
- description: 'Brings the automatic update of the dependencies into main. It is merged when its pipeline succeeds.',
1067
+ description:
1068
+ 'Brings the automatic update of the dependencies into main. It is merged when its pipeline succeeds.',
830
1069
  ...(await mergeRequestOptions()),
831
1070
  })
832
1071
 
833
- if (!request.ok) throw new Error(`The merge request could not be created: ${secret(JSON.stringify(request.data))}`)
1072
+ if (!request.ok)
1073
+ throw new Error(
1074
+ `The merge request could not be created: ${secret(JSON.stringify(request.data))}`,
1075
+ )
834
1076
 
835
1077
  log(`Merge request !${request.data.iid} created.`)
836
1078
  await mergeWhenGreen(request.data.iid)
@@ -843,15 +1085,29 @@ async function tag(config) {
843
1085
  const last = latestTag(run('git', ['tag', '--list']).split('\n'))
844
1086
  // git reads a basic regular expression: the parentheses of the subject are literal.
845
1087
  const commits = last
846
- ? run('git', ['log', `${last}..HEAD`, '--format=%s', `--grep=^${COMMIT_SUBJECT}`])
1088
+ ? run('git', [
1089
+ 'log',
1090
+ `${last}..HEAD`,
1091
+ '--format=%s',
1092
+ `--grep=^${COMMIT_SUBJECT}`,
1093
+ ])
847
1094
  .split('\n')
848
1095
  .filter(Boolean)
849
1096
  : []
850
1097
  const ecosystem = ECOSYSTEMS[detectEcosystem()]
851
- const before = last ? JSON.parse(run('git', ['show', `${last}:${ecosystem.file}`])) : null
1098
+ const before = last
1099
+ ? JSON.parse(run('git', ['show', `${last}:${ecosystem.file}`]))
1100
+ : null
852
1101
  const after = JSON.parse(readFileSync(ecosystem.file, 'utf8'))
853
1102
 
854
- const plan = planTag({ config, last, commits, before, after, sections: ecosystem.runtime })
1103
+ const plan = planTag({
1104
+ config,
1105
+ last,
1106
+ commits,
1107
+ before,
1108
+ after,
1109
+ sections: ecosystem.runtime,
1110
+ })
855
1111
 
856
1112
  if (!plan.version) {
857
1113
  log(`No tag: ${plan.reason}.`)
@@ -859,16 +1115,32 @@ async function tag(config) {
859
1115
  }
860
1116
 
861
1117
  configureSigning()
862
- run('git', ['tag', '-s', plan.version, '-m', plan.version, process.env.CI_COMMIT_SHA])
1118
+ run('git', [
1119
+ 'tag',
1120
+ '-s',
1121
+ plan.version,
1122
+ '-m',
1123
+ plan.version,
1124
+ process.env.CI_COMMIT_SHA,
1125
+ ])
863
1126
  run('git', ['push', remoteUrl(), `refs/tags/${plan.version}`])
864
1127
 
865
- log(`Tag ${plan.version} signed and pushed (last tag ${last}): ${plan.reason}. Its pipeline publishes the package.`)
1128
+ log(
1129
+ `Tag ${plan.version} signed and pushed (last tag ${last}): ${plan.reason}. Its pipeline publishes the package.`,
1130
+ )
866
1131
  }
867
1132
 
868
1133
  /** True when the registry of the ecosystem (npm or Packagist) lists the version. */
869
1134
  async function isAvailable(ecosystem, name, version) {
870
- if ('npm' === ecosystem) {
871
- return run('npm', ['view', `${name}@${version}`, 'version', '--prefer-online']) === version
1135
+ if (ecosystem === 'npm') {
1136
+ return (
1137
+ run('npm', [
1138
+ 'view',
1139
+ `${name}@${version}`,
1140
+ 'version',
1141
+ '--prefer-online',
1142
+ ]) === version
1143
+ )
872
1144
  }
873
1145
 
874
1146
  const response = await fetch(`https://repo.packagist.org/p2/${name}.json`)
@@ -877,7 +1149,9 @@ async function isAvailable(ecosystem, name, version) {
877
1149
 
878
1150
  const data = await response.json()
879
1151
 
880
- return (data.packages?.[name] ?? []).some((release) => release.version === version)
1152
+ return (data.packages?.[name] ?? []).some(
1153
+ (release) => release.version === version,
1154
+ )
881
1155
  }
882
1156
 
883
1157
  async function cascade(config) {
@@ -897,7 +1171,7 @@ async function cascade(config) {
897
1171
  // The registry needs a few minutes to list a new version: wait for it instead of waiting for a fixed hour.
898
1172
  let available = false
899
1173
 
900
- for (let attempt = 0; 60 > attempt && !available; attempt++) {
1174
+ for (let attempt = 0; attempt < 60 && !available; attempt++) {
901
1175
  try {
902
1176
  available = await isAvailable(ecosystem, name, version)
903
1177
  } catch {
@@ -907,12 +1181,15 @@ async function cascade(config) {
907
1181
  if (!available) await sleep(30_000)
908
1182
  }
909
1183
 
910
- if (!available) throw new Error(`${name}@${version} is not on the registry after 30 minutes.`)
1184
+ if (!available)
1185
+ throw new Error(
1186
+ `${name}@${version} is not on the registry after 30 minutes.`,
1187
+ )
911
1188
 
912
1189
  for (const group of config.cascade.groups ?? []) {
913
1190
  const projects = await api(
914
1191
  'GET',
915
- `/groups/${encodeURIComponent(group)}/projects?include_subgroups=true&archived=false&per_page=100`
1192
+ `/groups/${encodeURIComponent(group)}/projects?include_subgroups=true&archived=false&per_page=100`,
916
1193
  )
917
1194
 
918
1195
  for (const project of Array.isArray(projects.data) ? projects.data : []) {
@@ -921,7 +1198,7 @@ async function cascade(config) {
921
1198
  // Opt-in: a project is updated only if it has the same file.
922
1199
  const file = await api(
923
1200
  'GET',
924
- `/projects/${project.id}/repository/files/${encodeURIComponent(CONFIG_FILE)}?ref=develop`
1201
+ `/projects/${project.id}/repository/files/${encodeURIComponent(CONFIG_FILE)}?ref=develop`,
925
1202
  )
926
1203
 
927
1204
  if (!file.ok) {
@@ -934,34 +1211,56 @@ async function cascade(config) {
934
1211
  variables: [{ key: 'UPDATE_DEPENDENCIES', value: 'true' }],
935
1212
  })
936
1213
 
937
- log(`${project.path_with_namespace}: ${started.ok ? 'update started' : `not started (${started.status})`}.`)
1214
+ log(
1215
+ `${project.path_with_namespace}: ${started.ok ? 'update started' : `not started (${started.status})`}.`,
1216
+ )
938
1217
  }
939
1218
  }
940
1219
  }
941
1220
 
942
- // ---------------------------------------------------------------------------------------------------------------------
1221
+ // ================
943
1222
 
944
1223
  async function issueExists(title, token) {
945
- const query = new URLSearchParams({ state: 'opened', search: title, in: 'title' })
946
- const result = await api('GET', `/projects/${projectId()}/issues?${query}`, undefined, token)
1224
+ const query = new URLSearchParams({
1225
+ state: 'opened',
1226
+ search: title,
1227
+ in: 'title',
1228
+ })
1229
+ const result = await api(
1230
+ 'GET',
1231
+ `/projects/${projectId()}/issues?${query}`,
1232
+ undefined,
1233
+ token,
1234
+ )
947
1235
 
948
- return (Array.isArray(result.data) ? result.data : []).some((issue) => issue.title === title)
1236
+ return (Array.isArray(result.data) ? result.data : []).some(
1237
+ (issue) => issue.title === title,
1238
+ )
949
1239
  }
950
1240
 
951
1241
  async function createIssue(title, description, labels, token) {
952
1242
  // The assignee is given by the variable ISSUE_ASSIGNEE_ID; without it, the user of the token.
953
1243
  let assignee = Number(process.env.ISSUE_ASSIGNEE_ID) || 0
954
1244
 
955
- if (0 === assignee) assignee = (await api('GET', '/user', undefined, token)).data?.id ?? 0
1245
+ if (assignee === 0)
1246
+ assignee = (await api('GET', '/user', undefined, token)).data?.id ?? 0
956
1247
 
957
1248
  const issue = await api(
958
1249
  'POST',
959
1250
  `/projects/${projectId()}/issues`,
960
- { title, description, labels, assignee_ids: 0 < assignee ? [assignee] : [] },
961
- token
1251
+ {
1252
+ title,
1253
+ description,
1254
+ labels,
1255
+ assignee_ids: assignee > 0 ? [assignee] : [],
1256
+ },
1257
+ token,
962
1258
  )
963
1259
 
964
- if (!issue.ok) throw new Error(`The issue could not be created: ${secret(JSON.stringify(issue.data))}`)
1260
+ if (!issue.ok)
1261
+ throw new Error(
1262
+ `The issue could not be created: ${secret(JSON.stringify(issue.data))}`,
1263
+ )
965
1264
 
966
1265
  return issue.data.web_url
967
1266
  }
@@ -975,20 +1274,35 @@ async function closeResolved(titles, token) {
975
1274
  in: 'title',
976
1275
  per_page: '100',
977
1276
  })
978
- const result = await api('GET', `/projects/${projectId()}/issues?${query}`, undefined, token)
1277
+ const result = await api(
1278
+ 'GET',
1279
+ `/projects/${projectId()}/issues?${query}`,
1280
+ undefined,
1281
+ token,
1282
+ )
979
1283
 
980
1284
  for (const issue of Array.isArray(result.data) ? result.data : []) {
981
- if (!issue.title.startsWith(ADVISORY_TITLE_PREFIX) || titles.includes(issue.title)) continue
1285
+ if (
1286
+ !issue.title.startsWith(ADVISORY_TITLE_PREFIX) ||
1287
+ titles.includes(issue.title)
1288
+ )
1289
+ continue
982
1290
 
983
1291
  const path = `/projects/${projectId()}/issues/${issue.iid}`
984
1292
  await api(
985
1293
  'POST',
986
1294
  `${path}/notes`,
987
- { body: `The audit of ${process.env.CI_PIPELINE_URL} no longer finds this advisory.` },
988
- token
1295
+ {
1296
+ body: `The audit of ${process.env.CI_PIPELINE_URL} no longer finds this advisory.`,
1297
+ },
1298
+ token,
989
1299
  )
990
1300
  const closed = await api('PUT', path, { state_event: 'close' }, token)
991
- log(closed.ok ? `Issue closed: ${issue.title}` : `Issue not closed (${closed.status}): ${issue.title}`)
1301
+ log(
1302
+ closed.ok
1303
+ ? `Issue closed: ${issue.title}`
1304
+ : `Issue not closed (${closed.status}): ${issue.title}`,
1305
+ )
992
1306
  }
993
1307
  }
994
1308
 
@@ -1012,11 +1326,16 @@ async function notify(title, message) {
1012
1326
  async function readPolicy(ecosystem) {
1013
1327
  try {
1014
1328
  const policy = await fetchPolicy(ecosystem)
1015
- log(`Dependency policy: ${policy.entries.map((entry) => entry.package).join(', ') || 'nothing is held back'}.`)
1329
+ log(
1330
+ `Dependency policy: ${policy.entries.map((entry) => entry.package).join(', ') || 'nothing is held back'}.`,
1331
+ )
1016
1332
 
1017
1333
  return policy
1018
1334
  } catch (error) {
1019
- await notify('Dependency policy unreachable', `${process.env.CI_PROJECT_PATH}: ${secret(error.message)}`)
1335
+ await notify(
1336
+ 'Dependency policy unreachable',
1337
+ `${process.env.CI_PROJECT_PATH}: ${secret(error.message)}`,
1338
+ )
1020
1339
  throw error
1021
1340
  }
1022
1341
  }
@@ -1026,8 +1345,10 @@ async function audit() {
1026
1345
  assertEnvironment()
1027
1346
 
1028
1347
  const found = parseNpmAudit(JSON.parse(capture('npm', ['audit', '--json'])))
1029
- const lock = existsSync('package-lock.json') ? JSON.parse(readFileSync('package-lock.json', 'utf8')) : null
1030
- const auto = 'true' === process.env.AUTO_FIX
1348
+ const lock = existsSync('package-lock.json')
1349
+ ? JSON.parse(readFileSync('package-lock.json', 'utf8'))
1350
+ : null
1351
+ const auto = process.env.AUTO_FIX === 'true'
1031
1352
  const token = process.env.FRESHNESS_TOKEN || process.env.GITLAB_TOKEN
1032
1353
  const titles = []
1033
1354
  const opened = []
@@ -1048,7 +1369,9 @@ async function audit() {
1048
1369
  // Inherited from a package of the group: the issue is in the package that brings it, and the update of this project
1049
1370
  // takes the fix when there is one.
1050
1371
  if (isInherited(lock, row.package)) {
1051
- log(`Inherited from the group, followed in the package that brings it: ${title}`)
1372
+ log(
1373
+ `Inherited from the group, followed in the package that brings it: ${title}`,
1374
+ )
1052
1375
  continue
1053
1376
  }
1054
1377
 
@@ -1065,11 +1388,19 @@ async function audit() {
1065
1388
  await closeResolved(titles, token)
1066
1389
 
1067
1390
  if (!auto || !urgent) {
1068
- log(auto ? 'Nothing urgent: the weekly update takes the rest.' : 'AUTO_FIX is off: update by hand.')
1391
+ log(
1392
+ auto
1393
+ ? 'Nothing urgent: the weekly update takes the rest.'
1394
+ : 'AUTO_FIX is off: update by hand.',
1395
+ )
1069
1396
  return
1070
1397
  }
1071
1398
 
1072
- if ((await openMergeRequests()).some((request) => request.source_branch.startsWith(BRANCH_PREFIX))) {
1399
+ if (
1400
+ (await openMergeRequests()).some((request) =>
1401
+ request.source_branch.startsWith(BRANCH_PREFIX),
1402
+ )
1403
+ ) {
1073
1404
  log('An update is already open: nothing to start.')
1074
1405
  return
1075
1406
  }
@@ -1084,15 +1415,23 @@ async function audit() {
1084
1415
  { key: 'UPDATE_URGENT', value: 'true' },
1085
1416
  ],
1086
1417
  },
1087
- token
1418
+ token,
1419
+ )
1420
+ log(
1421
+ started.ok
1422
+ ? 'Update started.'
1423
+ : `The update could not be started (${started.status}).`,
1088
1424
  )
1089
- log(started.ok ? 'Update started.' : `The update could not be started (${started.status}).`)
1090
1425
  }
1091
1426
 
1092
1427
  /** The peer range of `name` in the latest release of `peer`, or null. */
1093
1428
  function peerRange(peer, name) {
1094
1429
  try {
1095
- return JSON.parse(run('npm', ['view', peer, 'peerDependencies', '--json']) || '{}')?.[name] ?? null
1430
+ return (
1431
+ JSON.parse(
1432
+ run('npm', ['view', peer, 'peerDependencies', '--json']) || '{}',
1433
+ )?.[name] ?? null
1434
+ )
1096
1435
  } catch {
1097
1436
  return null
1098
1437
  }
@@ -1113,7 +1452,7 @@ async function policyValidate() {
1113
1452
  }
1114
1453
 
1115
1454
  for (const message of errors) console.error(`${file}: ${message}`)
1116
- failed ||= 0 < errors.length
1455
+ failed ||= errors.length > 0
1117
1456
  }
1118
1457
 
1119
1458
  if (failed) throw new Error('The dependency policy is not valid.')
@@ -1127,22 +1466,29 @@ async function policyWatch() {
1127
1466
  const file = POLICY_FILES.npm
1128
1467
  const doc = JSON.parse(readFileSync(file, 'utf8'))
1129
1468
  const errors = validatePolicy(doc)
1130
- if (0 < errors.length) throw new Error(`${file} is not valid: ${errors.join(' ')}`)
1469
+ if (errors.length > 0)
1470
+ throw new Error(`${file} is not valid: ${errors.join(' ')}`)
1131
1471
 
1132
1472
  const lifted = doc.entries.filter((entry) => {
1133
1473
  if (!entry.unblock) return false
1134
1474
 
1135
- const ranges = Object.fromEntries(entry.unblock.peers.map((peer) => [peer, peerRange(peer, entry.package)]))
1475
+ const ranges = Object.fromEntries(
1476
+ entry.unblock.peers.map((peer) => [peer, peerRange(peer, entry.package)]),
1477
+ )
1136
1478
 
1137
1479
  return isLifted(entry, ranges)
1138
1480
  })
1139
1481
 
1140
- if (0 === lifted.length) {
1482
+ if (lifted.length === 0) {
1141
1483
  log('No entry can be lifted.')
1142
1484
  return
1143
1485
  }
1144
1486
 
1145
- if ((await openMergeRequests()).some((request) => request.source_branch.startsWith(POLICY_BRANCH_PREFIX))) {
1487
+ if (
1488
+ (await openMergeRequests()).some((request) =>
1489
+ request.source_branch.startsWith(POLICY_BRANCH_PREFIX),
1490
+ )
1491
+ ) {
1146
1492
  log('A merge request that lifts an entry is already open.')
1147
1493
  return
1148
1494
  }
@@ -1155,10 +1501,16 @@ async function policyWatch() {
1155
1501
  run('git', ['switch', '-C', branch])
1156
1502
  writeFileSync(
1157
1503
  file,
1158
- `${JSON.stringify({ ...doc, entries: doc.entries.filter((entry) => !lifted.includes(entry)) }, null, 2)}\n`
1504
+ `${JSON.stringify({ ...doc, entries: doc.entries.filter((entry) => !lifted.includes(entry)) }, null, 2)}\n`,
1159
1505
  )
1160
1506
  run('git', ['add', file])
1161
- run('git', ['commit', '-m', subject, '-m', 'The peer ranges of the packages that held it back now accept it.'])
1507
+ run('git', [
1508
+ 'commit',
1509
+ '-m',
1510
+ subject,
1511
+ '-m',
1512
+ 'The peer ranges of the packages that held it back now accept it.',
1513
+ ])
1162
1514
  run('git', ['push', remoteUrl(), `HEAD:refs/heads/${branch}`])
1163
1515
 
1164
1516
  const request = await api('POST', `/projects/${projectId()}/merge_requests`, {
@@ -1170,18 +1522,27 @@ async function policyWatch() {
1170
1522
  ...(await mergeRequestOptions()),
1171
1523
  })
1172
1524
 
1173
- if (!request.ok) throw new Error(`The merge request could not be created: ${secret(JSON.stringify(request.data))}`)
1525
+ if (!request.ok)
1526
+ throw new Error(
1527
+ `The merge request could not be created: ${secret(JSON.stringify(request.data))}`,
1528
+ )
1174
1529
 
1175
1530
  log(`Merge request !${request.data.iid} created.`)
1176
- await notify('Dependency policy: block lifted', `${names}: ${request.data.web_url}`)
1531
+ await notify(
1532
+ 'Dependency policy: block lifted',
1533
+ `${names}: ${request.data.web_url}`,
1534
+ )
1177
1535
  await mergeWhenGreen(request.data.iid)
1178
1536
  }
1179
1537
 
1180
1538
  /** Fail when the working tree updates a package of the policy compared with `base`: a range or a locked version. */
1181
1539
  async function policyCheck(_config, base) {
1182
- if (!base) throw new Error('Usage: node update-dependencies.mjs policy-check <ref>')
1540
+ if (!base)
1541
+ throw new Error('Usage: node update-dependencies.mjs policy-check <ref>')
1183
1542
 
1184
- const blocked = (await readPolicy('npm')).entries.map((entry) => entry.package)
1543
+ const blocked = (await readPolicy('npm')).entries.map(
1544
+ (entry) => entry.package,
1545
+ )
1185
1546
  const atBase = (file) => {
1186
1547
  try {
1187
1548
  return JSON.parse(capture('git', ['show', `${base}:${file}`]))
@@ -1189,7 +1550,8 @@ async function policyCheck(_config, base) {
1189
1550
  return null
1190
1551
  }
1191
1552
  }
1192
- const now = (file) => (existsSync(file) ? JSON.parse(readFileSync(file, 'utf8')) : null)
1553
+ const now = (file) =>
1554
+ existsSync(file) ? JSON.parse(readFileSync(file, 'utf8')) : null
1193
1555
 
1194
1556
  const problems = policyViolations({
1195
1557
  blocked,
@@ -1200,22 +1562,26 @@ async function policyCheck(_config, base) {
1200
1562
  })
1201
1563
 
1202
1564
  for (const problem of problems) console.error(problem)
1203
- if (0 < problems.length) throw new Error('The change updates a package that the dependency policy holds back.')
1565
+ if (problems.length > 0)
1566
+ throw new Error(
1567
+ 'The change updates a package that the dependency policy holds back.',
1568
+ )
1204
1569
 
1205
1570
  log('No package of the dependency policy is updated.')
1206
1571
  }
1207
1572
 
1208
1573
  /** Rewrite the ignore rules of .github/dependabot.yml from the policy. The workflow of the repository commits the result. */
1209
1574
  async function dependabotSync() {
1210
- if (!existsSync(DEPENDABOT_FILE)) throw new Error(`${DEPENDABOT_FILE} does not exist.`)
1575
+ if (!existsSync(DEPENDABOT_FILE))
1576
+ throw new Error(`${DEPENDABOT_FILE} does not exist.`)
1211
1577
 
1212
1578
  const names = (await readPolicy('npm')).entries.map((entry) => entry.package)
1213
1579
  const text = readFileSync(DEPENDABOT_FILE, 'utf8')
1214
1580
  const synced = syncDependabot(text, names, 'npm')
1215
1581
 
1216
- if (null === synced) {
1582
+ if (synced === null) {
1217
1583
  throw new Error(
1218
- `${DEPENDABOT_FILE} has no block "# BEGIN dependency-policy (npm)" ... "# END dependency-policy (npm)".`
1584
+ `${DEPENDABOT_FILE} has no block "# BEGIN dependency-policy (npm)" ... "# END dependency-policy (npm)".`,
1219
1585
  )
1220
1586
  }
1221
1587
 
@@ -1225,7 +1591,9 @@ async function dependabotSync() {
1225
1591
  }
1226
1592
 
1227
1593
  writeFileSync(DEPENDABOT_FILE, synced)
1228
- log(`The ignore rules of Dependabot are updated: ${names.join(', ') || 'none'}.`)
1594
+ log(
1595
+ `The ignore rules of Dependabot are updated: ${names.join(', ') || 'none'}.`,
1596
+ )
1229
1597
  }
1230
1598
 
1231
1599
  const modes = {
@@ -1240,10 +1608,10 @@ const modes = {
1240
1608
  'dependabot-sync': dependabotSync,
1241
1609
  }
1242
1610
 
1243
- async function main(mode, argument) {
1611
+ export async function main(mode, argument) {
1244
1612
  if (!modes[mode]) {
1245
1613
  console.error(
1246
- 'Usage: node update-dependencies.mjs update | release | tag | cascade | audit | policy | policy-validate'
1614
+ 'Usage: node update-dependencies.mjs update | release | tag | cascade | audit | policy | policy-validate',
1247
1615
  )
1248
1616
  process.exitCode = 1
1249
1617
  return
@@ -1257,6 +1625,9 @@ async function main(mode, argument) {
1257
1625
  }
1258
1626
  }
1259
1627
 
1260
- if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
1628
+ if (
1629
+ process.argv[1] &&
1630
+ import.meta.url === pathToFileURL(process.argv[1]).href
1631
+ ) {
1261
1632
  await main(process.argv[2], process.argv[3])
1262
1633
  }