Skip to content

Commit

Permalink
Merge pull request #293 from BranchMetrics/fix-npm-version
Browse files Browse the repository at this point in the history
Fix npm version
  • Loading branch information
ethanneff authored Mar 6, 2017
2 parents c7395a8 + 98139de commit b9c198a
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 38 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
4 changes: 2 additions & 2 deletions plugin.template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ SOFTWARE.
</engines>

<!-- Hooks -->
<hook src="src/scripts/beforePluginInstall.js" type="before_plugin_install" />
<hook src="src/scripts/afterPrepare.js" type="after_prepare" />
<hook src="src/scripts/hooks/beforePluginInstall.js" type="before_plugin_install" />
<hook src="src/scripts/hooks/afterPrepare.js" type="after_prepare" />

<!-- JavaScript -->
<js-module src="src/branch.js" name="Branch">
Expand Down
4 changes: 2 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ SOFTWARE.
</engines>

<!-- Hooks -->
<hook src="src/scripts/beforePluginInstall.js" type="before_plugin_install" />
<hook src="src/scripts/afterPrepare.js" type="after_prepare" />
<hook src="src/scripts/hooks/beforePluginInstall.js" type="before_plugin_install" />
<hook src="src/scripts/hooks/afterPrepare.js" type="after_prepare" />

<!-- JavaScript -->
<js-module src="src/branch.js" name="Branch">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// properties
'use strict'
var path = require('path')
var xmlHelper = require('../sdk/xmlHelper.js')
var xmlHelper = require('../lib/xmlHelper.js')

// entry
module.exports = {
Expand All @@ -25,7 +25,7 @@
manifest = updateBranchAppLinks(manifest, mainActivityIndex, preferences)

// save new version of the AndroidManifest
xmlHelper.writeJsonAsXml(manifest, pathToManifest)
xmlHelper.writeJsonAsXml(pathToManifest, manifest)
}

// adds to <application> for Branch init and testmode:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
(function () {
// properties
'use strict'
var configPrefrences = require('./lib/sdk/configXml.js')
var iosPlist = require('./lib/ios/plist.js')
var iosCapabilities = require('./lib/ios/capabilities.js')
var iosAssociatedDomains = require('./lib/ios/associatedDomains.js')
var iosDevelopmentTeam = require('./lib/ios/developmentTeam.js')
var androidManifest = require('./lib/android/androidManifest.js')
var configPrefrences = require('../sdk/configXml.js')
var iosPlist = require('../ios/plist.js')
var iosCapabilities = require('../ios/capabilities.js')
var iosAssociatedDomains = require('../ios/associatedDomains.js')
var iosDevelopmentTeam = require('../ios/developmentTeam.js')
var androidManifest = require('../android/androidManifest.js')
var IOS = 'ios'
var ANDROID = 'android'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function () {
// properties
'use strict'
var nodeDependencies = require('./lib/npm/nodeDependencies.js')
var nodeDependencies = require('../npm/nodeDependencies.js')

// entry
module.exports = run
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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)
}
}
})()
16 changes: 9 additions & 7 deletions src/scripts/lib/sdk/xmlHelper.js → src/scripts/lib/xmlHelper.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,37 @@
}

// read from xml file
function readXmlAsJson (filePath) {
function readXmlAsJson (file) {
var xmlData
var xmlParser
var parsedData

try {
xmlData = fs.readFileSync(filePath)
xmlData = fs.readFileSync(file)
xmlParser = new xml2js.Parser()
xmlParser.parseString(xmlData, function (err, data) {
if (!err && data) {
parsedData = data
}
})
} catch (err) {}
} catch (err) {
throw new Error('BRANCH SDK: Cannot write file ' + file)
}

return parsedData
}

// write to xml file
function writeJsonAsXml (jsData, filePath, options) {
function writeJsonAsXml (file, content, options) {
var xmlBuilder = new xml2js.Builder(options)
var changedXmlData = xmlBuilder.buildObject(jsData)
var changedXmlData = xmlBuilder.buildObject(content)
var isSaved = true

try {
fs.writeFileSync(filePath, changedXmlData)
fs.writeFileSync(file, changedXmlData)
} catch (err) {
console.error(err)
isSaved = false
throw new Error('BRANCH SDK: Cannot write file ' + file)
}

return isSaved
Expand Down
File renamed without changes.
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
}
})()
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// properties
'use strict'
var path = require('path')
var xmlHelper = require('./xmlHelper.js')
var xmlHelper = require('../lib/xmlHelper.js')

// entry
module.exports = {
Expand Down

0 comments on commit b9c198a

Please sign in to comment.