Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: revert to semantic-release-monorepo #1536

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default {
'playwright-test',
'react-native-test-runner',
'semantic-release',
'@anolilab/multi-semantic-release',
'semantic-release-monorepo',
'source-map-support',
'typedoc-plugin-mdn-links',
'typedoc-plugin-missing-exports',
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@
"release": "node src/index.js release --no-bundle"
},
"dependencies": {
"@anolilab/multi-semantic-release": "^1.0.3",
"@electron/get": "^3.0.0",
"@polka/send-type": "^0.5.2",
"@semantic-release/changelog": "^6.0.1",
Expand Down Expand Up @@ -263,6 +262,7 @@
"eslint-plugin-promise": "^6.1.1",
"execa": "^8.0.1",
"extract-zip": "^2.0.1",
"fast-glob": "^3.3.2",
"fs-extra": "^11.1.0",
"gh-pages": "^6.0.0",
"globby": "^14.0.0",
Expand All @@ -283,7 +283,6 @@
"micromark-extension-gfm-strikethrough": "^2.0.0",
"micromark-extension-gfm-table": "^2.0.0",
"micromark-extension-gfm-task-list-item": "^2.0.1",
"fast-glob": "^3.3.2",
"mocha": "^10.0.0",
"npm-package-json-lint": "^7.0.0",
"nyc": "^15.1.0",
Expand All @@ -300,6 +299,7 @@
"read-pkg-up": "^11.0.0",
"rimraf": "^5.0.0",
"semantic-release": "^23.0.0",
"semantic-release-monorepo": "^8.0.2",
"semver": "^7.3.8",
"source-map-support": "^0.5.20",
"strip-bom": "^5.0.0",
Expand Down
128 changes: 128 additions & 0 deletions src/align-versions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/* 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) => {
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'))

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) {
console.info('CI env var is not set, not pushing to git') // eslint-disable-line no-console
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
})
}

Check warning on line 99 in src/align-versions.js

View check run for this annotation

Codecov / codecov/patch

src/align-versions.js#L24-L99

Added lines #L24 - L99 were not covered by tests
}
], { 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
}
}

Check warning on line 126 in src/align-versions.js

View check run for this annotation

Codecov / codecov/patch

src/align-versions.js#L107-L126

Added lines #L107 - L126 were not covered by tests

export default tasks
46 changes: 46 additions & 0 deletions src/cmds/align-versions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import alignVersions from '../align-versions.js'
import { loadUserConfig } from '../config/user.js'

/**
* @typedef {import("yargs").Argv} Argv
* @typedef {import("yargs").Arguments} Arguments
* @typedef {import("yargs").CommandModule} CommandModule
*/

/** @type {CommandModule} */
export default {
command: 'align-versions',
describe: 'Align monorepo sibling dependency versions',
/**
* @param {Argv} yargs
*/
builder: async (yargs) => {
const userConfig = await loadUserConfig()

return yargs
.options({
siblingDepUpdateMessage: {
alias: 'm',
type: 'string',
describe: 'The commit message to use when updating sibling dependencies',
default: userConfig.release.siblingDepUpdateMessage
},
siblingDepUpdateName: {
type: 'string',
describe: 'The user name to use when updating sibling dependencies',
default: userConfig.release.siblingDepUpdateName
},
siblingDepUpdateEmail: {
type: 'string',
describe: 'The email to use when updating sibling dependencies',
default: userConfig.release.siblingDepUpdateEmail
}
})

Check warning on line 38 in src/cmds/align-versions.js

View check run for this annotation

Codecov / codecov/patch

src/cmds/align-versions.js#L18-L38

Added lines #L18 - L38 were not covered by tests
},
/**
* @param {any} argv
*/
async handler (argv) {
await alignVersions.run(argv)
}

Check warning on line 45 in src/cmds/align-versions.js

View check run for this annotation

Codecov / codecov/patch

src/cmds/align-versions.js#L44-L45

Added lines #L44 - L45 were not covered by tests
}
22 changes: 1 addition & 21 deletions src/cmds/release.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { loadUserConfig } from '../config/user.js'
import releaseCmd from '../release.js'

/**
Expand All @@ -19,28 +18,9 @@
* @param {Argv} yargs
*/
builder: async (yargs) => {
const userConfig = await loadUserConfig()

return yargs
.epilog(EPILOG)
.options({
siblingDepUpdateMessage: {
alias: 'm',
type: 'string',
describe: 'The commit message to use when updating sibling dependencies',
default: userConfig.release.siblingDepUpdateMessage
},
siblingDepUpdateName: {
type: 'string',
describe: 'The user name to use when updating sibling dependencies',
default: userConfig.release.siblingDepUpdateName
},
siblingDepUpdateEmail: {
type: 'string',
describe: 'The email to use when updating sibling dependencies',
default: userConfig.release.siblingDepUpdateEmail
}
})
.options({})

Check warning on line 23 in src/cmds/release.js

View check run for this annotation

Codecov / codecov/patch

src/cmds/release.js#L23

Added line #L23 was not covered by tests
},
/**
* @param {any} argv
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { readPackageUpSync } from 'read-pkg-up'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import alignVersionsCmd from './cmds/align-versions.js'
import buildCmd from './cmds/build.js'
import checkProjectCmd from './cmds/check-project.js'
import checkCmd from './cmds/check.js'
Expand Down Expand Up @@ -92,6 +93,7 @@ async function main () {
res.command(testCmd)
res.command(execCmd)
res.command(runCmd)
res.command(alignVersionsCmd)

try {
await res.parse()
Expand Down
Loading
Loading