Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: raineorshine/npm-check-updates
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v16.11.2
Choose a base ref
...
head repository: raineorshine/npm-check-updates
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v16.12.0
Choose a head ref
  • 4 commits
  • 10 files changed
  • 1 contributor

Commits on Aug 17, 2023

  1. Copy the full SHA
    98038a0 View commit details
  2. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    4083705 View commit details
  3. Copy the full SHA
    718ca80 View commit details
  4. 16.12.0

    raineorshine committed Aug 17, 2023
    Copy the full SHA
    fbcfcc6 View commit details
2 changes: 2 additions & 0 deletions .ncurc.js
Original file line number Diff line number Diff line change
@@ -17,5 +17,7 @@ module.exports = {
'prettier',
// Removed support for node v14 in v0.35.0
'makdownlint-cli',
// manually keep in alignment with pacote's version of make-fetch-happen
'make-fetch-happen',
],
}
23 changes: 21 additions & 2 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "npm-check-updates",
"version": "16.11.2",
"version": "16.12.0",
"author": "Tomas Junnonen <tomas1@gmail.com>",
"license": "Apache-2.0",
"contributors": [
@@ -66,6 +66,7 @@
"json-parse-helpfulerror": "^1.0.3",
"jsonlines": "^0.1.1",
"lodash": "^4.17.21",
"make-fetch-happen": "^11.1.1",
"minimatch": "^9.0.3",
"p-map": "^4.0.0",
"pacote": "15.2.0",
@@ -95,6 +96,7 @@
"@types/json-parse-helpfulerror": "^1.0.1",
"@types/jsonlines": "^0.1.2",
"@types/lodash": "^4.14.195",
"@types/make-fetch-happen": "^10.0.1",
"@types/minimatch": "^5.1.2",
"@types/mocha": "^10.0.1",
"@types/node": "^18.17.0",
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import spawn from 'spawn-please'
import { cliOptionsMap } from './cli-options'
import { cacheClear } from './lib/cache'
import chalk, { chalkInit } from './lib/chalk'
import determinePackageManager from './lib/determinePackageManager'
import doctor from './lib/doctor'
import exists from './lib/exists'
import findPackage from './lib/findPackage'
@@ -61,9 +62,11 @@ function checkIfVolta(options: Options): void {
}
}

