From a51fee25c633946b6aea9fe31f928e5e415a5171 Mon Sep 17 00:00:00 2001 From: Yvonnick FRIN Date: Sun, 5 Jan 2020 12:13:50 +0100 Subject: [PATCH] :recycle: Improve error messages for preset system (#161) --- packages/gitmoji-changelog-cli/src/cli.e2e.js | 20 +++++++++++++++++++ packages/gitmoji-changelog-cli/src/cli.js | 8 ++++++++ 2 files changed, 28 insertions(+) diff --git a/packages/gitmoji-changelog-cli/src/cli.e2e.js b/packages/gitmoji-changelog-cli/src/cli.e2e.js index 1f74edb..11d7a9b 100644 --- a/packages/gitmoji-changelog-cli/src/cli.e2e.js +++ b/packages/gitmoji-changelog-cli/src/cli.e2e.js @@ -53,6 +53,26 @@ describe('generate changelog', () => { expect(output.toString('utf8')).includes(["The preset unknown doesn't exist"]) }) + + it('should throw an Error if the preset could not find configuration', () => { + fs.unlinkSync(path.join(testDir, 'package.json')) + + const output = gitmojiChangelog() + + expect(output.toString('utf8')).includes(['Cannot retrieve configuration']) + }) + + it('should throw an Error if the preset did not return a version', () => { + const pkg = path.join(testDir, 'package.json') + // eslint-disable-next-line global-require + const content = JSON.parse(fs.readFileSync(pkg).toString('utf8')) + delete content.version + fs.writeFileSync(pkg, JSON.stringify(content)) + + const output = gitmojiChangelog() + + expect(output.toString('utf8')).includes(['Cannot retrieve the version from your configuration']) + }) }) describe('init', () => { diff --git a/packages/gitmoji-changelog-cli/src/cli.js b/packages/gitmoji-changelog-cli/src/cli.js index 882ef07..da40a44 100644 --- a/packages/gitmoji-changelog-cli/src/cli.js +++ b/packages/gitmoji-changelog-cli/src/cli.js @@ -44,6 +44,14 @@ async function main(options = {}) { // eslint-disable-next-line global-require const loadProjectInfo = require(`./presets/${options.preset}.js`) projectInfo = await loadProjectInfo() + + if (!projectInfo) { + throw Error(`Cannot retrieve configuration for preset ${options.preset}.`) + } + + if (!projectInfo.version) { + throw Error('Cannot retrieve the version from your configuration. Check it or you can do "gitmoji-changelog ".') + } } catch (e) { logger.error(e) // Force quit if the requested preset doesn't exist