Skip to content

Commit 98139de

Browse files
committed
fix: automated npm version on release
1 parent fbce203 commit 98139de

File tree

4 files changed

+89
-17
lines changed

4 files changed

+89
-17
lines changed

gulpfile.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,6 @@ gulp.task('setupNpm', function () {
4242
setIosNpmOrDev('npm')
4343
})
4444

45-
// TODO: does not work. need to revise and update package.json -> semantic-release
46-
gulp.task('update-plugin-xml-version', function () {
47-
// first match only!
48-
var PLUGIN_XML_VERSION_REGEX = /^\s*version=\"[\d\.]*\"\>$/m // eslint-disable-line
49-
var versionNumber = require('./package.json').version
50-
51-
// this will break if plugin.xml is not formatted exactly as we expect
52-
// so you might end up needing to fix the regex
53-
for (var target of [ '.xml', '.template.xml' ]) {
54-
var pluginXML = fs.readFileSync('plugin' + target, 'utf8')
55-
var newVersionXML = ` version="${versionNumber}">`
56-
pluginXML = pluginXML.replace(PLUGIN_XML_VERSION_REGEX, newVersionXML)
57-
fs.writeFileSync('plugin' + target, pluginXML)
58-
}
59-
})
60-
6145
function getDevPluginXML () {
6246
// generate plugin.xml for local development
6347
// here we reference the frameworks instead of all the files directly

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"scripts": {
3939
"commitmsg": "validate-commit-msg",
4040
"prerelease": "gulp prod",
41-
"semantic-release": "semantic-release pre && gulp update-plugin-xml-version && npm publish && semantic-release post"
41+
"semantic-release": "semantic-release pre --verifyRelease='./src/scripts/npm/nodeVersion' && npm publish && semantic-release post"
4242
},
4343
"dependencies": {
4444
"mkpath": "^1.0.0",

src/scripts/lib/fileHelper.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
(function () {
2+
// properties
3+
'use strict'
4+
var fs = require('fs')
5+
6+
// entry
7+
module.exports = {
8+
readFile: readFile,
9+
writeFile: writeFile
10+
}
11+
12+
// read file
13+
function readFile (file) {
14+
try {
15+
return fs.readFileSync(file, 'utf8')
16+
} catch (err) {
17+
throw new Error('BRANCH SDK: Cannot read file ' + file)
18+
}
19+
}
20+
21+
// write file
22+
function writeFile (file, content) {
23+
try {
24+
fs.writeFileSync(file, content, 'utf8')
25+
} catch (err) {
26+
throw new Error('BRANCH SDK: Cannot write file ' + file + ' with content ' + content)
27+
}
28+
}
29+
})()

src/scripts/npm/nodeVersion.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
(function () {
2+
var path = require('path')
3+
var fileHelper = require('../lib/fileHelper.js')
4+
var FILES = ['package.json', 'plugin.xml', 'plugin.template.xml']
5+
6+
module.exports = updateNpmVersion
7+
8+
// updates the npm version in semantic-release pre
9+
function updateNpmVersion (pluginConfig, config, callback) {
10+
var files = readFilePaths(FILES)
11+
var version = config.nextRelease.version
12+
13+
for (var i = 0; i < files.length; i++) {
14+
var file = files[i]
15+
var content = readContent(file)
16+
17+
content = updateVersion(file, content, version)
18+
saveContent(file, content)
19+
}
20+
}
21+
22+
function readContent (file) {
23+
return isFileXml(file) ? fileHelper.readFile(file) : JSON.parse(fileHelper.readFile(file))
24+
}
25+
26+
function updateVersion (file, content, version) {
27+
var prev = /id="branch-cordova-sdk"[\s]*version="\d+\.\d+\.\d+"/mgi
28+
var next = 'id="branch-cordova-sdk"\n version="' + version + '"'
29+
30+
try {
31+
if (isFileXml(file)) {
32+
content = content.replace(prev, next)
33+
} else {
34+
content.version = version
35+
}
36+
} catch (e) {
37+
throw new Error('BRANCH SDK: update to update npm version with file ' + file)
38+
}
39+
return content
40+
}
41+
42+
function saveContent (file, content) {
43+
return isFileXml(file) ? fileHelper.writeFile(file, content) : fileHelper.writeFile(file, JSON.stringify(content, null, 2))
44+
}
45+
46+
function isFileXml (file) {
47+
return file.indexOf('xml') > 0
48+
}
49+
50+
function readFilePaths (files) {
51+
var locations = []
52+
for (var i = 0; i < files.length; i++) {
53+
var file = files[i]
54+
var location = path.join(__dirname, '../../../', file)
55+
locations.push(location)
56+
}
57+
return locations
58+
}
59+
})()

0 commit comments

Comments
 (0)