-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into dependabot/npm_and_yarn/execa-9.1.0
- Loading branch information
Showing
26 changed files
with
617 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* eslint-disable no-console */ | ||
|
||
import path from 'path' | ||
import { execa } from 'execa' | ||
import fs from 'fs-extra' | ||
import Listr from 'listr' | ||
import { calculateSiblingVersion } from './check-project/utils.js' | ||
import { isMonorepoRoot, getSubprojectDirectories, pkg } from './utils.js' | ||
|
||
/** | ||
* @typedef {import("./types.js").GlobalOptions} GlobalOptions | ||
* @typedef {import("./types.js").ReleaseOptions} ReleaseOptions | ||
* @typedef {import("listr").ListrTaskWrapper} Task | ||
*/ | ||
|
||
const tasks = new Listr([ | ||
{ | ||
title: 'align sibling dependency versions', | ||
enabled: () => isMonorepoRoot(), | ||
/** | ||
* @param {GlobalOptions & ReleaseOptions} ctx | ||
*/ | ||
task: async (ctx) => { | ||
if (!process.env.CI) { | ||
console.info('⚠ This run was not triggered in a known CI environment, running in dry-run mode.') // eslint-disable-line no-console | ||
return | ||
} | ||
|
||
const rootDir = process.cwd() | ||
const workspaces = pkg.workspaces | ||
|
||
if (!workspaces || !Array.isArray(workspaces)) { | ||
throw new Error('No monorepo workspaces found') | ||
} | ||
|
||
const { | ||
siblingVersions, | ||
packageDirs | ||
} = await calculateSiblingVersions(rootDir, workspaces) | ||
|
||
// check these dependency types for monorepo siblings | ||
const dependencyTypes = [ | ||
'dependencies', | ||
'devDependencies', | ||
'peerDependencies', | ||
'optionalDependencies' | ||
] | ||
|
||
// align the versions of siblings in each package | ||
for (const packageDir of packageDirs) { | ||
const manifestPath = path.join(packageDir, 'package.json') | ||
const manifest = fs.readJSONSync(path.join(packageDir, 'package.json')) | ||
|
||
console.info('check project', manifest.name) | ||
|
||
for (const type of dependencyTypes) { | ||
for (const [dep, version] of Object.entries(siblingVersions)) { | ||
if (manifest[type] != null && manifest[type][dep] != null && manifest[type][dep] !== version) { | ||
console.info('Update', type, dep, manifest[type][dep], '->', version) // eslint-disable-line no-console | ||
manifest[type][dep] = version | ||
} | ||
} | ||
} | ||
|
||
fs.writeJSONSync(manifestPath, manifest, { | ||
spaces: 2 | ||
}) | ||
} | ||
|
||
// all done, commit changes and push to remote | ||
const status = await execa('git', ['status', '--porcelain'], { | ||
cwd: rootDir | ||
}) | ||
|
||
if (status.stdout === '') { | ||
// no changes, nothing to do | ||
return | ||
} | ||
|
||
if (!process.env.CI) { | ||
// do not push to remote repo if in dry-run mode | ||
return | ||
} | ||
|
||
// When running on CI, set the commits author and commiter info and prevent the `git` CLI to prompt for username/password. | ||
// Borrowed from `semantic-release` | ||
process.env.GIT_AUTHOR_NAME = ctx.siblingDepUpdateName | ||
process.env.GIT_AUTHOR_EMAIL = ctx.siblingDepUpdateEmail | ||
process.env.GIT_COMMITTER_NAME = ctx.siblingDepUpdateName | ||
process.env.GIT_COMMITTER_EMAIL = ctx.siblingDepUpdateEmail | ||
process.env.GIT_ASKPASS = 'echo' | ||
process.env.GIT_TERMINAL_PROMPT = '0' | ||
|
||
console.info(`Commit with message "${ctx.siblingDepUpdateMessage}"`) // eslint-disable-line no-console | ||
|
||
await execa('git', ['add', '-A'], { | ||
cwd: rootDir | ||
}) | ||
await execa('git', ['commit', '-m', ctx.siblingDepUpdateMessage], { | ||
cwd: rootDir | ||
}) | ||
console.info('Push to remote') // eslint-disable-line no-console | ||
await execa('git', ['push'], { | ||
cwd: rootDir | ||
}) | ||
} | ||
} | ||
], { renderer: 'verbose' }) | ||
|
||
/** | ||
* @param {string} rootDir | ||
* @param {string[]} workspaces | ||
*/ | ||
async function calculateSiblingVersions (rootDir, workspaces) { | ||
const packageDirs = [] | ||
|
||
/** @type {Record<string, string>} */ | ||
const siblingVersions = {} | ||
|
||
for (const subProjectDir of await getSubprojectDirectories(rootDir, workspaces)) { | ||
const pkg = JSON.parse(fs.readFileSync(path.join(subProjectDir, 'package.json'), { | ||
encoding: 'utf-8' | ||
})) | ||
|
||
siblingVersions[pkg.name] = calculateSiblingVersion(pkg.version) | ||
packageDirs.push(subProjectDir) | ||
} | ||
|
||
return { | ||
packageDirs, | ||
siblingVersions | ||
} | ||
} | ||
|
||
export default tasks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.