Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit ba1f72c

Browse files
authored
chore: automate updating example dependencies (#3500)
Due to not wanting to publish versions of examples on GH or npm we don't include the examples in our lerna workspace during publishing. Consequently the `package.json` of examples doesn't get updated with new `ipfs` etc modules during the publish process, which means they depend on old versions and can break, so we need to update the versions manually which is tiresome and error prone. I've added a script to do it at the end of the publish process.
1 parent 839e190 commit ba1f72c

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"release:post:remove-hoisted-modules": "json -I -f ./lerna.json -e \"delete this.command.bootstrap.nohoist\"",
4747
"release:post:remove-examples": "json -I -f ./lerna.json -e \"this.packages = this.packages.filter(p => !p.includes('examples'))\"",
4848
"release:post:revert-ignore-changes-to-lerna-config": "git update-index --no-assume-unchanged ./lerna.json",
49+
"release:post:update-example-dependencies": "node scripts/update-example-deps.js && git add examples && git commit -m 'chore: updated example dependencies' && git push",
4950
"release:rc": "run-s release:pre:* release:canary release:post:*",
5051
"release:canary": "lerna publish --canary --preid rc --dist-tag next --force-publish --yes",
5152
"docker:rc": "run-s docker:rc:*",

scripts/update-example-deps.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict'
2+
3+
const path = require('path')
4+
const fs = require('fs')
5+
6+
// Where an example depends on `"ipfs": "^0.51.0"` and we've just released `[email protected]`,
7+
// go through all of the examples and update the version to `"ipfs": "^0.52.0"` - do
8+
// that with every module under /packages
9+
10+
const PACKAGES_DIR = path.resolve(__dirname, '../packages')
11+
const EXAMPLES_DIR = path.resolve(__dirname, '../examples')
12+
const DEP_TYPES = ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies']
13+
14+
async function main () {
15+
for (const dir of fs.readdirSync(PACKAGES_DIR)) {
16+
const projectPkgPath = path.resolve(PACKAGES_DIR, dir, 'package.json')
17+
18+
if (!fs.existsSync(projectPkgPath)) {
19+
continue
20+
}
21+
22+
const projectPkg = JSON.parse(fs.readFileSync(projectPkgPath, { encoding: 'utf8' }))
23+
const projectDepVersion = `^${projectPkg.version}`
24+
25+
for (const dir of fs.readdirSync(EXAMPLES_DIR)) {
26+
const examplePkgPath = path.resolve(EXAMPLES_DIR, dir, 'package.json')
27+
28+
if (!fs.existsSync(examplePkgPath)) {
29+
continue
30+
}
31+
32+
const examplePkg = JSON.parse(fs.readFileSync(examplePkgPath, { encoding: 'utf8' }))
33+
let dirty = false
34+
35+
for (const depType of DEP_TYPES) {
36+
if (examplePkg[depType] && examplePkg[depType][projectPkg.name] && examplePkg[depType][projectPkg.name] !== projectDepVersion) {
37+
console.info(`Updating ${examplePkg.name} ${projectPkg.name}: ${examplePkg[depType][projectPkg.name]} -> ${projectDepVersion}`)
38+
examplePkg[depType][projectPkg.name] = projectDepVersion
39+
dirty = true
40+
}
41+
}
42+
43+
if (dirty) {
44+
fs.writeFileSync(examplePkgPath, JSON.stringify(examplePkg, null, 2) + '\n', { encoding: 'utf8' })
45+
}
46+
}
47+
}
48+
}
49+
50+
main().catch(err => {
51+
console.error(err)
52+
process.exit(1)
53+
})

0 commit comments

Comments
 (0)