(options: { allowProcessExit?: boolean } = {})
| 27 | } |
| 28 | |
| 29 | export function fetchLatestVersion(options: { allowProcessExit?: boolean } = {}): Promise<string> { |
| 30 | return new Promise((resolve, reject) => { |
| 31 | const req = https.get( |
| 32 | `https://registry.npmjs.org/${NPM_PACKAGE}/latest`, |
| 33 | { timeout: 5000 }, |
| 34 | res => { |
| 35 | let body = ''; |
| 36 | res.on('data', chunk => (body += chunk)); |
| 37 | res.on('end', () => { |
| 38 | try { |
| 39 | const data = JSON.parse(body); |
| 40 | if (typeof data.version === 'string') { |
| 41 | resolve(data.version); |
| 42 | } else { |
| 43 | reject(new Error('Unexpected registry response')); |
| 44 | } |
| 45 | } catch { |
| 46 | reject(new Error('Failed to parse registry response')); |
| 47 | } |
| 48 | }); |
| 49 | } |
| 50 | ); |
| 51 | req.on('error', reject); |
| 52 | req.on('timeout', () => { |
| 53 | req.destroy(); |
| 54 | reject(new Error('Registry request timed out')); |
| 55 | }); |
| 56 | if (options.allowProcessExit) { |
| 57 | // Allow the process to exit even if this request is still pending |
| 58 | req.on('socket', (socket: { unref: () => void }) => socket.unref()); |
| 59 | } |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | export function formatUpdateNotice(latestVersion: string): string { |
| 64 | return ( |
no test coverage detected