Skip to content

Commit

Permalink
[#4]: Add CLI flag configuration (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulshryock authored Apr 2, 2021
1 parent 809c919 commit 86ccad2
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 75 deletions.
14 changes: 14 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Summary

## Changelog

## Testing

## Ticket

## Screenshot(s)

## Checklist
- [ ] I have tested the code in this pull request.
- [ ] I have updated the Changelog.
- [ ] I have documented any new features or changes in the Readme.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased](https://github.com/paulshryock/release-bump/compare/HEAD..1.0.0)

### Added
- Add CLI flag configuration. [#4]

### Changed

Expand Down
98 changes: 79 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,62 @@
# Release Bump
# `release-bump`

Release Bump handles version bump tasks for a code release.
`release-bump` handles version bump tasks for a code release.

## Roadmap
## Features

- [x] Bump Changelog
- [ ] Bump docblock comments
- [ ] Add CLI flag configuration
[View roadmap](https://github.com/paulshryock/release-bump/issues?q=is%3Aissue+is%3Aopen+label%3Aenhancement)

### Bump Changelog

Add lines to your Changelog beneath the Unreleased header as you make changes.

`release-bump` will change this:

```bash
## [Unreleased]

### Added
- Add my new feature.

### Changed

### Deprecated

### Removed

### Fixed
- Fix a bug.
- Fix another bug.

### Security
```

to this:

```bash
## [Unreleased](https://example.com/repo/compare/HEAD..1.0.0)

### Added

### Changed

### Deprecated

### Removed

### Fixed

### Security

## [1.0.0](https://example.com/repo/releases/tags/v1.0.0) - 3/30/2021

### Added
- Add my new feature.

### Fixed
- Fix a bug.
- Fix another bug.
```

## Usage

Expand All @@ -28,11 +78,21 @@ Add `release-bump` to a `version` npm script.
}
```

Now whenever you run `npm version <major|minor|patch>`, all of the Release Bump tasks will execute with the new version number before npm creates a version commit.
Now whenever you run `npm version <major|minor|patch>`, all of the `release-bump` tasks will execute with the new version number before npm creates a version commit.

#### Configuration

| Option | Type | Default | Description |
| :--- | :--- | :--- | :--- |
| `-p` | string | `./CHANGELOG.md` | The Changelog file path. |
| `-r` | string | `github` | The Git remote. (`github`|`bitbucket`) |
| `-s` | boolean | `false` | Whether to skip `v` in the version. |
| `-t` | string | empty string | The initial Changelog text. |
| `-u` | string | `https://keepachangelog.com/en/1.0.0/` | The initial Changelog text URL. |

### JavaScript API

If you prefer to run Release Bump programattically, just require Release Bump as a class and instantiate it:
If you prefer to run `release-bump` programattically, just require and instantiate a `release-bump` class:

```javascript
const Bump = require('release-bump')
Expand All @@ -46,21 +106,21 @@ const Bump = require('release-bump')
new Bump({
changelog: {
filePath: './CHANGELOG.md',
includeV: true,
gitRemote: 'github',
initialText: '',
initialTextUrl: 'https://keepachangelog.com/en/1.0.0/',
remote: 'github',
}
skipV: false,
},
})
```

#### Configuration

| Option | Type | Default | Description |
| :--- | :--- | :--- | :--- |
| `changelog` | Object | | The Changelog options. |
| `changelog.filePath` | string | `./CHANGELOG.md` | The Changelog file path. |
| `changelog.includeV` | boolean | `true` | Whether to include v in the version. |
| `changelog.initialText` | string | empty string | The initial Changelog text. |
| `changelog.initialTextUrl` | string | `https://keepachangelog.com/en/1.0.0/` | The initial Changelog text URL. |
| `changelog.remote` | string | `github` | Accepts `github` or `bitbucket`. |
| Option | Type | Default | Description |
| :--- | :--- | :--- | :--- |
| `changelog` | Object | | The Changelog options. |
| `changelog.filePath` | string | `./CHANGELOG.md` | The Changelog file path. |
| `changelog.gitRemote` | string | `github` | The Git remote. (`github`|`bitbucket`) |
| `changelog.initialText` | string | empty string | The initial Changelog text. |
| `changelog.initialTextUrl` | string | `https://keepachangelog.com/en/1.0.0/` | The initial Changelog text URL. |
| `changelog.skipV` | boolean | `false` | Whether to skip `v` in the version. |
16 changes: 0 additions & 16 deletions cli.js

This file was deleted.

5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
"author": "Paul Shryock <paul@pshry.com> (https://github.com/paulshryock)",
"main": "src/bump.js",
"bin": {
"release-bump": "cli.js"
"release-bump": "src/cli.js"
},
"scripts": {
"version": "node cli.js && git add ."
"version": "node src/cli.js && git add .",
"postversion": "git push && git push --tags && npm publish"
},
"dependencies": {
"arg": "^5.0.0",
"axios": "^0.21.1",
"deepmerge": "^4.2.2"
},
Expand Down
69 changes: 42 additions & 27 deletions src/bump.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const Changelog = require('./changelog.js')
const defaults = require('./defaults.js')
const arg = require('arg')
const deepmerge = require('deepmerge')
// @todo: Include a deepmerge utility instead of a dependency.
// @todo: Use a deepmerge utility instead of a dependency.
// @see: https://thewebdev.info/2021/03/06/how-to-deep-merge-javascript-objects/
const Changelog = require('./changelog.js')

/**
* Bump class.
Expand All @@ -13,34 +15,47 @@ module.exports = class Bump {
/**
* Bump class constructor.
*
* @param {Object} options Configuration options.
* @param {Object} config Configuration options.
* @since 1.0.0
*/
constructor(options = {}) {
this.options = options
constructor(config = {}) {
// Define CLI args.
this.args = arg({
'--changelog-file-path': String,
'-p': '--changelog-file-path',

// Setup Changelog.
this.changelog()
}
'--git-remote': String,
'-r': '--git-remote',

/**
* Setup Changelog.
*
* @since 1.0.0
*/
changelog() {
const defaults = {
filePath: './CHANGELOG.md',
includeV: true,
initialText: '',
initialTextUrl: 'https://keepachangelog.com/en/1.0.0/',
remote: 'github',
}

new Changelog(
this.options.changelog
? deepmerge(defaults, this.options.changelog)
: defaults
)
'--skip-v-in-version': Boolean,
'--skip-v': '--skip-v-in-version',
'-s': '--skip-v-in-version',

'--initial-changelog-text': String,
'-t': '--initial-changelog-text',

'--initial-changelog-text-url': String,
'-u': '--initial-changelog-text-url',
})

// Get CLI arg values.
this.argv = { changelog: {} }
if (this.args['--changelog-file-path']) this.argv.changelog.filePath = this.args['--changelog-file-path']
if (this.args['--skip-v-in-version']) this.argv.changelog.skipV = this.args['--skip-v-in-version']
if (this.args['--initial-changelog-text']) this.argv.changelog.initialText = this.args['--initial-changelog-text']
if (this.args['--initial-changelog-text-url']) this.argv.changelog.initialTextUrl = this.args['--initial-changelog-text-url']
if (this.args['--git-remote']) this.argv.changelog.gitRemote = this.args['--git-remote']

// Setup defaults.
this.defaults = defaults

// Merge passed config object with defaults.
this.config = deepmerge(this.defaults, config)

// Merge CLI args with config.
this.options = deepmerge(this.config, this.argv)

// Setup Changelog.
new Changelog(this.options.changelog)
}
}
25 changes: 14 additions & 11 deletions src/changelog.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require('fs')
// @todo: Use fetch instead of a dependency.
const axios = require('axios')
// @todo: Use fetch instead of a dependency.
const fs = require('fs')

/**
* Changelog class.
Expand All @@ -13,29 +13,32 @@ module.exports = class Changelog {
* Changelog class constructor.
*
* @param {string} options.filePath The Changelog file path.
* @param {boolean} options.includeV Whether to include v in the version.
* @param {string} options.gitRemote The Git remote. (`github`|`bitbucket`)
* @param {string} options.initialText The initial Changelog text.
* @param {string} options.initialTextUrl The initial Changelog text URL.
* @param {string} options.remote Accepts 'github' or 'bitbucket'.
* @param {boolean} options.skipV Whether to skip v in the version.
* @since 1.0.0
*/
constructor({ filePath, includeV, initialText, initialTextUrl, remote }) {
constructor({ filePath, gitRemote, initialText, initialTextUrl, skipV }) {
const { version, repository } = JSON.parse(fs.readFileSync('./package.json', 'utf8'))
if (!version || !repository || !repository.url) {
console.error('version or repository url is missing from package.json.')
return
}
const [month, date, year] = new Date().toLocaleDateString('en-US').split('/')
this.version = version
this.repository = repository.url.replace('git+', '').replace('.git', '')
this.repository = repository.url
.replace(`^git\+?`, '')
.replace(/^(ssh)?:\/\//, 'https://')
.replace(/\.git(#.*$)?/, '')
this.today = `${month}/${date}/${year}`
this.filePath = filePath
this.includeV = includeV
this.gitRemote = gitRemote.toLowerCase()
this.initialText = initialText
this.initialTextUrl = initialTextUrl
this.remote = remote.toLowerCase()
this.skipV = skipV
this.unreleased =
`## [Unreleased](${this.repository}/${this.remote === 'bitbucket' ? 'branches/' : ''}compare/HEAD..${this.version})` +
`## [Unreleased](${this.repository}/${this.gitRemote === 'bitbucket' ? 'branches/' : ''}compare/HEAD..${this.version})` +
'\n\n### Added' +
'\n\n### Changed' +
'\n\n### Deprecated' +
Expand Down Expand Up @@ -143,8 +146,8 @@ module.exports = class Changelog {
*/
bump () {
this.header = `## [${this.version}](${this.repository}/` +
`${this.remote === 'bitbucket' ? 'commits/tag' : 'releases/tags'}/` +
`${this.includeV ? 'v' : ''}${this.version}) - ${this.today}`
`${this.gitRemote === 'bitbucket' ? 'commits/tag' : 'releases/tags'}/` +
`${this.skipV ? '' : 'v'}${this.version}) - ${this.today}`

this.write(this.text
// Bump unreleased version and add today's date.
Expand Down
5 changes: 5 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node
(function () {
const Bump = require('./bump.js')
new Bump()
})()
15 changes: 15 additions & 0 deletions src/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Configuration defaults.
*
* @since 1.0.0
* @type {Object}
*/
module.exports = {
changelog: {
filePath: './CHANGELOG.md',
gitRemote: 'github',
initialText: '',
initialTextUrl: 'https://keepachangelog.com/en/1.0.0/',
skipV: false,
},
}

0 comments on commit 86ccad2

Please sign in to comment.