diff --git a/README.md b/README.md index ac7ab157..3f77cb9b 100644 --- a/README.md +++ b/README.md @@ -49,8 +49,8 @@ any of the following configuration properties: - `timeoutInDays` - Duration between update checks. Defaults to 60. - `message` - Customize update message. -- `registry` - URL of registry. Defaults to the public npm registry: `https://registry.npmjs.org` -- `authorization` - Authorization header value for registries that require auth. +- `registry` - URL of registry. Defaults to following your .npmrc configuration +- `authorization` - Authorization header value for registries that require auth. Defaults to following your .npmrc configuration - `frequency` - The frequency that the new version warning should be shown. - `frequencyUnit` - The unit of time that should be used to calculate the frequency (`days`, `hours`, `minutes`, `seconds`, `milliseconds`). Defaults to `minutes`. diff --git a/package.json b/package.json index d877c58c..077fa98c 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "ansis": "^3.3.1", "debug": "^4.3.5", "http-call": "^5.2.2", - "lodash": "^4.17.21" + "lodash": "^4.17.21", + "registry-auth-token": "^5.0.2" }, "devDependencies": { "@commitlint/config-conventional": "^19", diff --git a/src/get-version.ts b/src/get-version.ts index 368a0a19..5c5099ff 100644 --- a/src/get-version.ts +++ b/src/get-version.ts @@ -9,16 +9,16 @@ async function run([name, file, version, registry, authorization]: string[]) { debug('file:', file) debug('version:', version) debug('registry:', registry) - debug('authorization:', authorization) + const url = [ registry.replace(/\/+$/, ''), // remove trailing slash name.replace('/', '%2f'), // scoped packages need escaped separator ].join('/') const headers = authorization ? {authorization} : {} await mkdir(dirname(file), {recursive: true}) - await writeFile(file, JSON.stringify({current: version, headers})) // touch file with current version to prevent multiple updates + await writeFile(file, JSON.stringify({current: version})) // touch file with current version to prevent multiple updates const {body} = await HTTP.get<{'dist-tags': string[]}>(url, {headers, timeout: 5000}) - await writeFile(file, JSON.stringify({...body['dist-tags'], authorization, current: version})) + await writeFile(file, JSON.stringify({...body['dist-tags'], current: version})) // eslint-disable-next-line n/no-process-exit, unicorn/no-process-exit process.exit(0) } diff --git a/src/hooks/init/check-update.ts b/src/hooks/init/check-update.ts index 9d92d84f..8209b09d 100644 --- a/src/hooks/init/check-update.ts +++ b/src/hooks/init/check-update.ts @@ -1,4 +1,5 @@ /* eslint-disable valid-jsdoc */ + import {Hook, Interfaces} from '@oclif/core' import {Ansis} from 'ansis' import makeDebug from 'debug' @@ -6,6 +7,8 @@ import {spawn} from 'node:child_process' import {readFile, stat, writeFile} from 'node:fs/promises' import {dirname, join, resolve} from 'node:path' import {fileURLToPath} from 'node:url' +import getAuthToken from 'registry-auth-token'; +import getRegistryUrl from 'registry-auth-token/registry-url.js'; const ansis = new Ansis() @@ -130,15 +133,25 @@ const hook: Hook.Init = async function ({config}) { const debug = makeDebug('update-check') const versionFile = join(config.cacheDir, 'version') const lastWarningFile = join(config.cacheDir, 'last-warning') + const scope = config.name.split('/')[0]; // Destructure package.json configuration with defaults const { - authorization = '', message = '<%= config.name %> update available from <%= chalk.greenBright(config.version) %> to <%= chalk.greenBright(latest) %>.', - registry = config.npmRegistry ?? 'https://registry.npmjs.org', + registry = config.npmRegistry ?? getRegistryUrl(scope), // Use custom registry or fallback to 1) registry set for the scope, 2) default registry in the npmrc, or 3) the default registry timeoutInDays = 60, } = config.pjson.oclif['warn-if-update-available'] ?? {} + // Get the authorization header next as we need the registry to be computed first + let { + authorization + } = config.pjson.oclif['warn-if-update-available'] ?? {} + + if (!authorization) { + const authToken = getAuthToken(registry); + authorization = authToken ? `${authToken.type} ${authToken.token}` : ''; + } + const refreshNeeded = async () => { if (this.config.scopedEnvVarTrue('FORCE_VERSION_CACHE_UPDATE')) return true if (this.config.scopedEnvVarTrue('SKIP_NEW_VERSION_CHECK')) return false @@ -175,7 +188,7 @@ const hook: Hook.Init = async function ({config}) { this.warn( lodash.default.template(message)({ ansis, - // chalk and ansis have the same api. Keeping chalk for backwards compatibility. + // Chalk and ansis have the same api. Keeping chalk for backwards compatibility. chalk: ansis, config, latest: newerVersion,