├── .gitignore ├── .github └── workflows │ ├── release.yml │ ├── test.yml │ └── update-prettier.yml ├── LICENSE ├── package.json ├── README.md ├── index.js ├── CODE_OF_CONDUCT.md └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | .tap 2 | coverage 3 | node_modules 4 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | "on": 3 | push: 4 | branches: 5 | - "*.x" 6 | - master 7 | - next 8 | - beta 9 | jobs: 10 | release: 11 | name: release 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v4 15 | - uses: actions/setup-node@v4 16 | with: 17 | node-version: lts/* 18 | cache: npm 19 | - run: npm ci 20 | - run: npx semantic-release 21 | env: 22 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 23 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }} 24 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | "on": 3 | push: 4 | branches: 5 | - main 6 | pull_request: 7 | types: 8 | - opened 9 | - synchronize 10 | jobs: 11 | test_matrix: 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v4 15 | - name: Use Node.js LTS 16 | uses: actions/setup-node@v4 17 | with: 18 | node-version: lts/* 19 | cache: npm 20 | - name: Install 21 | run: npm ci 22 | - name: Test 23 | run: npm run test 24 | test: 25 | runs-on: ubuntu-latest 26 | needs: test_matrix 27 | steps: 28 | - uses: actions/checkout@v4 29 | - run: npm ci 30 | - run: npm run lint 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2019 Gregor Martynus 2 | 3 | Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. 4 | 5 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 6 | -------------------------------------------------------------------------------- /.github/workflows/update-prettier.yml: -------------------------------------------------------------------------------- 1 | name: Update Prettier 2 | "on": 3 | push: 4 | branches: 5 | - renovate/prettier-* 6 | jobs: 7 | update_prettier: 8 | runs-on: ubuntu-latest 9 | steps: 10 | - uses: actions/checkout@v4 11 | - uses: actions/setup-node@v4 12 | with: 13 | node-version: lts/* 14 | - run: npm ci 15 | - run: npm run lint:fix 16 | - uses: gr2m/create-or-update-pull-request-action@v1.x 17 | env: 18 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 19 | with: 20 | title: Prettier updated 21 | body: An update to prettier required updates to your code. 22 | branch: ${{ github.ref }} 23 | commit-message: "style: prettier" 24 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "semantic-release-plugin-update-version-in-files", 3 | "version": "0.0.0-development", 4 | "description": "Replace version placeholders with calculated version in any files before publishing", 5 | "main": "index.js", 6 | "scripts": { 7 | "lint": "prettier --check README.md *.js package.json", 8 | "lint:fix": "prettier --write README.md *.js package.json", 9 | "pretest": "npm run -s lint", 10 | "test": "tap test.js", 11 | "coverage": "tap --coverage-report=html" 12 | }, 13 | "keywords": [ 14 | "semantic-release", 15 | "plugin" 16 | ], 17 | "author": "Gregor Martynus (https://twitter.com/gr2m)", 18 | "license": "ISC", 19 | "dependencies": { 20 | "debug": "^4.1.1", 21 | "glob": "^10.0.0" 22 | }, 23 | "devDependencies": { 24 | "memory-fs": "^0.5.0", 25 | "prettier": "3.5.1", 26 | "tap": "^21.0.0", 27 | "semantic-release": "^25.0.2" 28 | }, 29 | "engines": { 30 | "node": ">=18" 31 | }, 32 | "repository": "github:gr2m/semantic-release-plugin-update-version-in-files" 33 | } 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # semantic-release-plugin-update-version-in-files 2 | 3 | > Replace version placeholders with calculated version in any files before publishing 4 | 5 | [![@latest](https://img.shields.io/npm/v/semantic-release-plugin-update-version-in-files.svg)](https://www.npmjs.com/package/semantic-release-plugin-update-version-in-files) 6 | [![Build Status](https://travis-ci.com/gr2m/semantic-release-plugin-update-version-in-files.svg?branch=master)](https://travis-ci.com/gr2m/semantic-release-plugin-update-version-in-files) 7 | 8 | Example 9 | 10 | ```json 11 | "plugins": [ 12 | "@semantic-release/commit-analyzer", 13 | "@semantic-release/release-notes-generator", 14 | "@semantic-release/github", 15 | "@semantic-release/npm", 16 | ["semantic-release-plugin-update-version-in-files", { 17 | "files": [ 18 | "version.js" 19 | ], 20 | "placeholder": "0.0.0-development" 21 | }] 22 | ] 23 | ``` 24 | 25 | If `"files"` is not set, it defaults to `[ "version.js" ]`. Glob patterns are supported via [glob](https://www.npmjs.com/package/glob). If `placeholder` is not set, it defaults to `"0.0.0-development"`. 26 | 27 | See also: [semantic-release plugins configuration](https://semantic-release.gitbook.io/semantic-release/usage/plugins#plugins-configuration). 28 | 29 | ## License 30 | 31 | [ISC](LICENSE) 32 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const { join } = require("path"); 2 | const debug = require("debug")("semantic-release:update-version-in-files"); 3 | 4 | module.exports = { 5 | prepare( 6 | { 7 | files = ["version.js"], 8 | placeholder = "0.0.0-development", 9 | // Add fs/glob options for testing only 10 | fs = require("fs"), 11 | glob = require("glob"), 12 | }, 13 | { cwd, nextRelease: { version } }, 14 | ) { 15 | debug("config %o", { files, placeholder }); 16 | debug("nextRelease.version %s", version); 17 | 18 | // Turn placeholder string into regex 19 | const searchRegex = new RegExp(placeholder, "g"); 20 | 21 | // Normalize files parameter and prefix with root path 22 | const filesNormalized = Array.isArray(files) ? files : [files]; 23 | 24 | // Turn files into flat array of matche file paths 25 | const existingFilePaths = [] 26 | .concat(...filesNormalized.map((path) => glob.sync(join(cwd, path)))) 27 | .filter((path) => fs.statSync(path).isFile()); 28 | 29 | debug("Existing files found: %o", existingFilePaths); 30 | 31 | if (existingFilePaths.length === 0) { 32 | throw new Error(`No file matches for ${JSON.stringify(files)}`); 33 | } 34 | 35 | existingFilePaths.forEach((path) => { 36 | try { 37 | const content = fs.readFileSync(path, "utf8"); 38 | 39 | if (!searchRegex.test(content)) { 40 | debug("No match found in %s", path); 41 | return; 42 | } 43 | 44 | debug("Match found in %s", path); 45 | fs.writeFileSync(path, content.replace(searchRegex, version)); 46 | /* c8 ignore start: fatal error, that should not happen */ 47 | } catch (error) { 48 | throw new Error(`Could not update "${path}": ${error.message}`); 49 | } 50 | /* c8 ignore stop */ 51 | }); 52 | }, 53 | }; 54 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | - Using welcoming and inclusive language 18 | - Being respectful of differing viewpoints and experiences 19 | - Gracefully accepting constructive criticism 20 | - Focusing on what is best for the community 21 | - Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | - The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | - Trolling, insulting/derogatory comments, and personal or political attacks 28 | - Public or private harassment 29 | - Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | - Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at opensource+coc@martynus.net. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at [https://contributor-covenant.org/version/1/4][version] 72 | 73 | [homepage]: https://contributor-covenant.org 74 | [version]: https://contributor-covenant.org/version/1/4/ 75 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const MemoryFileSystem = require("memory-fs"); 2 | const { test } = require("tap"); 3 | const { prepare } = require("."); 4 | 5 | test("defaults", (t) => { 6 | const files = { 7 | "version.js": Buffer.from("module.exports = '0.0.0-development'"), 8 | }; 9 | const fs = new MemoryFileSystem(files); 10 | 11 | prepare( 12 | { 13 | fs, 14 | glob: { 15 | sync() { 16 | return ["/version.js"]; 17 | }, 18 | }, 19 | }, 20 | { 21 | cwd: "", 22 | nextRelease: { 23 | version: "1.2.3", 24 | }, 25 | logger: { 26 | error: (message) => t.fail(message), 27 | log() {}, 28 | success() {}, 29 | }, 30 | }, 31 | ); 32 | 33 | const newContent = fs.readFileSync("/version.js", "utf8"); 34 | t.match(newContent, /1.2.3/, "Version updated in /version.js"); 35 | t.end(); 36 | }); 37 | 38 | test("no file matches", (t) => { 39 | try { 40 | prepare( 41 | { 42 | files: ["DOES_NOT_EXIST.nope"], 43 | }, 44 | { 45 | cwd: "", 46 | nextRelease: { 47 | version: "1.2.3", 48 | }, 49 | logger: { 50 | error: (message) => t.fail(message), 51 | log() {}, 52 | success() {}, 53 | }, 54 | }, 55 | ); 56 | t.fail("Should throw error"); 57 | } catch (error) { 58 | t.equal(error.message, 'No file matches for ["DOES_NOT_EXIST.nope"]'); 59 | } 60 | t.end(); 61 | }); 62 | 63 | test("files: 'README.md'", (t) => { 64 | const files = { 65 | "README.md": Buffer.from(`# my-project 66 | 67 | current version: 0.0.0-development`), 68 | }; 69 | const fs = new MemoryFileSystem(files); 70 | 71 | prepare( 72 | { 73 | files: "README.md", 74 | fs, 75 | glob: { 76 | sync() { 77 | return ["/README.md"]; 78 | }, 79 | }, 80 | }, 81 | { 82 | cwd: "", 83 | nextRelease: { 84 | version: "1.2.3", 85 | }, 86 | logger: { 87 | error: (message) => t.fail(message), 88 | log() {}, 89 | success() {}, 90 | }, 91 | }, 92 | ); 93 | 94 | const newContent = fs.readFileSync("/README.md", "utf8"); 95 | t.match(newContent, /1.2.3/, "Version updated in README.md"); 96 | t.end(); 97 | }); 98 | 99 | test('multiple files: ["README.md", "my-app.js"]', (t) => { 100 | const files = { 101 | "README.md": Buffer.from(`# my-project 102 | 103 | current version: 0.0.0-development`), 104 | "my-app.js": Buffer.from(`module.exports.version = "0.0.0-development";`), 105 | }; 106 | const fs = new MemoryFileSystem(files); 107 | 108 | prepare( 109 | { 110 | files: "README.md", 111 | fs, 112 | glob: { 113 | sync() { 114 | return ["/README.md", "/my-app.js"]; 115 | }, 116 | }, 117 | }, 118 | { 119 | cwd: "", 120 | nextRelease: { 121 | version: "1.2.3", 122 | }, 123 | logger: { 124 | error: (message) => t.fail(message), 125 | log() {}, 126 | success() {}, 127 | }, 128 | }, 129 | ); 130 | 131 | t.match( 132 | fs.readFileSync("/README.md", "utf8"), 133 | /1.2.3/, 134 | "Version updated in README.md", 135 | ); 136 | t.match( 137 | fs.readFileSync("/my-app.js", "utf8"), 138 | /1.2.3/, 139 | "Version updated in my-app.js", 140 | ); 141 | t.end(); 142 | }); 143 | 144 | test("version not found", (t) => { 145 | const files = { 146 | "foo.js": Buffer.from(`module.exports = require("./bar");`), 147 | "bar.js": Buffer.from(`module.exports.version = "0.0.0-development";`), 148 | }; 149 | const fs = new MemoryFileSystem(files); 150 | 151 | prepare( 152 | { 153 | files: "*", 154 | fs, 155 | glob: { 156 | sync() { 157 | return ["/foo.js", "/bar.js"]; 158 | }, 159 | }, 160 | }, 161 | { 162 | cwd: "", 163 | nextRelease: { 164 | version: "1.2.3", 165 | }, 166 | logger: { 167 | error: (message) => t.fail(message), 168 | log() {}, 169 | success() {}, 170 | }, 171 | }, 172 | ); 173 | 174 | t.match( 175 | fs.readFileSync("/bar.js", "utf8"), 176 | /1.2.3/, 177 | "Version updated in bar.js", 178 | ); 179 | t.end(); 180 | }); 181 | 182 | test("multiple matches in same file", (t) => { 183 | const files = { 184 | "app.js": Buffer.from(`module.exports.version = "0.0.0-development"; 185 | 186 | module.exports.logVersion = () => console.log("0.0.0-development");`), 187 | }; 188 | const fs = new MemoryFileSystem(files); 189 | 190 | prepare( 191 | { 192 | fs, 193 | glob: { 194 | sync() { 195 | return ["/app.js"]; 196 | }, 197 | }, 198 | }, 199 | { 200 | cwd: "", 201 | nextRelease: { 202 | version: "1.2.3", 203 | }, 204 | logger: { 205 | error: (message) => t.fail(message), 206 | log() {}, 207 | success() {}, 208 | }, 209 | }, 210 | ); 211 | 212 | const newContent = fs.readFileSync("/app.js", "utf8"); 213 | t.equal( 214 | newContent, 215 | `module.exports.version = "1.2.3"; 216 | 217 | module.exports.logVersion = () => console.log("1.2.3");`, 218 | ); 219 | t.end(); 220 | }); 221 | 222 | test("custom search", (t) => { 223 | const files = { 224 | "version.js": Buffer.from("module.exports = '{{VERSION}}'"), 225 | }; 226 | const fs = new MemoryFileSystem(files); 227 | 228 | prepare( 229 | { 230 | placeholder: "{{VERSION}}", 231 | fs, 232 | glob: { 233 | sync() { 234 | return ["/version.js"]; 235 | }, 236 | }, 237 | }, 238 | { 239 | cwd: "", 240 | nextRelease: { 241 | version: "1.2.3", 242 | }, 243 | logger: { 244 | error: (message) => t.fail(message), 245 | log() {}, 246 | success() {}, 247 | }, 248 | }, 249 | ); 250 | 251 | const newContent = fs.readFileSync("/version.js", "utf8"); 252 | t.match(newContent, /1.2.3/, "Version updated in /version.js"); 253 | t.end(); 254 | }); 255 | --------------------------------------------------------------------------------