-
Notifications
You must be signed in to change notification settings - Fork 15
/
version-bump.js
78 lines (62 loc) · 1.97 KB
/
version-bump.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
*
* Discogs Enhancer
*
* @author: Matthew Salcido
* @website: http://www.msalcido.com
* @github: https://github.com/salcido
*
* ---------------------------------------------------------------------------
* Overview
* ---------------------------------------------------------------------------
*
* This is a utility that will update the version
* number in Discogs Enhancer's `manifest.json`
* and `package.json` files as well as update the badges
* within the `readme.md` file.
*
* Run it in the terminal like `npm run bump <version number>`
*
*/
const fs = require('fs');
// new version number {string}
const newVersion = process.argv[2];
// ========================================================
// Functions (Alphabetical)
// ========================================================
/**
* Updates the manifest and package json files
* with a new version string
* @param {string} version A version number (x.x.x)
* @returns {undefined}
*/
function updateJSONfiles(version) {
let files = ['manifest.json', 'package.json', 'package-lock.json'];
files.forEach(file => {
let data = JSON.parse(fs.readFileSync(file));
data.version = version;
fs.writeFileSync(file, JSON.stringify(data, null, 2));
// add new line to end of file
fs.appendFileSync(file, '\n');
console.log(`✅ Updated ${file} to ${version}.`);
});
}
/**
* Validates that the version string is in
* the correct format
* @param {string} version The version number
* @returns {undefined}
*/
function validateVersion(version) {
let pattern = /\d+\.\d+\.\d/;
if ( !version ) return console.log('⚠️ Error: No version argument was passed.');
if ( !pattern.test(version) ) return console.log('⚠️ Error: Invalid version.');
return true;
}
// ========================================================
// Init
// ========================================================
// Kick things off...
if ( validateVersion(newVersion) ) {
updateJSONfiles(newVersion);
}