Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Check CLI version #52

Merged
merged 5 commits into from
Oct 27, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/gitmoji-changelog-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"dependencies": {
"@gitmoji-changelog/core": "^1.0.1",
"@gitmoji-changelog/markdown": "^1.0.0",
"libnpm": "^1.0.0",
"semver-compare": "^1.0.0",
"yargs": "^12.0.1"
},
"publishConfig": {
Expand Down
21 changes: 21 additions & 0 deletions packages/gitmoji-changelog-cli/src/cli.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
const fs = require('fs')

const libnpm = require('libnpm')
const semverCompare = require('semver-compare')
const { generateChangelog, logger } = require('@gitmoji-changelog/core')
const { buildMarkdownFile } = require('@gitmoji-changelog/markdown')

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

async function getLatestVersion() {
const watchdog = new Promise(resolve => setTimeout(resolve, 500, { version: pkg.version }))
fabienjuif marked this conversation as resolved.
Show resolved Hide resolved
const request = libnpm.manifest('gitmoji-changelog@latest')

const { version } = await Promise.race([watchdog, request])

return version
}

async function main(options = {}) {
logger.start(`gitmoji-changelog v${pkg.version}`)
logger.info(`${options.mode} ${options.output}`)

try {
const latestVersion = await getLatestVersion()
if (semverCompare(latestVersion, pkg.version) > 0) {
logger.warn(`You got an outdated version of gitmoji-changelog, please update! (yours: ${pkg.version}, latest: ${latestVersion})`)
fabienjuif marked this conversation as resolved.
Show resolved Hide resolved
fabienjuif marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (e) { /* ignore error */ }

try {
const changelog = await generateChangelog(options)

Expand All @@ -31,6 +49,9 @@ async function main(options = {}) {
} catch (e) {
logger.error(e)
}

// force quit (if the latest version request is pending, we don't wait for it)
fabienjuif marked this conversation as resolved.
Show resolved Hide resolved
process.exit(0)
}

module.exports = { main }
61 changes: 61 additions & 0 deletions packages/gitmoji-changelog-cli/src/cli.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,66 @@
const { generateChangelog, logger } = require('@gitmoji-changelog/core')
const { manifest } = require('libnpm')
const { main } = require('./cli')

describe('cli', () => {
const realExitFunction = process.exit
beforeEach(() => {
process.exit = jest.fn(() => {})
jest.clearAllMocks()
})
afterEach(() => {
process.exit = realExitFunction
fabienjuif marked this conversation as resolved.
Show resolved Hide resolved
})

it('should throw an error if changelog generation fails', async () => {
generateChangelog.mockRejectedValue(new Error())

await main()

expect(logger.error).toHaveBeenCalled()
})

it('should call process.exit explicitly so promises are not waited for', async () => {
await main()

expect(process.exit).toHaveBeenCalledTimes(1)
})

describe('version control', () => {
it('should print a warning about a new version', async () => {
manifest.mockReturnValueOnce(Promise.resolve({ version: '2.0.0' }))
await main()

expect(logger.warn).toHaveBeenCalledWith('You got an outdated version of gitmoji-changelog, please update! (yours: 1.0.0, latest: 2.0.0)')
fabienjuif marked this conversation as resolved.
Show resolved Hide resolved
})

it('should NOT print a warning about a new version', async () => {
// older version in npm registry
manifest.mockReturnValueOnce(Promise.resolve({ version: '0.5.0' }))
await main()

// same version in npm registry
manifest.mockReturnValueOnce(Promise.resolve({ version: '1.0.0' }))
await main()

expect(manifest).toHaveBeenCalledTimes(2)
expect(logger.warn).not.toHaveBeenCalled()
})

it('should NOT print a warning about a new version when request took to much time', async () => {
manifest.mockImplementationOnce(() => new Promise((resolve) => { setTimeout(resolve, 1000, { version: '2.0.0' }) }))
await main()

expect(logger.warn).not.toHaveBeenCalled()
})

it('should NOT print a warning about a new version when request is on error', async () => {
manifest.mockReturnValueOnce(Promise.reject(new Error('faked error (manifest)')))
await main()

expect(logger.warn).not.toHaveBeenCalled()
})
})
})

jest.mock('@gitmoji-changelog/core', () => ({
Expand All @@ -18,5 +70,14 @@ jest.mock('@gitmoji-changelog/core', () => ({
error: jest.fn(),
success: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
},
}))

jest.mock('../package.json', () => ({
version: '1.0.0',
}))

jest.mock('libnpm', () => ({
manifest: jest.fn(),
}))
Loading