|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const path = require('path') |
| 4 | +const fs = require('fs') |
| 5 | +const execa = require('execa') |
| 6 | + |
| 7 | +// Where an example depends on `"ipfs": "^0.51.0"` and we've just released `ipfs@0.52.0`, |
| 8 | +// go through all of the examples and update the version to `"ipfs": "^0.52.0"` - do |
| 9 | +// that with every module under /packages |
| 10 | + |
| 11 | +const PACKAGES_DIR = path.resolve(__dirname, '../packages') |
| 12 | +const EXAMPLES_DIR = path.resolve(__dirname, '../examples') |
| 13 | +const DEP_TYPES = ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies'] |
| 14 | + |
| 15 | +async function main () { |
| 16 | + const { |
| 17 | + stdout: branch |
| 18 | + } = await execa('git', ['rev-parse', '--abbrev-ref', 'HEAD']) |
| 19 | + |
| 20 | + if (branch !== 'master') { |
| 21 | + console.info(`Not running on branch ${branch}`) |
| 22 | + return |
| 23 | + } |
| 24 | + |
| 25 | + if (process.env.CI) { |
| 26 | + console.info('Not running in CI') |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + console.info('Running on branch', branch) |
| 31 | + |
| 32 | + // list all of the package.json files we may have just updated |
| 33 | + const potentiallyUpdatedProjects = [] |
| 34 | + |
| 35 | + for (const dir of fs.readdirSync(PACKAGES_DIR)) { |
| 36 | + const projectPkgPath = path.resolve(PACKAGES_DIR, dir, 'package.json') |
| 37 | + |
| 38 | + if (!fs.existsSync(projectPkgPath)) { |
| 39 | + continue |
| 40 | + } |
| 41 | + |
| 42 | + potentiallyUpdatedProjects.push(projectPkgPath) |
| 43 | + } |
| 44 | + |
| 45 | + // add the example test runner |
| 46 | + potentiallyUpdatedProjects.push(path.resolve(EXAMPLES_DIR, 'test-ipfs-example', 'package.json')) |
| 47 | + |
| 48 | + for (const projectPkgPath of potentiallyUpdatedProjects) { |
| 49 | + const projectPkg = JSON.parse(fs.readFileSync(projectPkgPath, { encoding: 'utf8' })) |
| 50 | + const projectDepVersion = `^${projectPkg.version}` |
| 51 | + |
| 52 | + // look through all the example projects and update their deps |
| 53 | + for (const dir of fs.readdirSync(EXAMPLES_DIR)) { |
| 54 | + const examplePkgPath = path.resolve(EXAMPLES_DIR, dir, 'package.json') |
| 55 | + |
| 56 | + if (!fs.existsSync(examplePkgPath)) { |
| 57 | + continue |
| 58 | + } |
| 59 | + |
| 60 | + const examplePkg = JSON.parse(fs.readFileSync(examplePkgPath, { encoding: 'utf8' })) |
| 61 | + let dirty = false |
| 62 | + |
| 63 | + for (const depType of DEP_TYPES) { |
| 64 | + if (examplePkg[depType] && examplePkg[depType][projectPkg.name] && examplePkg[depType][projectPkg.name] !== projectDepVersion) { |
| 65 | + console.info(`Updating ${examplePkg.name} ${projectPkg.name}: ${examplePkg[depType][projectPkg.name]} -> ${projectDepVersion}`) |
| 66 | + examplePkg[depType][projectPkg.name] = projectDepVersion |
| 67 | + dirty = true |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if (dirty) { |
| 72 | + fs.writeFileSync(examplePkgPath, JSON.stringify(examplePkg, null, 2) + '\n', { encoding: 'utf8' }) |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + await execa('git', ['add', 'examples']) |
| 78 | + |
| 79 | + const { |
| 80 | + stdout: updated |
| 81 | + } = await execa('git', ['status', '--porcelain']) |
| 82 | + |
| 83 | + console.info(updated) |
| 84 | + |
| 85 | + if (!updated.match(/^M\s+examples/g)) { |
| 86 | + console.info('No examples were updated') |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + console.info('Pushing updated dependencies') |
| 91 | + await execa('git', ['commit', '-m', 'chore: updated example dependencies']) |
| 92 | + await execa('git', ['push']) |
| 93 | +} |
| 94 | + |
| 95 | +main().catch(err => { |
| 96 | + console.error(err) |
| 97 | + process.exit(1) |
| 98 | +}) |
0 commit comments