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: update patch deps during check-project #1460

Merged
merged 1 commit into from
Feb 6, 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@
"globby": "^14.0.0",
"is-plain-obj": "^4.1.0",
"kleur": "^4.1.4",
"latest-version": "^8.0.0",
"lilconfig": "^3.0.0",
"listr": "~0.14.2",
"mdast-util-from-markdown": "^2.0.0",
Expand Down
69 changes: 39 additions & 30 deletions src/check-project/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { fileURLToPath } from 'url'
import { execa } from 'execa'
import fs from 'fs-extra'
import latestVersion from 'latest-version'
import Listr from 'listr'
import prompt from 'prompt'
import semver from 'semver'
Expand Down Expand Up @@ -161,38 +162,33 @@
* @param {string[]} projectDirs
*/
async function alignMonorepoProjectDependencies (projectDirs) {
console.info('Align monorepo project dependencies')

/** @type {Record<string, string>} */
const siblingVersions = {}
/** @type {Record<string, string>} */
const deps = {}
/** @type {Record<string, string>} */
const devDeps = {}
/** @type {Record<string, string>} */
const optionalDeps = {}
/** @type {Record<string, string>} */
const peerDeps = {}

// first loop over every project and choose the most recent version of a given dep
for (const projectDir of projectDirs) {
const pkg = fs.readJSONSync(path.join(projectDir, 'package.json'))
siblingVersions[pkg.name] = calculateSiblingVersion(pkg.version)

chooseVersions(pkg.dependencies || {}, deps)
chooseVersions(pkg.devDependencies || {}, devDeps)
chooseVersions(pkg.optionalDependencies || {}, optionalDeps)
chooseVersions(pkg.peerDependencies || {}, peerDeps)
chooseVersions(pkg.dependencies ?? {}, deps)
chooseVersions(pkg.devDependencies ?? {}, deps)
chooseVersions(pkg.optionalDependencies ?? {}, deps)
chooseVersions(pkg.peerDependencies ?? {}, deps)

Check warning on line 178 in src/check-project/index.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/index.js#L175-L178

Added lines #L175 - L178 were not covered by tests
}

// get the latest patch release of every dep from npm
await findLatestVersions(deps)

Check warning on line 183 in src/check-project/index.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/index.js#L181-L183

Added lines #L181 - L183 were not covered by tests
// now propose the most recent version of a dep for all projects
for (const projectDir of projectDirs) {
const pkg = fs.readJSONSync(path.join(projectDir, 'package.json'))

selectVersions(pkg.dependencies || {}, deps, siblingVersions)
selectVersions(pkg.devDependencies || {}, devDeps, siblingVersions)
selectVersions(pkg.optionalDependencies || {}, optionalDeps, siblingVersions)
selectVersions(pkg.peerDependencies || {}, peerDeps, siblingVersions)
selectVersions(pkg.dependencies ?? {}, deps, siblingVersions)
selectVersions(pkg.devDependencies ?? {}, deps, siblingVersions)
selectVersions(pkg.optionalDependencies ?? {}, deps, siblingVersions)
selectVersions(pkg.peerDependencies ?? {}, deps, siblingVersions)

Check warning on line 191 in src/check-project/index.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/index.js#L188-L191

Added lines #L188 - L191 were not covered by tests

await ensureFileHasContents(projectDir, 'package.json', JSON.stringify(pkg, null, 2))
}
Expand All @@ -203,37 +199,50 @@
* @param {Record<string, string>} list
*/
function chooseVersions (deps, list) {
Object.entries(deps).forEach(([key, value]) => {
for (const [dep, version] of Object.entries(deps)) {

Check warning on line 202 in src/check-project/index.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/index.js#L202

Added line #L202 was not covered by tests
// not seen this dep before
if (!list[key]) {
list[key] = value
return
if (list[dep] == null) {
list[dep] = version
continue
}

// test for later version
if (semver.gt(version.replace(/\^|~/, ''), list[dep].replace(/\^|~/, ''))) {
list[dep] = version

Check warning on line 211 in src/check-project/index.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/index.js#L204-L211

Added lines #L204 - L211 were not covered by tests
}
}
}

Check warning on line 214 in src/check-project/index.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/index.js#L213-L214

Added lines #L213 - L214 were not covered by tests

const existingVersion = semver.minVersion(list[key])
const moduleVersion = semver.minVersion(value)
/**
* @param {Record<string, string>} deps
*/
async function findLatestVersions (deps) {
// find the latest semver-compatible release from npm
for (const [key, value] of Object.entries(deps)) {
try {
const npmVersion = `^${await latestVersion(key, { version: value })}`

Check warning on line 223 in src/check-project/index.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/index.js#L219-L223

Added lines #L219 - L223 were not covered by tests

// take the most recent range or version
const res = semver.compare(existingVersion ?? '0.0.0', moduleVersion ?? '0.0.0')
console.info(key, 'local version:', value, 'npm version:', npmVersion)

Check warning on line 225 in src/check-project/index.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/index.js#L225

Added line #L225 was not covered by tests

if (res === -1) {
list[key] = value
deps[key] = npmVersion
} catch (err) {
console.error(`Could not load latest npm version of "${key}"`, err)

Check warning on line 229 in src/check-project/index.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/index.js#L227-L229

Added lines #L227 - L229 were not covered by tests
}
})
}

Check warning on line 231 in src/check-project/index.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/index.js#L231

Added line #L231 was not covered by tests
}

/**
* @param {Record<string, string>} deps
* @param {Record<string, string>} list
* @param {Record<string, string>} siblingVersions
*/
function selectVersions (deps, list, siblingVersions) {
async function selectVersions (deps, list, siblingVersions) {

Check warning on line 239 in src/check-project/index.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/index.js#L239

Added line #L239 was not covered by tests
// release-please updates sibling versions to the latest patch releases but
// we try to update to the latest minor so skip that if release please is
// in use
const ignoreSiblingDeps = usesReleasePlease()

Object.entries(list).forEach(([key, value]) => {
for (const [key, value] of Object.entries(list)) {

Check warning on line 245 in src/check-project/index.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/index.js#L245

Added line #L245 was not covered by tests
if (deps[key] != null) {
if (siblingVersions[key] != null && !ignoreSiblingDeps) {
// take sibling version if available
Expand All @@ -243,7 +252,7 @@
deps[key] = value
}
}
})
}

Check warning on line 255 in src/check-project/index.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/index.js#L255

Added line #L255 was not covered by tests
}

/**
Expand Down
Loading