-
Notifications
You must be signed in to change notification settings - Fork 12
/
check-update.ts
73 lines (65 loc) · 2.14 KB
/
check-update.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import {Hook} from '@oclif/config'
import * as Chalk from 'chalk'
import {spawn} from 'child_process'
import * as fs from 'fs-extra'
import * as _ from 'lodash'
import * as path from 'path'
import * as semver from 'semver'
const debug = require('debug')('update-check')
const hook: Hook<'init'> = async function ({config}) {
const file = path.join(config.cacheDir, 'version')
// Destructure package.json configuration with defaults
const {
timeoutInDays = 60,
message = '<%= config.name %> update available from <%= chalk.greenBright(config.version) %> to <%= chalk.greenBright(latest) %>.',
registry = 'https://registry.npmjs.org',
authorization = '',
} = (config.pjson.oclif as any)['warn-if-update-available'] || {}
const checkVersion = async () => {
try {
// do not show warning if updating
if (process.argv[2] === 'update') return
const distTags = await fs.readJSON(file)
if (config.version.includes('-')) {
// TODO: handle channels
return
}
if (distTags && distTags.latest && semver.gt(distTags.latest.split('-')[0], config.version.split('-')[0])) {
const chalk: typeof Chalk = require('chalk')
const template = _.template
// Default message if the user doesn't provide one
this.warn(template(message)({
chalk,
config,
...distTags,
}))
}
} catch (error) {
if (error.code !== 'ENOENT') throw error
}
}
const refreshNeeded = async () => {
try {
const {mtime} = await fs.stat(file)
const staleAt = new Date(mtime.valueOf() + (1000 * 60 * 60 * 24 * timeoutInDays))
return staleAt < new Date()
} catch (error) {
debug(error)
return true
}
}
const spawnRefresh = async () => {
debug('spawning version refresh')
spawn(
process.execPath,
[path.join(__dirname, '../../../lib/get-version'), config.name, file, config.version, registry, authorization],
{
detached: !config.windows,
stdio: 'ignore',
},
).unref()
}
await checkVersion()
if (await refreshNeeded()) await spawnRefresh()
}
export default hook