|
| 1 | +const { execSync } = require('child_process') |
| 2 | +const { default: fetch } = require('node-fetch') |
| 3 | +const prompts = require('prompts') |
| 4 | +const semverGte = require('semver/functions/gte') |
| 5 | + |
| 6 | +const configStore = require('../helpers/configStore.js') |
| 7 | +const checkCurrentVersion = require('./version').getModuleVersion |
| 8 | + |
| 9 | +async function checkLatestVersion() { |
| 10 | + const response = await fetch('https://registry.npmjs.org/@livestorm/cli') |
| 11 | + const json = await response.json() |
| 12 | + return json['dist-tags']['latest'] |
| 13 | +} |
| 14 | + |
| 15 | +async function checkCandidateForUpgrade() { |
| 16 | + try { |
| 17 | + const currentDate = new Date().toISOString().split('T')[0] |
| 18 | + const latestUpgradeDate = configStore.get('latestUpgradeDate') |
| 19 | + if (currentDate === latestUpgradeDate) return false |
| 20 | + |
| 21 | + configStore.set('latestUpgradeDate', currentDate) |
| 22 | + |
| 23 | + const currentVersion = checkCurrentVersion() |
| 24 | + const latestVersion = await checkLatestVersion() |
| 25 | + if (semverGte(currentVersion, latestVersion)) return false |
| 26 | + |
| 27 | + return true |
| 28 | + } |
| 29 | + catch (error) { |
| 30 | + return false |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +async function promptUpgrade() { |
| 35 | + const answer = await prompts({ |
| 36 | + type: 'text', |
| 37 | + name: 'upgrade', |
| 38 | + message: "We noticed your CLI isn't up to date, do you want to upgrade? (yes/no)", |
| 39 | + validate: value => { |
| 40 | + return (value !== 'no' || value !== 'yes') |
| 41 | + } |
| 42 | + }) |
| 43 | + if (answer.upgrade === 'yes') return true |
| 44 | + |
| 45 | + return false |
| 46 | +} |
| 47 | + |
| 48 | +function upgrade() { |
| 49 | + console.log('Upgrading @livestorm/cli to the latest version ...') |
| 50 | + execSync('yarn global upgrade @livestorm/cli@latest') |
| 51 | + console.log('All done 🙌') |
| 52 | +} |
| 53 | + |
| 54 | +async function checkAndUpgradeCliVersion() { |
| 55 | + const isCandidateForUpgrade = await checkCandidateForUpgrade() |
| 56 | + if (isCandidateForUpgrade) { |
| 57 | + const yes = await promptUpgrade() |
| 58 | + if (yes) upgrade() |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +module.exports = { |
| 63 | + checkAndUpgradeCliVersion, |
| 64 | + upgrade |
| 65 | +} |
0 commit comments