Skip to content

Commit

Permalink
Merge pull request #1397 from SukkaW/reduce-bundle-size
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Apr 12, 2024
2 parents d830fd2 + 7b00844 commit 6a7ebb2
Show file tree
Hide file tree
Showing 17 changed files with 142 additions and 166 deletions.
117 changes: 34 additions & 83 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"@types/json-parse-helpfulerror": "^1.0.3",
"@types/jsonlines": "^0.1.5",
"@types/lodash": "^4.17.0",
"@types/make-fetch-happen": "^10.0.4",
"@types/mocha": "^10.0.6",
"@types/node": "^20.11.30",
"@types/npm-registry-fetch": "^8.0.7",
Expand All @@ -81,13 +80,15 @@
"@types/update-notifier": "^6.0.8",
"@typescript-eslint/eslint-plugin": "^7.4.0",
"@typescript-eslint/parser": "^7.4.0",
"camelcase": "^6.3.0",
"chai": "^4.3.10",
"chai-as-promised": "^7.1.1",
"chai-string": "^1.5.0",
"chalk": "^5.3.0",
"cli-table3": "^0.6.4",
"commander": "^12.0.0",
"cross-env": "^7.0.3",
"dequal": "^2.0.3",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-raine": "^0.5.0",
Expand All @@ -107,7 +108,6 @@
"jsonlines": "^0.1.1",
"lockfile-lint": "^4.13.2",
"lodash": "^4.17.21",
"make-fetch-happen": "^13.0.0",
"markdownlint-cli": "^0.39.0",
"mocha": "^10.4.0",
"npm-registry-fetch": "^16.1.0",
Expand All @@ -120,6 +120,7 @@
"prompts-ncu": "^3.0.0",
"rc-config-loader": "^4.1.3",
"remote-git-tags": "^3.0.0",
"rfdc": "^1.3.1",
"rimraf": "^5.0.5",
"rollup-plugin-node-externals": "^7.1.1",
"semver": "^7.6.0",
Expand Down
8 changes: 5 additions & 3 deletions src/bin/cli.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
#!/usr/bin/env node
import { Help, Option, program } from 'commander'
import cloneDeep from 'lodash/cloneDeep'
import pickBy from 'lodash/pickBy'
import createCloneDeep from 'rfdc'
import semver from 'semver'
import pkg from '../../package.json'
import cliOptions, { renderExtendedHelp } from '../cli-options'
import ncu from '../index'
import { chalkInit } from '../lib/chalk'
// async global contexts are only available in esm modules -> function
import getNcuRc from '../lib/getNcuRc'
import { pickBy } from '../lib/pick'

const optionVersionDescription = 'Output the version number of npm-check-updates.'

/** Removes inline code ticks. */
const uncode = (s: string) => s.replace(/`/g, '')

const cloneDeep = createCloneDeep()

;(async () => {
// importing update-notifier dynamically as esm modules are only allowed to be dynamically imported inside of cjs modules
const { default: updateNotifier } = await import('update-notifier')
Expand Down Expand Up @@ -203,7 +205,7 @@ ${chalk.dim.underline(
// filter out undefined program options and combine cli options with config file options
const options = {
...(rcResult && Object.keys(rcResult.config).length > 0 ? { rcConfigPath: rcResult.filePath } : null),
...pickBy(program.opts(), value => value !== undefined),
...pickBy(program.opts(), (value: unknown) => value !== undefined),
args: program.args,
...(combinedProgramOpts.filter ? { filter: combinedProgramOpts.filter } : null),
...(combinedProgramOpts.reject ? { reject: combinedProgramOpts.reject } : null),
Expand Down
4 changes: 2 additions & 2 deletions src/cli-options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sortBy from 'lodash/sortBy'
import path from 'path'
import { defaultCacheFile } from './lib/cache'
import chalk from './lib/chalk'
import { sortBy } from './lib/sortBy'
import table from './lib/table'
import CLIOption from './types/CLIOption'
import ExtendedHelp from './types/ExtendedHelp'
Expand Down Expand Up @@ -893,6 +893,6 @@ export const cliOptionsMap = cliOptions.reduce(
{} as Index<CLIOption>,
)

const cliOptionsSorted = sortBy(cliOptions, 'long')
const cliOptionsSorted = sortBy(cliOptions, v => v.long)

export default cliOptionsSorted
5 changes: 2 additions & 3 deletions src/lib/filterAndReject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { and, or } from 'fp-and-or'
import identity from 'lodash/identity'
import negate from 'lodash/negate'
import picomatch from 'picomatch'
import { parseRange } from 'semver-utils'
import { FilterPattern } from '../types/FilterPattern'
Expand Down Expand Up @@ -83,15 +82,15 @@ function filterAndReject(
return and(
// filter dep
(dependencyName: VersionSpec, version: string) =>
and(filter ? composeFilter(filter) : true, reject ? negate(composeFilter(reject)) : true)(
and(filter ? composeFilter(filter) : true, reject ? (...args) => !composeFilter(reject)(...args) : true)(
dependencyName,
version,
),
// filter version
(dependencyName: VersionSpec, version: string) =>
and(
filterVersion ? composeFilter(filterVersion) : true,
rejectVersion ? negate(composeFilter(rejectVersion)) : true,
rejectVersion ? (...args) => !composeFilter(rejectVersion)(...args) : true,
)(version),
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/getPreferredWildcard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sortBy from 'lodash/sortBy'
import { Index } from '../types/IndexType'
import { sortBy } from './sortBy'
import { WILDCARDS } from './version-util'

/**
Expand Down
4 changes: 2 additions & 2 deletions src/lib/initOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import isEqual from 'lodash/isEqual'
import { dequal } from 'dequal'
import propertyOf from 'lodash/propertyOf'
import cliOptions, { cliOptionsMap } from '../cli-options'
import { print } from '../lib/logging'
Expand Down Expand Up @@ -123,7 +123,7 @@ async function initOptions(runOptions: RunOptions, { cli }: { cli?: boolean } =

// convert to string for comparison purposes
// otherwise ['a b'] will not match ['a', 'b']
if (options.filter && args && !isEqual(args.join(' '), Array.isArray(filter) ? filter.join(' ') : filter)) {
if (options.filter && args && !dequal(args.join(' '), Array.isArray(filter) ? filter.join(' ') : filter)) {
programError(
options,
'Cannot specify a filter using both --filter and args. Did you forget to quote an argument?\nSee: https://github.com/raineorshine/npm-check-updates/issues/759#issuecomment-723587297',
Expand Down
30 changes: 30 additions & 0 deletions src/lib/pick.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/** Creates an object composed of the picked `object` properties. */
export function pick<T extends object, U extends keyof T>(obj: T, props: U[]): Pick<T, U> {
const newObject = {} as Pick<T, U>

for (const prop of props) {
newObject[prop] = obj[prop]
}

return newObject
}

/**
* Creates an object composed of the `object` properties `predicate` returns
* truthy for. The predicate is invoked with two arguments: (value, key).
*/
export function pickBy<R, K extends keyof R>(
object: R | null | undefined,
predicate: (value: R[K], key: keyof R) => any,
): Record<K, R[K]> {
const newObject = {} as Record<K, R[K]>

for (const [key, value] of Object.entries<R[K]>(object ?? {})) {
const _key = key as K
if (predicate(value, _key)) {
newObject[_key] = value
}
}

return newObject
}

0 comments on commit 6a7ebb2

Please sign in to comment.