Skip to content

Commit 18ead38

Browse files
authored
✨ Check CLI version (#52)
1 parent 4d52747 commit 18ead38

File tree

4 files changed

+1487
-22
lines changed

4 files changed

+1487
-22
lines changed

packages/gitmoji-changelog-cli/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
"dependencies": {
3434
"@gitmoji-changelog/core": "^1.0.1",
3535
"@gitmoji-changelog/markdown": "^1.0.0",
36+
"libnpm": "^1.0.0",
37+
"semver-compare": "^1.0.0",
3638
"yargs": "^12.0.1"
3739
},
3840
"publishConfig": {

packages/gitmoji-changelog-cli/src/cli.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
const fs = require('fs')
22

3+
const libnpm = require('libnpm')
4+
const semverCompare = require('semver-compare')
35
const { generateChangelog, logger } = require('@gitmoji-changelog/core')
46
const { buildMarkdownFile } = require('@gitmoji-changelog/markdown')
57

68
const pkg = require('../package.json')
79

10+
async function getLatestVersion() {
11+
const watchdog = new Promise(resolve => setTimeout(resolve, 500, { version: pkg.version }))
12+
const request = libnpm.manifest('gitmoji-changelog@latest')
13+
14+
const { version } = await Promise.race([watchdog, request])
15+
16+
return version
17+
}
18+
819
async function main(options = {}) {
920
logger.start(`gitmoji-changelog v${pkg.version}`)
1021
logger.info(`${options.mode} ${options.output}`)
1122

23+
try {
24+
const latestVersion = await getLatestVersion()
25+
if (semverCompare(latestVersion, pkg.version) > 0) {
26+
logger.warn(`You got an outdated version of gitmoji-changelog, please update! (yours: ${pkg.version}, latest: ${latestVersion})`)
27+
logger.warn('Just do the following npm command to update it:')
28+
logger.warn('\t> npm install -g gitmoji-changelog@latest')
29+
}
30+
} catch (e) { /* ignore error */ }
31+
1232
try {
1333
const changelog = await generateChangelog(options)
1434

@@ -31,6 +51,9 @@ async function main(options = {}) {
3151
} catch (e) {
3252
logger.error(e)
3353
}
54+
55+
// force quit (if the latest version request is pending, we don't wait for it)
56+
process.exit(0)
3457
}
3558

3659
module.exports = { main }

packages/gitmoji-changelog-cli/src/cli.spec.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,68 @@
11
const { generateChangelog, logger } = require('@gitmoji-changelog/core')
2+
const { manifest } = require('libnpm')
23
const { main } = require('./cli')
34

45
describe('cli', () => {
6+
const realExitFunction = process.exit
7+
beforeEach(() => {
8+
process.exit = jest.fn(() => {})
9+
jest.clearAllMocks()
10+
})
11+
afterEach(() => {
12+
process.exit = realExitFunction
13+
})
14+
515
it('should throw an error if changelog generation fails', async () => {
616
generateChangelog.mockRejectedValue(new Error())
717

818
await main()
919

1020
expect(logger.error).toHaveBeenCalled()
1121
})
22+
23+
it('should call process.exit explicitly so promises are not waited for', async () => {
24+
await main()
25+
26+
expect(process.exit).toHaveBeenCalledTimes(1)
27+
})
28+
29+
describe('version control', () => {
30+
const findOutdatedMessage = () => logger.warn.mock.calls.find(([message]) => message.includes('outdated'))
31+
32+
it('should print a warning about a new version', async () => {
33+
manifest.mockReturnValueOnce(Promise.resolve({ version: '2.0.0' }))
34+
await main()
35+
36+
expect(findOutdatedMessage()).toBeTruthy()
37+
})
38+
39+
it('should NOT print a warning about a new version', async () => {
40+
// older version in npm registry
41+
manifest.mockReturnValueOnce(Promise.resolve({ version: '0.5.0' }))
42+
await main()
43+
44+
// same version in npm registry
45+
manifest.mockReturnValueOnce(Promise.resolve({ version: '1.0.0' }))
46+
await main()
47+
48+
expect(manifest).toHaveBeenCalledTimes(2)
49+
expect(findOutdatedMessage()).toBeFalsy()
50+
})
51+
52+
it('should NOT print a warning about a new version when request took to much time', async () => {
53+
manifest.mockImplementationOnce(() => new Promise((resolve) => { setTimeout(resolve, 1000, { version: '2.0.0' }) }))
54+
await main()
55+
56+
expect(findOutdatedMessage()).toBeFalsy()
57+
})
58+
59+
it('should NOT print a warning about a new version when request is on error', async () => {
60+
manifest.mockReturnValueOnce(Promise.reject(new Error('faked error (manifest)')))
61+
await main()
62+
63+
expect(findOutdatedMessage()).toBeFalsy()
64+
})
65+
})
1266
})
1367

1468
jest.mock('@gitmoji-changelog/core', () => ({
@@ -18,5 +72,14 @@ jest.mock('@gitmoji-changelog/core', () => ({
1872
error: jest.fn(),
1973
success: jest.fn(),
2074
info: jest.fn(),
75+
warn: jest.fn(),
2176
},
2277
}))
78+
79+
jest.mock('../package.json', () => ({
80+
version: '1.0.0',
81+
}))
82+
83+
jest.mock('libnpm', () => ({
84+
manifest: jest.fn(),
85+
}))

0 commit comments

Comments
 (0)