Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const { join } = require("path");
const debug = require("debug")("semantic-release:update-version-in-files");

function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}

module.exports = {
prepare(
{
Expand All @@ -10,13 +14,20 @@ module.exports = {
fs = require("fs"),
glob = require("glob"),
},
{ cwd, nextRelease: { version } },
{ cwd, nextRelease: { version }, lastRelease },
) {
debug("config %o", { files, placeholder });
debug("nextRelease.version %s", version);
debug("lastRelease.version %s", lastRelease?.version);

// Turn placeholder string into regex
const searchRegex = new RegExp(placeholder, "g");
// Search for placeholder and, when available, the previous release version
const searchRegex = new RegExp(
[placeholder, lastRelease?.version]
.filter(Boolean)
.map(escapeRegExp)
.join("|"),
"g",
);

// Normalize files parameter and prefix with root path
const filesNormalized = Array.isArray(files) ? files : [files];
Expand Down
36 changes: 36 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,39 @@ test("custom search", (t) => {
t.match(newContent, /1.2.3/, "Version updated in /version.js");
t.end();
});

test("replaces lastRelease.version", (t) => {
const files = {
"version.js": Buffer.from("module.exports = '2.0.0-beta.1'"),
};
const fs = new MemoryFileSystem(files);

prepare(
{
fs,
glob: {
sync() {
return ["/version.js"];
},
},
},
{
cwd: "",
nextRelease: {
version: "2.0.0-beta.2",
},
lastRelease: {
version: "2.0.0-beta.1",
},
logger: {
error: (message) => t.fail(message),
log() {},
success() {},
},
},
);

const newContent = fs.readFileSync("/version.js", "utf8");
t.equal(newContent, "module.exports = '2.0.0-beta.2'");
t.end();
});
Loading