Skip to content

Commit b55052f

Browse files
authored
[#2]: Bump WordPress theme version (#17)
1 parent 2d551ce commit b55052f

File tree

7 files changed

+140
-8
lines changed

7 files changed

+140
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased](https://github.com/paulshryock/release-bump/compare/HEAD..1.2.1)
88

99
### Added
10+
- Bump WordPress theme version.
1011

1112
### Changed
1213

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,30 @@ to this:
5858
- Fix another bug.
5959
```
6060

61+
### Bump WordPress theme
62+
63+
If your project has a file named `style.css` in the root directory, it will bump the version, if there is one.
64+
65+
`release-bump` will change this:
66+
67+
```css
68+
/*
69+
Theme Name: ...
70+
Version: 0.0.1
71+
...
72+
*/
73+
```
74+
75+
to this:
76+
77+
```css
78+
/*
79+
Theme Name: ...
80+
Version: 1.0.0
81+
...
82+
*/
83+
```
84+
6185
## Usage
6286

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

95120
### JavaScript API
96121

@@ -114,6 +139,7 @@ new Bump({
114139
skipV: false,
115140
},
116141
help: false,
142+
skipWordPress: false,
117143
version: false,
118144
})
119145
```
@@ -129,4 +155,5 @@ new Bump({
129155
| `changelog.initialTextUrl` | string | `https://keepachangelog.com/en/1.0.0/` | The initial Changelog text URL. |
130156
| `changelog.skipV` | boolean | `false` | Whether to skip `v` in the version. |
131157
| `help` | boolean | `false` | Whether to log help information. |
158+
| `skipWordPress` | boolean | `false` | Whether to skip WordPress theme bump. |
132159
| `version` | boolean | `false` | Whether to log package version. |

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"release-bump": "src/cli.js"
2424
},
2525
"scripts": {
26+
"test": "node src/cli.js",
2627
"version": "node src/cli.js && git add .",
2728
"postversion": "git push && git push --tags && npm publish"
2829
},

src/bump.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const Changelog = require('./changelog.js')
2+
const WordPress = require('./wordpress.js')
23
const defaults = require('./defaults.js')
34
const pkg = require('../package.json')
45
const fs = require('fs')
@@ -44,6 +45,9 @@ module.exports = class Bump {
4445

4546
'--version': Boolean,
4647
'-v': '--version',
48+
49+
'--skip-wordpress': Boolean,
50+
'-w': '--skip-wordpress',
4751
})
4852

4953
// Get CLI arg values.
@@ -54,6 +58,7 @@ module.exports = class Bump {
5458
if (this.args['--skip-v-in-version']) this.argv.changelog.skipV = this.args['--skip-v-in-version']
5559
if (this.args['--initial-changelog-text']) this.argv.changelog.initialText = this.args['--initial-changelog-text']
5660
if (this.args['--initial-changelog-text-url']) this.argv.changelog.initialTextUrl = this.args['--initial-changelog-text-url']
61+
if (this.args['--skip-wordpress']) this.argv.skipWordPress = this.args['--skip-wordpress']
5762
if (this.args['--version']) this.argv.version = this.args['--version']
5863

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

7176
// Log package version.
72-
if (this.options.version) return this.version()
77+
if (this.options.version) return this.logPackageVersion()
78+
79+
// @todo: Get version once and pass it to Changelog and WordPress.
7380

74-
// Setup Changelog.
81+
// Handle Changelog bump.
7582
new Changelog(this.options.changelog)
83+
84+
// Handle WordPress bump.
85+
new WordPress({ skipWordPress: this.options.skipWordPress })
7686
}
7787

7888
/**
@@ -81,22 +91,27 @@ module.exports = class Bump {
8191
* @since unreleased
8292
*/
8393
help () {
84-
const readme = fs.readFileSync('./node_modules/release-bump/README.md', 'utf-8')
94+
const path = './node_modules/release-bump/README.md'
95+
this.readme = fs.readFileSync(path, 'utf-8')
8596
// Get CLI documentation.
8697
.match(/### CLI.*### JavaScript API/s)[0]
8798
// Filter configuration details.
8899
.replace(/### CLI.*#### Configuration\n\n/s, '')
89100
// Remove last line.
90101
.replace(/\n\n### JavaScript API/s, '')
91-
console.info(readme)
102+
console.info(this.readme)
92103
}
93104

94105
/**
95106
* Log package version.
96107
*
97108
* @since unreleased
98109
*/
99-
version () {
100-
console.info(pkg.version)
110+
logPackageVersion () {
111+
if (pkg.version) {
112+
console.info(pkg.version)
113+
} else {
114+
console.warn('No version found.')
115+
}
101116
}
102117
}

src/changelog.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ module.exports = class Changelog {
2121
constructor({ filePath, gitRemote, initialText, initialTextUrl, skipV }) {
2222
const { version, repository } = JSON.parse(fs.readFileSync('./package.json', 'utf8'))
2323
if (!version) {
24-
console.error('version is missing from package.json.')
24+
console.error('Missing package.json version. Can not bump.')
2525
return
2626
}
2727
if (!repository || !repository.url) {
28-
console.error('repository url is missing from package.json.')
28+
console.error('Missing package.json repository url. Can not bump.')
2929
return
3030
}
3131
const [month, date, year] = new Date().toLocaleDateString('en-US').split('/')

src/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ module.exports = {
1313
skipV: false,
1414
},
1515
help: false,
16+
skipWordPress: false,
1617
version: false,
1718
}

src/wordpress.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
const axios = require('axios')
2+
const { readFile, writeFile } = require('fs/promises')
3+
4+
/**
5+
* WordPress class.
6+
*
7+
* @since 1.0.0
8+
* @type {Class}
9+
*/
10+
module.exports = class WordPress {
11+
/**
12+
* WordPress class constructor.
13+
*
14+
* @param {boolean} skipWordPress Whether to skip WordPress theme bump.
15+
* @since unreleased
16+
*/
17+
constructor({ skipWordPress }) {
18+
// Bail early.
19+
if (skipWordPress) return
20+
21+
this.filePath = './style.css'
22+
23+
// Bump WordPress theme version.
24+
this.bump()
25+
}
26+
27+
/**
28+
* Bump WordPress theme version.
29+
*
30+
* @since unreleased
31+
*/
32+
async bump () {
33+
try {
34+
// If there is no text version, bail.
35+
const text = await readFile(this.filePath, 'utf8')
36+
const versionRegex = /Version: \d*\.?\d*\.?\d*/
37+
if (!text.match(versionRegex)) {
38+
console.error(`${this.filePath} does not contain a version. Can not bump WordPress theme.`)
39+
return
40+
}
41+
42+
// If there is no package version, bail.
43+
const pkg = await readFile('./package.json', 'utf8')
44+
const { version } = JSON.parse(pkg)
45+
if (!version) {
46+
console.error(
47+
`Missing package.json version. Can not bump.`
48+
)
49+
return
50+
}
51+
52+
// Bump WordPress theme version.
53+
this.version = version
54+
this.text = text.replace(versionRegex, `Version: ${this.version}`)
55+
56+
// Write WordPress theme version.
57+
this.write()
58+
}
59+
60+
catch (error) {
61+
switch (error.code) {
62+
case 'ENOENT': // File does not exist.
63+
console.error(`${error.path} does not exist. Can not bump WordPress theme.`)
64+
break
65+
default:
66+
throw error
67+
break
68+
}
69+
}
70+
}
71+
72+
/**
73+
* Write WordPress theme version.
74+
*
75+
* @since unreleased
76+
*/
77+
async write () {
78+
try {
79+
console.info(`Bumping WordPress theme to ${this.version}.`)
80+
writeFile(this.filePath, this.text, 'utf8')
81+
}
82+
83+
catch (error) {
84+
console.error(error)
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)