-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: automated npm version on release
- Loading branch information
Showing
4 changed files
with
89 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
})() |