Skip to content

Commit

Permalink
fix: automated npm version on release
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanneff committed Mar 6, 2017
1 parent fbce203 commit 98139de
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 17 deletions.
16 changes: 0 additions & 16 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,6 @@ gulp.task('setupNpm', function () {
setIosNpmOrDev('npm')
})

// TODO: does not work. need to revise and update package.json -> semantic-release
gulp.task('update-plugin-xml-version', function () {
// first match only!
var PLUGIN_XML_VERSION_REGEX = /^\s*version=\"[\d\.]*\"\>$/m // eslint-disable-line
var versionNumber = require('./package.json').version

// this will break if plugin.xml is not formatted exactly as we expect
// so you might end up needing to fix the regex
for (var target of [ '.xml', '.template.xml' ]) {
var pluginXML = fs.readFileSync('plugin' + target, 'utf8')
var newVersionXML = ` version="${versionNumber}">`
pluginXML = pluginXML.replace(PLUGIN_XML_VERSION_REGEX, newVersionXML)
fs.writeFileSync('plugin' + target, pluginXML)
}
})

function getDevPluginXML () {
// generate plugin.xml for local development
// here we reference the frameworks instead of all the files directly
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"scripts": {
"commitmsg": "validate-commit-msg",
"prerelease": "gulp prod",
"semantic-release": "semantic-release pre && gulp update-plugin-xml-version && npm publish && semantic-release post"
"semantic-release": "semantic-release pre --verifyRelease='./src/scripts/npm/nodeVersion' && npm publish && semantic-release post"
},
"dependencies": {
"mkpath": "^1.0.0",
Expand Down
29 changes: 29 additions & 0 deletions src/scripts/lib/fileHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(function () {
// properties
'use strict'
var fs = require('fs')

// entry
module.exports = {
readFile: readFile,
writeFile: writeFile
}

// read file
function readFile (file) {
try {
return fs.readFileSync(file, 'utf8')
} catch (err) {
throw new Error('BRANCH SDK: Cannot read file ' + file)
}
}

// write file
function writeFile (file, content) {
try {
fs.writeFileSync(file, content, 'utf8')
} catch (err) {
throw new Error('BRANCH SDK: Cannot write file ' + file + ' with content ' + content)
}
}
})()
59 changes: 59 additions & 0 deletions src/scripts/npm/nodeVersion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
(function () {
var path = require('path')
var fileHelper = require('../lib/fileHelper.js')
var FILES = ['package.json', 'plugin.xml', 'plugin.template.xml']

module.exports = updateNpmVersion

// updates the npm version in semantic-release pre
function updateNpmVersion (pluginConfig, config, callback) {
var files = readFilePaths(FILES)
var version = config.nextRelease.version

for (var i = 0; i < files.length; i++) {
var file = files[i]
var content = readContent(file)

content = updateVersion(file, content, version)
saveContent(file, content)
}
}

function readContent (file) {
return isFileXml(file) ? fileHelper.readFile(file) : JSON.parse(fileHelper.readFile(file))
}

function updateVersion (file, content, version) {
var prev = /id="branch-cordova-sdk"[\s]*version="\d+\.\d+\.\d+"/mgi
var next = 'id="branch-cordova-sdk"\n version="' + version + '"'

try {
if (isFileXml(file)) {
content = content.replace(prev, next)
} else {
content.version = version
}
} catch (e) {
throw new Error('BRANCH SDK: update to update npm version with file ' + file)
}
return content
}

function saveContent (file, content) {
return isFileXml(file) ? fileHelper.writeFile(file, content) : fileHelper.writeFile(file, JSON.stringify(content, null, 2))
}

function isFileXml (file) {
return file.indexOf('xml') > 0
}

function readFilePaths (files) {
var locations = []
for (var i = 0; i < files.length; i++) {
var file = files[i]
var location = path.join(__dirname, '../../../', file)
locations.push(location)
}
return locations
}
})()

0 comments on commit 98139de

Please sign in to comment.