|
1 | 1 | (function () { |
2 | 2 | var path = require('path') |
| 3 | + var exec = require('child_process').exec |
3 | 4 | var fileHelper = require('../lib/fileHelper.js') |
4 | 5 | var FILES = ['package.json', 'plugin.xml', 'plugin.template.xml'] |
5 | 6 |
|
|
9 | 10 | function updateNpmVersion (pluginConfig, config, callback) { |
10 | 11 | var files = readFilePaths(FILES) |
11 | 12 | var version = config.nextRelease.version |
| 13 | + var git = '' |
12 | 14 |
|
13 | 15 | for (var i = 0; i < files.length; i++) { |
| 16 | + // update |
14 | 17 | var file = files[i] |
15 | 18 | var content = readContent(file) |
| 19 | + var updated = updateVersion(file, content, version) |
16 | 20 |
|
17 | | - content = updateVersion(file, content, version) |
18 | | - saveContent(file, content) |
| 21 | + // early exit (made no changes) |
| 22 | + if (content === updated) return |
| 23 | + |
| 24 | + // save |
| 25 | + git += 'git add ' + file + ' && ' |
| 26 | + saveContent(file, updated) |
19 | 27 | } |
| 28 | + commitChanges(git, version) |
20 | 29 | } |
21 | 30 |
|
| 31 | + // handle content |
22 | 32 | function readContent (file) { |
23 | 33 | return isFileXml(file) ? fileHelper.readFile(file) : JSON.parse(fileHelper.readFile(file)) |
24 | 34 | } |
25 | 35 |
|
| 36 | + function saveContent (file, content) { |
| 37 | + return isFileXml(file) ? fileHelper.writeFile(file, content) : fileHelper.writeFile(file, JSON.stringify(content, null, 2)) |
| 38 | + } |
| 39 | + |
| 40 | + function isFileXml (file) { |
| 41 | + return file.indexOf('xml') > 0 |
| 42 | + } |
| 43 | + |
| 44 | + // update content based on xml or json |
26 | 45 | function updateVersion (file, content, version) { |
27 | 46 | var prev = /id="branch-cordova-sdk"[\s]*version="\d+\.\d+\.\d+"/mgi |
28 | 47 | var next = 'id="branch-cordova-sdk"\n version="' + version + '"' |
|
39 | 58 | return content |
40 | 59 | } |
41 | 60 |
|
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 | | - |
| 61 | + // get the absolute path of the files within the root directory |
50 | 62 | function readFilePaths (files) { |
51 | 63 | var locations = [] |
52 | 64 | for (var i = 0; i < files.length; i++) { |
|
56 | 68 | } |
57 | 69 | return locations |
58 | 70 | } |
| 71 | + |
| 72 | + // push file code changes to github |
| 73 | + function commitChanges (git, version) { |
| 74 | + git += 'git commit -m "chore: updated npm version to ' + version + '" && git push' |
| 75 | + exec(git, function (err, stdout, stderr) { |
| 76 | + if (err) { |
| 77 | + throw new Error('BRANCH SDK: Failed commit git changes to npm version. Docs https://goo.gl/GijGKP') |
| 78 | + } |
| 79 | + }) |
| 80 | + } |
59 | 81 | })() |
0 commit comments