-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbump-version.js
More file actions
102 lines (91 loc) · 3.65 KB
/
bump-version.js
File metadata and controls
102 lines (91 loc) · 3.65 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const fs = require('fs');
const child = require("child_process");
const pubspecFilePath = 'pubspec.yaml';
const iOSVersionFilePath = 'ios/Classes/Constants.swift';
const androidVersionFilePath = 'android/src/main/java/com/letscooee/flutter/utils/Constants.java';
let newVersion = process.argv[2];
/**************** Find Old Version ***************/
let pubspecData = fs.readFileSync(pubspecFilePath, "utf8");
const oldVersion = pubspecData.match(/version: \d*\.\d*\.\d*/g)[0].split(':')[1].trim().replaceAll('"', '');
/**************** End Find Old Version ***************/
if (!newVersion) {
console.log('Please specify a version number/updater');
return;
}
/**************** Bump Version ***************/
if (newVersion === 'patch') {
newVersion = updatePatch(oldVersion);
} else if (newVersion === 'minor') {
newVersion = updateMinor(oldVersion);
} else if (newVersion === 'major') {
newVersion = updateMajor(oldVersion);
} else if (!isValidVersion(newVersion)) {
console.log('Please specify a valid argument - patch|minor|major|<version>');
console.log('Check:\n\tnode publish.js patch|minor|major|<version>');
console.log('parameter:\n' +
'\tpatch: update patch version i.e 1.0.0 -> 1.0.1\n' +
'\tminor: update minor version i.e 1.0.0 -> 1.1.0\n' +
'\tmajor: update major version i.e 1.0.0 -> 2.0.0\n' +
'\t<version>: valid version string in 1.1.1 format\n');
return;
}
/**************** End Bump Version ***************/
console.log(`updating [${oldVersion}] --> [${newVersion}]`);
const newVersionCode = parseInt(newVersion.split('.').map(v => v.padStart(2, '0')).join(''));
/**************** Write New Version in pubspec.yaml ***************/
pubspecData = pubspecData.replace(/version: \d*\.\d*\.\d*/g, `version: ${newVersion}`);
fs.writeFileSync(pubspecFilePath, pubspecData);
/**************** End Write New Version in pubspec.yaml ***************/
/**************** Write New Version in Constant.java & Constant.swift ***************/
bumpVersionInNativeFiles(iOSVersionFilePath);
bumpVersionInNativeFiles(androidVersionFilePath);
/**************** End Write New Version in pubspec.yaml ***************/
/**
* Access file at given path and write VERSION_NAME & VERSION_CODE
*
* @param path {string} path of the file
*/
function bumpVersionInNativeFiles(path){
let cooeeMetaData = fs.readFileSync(path, "utf8");
cooeeMetaData = cooeeMetaData.replace(/VERSION_NAME = "[^"]+"/, `VERSION_NAME = "${newVersion}"`);
if(path.includes('android')){
cooeeMetaData = cooeeMetaData.replace(/VERSION_CODE = [^\n]+/, `VERSION_CODE = ${newVersionCode};`);
}else{
cooeeMetaData = cooeeMetaData.replace(/VERSION_CODE = [^\n]+/, `VERSION_CODE = ${newVersionCode}`);
}
fs.writeFileSync(path, cooeeMetaData);
}
/**
* Update patch version
*
* @param oldVersion {string} old version string
* @returns {string} new version string
*/
function updatePatch(oldVersion) {
let newVersionArr = oldVersion.split('.');
newVersionArr[2] = parseInt(newVersionArr[2]) + 1;
return newVersionArr.join('.');
}
/**
* Update minor version
*
* @param oldVersion {string} old version string
* @returns {string} new version string
*/
function updateMinor(oldVersion) {
let newVersionArr = oldVersion.split('.');
newVersionArr[1] = parseInt(newVersionArr[1]) + 1;
newVersionArr[2] = 0;
return newVersionArr.join('.');
}
/**
* Update major version
*
* @param oldVersion {string} old version string
* @returns {string} new version string
*/
function updateMajor(oldVersion) {
let majorVersion = oldVersion.split('.')[0];
majorVersion = parseInt(majorVersion) + 1;
return `${majorVersion}.0.0`;
}