/** Returns the package manager that should be used to install packages after running "ncu -u". Detects pnpm via pnpm-lock.yarn. This is the one place that pnpm needs to be detected, since otherwise it is backwards compatible with npm. */
/** Returns the package manager that should be used to install packages after running "ncu -u". Detects pnpm via pnpm-lock.yarn. */
const getPackageManagerForInstall = async (options: Options, pkgFile: string) => {
if (options.packageManager !== 'npm') return options.packageManager
// when packageManager is set to staticRegistry, we need to infer the package manager from lock files
if (options.packageManager === 'staticRegistry') determinePackageManager({ ...options, packageManager: undefined })
else if (options.packageManager !== 'npm') return options.packageManager
const cwd = options.cwd ?? pkgFile ? `${pkgFile}/..` : process.cwd()
const pnpmDetected = await exists(path.join(cwd, 'pnpm-lock.yaml'))
return pnpmDetected ? 'pnpm' : 'npm'
16 changes: 5 additions & 11 deletions src/lib/initOptions.ts
Original file line number Diff line number Diff line change
@@ -142,17 +142,11 @@ async function initOptions(runOptions: RunOptions, { cli }: { cli?: boolean } =
}

// disallow incorrect or missing registry path when selecting staticRegistry as packageManager
if (options.packageManager === 'staticRegistry') {
if (options.registry === undefined || options.registry === null) {
programError(
options,

'When --package-manager staticRegistry is specified, you must provide the path for the registry file with --registry. Run "ncu --help --packageManager" for details.',
)
}
if (!(await exists(options.registry!))) {
programError(options, `The specified static registry file does not exist: ${options.registry}`)
}
if (options.packageManager === 'staticRegistry' && !options.registry) {
programError(
options,
'When --package-manager staticRegistry is specified, you must provide the path for the registry file with --registry. Run "ncu --help --packageManager" for details.',
)
}
const target: Target = options.target || 'latest'

34 changes: 29 additions & 5 deletions src/package-managers/staticRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
import memoize from 'fast-memoize'
import fs from 'fs/promises'
import fetch from 'make-fetch-happen'
import programError from '../lib/programError'
import { GetVersion } from '../types/GetVersion'
import { Options } from '../types/Options'
import { StaticRegistry } from '../types/StaticRegistry'
import { Version } from '../types/Version'

/** Returns true if a string is a url. */
const isUrl = (s: string) => (s && s.startsWith('http://')) || s.startsWith('https://')

/**
* Returns registry object given a valid path
* Returns a registry object given a valid file path or url.
*
* @param path
* @returns a registry object
*/
const readStaticRegistry = async (path: string): Promise<StaticRegistry> => JSON.parse(await fs.readFile(path, 'utf8'))
const readStaticRegistry = async (options: Options): Promise<StaticRegistry> => {
const path = options.registry!
let content: string

// url
if (isUrl(path)) {
const body = await fetch(path)
content = await body.text()
}
// local path
else {
try {
content = await fs.readFile(path, 'utf8')
} catch (err) {
programError(options, `\nThe specified static registry file does not exist: ${options.registry}`)
}
}

return JSON.parse(content)
}

const registryMemoized = memoize(readStaticRegistry)

@@ -23,7 +47,7 @@ const registryMemoized = memoize(readStaticRegistry)
* @param options
* @returns A promise that fulfills to string value or null
*/
export const latest: GetVersion = async (packageName: string, currentVersion: Version, options: Options = {}) => {
const registry: { [key: string]: string } = await registryMemoized(options.registry!)
return { version: registry[packageName] }
export const latest: GetVersion = async (packageName: string, currentVersion: Version, options?: Options) => {
const registry: StaticRegistry = await registryMemoized(options || {})
return { version: registry[packageName] || null }
}
4 changes: 3 additions & 1 deletion src/types/StaticRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Version } from './Version'

export type StaticRegistry = {
[key: string]: string
[key: string]: Version
}
54 changes: 48 additions & 6 deletions test/package-managers/staticRegistry/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,56 @@
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import * as staticRegistry from '../../../src/package-managers/staticRegistry'
import ncu from '../../../src/index'

chai.should()
chai.use(chaiAsPromised)

describe('staticRegistry', function () {
it('latest', async () => {
it('upgrade to the version specified in the static registry file', async () => {
const registry = './test/package-managers/staticRegistry/staticRegistry.json'
const { version } = await staticRegistry.latest('express', '', { cwd: __dirname, registry })
version!.should.equal('4.1.2')

const output = await ncu({
packageData: {
dependencies: {
'ncu-test-v2': '1.0.0',
},
},
packageManager: 'staticRegistry',
registry,
})

output!.should.deep.equal({
'ncu-test-v2': '99.9.9',
})
})

it('ignore dependencies that are not in the static registry', async () => {
const registry = './test/package-managers/staticRegistry/staticRegistry.json'

const output = await ncu({
packageData: {
dependencies: {
'ncu-test-tag': '1.0.0',
},
},
packageManager: 'staticRegistry',
registry,
})

output!.should.deep.equal({})
})

it('fetch static registry from a url', async () => {
const output = await ncu({
packageData: {
dependencies: {
'ncu-test-tag': '1.0.0',
},
},
packageManager: 'staticRegistry',
registry:
// https://gist.github.com/raineorshine/0802d7388c69193bed49c5ee6ab611b9
'https://gist.githubusercontent.com/raineorshine/0802d7388c69193bed49c5ee6ab611b9/raw/6f22bfdf19b7596089e56e0b14cd66d077f049d5/staticRegistry.json',
})

output!.should.deep.equal({})
})
})
6 changes: 0 additions & 6 deletions test/package-managers/staticRegistry/package.json

This file was deleted.

2 changes: 1 addition & 1 deletion test/package-managers/staticRegistry/staticRegistry.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"express": "4.1.2"
"ncu-test-v2": "99.9.9"
}