|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Versions a WordPress plugin. |
| 5 | + * |
| 6 | + * Ported over from FaustJS |
| 7 | + */ |
| 8 | +import fs from 'fs/promises'; |
| 9 | +import path from 'path'; |
| 10 | + |
| 11 | +const readFile = (filename) => fs.readFile(filename, { encoding: 'utf8' }); |
| 12 | +const writeFile = fs.writeFile; |
| 13 | + |
| 14 | +/** |
| 15 | + * Runs all WordPress plugin versioning operations for this plugin |
| 16 | + * including version bumps and readme.txt changelog updates. |
| 17 | + */ |
| 18 | +async function versionPlugin() { |
| 19 | + const pluginPath = path.join(__dirname, '../'); |
| 20 | + const pluginFile = path.join(pluginPath, 'wp-graphql-content-blocks.php'); |
| 21 | + const readmeTxt = path.join(pluginPath, 'readme.txt'); |
| 22 | + const changelog = path.join(pluginPath, 'CHANGELOG.md'); |
| 23 | + |
| 24 | + const version = await getNewVersion(pluginPath); |
| 25 | + |
| 26 | + if (version) { |
| 27 | + bumpPluginHeader(pluginFile, version); |
| 28 | + await bumpStableTag(readmeTxt, version); |
| 29 | + generateReadmeChangelog(readmeTxt, changelog); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Updates the version number found in the header comment of a given |
| 35 | + * WordPress plugin's main PHP file. |
| 36 | + * |
| 37 | + * @param {String} pluginFile Full path to a file containing a WordPress |
| 38 | + * plugin header comment. |
| 39 | + * @param {String} version The new version number. |
| 40 | + */ |
| 41 | +async function bumpPluginHeader(pluginFile, version) { |
| 42 | + return bumpVersion(pluginFile, /^\s*\*\s*Version:\s*([0-9.]+)$/gm, version); |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Updates the stable tag found in a given WordPress plugin's readme.txt file. |
| 47 | + * |
| 48 | + * @param {String} pluginFile Full path to a file containing a WordPress |
| 49 | + * plugin header comment. |
| 50 | + * @param {String} version The new version number. |
| 51 | + */ |
| 52 | +async function bumpStableTag(readmeTxt, version) { |
| 53 | + return bumpVersion(readmeTxt, /^Stable tag:\s*([0-9.]+)$/gm, version); |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Replaces the version number in the first line of a file matching the given |
| 58 | + * regular expression. |
| 59 | + * |
| 60 | + * Note that this function depends on a properly formatted regular expression. |
| 61 | + * The given regex should meet the following criteria: |
| 62 | + * |
| 63 | + * 1. Begins with ^ and ends with $ so that we can match an entire line. |
| 64 | + * 2. Contains one and only one capturing group that matches only the version |
| 65 | + * number portion of the line. For example, in the line " * Version: 1.0.0" |
| 66 | + * capturing group 1 of the regex must resolve to "1.0.0". |
| 67 | + * |
| 68 | + * @param {String} file Full path to the file to update. |
| 69 | + * @param {RegExp} regex A valid regular expression as noted above. |
| 70 | + * @param {String} version The new version number. |
| 71 | + */ |
| 72 | +async function bumpVersion(file, regex, version) { |
| 73 | + try { |
| 74 | + let data = await readFile(file); |
| 75 | + const matches = regex.exec(data); |
| 76 | + |
| 77 | + if (!matches) { |
| 78 | + throw new Error(`Version string does not exist in ${file}`); |
| 79 | + } |
| 80 | + |
| 81 | + // Replace the version number in the captured line. |
| 82 | + let versionString = matches[0].replace(matches[1], version); |
| 83 | + |
| 84 | + // Replace the captured line with the new version string. |
| 85 | + data = data.replace(matches[0], versionString); |
| 86 | + |
| 87 | + return writeFile(file, data); |
| 88 | + } catch (e) { |
| 89 | + console.warn(e); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Get the current version number from a plugin's package.json file. |
| 95 | + * |
| 96 | + * @param {String} pluginPath Full path to the directory containing the plugin's |
| 97 | + * package.json file. |
| 98 | + * @returns The version number string found in the plugin's package.json. |
| 99 | + */ |
| 100 | +async function getNewVersion(pluginPath) { |
| 101 | + const packageJsonFile = path.join(pluginPath, 'package.json'); |
| 102 | + |
| 103 | + try { |
| 104 | + let packageJson = await readFile(packageJsonFile); |
| 105 | + |
| 106 | + return JSON.parse(packageJson)?.version; |
| 107 | + } catch (e) { |
| 108 | + if (e instanceof SyntaxError) { |
| 109 | + e.message = `${e.message} in ${packageJsonFile}.\n`; |
| 110 | + } |
| 111 | + |
| 112 | + console.warn(e); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Updates the plugin's readme.txt changelog with the latest 3 releases |
| 118 | + * found in the plugin's CHANGELOG.md file. |
| 119 | + * |
| 120 | + * @param {String} readmeTxtFile Full path to the plugin's readme.txt file. |
| 121 | + * @param {String} changelog Full path to the plugin's CHANGELOG.md file. |
| 122 | + */ |
| 123 | +async function generateReadmeChangelog(readmeTxtFile, changelog) { |
| 124 | + let output = ""; |
| 125 | + |
| 126 | + try { |
| 127 | + let readmeTxt = await readFile(readmeTxtFile); |
| 128 | + changelog = await readFile(changelog); |
| 129 | + |
| 130 | + changelog = changelog.replace( |
| 131 | + '# WP GraphQL Content Blocks', |
| 132 | + '== Changelog ==' |
| 133 | + ); |
| 134 | + |
| 135 | + // split the contents by new line |
| 136 | + const changelogLines = changelog.split(/\r?\n/); |
| 137 | + const processedLines = []; |
| 138 | + let versionCount = 0; |
| 139 | + |
| 140 | + // print all lines in current version |
| 141 | + changelogLines.every((line) => { |
| 142 | + // Version numbers in CHANGELOG.md are h2 |
| 143 | + if (line.startsWith('## ')) { |
| 144 | + if (versionCount == 3) { |
| 145 | + return false; |
| 146 | + } |
| 147 | + // Format version number for WordPress |
| 148 | + line = line.replace('## ', '= ') + ' ='; |
| 149 | + versionCount++; |
| 150 | + } |
| 151 | + |
| 152 | + processedLines.push(line); |
| 153 | + |
| 154 | + return true; |
| 155 | + }); |
| 156 | + |
| 157 | + changelog = processedLines.join("\n"); |
| 158 | + |
| 159 | + const changelogStart = readmeTxt.indexOf('== Changelog =='); |
| 160 | + |
| 161 | + output = readmeTxt.substring(0, changelogStart) + changelog; |
| 162 | + output += "\n[View the full changelog](https://github.com/wpengine/wp-graphql-content-blocks/blob/main/CHANGELOG.md)"; |
| 163 | + |
| 164 | + return writeFile(readmeTxtFile, output); |
| 165 | + } catch (e) { |
| 166 | + console.warn(e); |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +versionPlugin(); |
0 commit comments