From 94a7330724db17d4067c55b7d79712e48249f223 Mon Sep 17 00:00:00 2001 From: Paul Shryock Date: Tue, 20 Jul 2021 20:13:10 -0400 Subject: [PATCH] Rewrite classes (#19) --- .gitignore | 5 +- CHANGELOG.md | 5 +- README.md | 69 +++----- cli.js | 7 + package-lock.json | 422 +++++++++++++++++++++++++++++++++++++++++++++- package.json | 12 +- src/Git.js | 51 ++++++ src/WordPress.js | 87 ++++++++++ src/bump.js | 152 ++++++----------- src/changelog.js | 230 +++++++------------------ src/cli.js | 10 -- src/defaults.js | 18 -- src/utils/file.js | 57 +++++++ src/wordpress.js | 87 ---------- 14 files changed, 771 insertions(+), 441 deletions(-) create mode 100755 cli.js create mode 100644 src/Git.js create mode 100644 src/WordPress.js delete mode 100755 src/cli.js delete mode 100644 src/defaults.js create mode 100644 src/utils/file.js delete mode 100644 src/wordpress.js diff --git a/.gitignore b/.gitignore index d5a06f7a..0cbb00bf 100644 --- a/.gitignore +++ b/.gitignore @@ -113,4 +113,7 @@ dist .yarn/unplugged .yarn/build-state.yml .yarn/install-state.gz -.pnp.* \ No newline at end of file +.pnp.* + +# temp files +legacy diff --git a/CHANGELOG.md b/CHANGELOG.md index b6130bcf..4adce842 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,12 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/paulshryock/release-bump/compare/HEAD..1.3.0) ### Added +- Bump WordPress plugin. ### Changed +- Rewrite classes. ### Deprecated ### Removed +- Remove configuration. ### Fixed @@ -21,7 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [1.3.0](https://github.com/paulshryock/release-bump/releases/tag/v1.3.0) - 5/20/2021 ### Added -- Bump WordPress theme version. +- Bump WordPress theme. ## [1.2.1](https://github.com/paulshryock/release-bump/releases/tag/v1.2.1) - 4/5/2021 diff --git a/README.md b/README.md index 1143906e..ba946843 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,23 @@ `release-bump` handles version bump tasks for a code release. +## Announcements + +Version `2.0.0` includes breaking changes: +- CLI configuration has been removed +- JavaScript API configuration has been removed +- [JavaScript class instantiation](#javascript-api) should be followed by a call to `init()` inside an async function. + ## Features +- [Bump Changelog](#bump-changelog) +- [Bump WordPress theme or plugin](#bump-wordpress-theme-or-plugin) + [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. +If your project has a Changelog, add lines beneath the Unreleased header as you make changes. If there is no Changelog, one is automatically created. `release-bump` will change this: @@ -58,15 +68,15 @@ to this: - Fix another bug. ``` -### Bump WordPress theme +### Bump WordPress theme or plugin -If your project has a file named `style.css` in the root directory, it will bump the version, if there is one. +If your project has a file named `style.css` or a PHP file of the folder name in the root directory, it will bump the version. `release-bump` will change this: ```css /* -Theme Name: ... +... Version: 0.0.1 ... */ @@ -76,7 +86,7 @@ to this: ```css /* -Theme Name: ... +... Version: 1.0.0 ... */ @@ -106,54 +116,21 @@ Now whenever you run `npm version `, all of the `release-bump #### Configuration -| Option | Type | Default | Description | -| :--- | :--- | :--- | :--- | -| `-h` | boolean | `false` | Log help information. | -| `-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. | -| `-v` | boolean | `false` | Log package version. | -| `-w` | boolean | `false` | Whether to skip WordPress theme bump. | +In version `2.0.0`, CLI configuration has been removed. This will be added back in a future version. ### JavaScript API -If you prefer to run `release-bump` programattically, just require and instantiate a `release-bump` class: +If you prefer to run `release-bump` programmatically, just require and instantiate a `release-bump` class inside an async function; then call `init()`: ```javascript -const Bump = require('release-bump') -new Bump() -``` +import Bump from 'release-bump' -Pass configuration options if you want to override the defaults: - -```javascript -const Bump = require('release-bump') -new Bump({ - changelog: { - filePath: './CHANGELOG.md', - gitRemote: 'github', - initialText: '', - initialTextUrl: 'https://keepachangelog.com/en/1.0.0/', - skipV: false, - }, - help: false, - skipWordPress: false, - version: false, -}) +;(async function () { + const bump = new Bump() + await bump.init() +})(); ``` #### Configuration -| 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. | -| `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. | +In version `2.0.0`, JavaScript API configuration has been removed. This will be added back in a future version. diff --git a/cli.js b/cli.js new file mode 100755 index 00000000..46773f0c --- /dev/null +++ b/cli.js @@ -0,0 +1,7 @@ +#!/usr/bin/env node +import Bump from './src/Bump.js' + +;(async function () { + const bump = new Bump() + await bump.init() +})(); diff --git a/package-lock.json b/package-lock.json index 652a71b4..9c7581b1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,22 +5,69 @@ "requires": true, "packages": { "": { - "version": "1.1.0", + "version": "1.3.0", "license": "Hippocratic 2.1", "dependencies": { "arg": "^5.0.0", "axios": "^0.21.1", - "deepmerge": "^4.2.2" + "deepmerge": "^4.2.2", + "zx": "^2.0.0" }, "bin": { "release-bump": "src/cli.js" } }, + "node_modules/@types/fs-extra": { + "version": "9.0.12", + "resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-9.0.12.tgz", + "integrity": "sha512-I+bsBr67CurCGnSenZZ7v94gd3tc3+Aj2taxMT4yu4ABLuOgOjeFxX3dokG24ztSRg5tnT00sL8BszO7gSMoIw==", + "dependencies": { + "@types/node": "*" + } + }, + "node_modules/@types/minimist": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.2.tgz", + "integrity": "sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==" + }, + "node_modules/@types/node": { + "version": "16.3.3", + "resolved": "https://registry.npmjs.org/@types/node/-/node-16.3.3.tgz", + "integrity": "sha512-8h7k1YgQKxKXWckzFCMfsIwn0Y61UK6tlD6y2lOb3hTOIMlK3t9/QwHOhc81TwU+RMf0As5fj7NPjroERCnejQ==" + }, + "node_modules/@types/node-fetch": { + "version": "2.5.11", + "resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.5.11.tgz", + "integrity": "sha512-2upCKaqVZETDRb8A2VTaRymqFBEgH8u6yr96b/u3+1uQEPDRo3mJLEiPk7vdXBHRtjwkjqzFYMJXrt0Z9QsYjQ==", + "dependencies": { + "@types/node": "*", + "form-data": "^3.0.0" + } + }, + "node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, "node_modules/arg": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/arg/-/arg-5.0.0.tgz", "integrity": "sha512-4P8Zm2H+BRS+c/xX1LrHw0qKpEhdlZjLCgWy+d78T9vqa2Z2SiD2wMrYuWIAFy5IZUD7nnNXroRttz+0RzlrzQ==" }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" + }, "node_modules/axios": { "version": "0.21.1", "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz", @@ -29,6 +76,48 @@ "follow-redirects": "^1.10.0" } }, + "node_modules/chalk": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.1.tgz", + "integrity": "sha512-diHzdDKxcU+bAsUboHLPEDQiw0qEe0qd7SYUn3HgcFlWgbDcfLGswOHYeGrHKzG9z6UYf01d9VFMfZxPM1xZSg==", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dependencies": { + "color-name": "~1.1.4" + }, + "engines": { + "node": ">=7.0.0" + } + }, + "node_modules/color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" + }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/deepmerge": { "version": "4.2.2", "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.2.2.tgz", @@ -37,6 +126,14 @@ "node": ">=0.10.0" } }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/follow-redirects": { "version": "1.13.3", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.3.tgz", @@ -55,14 +152,196 @@ "optional": true } } + }, + "node_modules/form-data": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz", + "integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/fs-extra": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.0.0.tgz", + "integrity": "sha512-C5owb14u9eJwizKGdchcDUQeFtlSHHthBk8pbX9Vc1PFZrLombudjDnNns88aYslCyF6IY5SUw3Roz6xShcEIQ==", + "dependencies": { + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/graceful-fs": { + "version": "4.2.6", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.6.tgz", + "integrity": "sha512-nTnJ528pbqxYanhpDYsi4Rd8MAeaBA67+RZ10CM1m3bTAVFEDcd5AuA4a6W5YkGZ1iNXHzZz8T6TBKLeBuNriQ==" + }, + "node_modules/has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "engines": { + "node": ">=8" + } + }, + "node_modules/isexe": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", + "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=" + }, + "node_modules/jsonfile": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", + "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", + "dependencies": { + "universalify": "^2.0.0" + }, + "optionalDependencies": { + "graceful-fs": "^4.1.6" + } + }, + "node_modules/mime-db": { + "version": "1.48.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.48.0.tgz", + "integrity": "sha512-FM3QwxV+TnZYQ2aRqhlKBMHxk10lTbMt3bBkMAp54ddrNeVSfcQYOOKuGuy3Ddrm38I04If834fOUSq1yzslJQ==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.31", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.31.tgz", + "integrity": "sha512-XGZnNzm3QvgKxa8dpzyhFTHmpP3l5YNusmne07VUOXxou9CqUqYa/HBy124RqtVh/O2pECas/MOcsDgpilPOPg==", + "dependencies": { + "mime-db": "1.48.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/minimist": { + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", + "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" + }, + "node_modules/node-fetch": { + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", + "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==", + "engines": { + "node": "4.x || >=6.0.0" + } + }, + "node_modules/supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/universalify": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", + "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", + "engines": { + "node": ">= 10.0.0" + } + }, + "node_modules/which": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", + "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", + "dependencies": { + "isexe": "^2.0.0" + }, + "bin": { + "node-which": "bin/node-which" + }, + "engines": { + "node": ">= 8" + } + }, + "node_modules/zx": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/zx/-/zx-2.0.0.tgz", + "integrity": "sha512-OF8YvqseMMmtDaASqO+8+0/tJZvykLK0hX9YBAaRO9l7Hc+YjNKjpgJTjrmncgEURoyDr9Ln4r/qBtEuDNZstg==", + "dependencies": { + "@types/fs-extra": "^9.0.11", + "@types/minimist": "^1.2.1", + "@types/node": "^16.0", + "@types/node-fetch": "^2.5.10", + "chalk": "^4.1.1", + "fs-extra": "^10.0.0", + "minimist": "^1.2.5", + "node-fetch": "^2.6.1", + "which": "^2.0.2" + }, + "bin": { + "zx": "zx.mjs" + }, + "engines": { + "node": ">= 14.8.0" + } } }, "dependencies": { + "@types/fs-extra": { + "version": "9.0.12", + "resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-9.0.12.tgz", + "integrity": "sha512-I+bsBr67CurCGnSenZZ7v94gd3tc3+Aj2taxMT4yu4ABLuOgOjeFxX3dokG24ztSRg5tnT00sL8BszO7gSMoIw==", + "requires": { + "@types/node": "*" + } + }, + "@types/minimist": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.2.tgz", + "integrity": "sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==" + }, + "@types/node": { + "version": "16.3.3", + "resolved": "https://registry.npmjs.org/@types/node/-/node-16.3.3.tgz", + "integrity": "sha512-8h7k1YgQKxKXWckzFCMfsIwn0Y61UK6tlD6y2lOb3hTOIMlK3t9/QwHOhc81TwU+RMf0As5fj7NPjroERCnejQ==" + }, + "@types/node-fetch": { + "version": "2.5.11", + "resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.5.11.tgz", + "integrity": "sha512-2upCKaqVZETDRb8A2VTaRymqFBEgH8u6yr96b/u3+1uQEPDRo3mJLEiPk7vdXBHRtjwkjqzFYMJXrt0Z9QsYjQ==", + "requires": { + "@types/node": "*", + "form-data": "^3.0.0" + } + }, + "ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "requires": { + "color-convert": "^2.0.1" + } + }, "arg": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/arg/-/arg-5.0.0.tgz", "integrity": "sha512-4P8Zm2H+BRS+c/xX1LrHw0qKpEhdlZjLCgWy+d78T9vqa2Z2SiD2wMrYuWIAFy5IZUD7nnNXroRttz+0RzlrzQ==" }, + "asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" + }, "axios": { "version": "0.21.1", "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz", @@ -71,15 +350,154 @@ "follow-redirects": "^1.10.0" } }, + "chalk": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.1.tgz", + "integrity": "sha512-diHzdDKxcU+bAsUboHLPEDQiw0qEe0qd7SYUn3HgcFlWgbDcfLGswOHYeGrHKzG9z6UYf01d9VFMfZxPM1xZSg==", + "requires": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + } + }, + "color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "requires": { + "color-name": "~1.1.4" + } + }, + "color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" + }, + "combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "requires": { + "delayed-stream": "~1.0.0" + } + }, "deepmerge": { "version": "4.2.2", "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.2.2.tgz", "integrity": "sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg==" }, + "delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" + }, "follow-redirects": { "version": "1.13.3", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.3.tgz", "integrity": "sha512-DUgl6+HDzB0iEptNQEXLx/KhTmDb8tZUHSeLqpnjpknR70H0nC2t9N73BK6fN4hOvJ84pKlIQVQ4k5FFlBedKA==" + }, + "form-data": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz", + "integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } + }, + "fs-extra": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.0.0.tgz", + "integrity": "sha512-C5owb14u9eJwizKGdchcDUQeFtlSHHthBk8pbX9Vc1PFZrLombudjDnNns88aYslCyF6IY5SUw3Roz6xShcEIQ==", + "requires": { + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + } + }, + "graceful-fs": { + "version": "4.2.6", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.6.tgz", + "integrity": "sha512-nTnJ528pbqxYanhpDYsi4Rd8MAeaBA67+RZ10CM1m3bTAVFEDcd5AuA4a6W5YkGZ1iNXHzZz8T6TBKLeBuNriQ==" + }, + "has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==" + }, + "isexe": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", + "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=" + }, + "jsonfile": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", + "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", + "requires": { + "graceful-fs": "^4.1.6", + "universalify": "^2.0.0" + } + }, + "mime-db": { + "version": "1.48.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.48.0.tgz", + "integrity": "sha512-FM3QwxV+TnZYQ2aRqhlKBMHxk10lTbMt3bBkMAp54ddrNeVSfcQYOOKuGuy3Ddrm38I04If834fOUSq1yzslJQ==" + }, + "mime-types": { + "version": "2.1.31", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.31.tgz", + "integrity": "sha512-XGZnNzm3QvgKxa8dpzyhFTHmpP3l5YNusmne07VUOXxou9CqUqYa/HBy124RqtVh/O2pECas/MOcsDgpilPOPg==", + "requires": { + "mime-db": "1.48.0" + } + }, + "minimist": { + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", + "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" + }, + "node-fetch": { + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", + "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==" + }, + "supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "requires": { + "has-flag": "^4.0.0" + } + }, + "universalify": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", + "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==" + }, + "which": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", + "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", + "requires": { + "isexe": "^2.0.0" + } + }, + "zx": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/zx/-/zx-2.0.0.tgz", + "integrity": "sha512-OF8YvqseMMmtDaASqO+8+0/tJZvykLK0hX9YBAaRO9l7Hc+YjNKjpgJTjrmncgEURoyDr9Ln4r/qBtEuDNZstg==", + "requires": { + "@types/fs-extra": "^9.0.11", + "@types/minimist": "^1.2.1", + "@types/node": "^16.0", + "@types/node-fetch": "^2.5.10", + "chalk": "^4.1.1", + "fs-extra": "^10.0.0", + "minimist": "^1.2.5", + "node-fetch": "^2.6.1", + "which": "^2.0.2" + } } } } diff --git a/package.json b/package.json index 2f147e87..d65b66c4 100644 --- a/package.json +++ b/package.json @@ -16,20 +16,22 @@ "type": "git", "url": "git+https://github.com/paulshryock/release-bump.git" }, + "type": "module", "license": "Hippocratic 2.1", "author": "Paul Shryock (https://github.com/paulshryock)", - "main": "src/bump.js", + "main": "src/Bump.js", "bin": { - "release-bump": "src/cli.js" + "release-bump": "cli.js" }, "scripts": { - "test": "node src/cli.js", - "version": "node src/cli.js && git add .", + "test": "node --trace-warnings cli.js", + "version": "node 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" + "deepmerge": "^4.2.2", + "zx": "^2.0.0" } } diff --git a/src/Git.js b/src/Git.js new file mode 100644 index 00000000..3b516e74 --- /dev/null +++ b/src/Git.js @@ -0,0 +1,51 @@ +import { $, chalk, fs } from 'zx' + +$.verbose = false + +export default class Git { + constructor (options = {}) { + const { version } = options + if (!version) { + console.log(chalk.yellow('Could not read git info.')) + $`exit 1` + } + this.version = version + } + /** + * Initialize. + */ + async init () { + try { + const pkg = await fs.readFile('./package.json') + const { repository} = JSON.parse(pkg.toString()) + this.branch = (await $`git branch --show-current`).toString() + .replace('\n', '') + this.repository = repository?.url + if (!repository?.url) { + this.repository = (await $`git remote get-url --push origin`).toString() + .replace('\n', '') + } + this.repository = 'https://' + + this.repository + .replace(/^git[+@]?/, '') + .replace(/^(https?|ssh)?:\/\//, '') + .replace(/\.git(#.*$)?/, '') + .replace(':', '/') + this.remote = this.repository + .replace(/(https?:\/\/|www\.)/g, '') + .split('/')[0] + .split('.')[0] + this.commit = (await $`git log -n 1 --pretty=format:"%H"`).toString() + this.tags = { + all: (await $`git tag`).toString().split('\n') + .filter(tag => tag !== ''), + } + this.tags.current = this.tags.all + .filter(tag => tag.includes(this.version)) + } catch (p) { + console.log(chalk.yellow('Could not read git info')) + console.error(p.stderr) + await $`exit 1` + } + } +} diff --git a/src/WordPress.js b/src/WordPress.js new file mode 100644 index 00000000..93a48d0c --- /dev/null +++ b/src/WordPress.js @@ -0,0 +1,87 @@ +import { $, chalk, fs } from 'zx' +import { writeFile } from 'fs/promises' +import { parse } from 'path' +import { get } from './utils/file.js' + +$.verbose = false + +export default class WordPress { + constructor (options = {}) { + try { + const { version } = options + if (!version) { + throw new Error('Could not bump WordPress.') + } + + this.version = version + } catch (error) { + console.error(chalk.red(error)) + $`exit 1` + } + } + + async init () { + try { + this.theme = { + path: 'style.css', + text: await get({ type: 'theme', path: 'style.css' }), + } + const plugin = parse(process.cwd()).name + '.php' + this.plugin = { + path: plugin, + text: await get({ type: 'plugin', path: plugin }), + } + + if (!this.theme?.text && !this.plugin?.text) { + console.log(chalk.yellow('No WordPress theme or plugin to bump.')) + this.theme = null + this.plugin = null + return + } + + const regex = /Version:(\s+)\d*\.?\d*\.?\d*/ + + if (!this.theme?.text) { + console.log(chalk.yellow('No WordPress theme to bump.')) + this.theme = null + } else { + // Bump theme. + if (!this.theme?.text?.match(regex)) { + console.log( + chalk.red('Can not bump WordPress theme. Missing Version header.') + ) + return + } + + // Bump WordPress theme version. + this.theme.new = this.theme.text + .replace(regex, `Version:$1${this.version}`) + writeFile(this.theme.path, this.theme.new, 'utf8') + console.log(chalk.green('Bumped WordPress theme.')) + } + + // Bump WordPress plugin version. + if (!this.plugin?.text) { + console.log(chalk.yellow('No WordPress plugin to bump.')) + this.plugin = null + } else { + // Bump plugin. + if (!this.plugin?.text?.match(regex)) { + console.log( + chalk.red('Can not bump WordPress plugin. Missing Version header.') + ) + return + } + + // Bump WordPress plugin version. + this.plugin.new = this.plugin.text + .replace(regex, `Version:$1${this.version}`) + writeFile(this.plugin.path, this.plugin.new, 'utf8') + console.log(chalk.green('Bumped WordPress plugin.')) + } + } catch (error) { + console.error(chalk.red(error)) + $`exit 1` + } + } +} diff --git a/src/bump.js b/src/bump.js index ba9970c9..fc34782c 100644 --- a/src/bump.js +++ b/src/bump.js @@ -1,117 +1,59 @@ -const Changelog = require('./changelog.js') -const WordPress = require('./wordpress.js') -const defaults = require('./defaults.js') -const pkg = require('../package.json') -const fs = require('fs') -const arg = require('arg') -const deepmerge = require('deepmerge') -// @todo: Use a deepmerge utility instead of a dependency. -// @see: https://thewebdev.info/2021/03/06/how-to-deep-merge-javascript-objects/ +import { $, chalk } from 'zx' +import Git from './Git.js' +import Changelog from './Changelog.js' +import WordPress from './WordPress.js' -/** - * Bump class. - * - * @since 1.0.0 - * @type {Class} - */ -module.exports = class Bump { - /** - * Bump class constructor. - * - * @param {Object} config Configuration options. - * @since 1.0.0 - */ - constructor(config = {}) { - // Define CLI args. - this.args = arg({ - '--help': Boolean, - '-h': '--help', - - '--changelog-file-path': String, - '-p': '--changelog-file-path', - - '--git-remote': String, - '-r': '--git-remote', - - '--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', - - '--version': Boolean, - '-v': '--version', - - '--skip-wordpress': Boolean, - '-w': '--skip-wordpress', - }) - - // Get CLI arg values. - this.argv = { changelog: {} } - if (this.args['--help']) this.argv.help = this.args['--help'] - if (this.args['--changelog-file-path']) this.argv.changelog.filePath = this.args['--changelog-file-path'] - if (this.args['--git-remote']) this.argv.changelog.gitRemote = this.args['--git-remote'] - 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'] +$.verbose = false - // 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) - - // Log help information. - if (this.options.help) return this.help() - - // Log package version. - if (this.options.version) return this.logPackageVersion() - - // @todo: Get version once and pass it to Changelog and WordPress. - - // Handle Changelog bump. - new Changelog(this.options.changelog) - - // Handle WordPress bump. - new WordPress({ skipWordPress: this.options.skipWordPress }) +export default class Bump { + constructor () { + this.prefix = 'v' } /** - * Log help information. - * - * @since unreleased + * Initialize. */ - help () { - 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(this.readme) + async init () { + try { + const pkg = await fs.readFile('./package.json') + const { version } = JSON.parse(pkg.toString()) + + if (!version) throw new Error('Missing package.json version') + + const [month, date, year] = new Date() + .toLocaleDateString('en-US') + .split('/') + const formattedMonth = month < 10 ? `0${month}` : month + const formattedDate = date < 10 ? `0${date}` : date + this.today = `${year}/${formattedMonth}/${formattedDate}` + + // Get git info. + this.git = new Git({ version }) + await this.git.init() + + // Bump Changelog. + this.changelog = new Changelog({ + prefix: this.prefix, + remote: this.git.remote, + repository: this.git.repository, + today: this.today, + version: this.git.version, + }) + await this.changelog.init() + + // Bump WordPress. + this.wordpress = new WordPress({ version }) + await this.wordpress.init() + } catch (p) { + console.error(p.stderr) + $`exit 1` + } } /** - * Log package version. - * - * @since unreleased + * Log the class instance to the console. */ - logPackageVersion () { - if (pkg.version) { - console.info(pkg.version) - } else { - console.warn('No version found.') - } + debug () { + console.log(this) } } diff --git a/src/changelog.js b/src/changelog.js index 49fd2215..487ec388 100644 --- a/src/changelog.js +++ b/src/changelog.js @@ -1,178 +1,76 @@ -const axios = require('axios') -const fs = require('fs') +import { $, chalk, fs } from 'zx' +import { writeFile } from 'fs/promises' +import { get } from './utils/file.js' -/** - * Changelog class. - * - * @since 1.0.0 - * @type {Class} - */ -module.exports = class Changelog { - /** - * Changelog class constructor. - * - * @param {string} options.filePath The Changelog file path. - * @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 {boolean} options.skipV Whether to skip v in the version. - * @since 1.0.0 - */ - constructor({ filePath, gitRemote, initialText, initialTextUrl, skipV }) { - const { version, repository } = JSON.parse(fs.readFileSync('./package.json', 'utf8')) - if (!version) { - console.error('Missing package.json version. Can not bump.') - return - } - if (!repository || !repository.url) { - console.error('Missing package.json repository url. Can not bump.') - return - } - const [month, date, year] = new Date().toLocaleDateString('en-US').split('/') - this.version = version - this.repository = repository.url - .replace(/^git\+?/, '') - .replace(/^(ssh)?:\/\//, 'https://') - .replace(/\.git(#.*$)?/, '') - this.today = `${month}/${date}/${year}` - this.filePath = filePath - this.gitRemote = gitRemote.toLowerCase() - this.initialText = initialText - this.initialTextUrl = initialTextUrl - this.skipV = skipV - this.unreleased = - '## [Unreleased]' + - `(${this.repository}/${this.gitRemote === 'bitbucket' ? 'branches/' : ''}compare/HEAD..${this.version})` + - '\n\n### Added' + - '\n\n### Changed' + - '\n\n### Deprecated' + - '\n\n### Removed' + - '\n\n### Fixed' + - '\n\n### Security' - - // Handle asyncronous constructor tasks. - this.init() - } - - /** - * Handle asyncronous constructor tasks. - * - * @since 1.0.0 - */ - async init () { - try { - // Set the text. - await this.setText() - - // Bump the Changelog. - this.bump() - } - - catch (error) { - console.error(error) - } - } +$.verbose = false - /** - * Set the initialText. If there is none passed, get it from initialTextUrl. - * - * @since 1.0.0 - */ - async setInitialText () { - console.info(`Getting Changelog initial text from ${this.initialTextUrl}.`) +export default class Changelog { + constructor (options = {}) { try { - const { data } = await axios.get(this.initialTextUrl) - this.initialText = data - // Get Changelog intro text. - .match(/# Changelog.*## \[Unreleased\]/s)[0] - // Fix newlines. - .replace(/ /g, '\n') - // Add unreleased lines. - .replace(/## \[Unreleased\](\(.*\))?/, this.unreleased) - // Remove link and add a newline. - .replace(/## \[Unreleased\](\(.*\))?/, '## [Unreleased]') + '\n' - } + const { prefix, remote, repository, today, version } = options + if (!prefix || !remote || !repository || !today || !version) { + throw new Error('Could not bump changelog.') + } - catch (error) { - console.warn(`Bad response from ${this.initialTextUrl}.`) - this.initialText = this.unreleased - ? this.unreleased - // Remove link and add a newline. - .replace(/## \[Unreleased\](\(.*\))?/, '## [Unreleased]') + '\n' - : '' + this.prefix = prefix + this.remote = remote + this.repository = repository + this.today = today + this.version = version + this.path = 'CHANGELOG.md' + this.header = `## [${this.version}](${this.repository}/` + + `${this.remote === 'bitbucket' ? 'commits/tag' : 'releases/tag'}/` + + `${this.prefix}${this.version}) - ${this.today}` + this.unreleased = + `## [Unreleased](${this.repository}/` + + `${this.remote === 'bitbucket' ? 'branches/' : ''}` + + `compare/HEAD..${this.prefix}${this.version})` + + '\n\n### Added' + + '\n\n### Changed' + + '\n\n### Deprecated' + + '\n\n### Removed' + + '\n\n### Fixed' + + '\n\n### Security' + } catch (error) { + console.error(chalk.red(error)) + $`exit 1` } } - /** - * Set text. - * - * @since 1.0.0 - */ - async setText () { + async init () { try { - this.text = fs.readFileSync(this.filePath, 'utf8') - } - - catch (error) { - switch (error.code) { - case 'ENOENT': // File does not exist. - console.error(`${error.path} does not exist.`) - if (!this.initialText) await this.setInitialText() - this.write(this.initialText) - throw 'Cancelling Changelog bump.' - break - default: - throw error - break + const file = await get({ path: this.path }) + if (!file) { + console.log(chalk.yellow('No Changelog to bump.')) + return } - } - } - - /** - * Write Changelog. - * - * @param {string} content The Changelog content. - * @since 1.0.0 - */ - write (content) { - fs.writeFile(this.filePath, content, 'utf8', (err) => { - if (err) throw err - console.info('Writing Changelog to:', this.filePath) - }) - this.text = content - } - /** - * Bump Changelog. - * - * @since 1.0.0 - */ - bump () { - this.header = `## [${this.version}](${this.repository}/` + - `${this.gitRemote === 'bitbucket' ? 'commits/tag' : 'releases/tag'}/` + - `${this.skipV ? '' : 'v'}${this.version}) - ${this.today}` - - console.info('Bumping Changelog to:', this.version) - this.write(this.text - // Bump unreleased version and add today's date. - .replace(/## \[Unreleased\](\(.*\))?/, this.header) - // Remove empty changelog subheads. - .replace( - /### (Added|Changed|Deprecated|Removed|Fixed|Security)\n\n/g, - '' - ) - // Remove last empty changelog subhead. - .replace( - /### (Added|Changed|Deprecated|Removed|Fixed|Security)\n$/g, - '' - ) - // Remove any duplicate trailing newline. - .replace(/\n\n$/g, '\n') - // Add unreleased section. - .replace( - this.header, - this.unreleased + '\n\n' + this.header - ) - ) + this.text = file + this.new = this.text + // Bump unreleased version and add today's date. + .replace(/## \[Unreleased\](\(.*\))?/, this.header) + // Remove empty changelog subheads. + .replace( + /### (Added|Changed|Deprecated|Removed|Fixed|Security)\n\n/g, + '' + ) + // Remove last empty changelog subhead. + .replace( + /### (Added|Changed|Deprecated|Removed|Fixed|Security)\n$/g, + '' + ) + // Remove any duplicate trailing newline. + .replace(/\n\n$/g, '\n') + // Add unreleased section. + .replace( + this.header, + this.unreleased + '\n\n' + this.header + ) + writeFile(this.path, this.new, 'utf8') + console.log(chalk.green('Bumped Changelog.')) + } catch (error) { + console.error(chalk.red(error)) + $`exit 1` + } } } diff --git a/src/cli.js b/src/cli.js deleted file mode 100755 index d940d961..00000000 --- a/src/cli.js +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env node -/** - * Instantiate Bump class. - * - * @since 1.1.0 - */ -(function () { - const Bump = require('./bump.js') - new Bump() -})() diff --git a/src/defaults.js b/src/defaults.js deleted file mode 100644 index 9f455bac..00000000 --- a/src/defaults.js +++ /dev/null @@ -1,18 +0,0 @@ -/** - * Configuration defaults. - * - * @since 1.1.0 - * @type {Object} - */ -module.exports = { - changelog: { - filePath: './CHANGELOG.md', - gitRemote: 'github', - initialText: '', - initialTextUrl: 'https://keepachangelog.com/en/1.0.0/', - skipV: false, - }, - help: false, - skipWordPress: false, - version: false, -} diff --git a/src/utils/file.js b/src/utils/file.js new file mode 100644 index 00000000..830e2aa5 --- /dev/null +++ b/src/utils/file.js @@ -0,0 +1,57 @@ +import { constants, readFileSync } from 'fs' +import { access } from 'fs/promises' + +/** + * Check if the file exists in the current directory, and if it is readable. + * + * @param {string} file The file path. + * @return {boolean} Whether or not the file exists and is readable. + * @since unreleased + */ +async function exists (file) { + if (!file) { + throw new Error('fileExists() missing file.') + return + } + + let exists + try { + await access(file, constants.F_OK | constants.R_OK) + exists = true + } catch (error) { + exists = false + } + + return exists +} + +/** + * Get file. + * + * @param {Object} options File options. + * @param {string} options.path File path. + * @param {string} options.type File type. + * @since unreleased + */ +export async function get (options = {}) { + const { path } = options + if (!path) { + throw new Error('getFile() missing path.') + return + } + + let file = null + let fileExists = false + try { + // If the file doesn't exist, bail. + fileExists = await exists(path) + if (!fileExists) return + + // Get the file text. + file = readFileSync(path, 'utf8') + } catch (error) { + return file + } + + return file +} diff --git a/src/wordpress.js b/src/wordpress.js deleted file mode 100644 index d6aae338..00000000 --- a/src/wordpress.js +++ /dev/null @@ -1,87 +0,0 @@ -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) - } - } -}