Skip to content

Commit

Permalink
Add support for --dep packageManager (#1252).
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Dec 14, 2022
1 parent 7d0d266 commit a668ef6
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 113 deletions.
2 changes: 1 addition & 1 deletion src/cli-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ const cliOptions: CLIOption[] = [
long: 'dep',
arg: 'value',
description:
'Check one or more sections of dependencies only: dev, optional, peer, prod, bundle (comma-delimited).',
'Check one or more sections of dependencies only: dev, optional, peer, prod, bundle, packageManager (comma-delimited).',
default: ['prod', 'dev', 'bundle', 'optional'],
parse: value => (value && typeof value === 'string' ? value.split(',') : value),
type: 'string | string[]',
Expand Down
23 changes: 18 additions & 5 deletions src/lib/getCurrentDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ const isGreaterThanSafe = (spec1: VersionSpec, spec2: VersionSpec) =>
// otherwise return true if spec2 is smaller than spec1
semver.gt(semver.minVersion(spec1)!, semver.minVersion(spec2)!)

/** Parses the packageManager field into a { [name]: version } pair. */
const parsePackageManager = (pkgData: PackageFile) => {
if (!pkgData.packageManager) return {}
const [name, version] = pkgData.packageManager.split('@')
return { [name]: version }
}
/**
* Get the current dependencies from the package file.
*
Expand All @@ -34,18 +40,25 @@ function getCurrentDependencies(pkgData: PackageFile = {}, options: Options = {}

// map the dependency section option to a full dependency section name
const depSections = depOptions.map(
short => (short === 'prod' ? 'dependencies' : short + 'Dependencies') as keyof PackageFile,
short =>
(short === 'prod'
? 'dependencies'
: short === 'packageManager'
? short
: short + 'Dependencies') as keyof PackageFile,
)

// get all dependencies from the selected sections
// if a dependency appears in more than one section, take the lowest version number
const allDependencies = depSections.reduce((accum, depSection) => {
return {
...accum,
...filterObject(
(pkgData[depSection] as Index<string>) || {},
(dep, spec) => !isGreaterThanSafe(spec, accum[dep]),
),
...(depSection === 'packageManager'
? parsePackageManager(pkgData)
: filterObject(
(pkgData[depSection] as Index<string>) || {},
(dep, spec) => !isGreaterThanSafe(spec, accum[dep]),
)),
}
}, {} as Index<VersionSpec>)

Expand Down
23 changes: 21 additions & 2 deletions src/lib/upgradePackageData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ async function upgradePackageData(
: options.dep
: ['prod', 'dev', 'bundle', 'optional']

// map the dependency section option to a full dependency section name
const depSections = depOptions.map(
short => (short === 'prod' ? 'dependencies' : short + 'Dependencies') as keyof PackageFile,
short =>
(short === 'prod'
? 'dependencies'
: short === 'packageManager'
? short
: short + 'Dependencies') as keyof PackageFile,
)

// iterate through each dependency section
const sectionRegExp = new RegExp(`"(${depSections.join(`|`)})"s*:[^}]*`, 'g')
const newPkgData = pkgData.replace(sectionRegExp, section => {
let newPkgData = pkgData.replace(sectionRegExp, section => {
// replace each upgraded dependency in the section
Object.keys(upgraded).forEach(dep => {
const expression = `"${dep}"\\s*:\\s*"(${escapeRegexp(current[dep])})"`
Expand All @@ -48,6 +54,19 @@ async function upgradePackageData(
return section
})

if (depOptions.includes('packageManager')) {
const pkg = JSON.parse(pkgData) as PackageFile
if (pkg.packageManager) {
const [name] = pkg.packageManager.split('@')
if (upgraded[name]) {
newPkgData = newPkgData.replace(
/"packageManager"\s*:\s*".*?@[^"]*"/,
`"packageManager": "${name}@${upgraded[name]}"`,
)
}
}
}

return newPkgData
}

Expand Down
2 changes: 2 additions & 0 deletions src/types/PackageFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export interface PackageFile {
devDependencies?: Index<VersionSpec>
engines?: Index<VersionSpec>
name?: string
// https://nodejs.org/api/packages.html#packagemanager
packageManager?: string
optionalDependencies?: Index<VersionSpec>
peerDependencies?: Index<VersionSpec>
repository?: string | PackageFileRepository
Expand Down
Loading

0 comments on commit a668ef6

Please sign in to comment.