Skip to content

Commit

Permalink
[#2]: Bump WordPress theme version (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulshryock authored May 20, 2021
1 parent 2d551ce commit b55052f
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 8 deletions.
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.2.1)

### Added
- Bump WordPress theme version.

### Changed

Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@ to this:
- Fix another bug.
```

### Bump WordPress theme

If your project has a file named `style.css` in the root directory, it will bump the version, if there is one.

`release-bump` will change this:

```css
/*
Theme Name: ...
Version: 0.0.1
...
*/
```

to this:

```css
/*
Theme Name: ...
Version: 1.0.0
...
*/
```

## Usage

### Install
Expand Down Expand Up @@ -91,6 +115,7 @@ Now whenever you run `npm version <major|minor|patch>`, all of the `release-bump
| `-t` | string | empty string | The initial Changelog text. |
| `-u` | string | `https://keepachangelog.com/en/1.0.0/` | The initial Changelog text URL. |
| `-v` | boolean | `false` | Log package version. |
| `-w` | boolean | `false` | Whether to skip WordPress theme bump. |

### JavaScript API

Expand All @@ -114,6 +139,7 @@ new Bump({
skipV: false,
},
help: false,
skipWordPress: false,
version: false,
})
```
Expand All @@ -129,4 +155,5 @@ new Bump({
| `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. |
| `help` | boolean | `false` | Whether to log help information. |
| `skipWordPress` | boolean | `false` | Whether to skip WordPress theme bump. |
| `version` | boolean | `false` | Whether to log package version. |
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"release-bump": "src/cli.js"
},
"scripts": {
"test": "node src/cli.js",
"version": "node src/cli.js && git add .",
"postversion": "git push && git push --tags && npm publish"
},
Expand Down
27 changes: 21 additions & 6 deletions src/bump.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Changelog = require('./changelog.js')
const WordPress = require('./wordpress.js')
const defaults = require('./defaults.js')
const pkg = require('../package.json')
const fs = require('fs')
Expand Down Expand Up @@ -44,6 +45,9 @@ module.exports = class Bump {

'--version': Boolean,
'-v': '--version',

'--skip-wordpress': Boolean,
'-w': '--skip-wordpress',
})

// Get CLI arg values.
Expand All @@ -54,6 +58,7 @@ module.exports = class Bump {
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['--skip-wordpress']) this.argv.skipWordPress = this.args['--skip-wordpress']
if (this.args['--version']) this.argv.version = this.args['--version']

// Setup defaults.
Expand All @@ -69,10 +74,15 @@ module.exports = class Bump {
if (this.options.help) return this.help()

// Log package version.
if (this.options.version) return this.version()
if (this.options.version) return this.logPackageVersion()

// @todo: Get version once and pass it to Changelog and WordPress.

// Setup Changelog.
// Handle Changelog bump.
new Changelog(this.options.changelog)

// Handle WordPress bump.
new WordPress({ skipWordPress: this.options.skipWordPress })
}

/**
Expand All @@ -81,22 +91,27 @@ module.exports = class Bump {
* @since unreleased
*/
help () {
const readme = fs.readFileSync('./node_modules/release-bump/README.md', 'utf-8')
const path = './node_modules/release-bump/README.md'
this.readme = fs.readFileSync(path, 'utf-8')
// Get CLI documentation.
.match(/### CLI.*### JavaScript API/s)[0]
// Filter configuration details.
.replace(/### CLI.*#### Configuration\n\n/s, '')
// Remove last line.
.replace(/\n\n### JavaScript API/s, '')
console.info(readme)
console.info(this.readme)
}

/**
* Log package version.
*
* @since unreleased
*/
version () {
console.info(pkg.version)
logPackageVersion () {
if (pkg.version) {
console.info(pkg.version)
} else {
console.warn('No version found.')
}
}
}
4 changes: 2 additions & 2 deletions src/changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ module.exports = class Changelog {
constructor({ filePath, gitRemote, initialText, initialTextUrl, skipV }) {
const { version, repository } = JSON.parse(fs.readFileSync('./package.json', 'utf8'))
if (!version) {
console.error('version is missing from package.json.')
console.error('Missing package.json version. Can not bump.')
return
}
if (!repository || !repository.url) {
console.error('repository url is missing from package.json.')
console.error('Missing package.json repository url. Can not bump.')
return
}
const [month, date, year] = new Date().toLocaleDateString('en-US').split('/')
Expand Down
1 change: 1 addition & 0 deletions src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ module.exports = {
skipV: false,
},
help: false,
skipWordPress: false,
version: false,
}
87 changes: 87 additions & 0 deletions src/wordpress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const axios = require('axios')
const { readFile, writeFile } = require('fs/promises')

/**
* WordPress class.
*
* @since 1.0.0
* @type {Class}
*/
module.exports = class WordPress {
/**
* WordPress class constructor.
*
* @param {boolean} skipWordPress Whether to skip WordPress theme bump.
* @since unreleased
*/
constructor({ skipWordPress }) {
// Bail early.
if (skipWordPress) return

this.filePath = './style.css'

// Bump WordPress theme version.
this.bump()
}

/**
* Bump WordPress theme version.
*
* @since unreleased
*/
async bump () {
try {
// If there is no text version, bail.
const text = await readFile(this.filePath, 'utf8')
const versionRegex = /Version: \d*\.?\d*\.?\d*/
if (!text.match(versionRegex)) {
console.error(`${this.filePath} does not contain a version. Can not bump WordPress theme.`)
return
}

// If there is no package version, bail.
const pkg = await readFile('./package.json', 'utf8')
const { version } = JSON.parse(pkg)
if (!version) {
console.error(
`Missing package.json version. Can not bump.`
)
return
}

// Bump WordPress theme version.
this.version = version
this.text = text.replace(versionRegex, `Version: ${this.version}`)

// Write WordPress theme version.
this.write()
}

catch (error) {
switch (error.code) {
case 'ENOENT': // File does not exist.
console.error(`${error.path} does not exist. Can not bump WordPress theme.`)
break
default:
throw error
break
}
}
}

/**
* Write WordPress theme version.
*
* @since unreleased
*/
async write () {
try {
console.info(`Bumping WordPress theme to ${this.version}.`)
writeFile(this.filePath, this.text, 'utf8')
}

catch (error) {
console.error(error)
}
}
}

0 comments on commit b55052f

Please sign in to comment.