@zairakai/js-dev-tools 3.14.0 → 3.16.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/README.md CHANGED
@@ -178,6 +178,7 @@ The pipeline template updates the dependencies of a project by itself, and this
178
178
  - A weekly schedule with the variable `UPDATE_DEPENDENCIES=true`, or a pipeline started by the cascade, runs `deps:update`: it takes the **minor and patch** updates after a cooling period, opens a merge request into `develop` and merges it **when its pipeline succeeds**. Major versions are never applied.
179
179
  - `deps:release` brings `develop` into `main` after such an update, and `deps:tag` tags `main` when a published range (`dependencies`, `peerDependencies`) changed. A project that changed only its development dependencies is not tagged: its published archive is the same.
180
180
  - After the publication of a tag, `deps:cascade` waits for the version to be on the registry, then starts the update of the projects that have a `.dependency-update.json`.
181
+ - The packages of the [dependency policy](https://gitlab.com/zairakai/dependency-policy) (`node.json`) are never updated. `node node_modules/@zairakai/js-dev-tools/scripts/update-dependencies.mjs policy-check <ref>` fails when the change since `<ref>` updates one of them in `package.json` or `package-lock.json`; it only reads, so it runs on GitHub as well.
181
182
 
182
183
  The same script serves the PHP projects (`composer.json`): a job in the PHP image runs `composer update`, then this script commits, signs and opens the merge request, and `deps:tag` looks at `require` only. The jobs that sign (`deps:update`, `deps:tag`) run on an exact version of the node image (`NODE_UPDATE_IMAGE`, published on Docker Hub), because they need `gpg` and the image of the runner is not updated by itself.
183
184
 
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.16.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",
@@ -2,7 +2,7 @@
2
2
  /**
3
3
  * Automatic updates of the dependencies of a package of the zairakai group.
4
4
  *
5
- * Usage: node update-dependencies.mjs update | release | tag | cascade | audit | policy | policy-validate
5
+ * Usage: node update-dependencies.mjs update | release | tag | cascade | audit | policy | policy-validate | policy-check <ref>
6
6
  *
7
7
  * - update: prepare a branch with the minor and patch updates, open a merge request into develop and merge it when its
8
8
  * pipeline succeeds. The pipeline of the merge request is the gate: nothing is tested here.
@@ -13,6 +13,7 @@
13
13
  * (UPDATE_URGENT=true: the new versions are taken without waiting, and `npm audit fix` is run).
14
14
  * - policy: in the repository dependency-policy, lift the entries whose condition is met (merge request).
15
15
  * - policy-validate: in the repository dependency-policy, check the shape of the files.
16
+ * - policy-check: fail when the change since a ref updates a package of the policy (GitLab or GitHub, it only reads).
16
17
  *
17
18
  * The packages listed in the dependency policy (https://gitlab.com/zairakai/dependency-policy) are never updated, whatever
18
19
  * the project. DEPENDENCY_POLICY_URL replaces the address of the files.
@@ -252,6 +253,45 @@ export function isLifted(entry, ranges) {
252
253
  })
253
254
  }
254
255
 
256
+ /** The versions of every package of a package-lock.json, by name (a package installed twice has several). */
257
+ export function lockedVersions(lock) {
258
+ const versions = {}
259
+
260
+ for (const [path, info] of Object.entries(lock?.packages ?? {})) {
261
+ if ('' === path || !info?.version) continue
262
+
263
+ const name = path.slice(path.lastIndexOf('node_modules/') + 'node_modules/'.length)
264
+ versions[name] = [...new Set([...(versions[name] ?? []), info.version])].sort()
265
+ }
266
+
267
+ return versions
268
+ }
269
+
270
+ /**
271
+ * The packages of the policy whose version changes: a range of package.json or a locked version. A package that is
272
+ * added or removed is not an update.
273
+ */
274
+ export function policyViolations({ blocked, beforeManifest, afterManifest, beforeLock, afterLock }) {
275
+ const problems = []
276
+
277
+ for (const row of diffDependencies(beforeManifest, afterManifest)) {
278
+ if (blocked.includes(row.name) && null !== row.from && null !== row.to) {
279
+ problems.push(`package.json: ${row.name} moves from ${row.from} to ${row.to}.`)
280
+ }
281
+ }
282
+
283
+ const before = lockedVersions(beforeLock)
284
+ const after = lockedVersions(afterLock)
285
+
286
+ for (const name of blocked) {
287
+ if (before[name] && after[name] && before[name].join(', ') !== after[name].join(', ')) {
288
+ problems.push(`package-lock.json: ${name} moves from ${before[name].join(', ')} to ${after[name].join(', ')}.`)
289
+ }
290
+ }
291
+
292
+ return problems
293
+ }
294
+
255
295
  /**
256
296
  * The packages of a lock file that the refresh may update: the ones of the group are left out, because they are trusted
257
297
  * and not made to wait, and the cooling date would refuse a version that was just published.
@@ -1052,9 +1092,46 @@ async function policyWatch() {
1052
1092
  await mergeWhenGreen(request.data.iid)
1053
1093
  }
1054
1094
 
1055
- const modes = { update, release, tag, cascade, audit, policy: policyWatch, 'policy-validate': policyValidate }
1095
+ /** Fail when the working tree updates a package of the policy compared with `base`: a range or a locked version. */
1096
+ async function policyCheck(_config, base) {
1097
+ if (!base) throw new Error('Usage: node update-dependencies.mjs policy-check <ref>')
1098
+
1099
+ const blocked = (await readPolicy('npm')).entries.map((entry) => entry.package)
1100
+ const atBase = (file) => {
1101
+ try {
1102
+ return JSON.parse(capture('git', ['show', `${base}:${file}`]))
1103
+ } catch {
1104
+ return null
1105
+ }
1106
+ }
1107
+ const now = (file) => (existsSync(file) ? JSON.parse(readFileSync(file, 'utf8')) : null)
1108
+
1109
+ const problems = policyViolations({
1110
+ blocked,
1111
+ beforeManifest: atBase('package.json'),
1112
+ afterManifest: now('package.json'),
1113
+ beforeLock: atBase('package-lock.json'),
1114
+ afterLock: now('package-lock.json'),
1115
+ })
1116
+
1117
+ for (const problem of problems) console.error(problem)
1118
+ if (0 < problems.length) throw new Error('The change updates a package that the dependency policy holds back.')
1119
+
1120
+ log('No package of the dependency policy is updated.')
1121
+ }
1122
+
1123
+ const modes = {
1124
+ update,
1125
+ release,
1126
+ tag,
1127
+ cascade,
1128
+ audit,
1129
+ policy: policyWatch,
1130
+ 'policy-validate': policyValidate,
1131
+ 'policy-check': policyCheck,
1132
+ }
1056
1133
 
1057
- async function main(mode) {
1134
+ async function main(mode, argument) {
1058
1135
  if (!modes[mode]) {
1059
1136
  console.error(
1060
1137
  'Usage: node update-dependencies.mjs update | release | tag | cascade | audit | policy | policy-validate'
@@ -1064,7 +1141,7 @@ async function main(mode) {
1064
1141
  }
1065
1142
 
1066
1143
  try {
1067
- await modes[mode](loadConfig())
1144
+ await modes[mode](loadConfig(), argument)
1068
1145
  } catch (error) {
1069
1146
  console.error(secret(error.message))
1070
1147
  process.exitCode = 1
@@ -1072,5 +1149,5 @@ async function main(mode) {
1072
1149
  }
1073
1150
 
1074
1151
  if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
1075
- await main(process.argv[2])
1152
+ await main(process.argv[2], process.argv[3])
1076
1153
  }