Skip to content

Commit

Permalink
Merge pull request #1396 from SukkaW/reduce-bundle-size-2
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Apr 10, 2024
2 parents e751ca5 + ad0e960 commit 6dba8b4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 60 deletions.
14 changes: 8 additions & 6 deletions src/lib/getPreferredWildcard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import groupBy from 'lodash/groupBy'
import sortBy from 'lodash/sortBy'
import { Index } from '../types/IndexType'
import { WILDCARDS } from './version-util'
Expand All @@ -17,11 +16,14 @@ function getPreferredWildcard(dependencies: Index<string | null>) {
}

// group the dependencies by wildcard
const groups = groupBy(Object.values(dependencies), dep =>
WILDCARDS.find((wildcard: string) => dep && dep.includes(wildcard)),
)

delete groups.undefined
const groups = Object.values(dependencies).reduce<Record<string, (string | null)[]>>((acc, dep) => {
const wildcard = WILDCARDS.find((wildcard: string) => dep && dep.includes(wildcard))
if (wildcard !== undefined) {
acc[wildcard] ||= []
acc[wildcard].push(dep)
}
return acc
}, {})

const arrOfGroups = Object.entries(groups).map(([wildcard, instances]) => ({ wildcard, instances }))

Expand Down
27 changes: 13 additions & 14 deletions src/lib/runGlobal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pick from 'lodash/pick'
import { print, printJson, printSorted, printUpgrades } from '../lib/logging'
import { Index } from '../types/IndexType'
import { Options } from '../types/Options'
Expand All @@ -16,19 +15,19 @@ async function runGlobal(options: Options): Promise<Index<string> | void> {
print(options, '\nGetting installed packages', 'verbose')
let globalPackages: Index<string> = {}
try {
globalPackages = await getInstalledPackages(
pick(options, [
'cli',
'cwd',
'filter',
'filterVersion',
'global',
'packageManager',
'prefix',
'reject',
'rejectVersion',
]),
)
const { cli, cwd, filter, filterVersion, global, packageManager, prefix, reject, rejectVersion } = options

globalPackages = await getInstalledPackages({
cli,
cwd,
filter,
filterVersion,
global,
packageManager,
prefix,
reject,
rejectVersion,
})
} catch (e: any) {
programError(options, e.message)
}
Expand Down
50 changes: 23 additions & 27 deletions src/lib/version-util.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import ary from 'lodash/ary'
import curry from 'lodash/curry'
import flow from 'lodash/flow'
import intersection from 'lodash/intersection'
import propertyOf from 'lodash/propertyOf'
import reject from 'lodash/reject'
import sortBy from 'lodash/sortBy'
import uniq from 'lodash/uniq'
import parseGithubUrl from 'parse-github-url'
import semver from 'semver'
import semverutils, { SemVer, parse, parseRange } from 'semver-utils'
Expand Down Expand Up @@ -224,7 +219,9 @@ export function getDependencyGroups(
majorVersionZero: chalk.magenta(chalk.bold('Major version zero') + ' Anything may change'),
}

const groupOrder = uniq(['patch', 'minor', 'major', 'majorVersionZero', ...sortBy(Object.keys(groups))])
const groupOrder = Array.from(
new Set(['patch', 'minor', 'major', 'majorVersionZero', ...sortBy(Object.keys(groups))]),
)

return groupOrder
.filter(groupName => {
Expand Down Expand Up @@ -359,7 +356,7 @@ const fixMissingMinorAndPatch = (s: string) => (isMissingMinorAndPatch(s) ? s +
const fixMissingPatch = (s: string) => (isMissingPatch(s) ? s + '.0' : s)

/** Converts a pseudo version into a valid semver version. NOOP for valid semver versions. */
export const fixPseudoVersion = flow(fixLeadingV, fixMissingMinorAndPatch, fixMissingPatch)
export const fixPseudoVersion = (s: string) => fixMissingPatch(fixMissingMinorAndPatch(fixLeadingV(s)))

/**
* Returns 'v' if the string starts with a v, otherwise returns empty string.
Expand Down Expand Up @@ -471,12 +468,11 @@ export function upgradeDependencyDeclaration(

// parse the declaration
// if multiple ranges, use the semver with the least number of parts
const parsedRange: SemVer[] = flow([
// semver-utils includes empty entries for the || and - operators. We can remove them completely
ranges => reject(ranges, { operator: '||' }),
ranges => reject(ranges, { operator: '-' }),
ranges => sortBy(ranges, ary(flow(stringify, numParts), 1)),
])(semverutils.parseRange(declaration))
const parsedRange = sortBy(
semverutils.parseRange(declaration).filter(range => range.operator !== '||' && range.operator !== '-'),
s => numParts(stringify(s)),
) as SemVer[]

const [declaredSemver] = parsedRange

/**
Expand Down Expand Up @@ -506,7 +502,7 @@ export function upgradeDependencyDeclaration(

// determine the operator
// do not compact, because [undefined, '<'] must be differentiated from ['<']
const uniqueOperators = uniq(parsedRange.map(range => range.operator))
const uniqueOperators = Array.from(new Set(parsedRange.map(range => range.operator)))
const operator = uniqueOperators[0] || ''

const hasWildCard = WILDCARDS.some(wildcard => newSemverString.includes(wildcard))
Expand All @@ -523,22 +519,22 @@ export function upgradeDependencyDeclaration(
(isGreaterThan ? '>=' : operator) + version
}

/** Reverts a valid semver version to a pseudo version that is missing its minor and patch components. NOOP If the original version was a valid semver version. */
const revertMissingMinorAndPatch = curry((current: string, latest: string) =>
isMissingMinorAndPatch(current) ? latest.slice(0, latest.length - '.0.0'.length) : latest,
)
/** Reverts a valid semver version to a pseudo version. NOOP If the original version was a valid semver version. */
const revertPseudoVersion = (current: string, latest: string) => {
/** Reverts a valid semver version to a pseudo version with a leading 'v'. NOOP If the original version was a valid semver version. */
const leadingV = v(current)
let result = leadingV ? leadingV + latest : latest

/** Reverts a valid semver version to a pseudo version that is missing its patch components. NOOP If the original version was a valid semver version. */
const revertMissingPatch = curry((current: string, latest: string) =>
isMissingPatch(current) ? latest.slice(0, latest.length - '.0'.length) : latest,
)
/** Reverts a valid semver version to a pseudo version that is missing its minor and patch components. NOOP If the original version was a valid semver version. */
const missingMinorAndPatch = isMissingMinorAndPatch(current)
result = missingMinorAndPatch ? result.slice(0, result.length - '.0.0'.length) : result

/** Reverts a valid semver version to a pseudo version with a leading 'v'. NOOP If the original version was a valid semver version. */
const revertLeadingV = curry((current: string, latest: string) => (v(current) ? v(current) + latest : latest))
/** Reverts a valid semver version to a pseudo version that is missing its patch components. NOOP If the original version was a valid semver version. */
const missingPatch = isMissingPatch(current)
result = missingPatch ? result.slice(0, result.length - '.0'.length) : result

/** Reverts a valid semver version to a pseudo version. NOOP If the original version was a valid semver version. */
const revertPseudoVersion = (current: string, latest: string) =>
flow(revertLeadingV(current), revertMissingMinorAndPatch(current), revertMissingPatch(current))(latest)
return result
}

/**
* Replaces the version number embedded in a Github URL.
Expand Down
13 changes: 7 additions & 6 deletions src/package-managers/filters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import overEvery from 'lodash/overEvery'
import semver from 'semver'
import * as versionUtil from '../lib/version-util'
import { Index } from '../types/IndexType'
Expand Down Expand Up @@ -57,11 +56,13 @@ export function satisfiesPeerDependencies(versionResult: Partial<Packument>, pee
}

/** Returns a composite predicate that filters out deprecated, prerelease, and node engine incompatibilies from version objects returns by packument. */
export function filterPredicate(options: Options): (o: Partial<Packument>) => boolean {
return overEvery([
export function filterPredicate(options: Options) {
const predicators: (((o: Partial<Packument>) => boolean) | null)[] = [
o => allowDeprecatedOrIsNotDeprecated(o, options),
o => allowPreOrIsNotPre(o, options),
options.enginesNode ? o => satisfiesNodeEngine(o, options.nodeEngineVersion) : null!,
options.peerDependencies ? o => satisfiesPeerDependencies(o, options.peerDependencies!) : null!,
])
options.enginesNode ? o => satisfiesNodeEngine(o, options.nodeEngineVersion) : null,
options.peerDependencies ? o => satisfiesPeerDependencies(o, options.peerDependencies!) : null,
]

return (o: Partial<Packument>) => predicators.every(predicator => (predicator ? predicator(o) : true))
}
15 changes: 8 additions & 7 deletions src/package-managers/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import ini from 'ini'
import camelCase from 'lodash/camelCase'
import isEqual from 'lodash/isEqual'
import sortBy from 'lodash/sortBy'
import uniq from 'lodash/uniq'
import npmRegistryFetch from 'npm-registry-fetch'
import path from 'path'
import nodeSemver from 'semver'
Expand Down Expand Up @@ -501,12 +500,14 @@ async function fetchUpgradedPackument(
const tag = options.distTag || 'latest'
result = await fetchPartialPackument(
packageName,
uniq([
'dist-tags',
...fields,
...(!options.deprecated ? (['deprecated', 'versions'] as const) : []),
...(options.enginesNode ? (['engines', 'versions'] as const) : []),
]),
Array.from(
new Set([
'dist-tags',
...fields,
...(!options.deprecated ? (['deprecated', 'versions'] as const) : []),
...(options.enginesNode ? (['engines', 'versions'] as const) : []),
]),
),
fullMetadata ? null : tag,
npmConfigMerged,
)
Expand Down

0 comments on commit 6dba8b4

Please sign in to comment.