Skip to content

Commit

Permalink
✨ add cli commands to manage init and update
Browse files Browse the repository at this point in the history
  • Loading branch information
bpetetot committed Oct 7, 2018
1 parent 60a0340 commit 0b20404
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 32 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# Changelog

<a name="vnext"></a>
## next
<a name="0.0.1"></a>
## 0.0.1

### Added

- :white_check_mark: add (bad) test for incremental writing ([60a0340](https://github.com/frinyvonnick/gitmoji-changelog/commit/60a034027315884f33b30d803e67faab049629f7))
- :sparkles: order releases by reverse semver versions ([2df2bbf](https://github.com/frinyvonnick/gitmoji-changelog/commit/2df2bbffc5218a9eb1556be61a8a401f2cc74799))
- :white_check_mark: add tests when no commits available ([90903c6](https://github.com/frinyvonnick/gitmoji-changelog/commit/90903c61233418aeb12515c11b7f269e85bc9afc))
- :white_check_mark: fix tests ([80425f9](https://github.com/frinyvonnick/gitmoji-changelog/commit/80425f9009ab6ffc5e738cb6270f38189f34f28b))
- :sparkles: add incremental markdown ([11b71e6](https://github.com/frinyvonnick/gitmoji-changelog/commit/11b71e686c563e3cbfdfbd4ad5d076c174c82c2a))
- :sparkles: write incremental markdown ([2c148a6](https://github.com/frinyvonnick/gitmoji-changelog/commit/2c148a696eede21e2a35740bb25ac34db87e2371))
- :sparkles: write process to generate incremental markdown file ([f86030c](https://github.com/frinyvonnick/gitmoji-changelog/commit/f86030c2b43f71d709eae9920be53dcfa91d34a4))
- :sparkles: Add logger ([#21](https://github.com/frinyvonnick/gitmoji-changelog/issues/21)) ([93887ca](https://github.com/frinyvonnick/gitmoji-changelog/commit/93887ca1f2e59263a31b3b700507aa1e9853118c))
Expand Down
23 changes: 13 additions & 10 deletions packages/gitmoji-changelog-cli/src/cli.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
const fs = require('fs')

const { generateChangelog, logger } = require('@gitmoji-changelog/core')
const { buildMarkdownFile } = require('@gitmoji-changelog/markdown')
const fs = require('fs')

async function main({ format, mode, release } = {}) {
const pkg = require('../package.json')

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

try {
const changelog = await generateChangelog({ mode, release })
const changelog = await generateChangelog(options)

if (changelog.meta.package) {
const { name, version } = changelog.meta.package
Expand All @@ -14,17 +20,14 @@ async function main({ format, mode, release } = {}) {
logger.info(changelog.meta.repository.url)
}

let output
switch (format) {
switch (options.format) {
case 'json':
output = './CHANGELOG.json'
fs.writeFileSync(output, JSON.stringify(changelog))
fs.writeFileSync(options.ouptut, JSON.stringify(changelog))
break
default:
output = './CHANGELOG.md'
await buildMarkdownFile(changelog, { mode, output })
await buildMarkdownFile(changelog, options)
}
logger.success(`changelog updated into ${output}`)
logger.success(`changelog updated into ${options.output}`)
} catch (e) {
logger.error(e)
}
Expand Down
1 change: 1 addition & 0 deletions packages/gitmoji-changelog-cli/src/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('cli', () => {
jest.mock('@gitmoji-changelog/core', () => ({
generateChangelog: jest.fn(),
logger: {
start: jest.fn(),
error: jest.fn(),
success: jest.fn(),
info: jest.fn(),
Expand Down
71 changes: 51 additions & 20 deletions packages/gitmoji-changelog-cli/src/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,63 @@
#! /usr/bin/env node

const fs = require('fs')
const yargs = require('yargs')
const { logger } = require('@gitmoji-changelog/core')
const { noop } = require('lodash')

const { version } = require('../package.json')
const { homepage } = require('../package.json')
const { main } = require('./cli')

logger.start(`gitmoji-changelog v${version}`)
function getOutputFile({ output, format }) {
if (output) {
return output
}
if (format === 'json') {
return './CHANGELOG.json'
}
return './CHANGELOG.md'
}

const args = yargs
.option('mode', {
alias: 'm',
default: 'init',
description: 'build from scratch or incremental changelog',
choices: ['init', 'incremental'],
yargs
.usage('Usage: $0 <command> [options]')
.command('$0 [release]', 'Generate changelog', (command) => {
command.positional('release', {
desc: 'Next version (from-package, next)',
default: 'from-package',
})
}, (argv) => {
const output = getOutputFile(argv)
const existsOuput = fs.existsSync(output)
main({
...argv,
mode: existsOuput ? 'update' : 'init',
output,
})
})
.command('init', 'Initialize changelog from tags', noop, (argv) => {
main({
...argv,
mode: 'init',
output: getOutputFile(argv),
})
})
.option('release', {
alias: 'r',
default: 'from-package',
description: 'next release version for the changelog from package.json or next release',
choices: ['from-package', 'next'],
.command('update [release]', 'Update changelog with a new version', (command) => {
command.positional('release', {
desc: 'Next version (from-package, next)',
default: 'from-package',
})
}, (argv) => {
main({
...argv,
mode: 'update',
output: getOutputFile(argv),
})
})
.option('format', {
alias: 'f',
default: 'markdown',
description: 'changelog output format',
choices: ['json', 'markdown'],
desc: 'changelog format (markdown, json)',
})
.option('output', {
desc: 'output changelog file',
})
.help('help')
.epilog(`For more information visit: ${homepage}`)
.parse()

main(args)

0 comments on commit 0b20404

Please sign in to comment.