|
| 1 | +/* eslint-disable no-console */ |
| 2 | + |
| 3 | +import path from 'path' |
| 4 | +import { execa } from 'execa' |
| 5 | +import fs from 'fs-extra' |
| 6 | +import Listr from 'listr' |
| 7 | +import { calculateSiblingVersion } from './check-project/utils.js' |
| 8 | +import { isMonorepoRoot, getSubprojectDirectories, pkg } from './utils.js' |
| 9 | + |
| 10 | +/** |
| 11 | + * @typedef {import("./types.js").GlobalOptions} GlobalOptions |
| 12 | + * @typedef {import("./types.js").ReleaseOptions} ReleaseOptions |
| 13 | + * @typedef {import("listr").ListrTaskWrapper} Task |
| 14 | + */ |
| 15 | + |
| 16 | +const tasks = new Listr([ |
| 17 | + { |
| 18 | + title: 'align sibling dependency versions', |
| 19 | + enabled: () => isMonorepoRoot(), |
| 20 | + /** |
| 21 | + * @param {GlobalOptions & ReleaseOptions} ctx |
| 22 | + */ |
| 23 | + task: async (ctx) => { |
| 24 | + const rootDir = process.cwd() |
| 25 | + const workspaces = pkg.workspaces |
| 26 | + |
| 27 | + if (!workspaces || !Array.isArray(workspaces)) { |
| 28 | + throw new Error('No monorepo workspaces found') |
| 29 | + } |
| 30 | + |
| 31 | + const { |
| 32 | + siblingVersions, |
| 33 | + packageDirs |
| 34 | + } = await calculateSiblingVersions(rootDir, workspaces) |
| 35 | + |
| 36 | + // check these dependency types for monorepo siblings |
| 37 | + const dependencyTypes = [ |
| 38 | + 'dependencies', |
| 39 | + 'devDependencies', |
| 40 | + 'peerDependencies', |
| 41 | + 'optionalDependencies' |
| 42 | + ] |
| 43 | + |
| 44 | + // align the versions of siblings in each package |
| 45 | + for (const packageDir of packageDirs) { |
| 46 | + const manifestPath = path.join(packageDir, 'package.json') |
| 47 | + const manifest = fs.readJSONSync(path.join(packageDir, 'package.json')) |
| 48 | + |
| 49 | + for (const type of dependencyTypes) { |
| 50 | + for (const [dep, version] of Object.entries(siblingVersions)) { |
| 51 | + if (manifest[type] != null && manifest[type][dep] != null && manifest[type][dep] !== version) { |
| 52 | + console.info('Update', type, dep, manifest[type][dep], '->', version) // eslint-disable-line no-console |
| 53 | + manifest[type][dep] = version |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + fs.writeJSONSync(manifestPath, manifest, { |
| 59 | + spaces: 2 |
| 60 | + }) |
| 61 | + } |
| 62 | + |
| 63 | + // all done, commit changes and push to remote |
| 64 | + const status = await execa('git', ['status', '--porcelain'], { |
| 65 | + cwd: rootDir |
| 66 | + }) |
| 67 | + |
| 68 | + if (status.stdout === '') { |
| 69 | + // no changes, nothing to do |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + if (!process.env.CI) { |
| 74 | + console.info('CI env var is not set, not pushing to git') // eslint-disable-line no-console |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + // When running on CI, set the commits author and commiter info and prevent the `git` CLI to prompt for username/password. |
| 79 | + // Borrowed from `semantic-release` |
| 80 | + process.env.GIT_AUTHOR_NAME = ctx.siblingDepUpdateName |
| 81 | + process.env.GIT_AUTHOR_EMAIL = ctx.siblingDepUpdateEmail |
| 82 | + process.env.GIT_COMMITTER_NAME = ctx.siblingDepUpdateName |
| 83 | + process.env.GIT_COMMITTER_EMAIL = ctx.siblingDepUpdateEmail |
| 84 | + process.env.GIT_ASKPASS = 'echo' |
| 85 | + process.env.GIT_TERMINAL_PROMPT = '0' |
| 86 | + |
| 87 | + console.info(`Commit with message "${ctx.siblingDepUpdateMessage}"`) // eslint-disable-line no-console |
| 88 | + |
| 89 | + await execa('git', ['add', '-A'], { |
| 90 | + cwd: rootDir |
| 91 | + }) |
| 92 | + await execa('git', ['commit', '-m', ctx.siblingDepUpdateMessage], { |
| 93 | + cwd: rootDir |
| 94 | + }) |
| 95 | + console.info('Push to remote') // eslint-disable-line no-console |
| 96 | + await execa('git', ['push'], { |
| 97 | + cwd: rootDir |
| 98 | + }) |
| 99 | + } |
| 100 | + } |
| 101 | +], { renderer: 'verbose' }) |
| 102 | + |
| 103 | +/** |
| 104 | + * @param {string} rootDir |
| 105 | + * @param {string[]} workspaces |
| 106 | + */ |
| 107 | +async function calculateSiblingVersions (rootDir, workspaces) { |
| 108 | + const packageDirs = [] |
| 109 | + |
| 110 | + /** @type {Record<string, string>} */ |
| 111 | + const siblingVersions = {} |
| 112 | + |
| 113 | + for (const subProjectDir of await getSubprojectDirectories(rootDir, workspaces)) { |
| 114 | + const pkg = JSON.parse(fs.readFileSync(path.join(subProjectDir, 'package.json'), { |
| 115 | + encoding: 'utf-8' |
| 116 | + })) |
| 117 | + |
| 118 | + siblingVersions[pkg.name] = calculateSiblingVersion(pkg.version) |
| 119 | + packageDirs.push(subProjectDir) |
| 120 | + } |
| 121 | + |
| 122 | + return { |
| 123 | + packageDirs, |
| 124 | + siblingVersions |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +export default tasks |
0 commit comments