Skip to content

Commit aed5b4e

Browse files
kinetifexjdx
authored andcommitted
feat: support private registries (#12)
1 parent ecbdf91 commit aed5b4e

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,26 @@ Add the plugin to your project with `yarn add @oclif/plugin-warn-if-update-avail
4545

4646
# Configuration
4747

48-
In `package.json`, set `oclif['warn-if-update-available'].timeoutInDays` to change the timeout duration between checks.
48+
In `package.json`, set `oclif['warn-if-update-available']` to an object with
49+
any of the following configuration properties:
50+
51+
- `timeoutInDays` - Duration between update checks. Defaults to 60.
52+
- `registry` - URL of registry. Defaults to the public npm registry: `https://registry.npmjs.org`
53+
- `authorization` - Authorization header value for registries that require auth.
54+
55+
## Example configuration
56+
57+
```json
58+
{
59+
"oclif": {
60+
"plugins": [
61+
"@oclif/plugin-warn-if-update-available"
62+
],
63+
"warn-if-update-available": {
64+
"timeoutInDays": 7,
65+
"registry": "https://my.example.com/module/registry",
66+
"authorization": "Basic <SOME READ ONLY AUTH TOKEN>"
67+
}
68+
}
69+
}
70+
```

src/get-version.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import * as fs from 'fs-extra'
22
import HTTP from 'http-call'
33

4-
async function run(name: string, file: string, version: string) {
5-
await fs.outputJSON(file, {current: version}) // touch file with current version to prevent multiple updates
6-
const {body} = await HTTP.get<any>(`https://registry.npmjs.org/${name.replace('/', '%2f')}`, {timeout: 5000})
7-
await fs.outputJSON(file, {...body['dist-tags'], current: version})
4+
async function run(name: string, file: string, version: string, registry: string, authorization: string) {
5+
const url = [
6+
registry.replace(/\/+$/, ''), // remove trailing slash
7+
name.replace('/', '%2f') // scoped packages need escaped separator
8+
].join('/')
9+
const headers = authorization ? {authorization} : {}
10+
11+
await fs.outputJSON(file, {current: version, headers}) // touch file with current version to prevent multiple updates
12+
const {body} = await HTTP.get<any>(url, {headers, timeout: 5000})
13+
await fs.outputJSON(file, {...body['dist-tags'], current: version, authorization})
814
process.exit(0)
915
}
1016

11-
run(process.argv[2], process.argv[3], process.argv[4])
17+
run(process.argv[2], process.argv[3], process.argv[4], process.argv[5], process.argv[6])
1218
.catch(require('@oclif/errors/handle'))

src/hooks/init/check-update.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ const debug = require('debug')('update-check')
1010
const hook: Hook<'init'> = async function ({config}) {
1111
const file = path.join(config.cacheDir, 'version')
1212

13+
// Destructure package.json configuration with defaults
14+
const {
15+
timeoutInDays = 60,
16+
registry = 'https://registry.npmjs.org',
17+
authorization = '',
18+
} = (config.pjson.oclif as any)['warn-if-update-available'] || {}
19+
1320
const checkVersion = async () => {
1421
try {
1522
const distTags = await fs.readJSON(file)
@@ -28,8 +35,6 @@ const hook: Hook<'init'> = async function ({config}) {
2835

2936
const refreshNeeded = async () => {
3037
try {
31-
const cfg = (config.pjson.oclif as any)['warn-if-update-available'] || {}
32-
const timeoutInDays = cfg.timeoutInDays || 60
3338
const {mtime} = await fs.stat(file)
3439
const staleAt = new Date(mtime.valueOf() + 1000 * 60 * 60 * 24 * timeoutInDays)
3540
return staleAt < new Date()
@@ -43,7 +48,7 @@ const hook: Hook<'init'> = async function ({config}) {
4348
debug('spawning version refresh')
4449
spawn(
4550
process.execPath,
46-
[path.join(__dirname, '../../../lib/get-version'), config.name, file, config.version],
51+
[path.join(__dirname, '../../../lib/get-version'), config.name, file, config.version, registry, authorization],
4752
{
4853
detached: !config.windows,
4954
stdio: 'ignore',

0 commit comments

Comments
 (0)