Skip to content
This repository was archived by the owner on Dec 18, 2023. It is now read-only.

Commit 1ec8983

Browse files
feat: add auto upgrade feature, liv-6434 (#22)
* feat: add upgrade CLI module * fix: set upgrade frequency to once a day * feat: add version checker from binary * refactor: separate prompt check and upgrade to different methods
1 parent 93a6fc0 commit 1ec8983

13 files changed

+114
-20
lines changed

__tests__/cmds/envs/list.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import configStore from 'configstore'
22
import envs from '../../../src/cmds/envs'
33

4-
jest.mock('configstore');
4+
jest.mock('configstore')
55

66
const mockedConfigstore = jest.mocked(configStore, true)
77

@@ -17,4 +17,4 @@ describe('List command', () => {
1717
const [ key ] = get.mock.calls[0]
1818
expect(key).toBe(`envs`)
1919
});
20-
})
20+
})

__tests__/cmds/envs/remove.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import configStore from 'configstore'
22
import envs from '../../../src/cmds/envs'
33

4-
jest.mock('configstore');
4+
jest.mock('configstore')
55

66
const mockedConfigstore = jest.mocked(configStore, true)
77

@@ -18,4 +18,4 @@ describe('Remove command', () => {
1818
const [ key ] = deleteFn.mock.calls[0]
1919
expect(key).toBe(`envs.${envName}`)
2020
});
21-
})
21+
})

index.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
#!/usr/bin/env node
22
const minimist = require('minimist')
33
const availableCommands = require('./src/cmds')
4+
const { checkAndUpgradeCliVersion } = require('./src/cmds/upgrade')
45

5-
const command = process.argv[2]
6+
async function executeCommand() {
7+
await checkAndUpgradeCliVersion()
68

7-
if (!command || !availableCommands[command]) {
8-
console.log('Usage: livestorm <command>')
9-
availableCommands.help()
10-
process.exit(1)
9+
const command = process.argv[2]
10+
11+
if (!command || !availableCommands[command]) {
12+
console.log('Usage: livestorm <command>')
13+
availableCommands.help()
14+
process.exit(1)
15+
}
16+
17+
availableCommands[command](minimist(process.argv.slice(3)))
1118
}
1219

13-
availableCommands[command](minimist(process.argv.slice(3)))
20+
executeCommand()

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@livestorm/cli",
3-
"version": "1.0.3",
3+
"version": "1.1.0",
44
"description": "CLI that allows you to build and deploy your Livestorm plugin",
55
"main": "index.js",
66
"bin": {
@@ -23,7 +23,8 @@
2323
"node-fetch": "^2.6.1",
2424
"node-watch": "^0.7.2",
2525
"prompts": "^2.4.0",
26-
"rimraf": "^3.0.2"
26+
"rimraf": "^3.0.2",
27+
"semver": "^7.3.5"
2728
},
2829
"typings": "types/index.d.ts",
2930
"devDependencies": {

src/cmds/build.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ module.exports = () => {
66
execSync('yarn build')
77
console.log(`Bundling done`)
88
return fs.readFileSync(`${process.cwd()}/build/bundle.js`)
9-
}
9+
}

src/cmds/create.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,4 @@ module.exports = () => {
8282
console.log('Oops, an error happened. Please drop us an email with the error detail.')
8383
}
8484
})
85-
}
85+
}

src/cmds/help.js

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ _ /____/ / __ |/ / _ /___ ____/ /_ / / /_/ /_ _, _/_ / / /
2323
['asset <file>', 'Upload files and use them in your plugins'],
2424
['list [--environment|--api-token]', 'List the published plugins'],
2525
['envs [add|remove|list] <environment> [--api-token|--endpoint]', 'Manage your environments'],
26+
['upgrade', 'Upgrade Livestorm Plugins CLI'],
27+
['version', 'Output the current version of the CLI'],
2628
], ['blue', 'blue'])
2729
)
2830

src/cmds/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const asset = require('./asset')
88
const help = require('./help')
99
const list = require('./list')
1010
const envs = require('./envs')
11+
const { upgrade } = require('./upgrade')
12+
const version = require('./version').printModuleVersion
1113

1214
module.exports = {
1315
publish,
@@ -20,4 +22,6 @@ module.exports = {
2022
help,
2123
list,
2224
envs,
23-
}
25+
upgrade,
26+
version
27+
}

src/cmds/list.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const { default: fetch } = require('node-fetch')
22
const commandLineArgs = require('command-line-args')
3+
const cliff = require('cliff')
4+
35
const getLivestormConfig = require('../helpers/getLivestormConfig')
46
const setLocalProxyIfNeeded = require('../helpers/setLocalProxyIfNeeded')
57
const setLocalHostIfNeeded = require('../helpers/setLocalHostIfNeeded')
6-
const cliff = require('cliff')
7-
88

99
module.exports = async () => {
1010
const options = commandLineArgs([
@@ -59,4 +59,4 @@ function handleResponse(response) {
5959
...response.plugins.map((plugin) => [plugin.id, plugin.name])
6060
]
6161
console.log(cliff.stringifyRows(rows, ['blue', 'blue']))
62-
}
62+
}

src/cmds/remove.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ function handleNetworkError(err, json) {
4848
console.log(err)
4949
console.log(`Failed to remove plugin ${json.name} from ${json.endpoint}.`)
5050
console.log('Make sure your internet connection is working and check https://status.livestorm.co/')
51-
}
51+
}

src/cmds/upgrade.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
}

src/cmds/version.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const fs = require('fs')
2+
3+
function getModuleVersion() {
4+
const file = fs.readFileSync('package.json', 'utf8')
5+
return JSON.parse(file).version
6+
}
7+
8+
function printModuleVersion() {
9+
console.log(getModuleVersion())
10+
}
11+
12+
module.exports = {
13+
getModuleVersion,
14+
printModuleVersion
15+
}

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -3221,7 +3221,7 @@ semver@^6.0.0, semver@^6.1.1, semver@^6.1.2, semver@^6.3.0:
32213221
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
32223222
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==
32233223

3224-
semver@^7.3.2:
3224+
semver@^7.3.2, semver@^7.3.5:
32253225
version "7.3.5"
32263226
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.5.tgz#0b621c879348d8998e4b0e4be94b3f12e6018ef7"
32273227
integrity sha512-PoeGJYh8HK4BTO/a9Tf6ZG3veo/A7ZVsYrSA6J8ny9nb3B1VrpkuN+z9OE5wfE5p6H4LchYZsegiQgbJD94ZFQ==

0 commit comments

Comments
 (0)