Skip to content

Commit

Permalink
implement a bit of automated version number management
Browse files Browse the repository at this point in the history
  • Loading branch information
dslmeinte committed Jul 31, 2024
1 parent a0d9bdd commit d007a4e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ The output should look similar to this (but much longer):
<img src="./documentation/images/test-output.png" alt="test" width="50%"/>


### Updating version numbers

To keep the version numbers of the various packages under `packages/` aligned throughout this repository, you can use the Node.js script [`update-package-versions.js`](./update-package-versions.js).
You execute this script as follows from the repo's root:

```shell
node update-package-versions.js
```

This reads the file [`packages/versions.json`](./packages/versions.json) and updates the `package.json` files of all packages under `packages/` according to it.
This script runs `npm install` afterward to update the `package-lock.json`.
Inspect the resulting diffs to ensure correctness, and don't forget to run `npm install` to update the `package-lock.json` in case you made corrections.


### Releasing/publishing packages

Packages are released to the [npm registry (website)](https://www.npmjs.com/): see the badges at the top of this document.
Expand Down
8 changes: 8 additions & 0 deletions packages/versions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"artifacts" : "0.7.0-dev.0",
"cli" : "0.6.9-beta.0",
"core" : "0.6.8-beta.0",
"test" : "0.7.0-dev.0",
"utilities" : "0.6.8-beta.0",
"validation": "0.6.3-beta.0"
}
59 changes: 59 additions & 0 deletions update-package-versions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const { exec } = require("child_process")
const { writeFileSync, readFileSync } = require("fs")
const { join } = require("path")
const { EOL } = require("os")


const package2version = require("./packages/versions.json")


const fqPrefix = "@lionweb/"

const replaceVersionsIn = (deps) => {
Object.entries(deps)
.forEach(([dep, currentVersion]) => {
if (dep.startsWith(fqPrefix)) {
const uqDep = dep.substring(fqPrefix.length)
if (uqDep in package2version) {
const desiredVersion = package2version[uqDep]
if (desiredVersion !== currentVersion) {
console.log(` replacing dep for ${uqDep}: ${currentVersion} -> ${desiredVersion}`)
deps[dep] = desiredVersion
}
}
}
})
}


const readFileAsJson = (path) =>
JSON.parse(readFileSync(path, { encoding: "utf8" }))

const writeJsonAsFile = (path, json) => {
writeFileSync(path, JSON.stringify(json, null, 2) + EOL)
}


Object
.keys(package2version)
.forEach((pkg) => {
console.log(`updating versions in package.json of package "${pkg}"...`)
const packageJsonPath = join("packages", pkg, "package.json")
const packageJson = readFileAsJson(packageJsonPath)
packageJson.version = package2version[pkg]
if ("dependencies" in packageJson) {
replaceVersionsIn(packageJson.dependencies)
}
if ("devDependencies" in packageJson) {
replaceVersionsIn(packageJson.devDependencies)
}
writeJsonAsFile(packageJsonPath, packageJson)
console.log(`(done)`)
console.log()
})

console.log(`updating package-lock.json...`)
exec("npm install")
console.log(`(done)`)
console.log()

0 comments on commit d007a4e

Please sign in to comment.