semantic-release-plugin-update-version-in-files
Advanced tools
Comparing version
27
index.js
const { join } = require("path"); | ||
const debug = require("debug")("semantic-release:update-version-in-files"); | ||
@@ -10,9 +11,9 @@ module.exports = { | ||
fs = require("fs"), | ||
glob = require("glob") | ||
glob = require("glob"), | ||
}, | ||
{ | ||
cwd, | ||
nextRelease: { version } | ||
} | ||
{ cwd, nextRelease: { version } } | ||
) { | ||
debug("config %o", { files, placeholder }); | ||
debug("nextRelease.version %s", version); | ||
// Turn placeholder string into regex | ||
@@ -25,11 +26,13 @@ const searchRegex = new RegExp(placeholder, "g"); | ||
// Turn files into flat array of matche file paths | ||
const filePaths = [] | ||
.concat(...filesNormalized.map(path => glob.sync(join(cwd, path)))) | ||
.filter(path => fs.statSync(path).isFile()); | ||
const existingFilePaths = [] | ||
.concat(...filesNormalized.map((path) => glob.sync(join(cwd, path)))) | ||
.filter((path) => fs.statSync(path).isFile()); | ||
if (filePaths.length === 0) { | ||
debug("Existing files found: %o", existingFilePaths); | ||
if (existingFilePaths.length === 0) { | ||
throw new Error(`No file matches for ${JSON.stringify(files)}`); | ||
} | ||
filePaths.forEach(path => { | ||
existingFilePaths.forEach((path) => { | ||
try { | ||
@@ -39,5 +42,7 @@ const content = fs.readFileSync(path, "utf8"); | ||
if (!searchRegex.test(content)) { | ||
debug("No match found in %s", path); | ||
return; | ||
} | ||
debug("Match found in %s", path); | ||
fs.writeFileSync(path, content.replace(searchRegex, version)); | ||
@@ -49,3 +54,3 @@ } catch (error) { | ||
}); | ||
} | ||
}, | ||
}; |
{ | ||
"name": "semantic-release-plugin-update-version-in-files", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Replace version placeholders with calculated version in any files before publishing", | ||
@@ -8,2 +8,3 @@ "main": "index.js", | ||
"lint": "prettier --check README.md *.js package.json", | ||
"lint:fix": "prettier --write README.md *.js package.json", | ||
"pretest": "npm run -s lint", | ||
@@ -20,9 +21,10 @@ "test": "tap test.js --100", | ||
"dependencies": { | ||
"debug": "^4.1.1", | ||
"glob": "^7.1.3" | ||
}, | ||
"devDependencies": { | ||
"memory-fs": "^0.4.1", | ||
"prettier": "^1.17.0", | ||
"tap": "^13.1.2", | ||
"semantic-release": "^15.13.3" | ||
"memory-fs": "^0.5.0", | ||
"prettier": "^2.0.1", | ||
"tap": "^14.0.0", | ||
"semantic-release": "^17.0.0" | ||
}, | ||
@@ -29,0 +31,0 @@ "repository": { |
@@ -6,4 +6,4 @@ # semantic-release-plugin-update-version-in-files | ||
[](https://www.npmjs.com/package/semantic-release-plugin-update-version-in-files) | ||
[](https://travis-ci.com/gr2m/semantic-release-plugin-update-version-in-files.js) | ||
[](https://greenkeeper.io/) | ||
[](https://travis-ci.com/gr2m/semantic-release-plugin-update-version-in-files) | ||
[](https://greenkeeper.io/) | ||
@@ -10,0 +10,0 @@ Example |
108
test.js
@@ -5,5 +5,5 @@ const MemoryFileSystem = require("memory-fs"); | ||
test("defaults", t => { | ||
test("defaults", (t) => { | ||
const files = { | ||
"version.js": Buffer.from("module.exports = '0.0.0-development'") | ||
"version.js": Buffer.from("module.exports = '0.0.0-development'"), | ||
}; | ||
@@ -18,4 +18,4 @@ const fs = new MemoryFileSystem(files); | ||
return ["/version.js"]; | ||
} | ||
} | ||
}, | ||
}, | ||
}, | ||
@@ -25,9 +25,9 @@ { | ||
nextRelease: { | ||
version: "1.2.3" | ||
version: "1.2.3", | ||
}, | ||
logger: { | ||
error: message => t.fail(message), | ||
error: (message) => t.fail(message), | ||
log() {}, | ||
success() {} | ||
} | ||
success() {}, | ||
}, | ||
} | ||
@@ -41,7 +41,7 @@ ); | ||
test("no file matches", t => { | ||
test("no file matches", (t) => { | ||
try { | ||
prepare( | ||
{ | ||
files: ["DOES_NOT_EXIST.nope"] | ||
files: ["DOES_NOT_EXIST.nope"], | ||
}, | ||
@@ -51,9 +51,9 @@ { | ||
nextRelease: { | ||
version: "1.2.3" | ||
version: "1.2.3", | ||
}, | ||
logger: { | ||
error: message => t.fail(message), | ||
error: (message) => t.fail(message), | ||
log() {}, | ||
success() {} | ||
} | ||
success() {}, | ||
}, | ||
} | ||
@@ -68,7 +68,7 @@ ); | ||
test("files: 'README.md'", t => { | ||
test("files: 'README.md'", (t) => { | ||
const files = { | ||
"README.md": Buffer.from(`# my-project | ||
current version: 0.0.0-development`) | ||
current version: 0.0.0-development`), | ||
}; | ||
@@ -84,4 +84,4 @@ const fs = new MemoryFileSystem(files); | ||
return ["/README.md"]; | ||
} | ||
} | ||
}, | ||
}, | ||
}, | ||
@@ -91,9 +91,9 @@ { | ||
nextRelease: { | ||
version: "1.2.3" | ||
version: "1.2.3", | ||
}, | ||
logger: { | ||
error: message => t.fail(message), | ||
error: (message) => t.fail(message), | ||
log() {}, | ||
success() {} | ||
} | ||
success() {}, | ||
}, | ||
} | ||
@@ -107,3 +107,3 @@ ); | ||
test('multiple files: ["README.md", "my-app.js"]', t => { | ||
test('multiple files: ["README.md", "my-app.js"]', (t) => { | ||
const files = { | ||
@@ -113,3 +113,3 @@ "README.md": Buffer.from(`# my-project | ||
current version: 0.0.0-development`), | ||
"my-app.js": Buffer.from(`module.exports.version = "0.0.0-development";`) | ||
"my-app.js": Buffer.from(`module.exports.version = "0.0.0-development";`), | ||
}; | ||
@@ -125,4 +125,4 @@ const fs = new MemoryFileSystem(files); | ||
return ["/README.md", "/my-app.js"]; | ||
} | ||
} | ||
}, | ||
}, | ||
}, | ||
@@ -132,9 +132,9 @@ { | ||
nextRelease: { | ||
version: "1.2.3" | ||
version: "1.2.3", | ||
}, | ||
logger: { | ||
error: message => t.fail(message), | ||
error: (message) => t.fail(message), | ||
log() {}, | ||
success() {} | ||
} | ||
success() {}, | ||
}, | ||
} | ||
@@ -156,6 +156,6 @@ ); | ||
test("version not found", t => { | ||
test("version not found", (t) => { | ||
const files = { | ||
"foo.js": Buffer.from(`module.exports = require("./bar");`), | ||
"bar.js": Buffer.from(`module.exports.version = "0.0.0-development";`) | ||
"bar.js": Buffer.from(`module.exports.version = "0.0.0-development";`), | ||
}; | ||
@@ -171,4 +171,4 @@ const fs = new MemoryFileSystem(files); | ||
return ["/foo.js", "/bar.js"]; | ||
} | ||
} | ||
}, | ||
}, | ||
}, | ||
@@ -178,9 +178,9 @@ { | ||
nextRelease: { | ||
version: "1.2.3" | ||
version: "1.2.3", | ||
}, | ||
logger: { | ||
error: message => t.fail(message), | ||
error: (message) => t.fail(message), | ||
log() {}, | ||
success() {} | ||
} | ||
success() {}, | ||
}, | ||
} | ||
@@ -197,7 +197,7 @@ ); | ||
test("multiple matches in same file", t => { | ||
test("multiple matches in same file", (t) => { | ||
const files = { | ||
"app.js": Buffer.from(`module.exports.version = "0.0.0-development"; | ||
module.exports.logVersion = () => console.log("0.0.0-development");`) | ||
module.exports.logVersion = () => console.log("0.0.0-development");`), | ||
}; | ||
@@ -212,4 +212,4 @@ const fs = new MemoryFileSystem(files); | ||
return ["/app.js"]; | ||
} | ||
} | ||
}, | ||
}, | ||
}, | ||
@@ -219,9 +219,9 @@ { | ||
nextRelease: { | ||
version: "1.2.3" | ||
version: "1.2.3", | ||
}, | ||
logger: { | ||
error: message => t.fail(message), | ||
error: (message) => t.fail(message), | ||
log() {}, | ||
success() {} | ||
} | ||
success() {}, | ||
}, | ||
} | ||
@@ -240,5 +240,5 @@ ); | ||
test("custom search", t => { | ||
test("custom search", (t) => { | ||
const files = { | ||
"version.js": Buffer.from("module.exports = '{{VERSION}}'") | ||
"version.js": Buffer.from("module.exports = '{{VERSION}}'"), | ||
}; | ||
@@ -254,4 +254,4 @@ const fs = new MemoryFileSystem(files); | ||
return ["/version.js"]; | ||
} | ||
} | ||
}, | ||
}, | ||
}, | ||
@@ -261,9 +261,9 @@ { | ||
nextRelease: { | ||
version: "1.2.3" | ||
version: "1.2.3", | ||
}, | ||
logger: { | ||
error: message => t.fail(message), | ||
error: (message) => t.fail(message), | ||
log() {}, | ||
success() {} | ||
} | ||
success() {}, | ||
}, | ||
} | ||
@@ -270,0 +270,0 @@ ); |
10700
11.57%7
16.67%274
1.11%2
100%+ Added
+ Added
+ Added