-
Notifications
You must be signed in to change notification settings - Fork 1
/
tonVersion.js
123 lines (117 loc) · 3.98 KB
/
tonVersion.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const fs = require('fs');
const os = require('os');
const path = require('path');
const https = require('https');
function canonical(versionString) {
if(!versionString) {
return [];
}
verArray = versionString.split('.');
for(let i=0; i < verArray.length; i++) {
const part = Number.parseInt(verArray[i]);
if(!Number.isInteger(part)) {
throw new Error(`Invalid version part "${verArray[i]}" in version string "${versionString}"`);
} else {
verArray[i] = part;
}
}
if(verArray.length < 2) {
throw new Error(`Invalid version format "${versionString}". Expected [X]*.[Y]*.[N]*`);
}
if(verArray.length === 2) {
verArray.push(1);
}
return verArray;
};
function increment(canonicalVersion) {
canonicalVersion[2]++;
return canonicalVersion
};
function setVersion(dest, version) {
const packagePath = path.join(dest, 'package.json');
const cargoPath = path.join(dest, 'Cargo.toml');
if(fs.existsSync(packagePath)) {
const conf = JSON.parse(fs.readFileSync(packagePath).toString());
conf.version = version;
fs.writeFileSync(
packagePath,
JSON.stringify(conf, null, ' ')
);
}
if(fs.existsSync(cargoPath)) {
const conf = fs.readFileSync(cargoPath).toString().split(os.EOL);
let pos = conf.indexOf('[package]') + 1;
while(conf[pos] !== '') {
if(conf[pos].startsWith('version = ')) {
conf[pos] = `version = "${version}"`
break;
}
pos++;
}
fs.writeFileSync(cargoPath, conf.join(os.EOL));
}
};
function allowed(ver) {
const n = canonical(ver);
const c = canonical(lastVersion.candidate ? lastVersion.candidate : lastVersion.release);
return (
c[0] <= n[0] &&
(c[1] <= n[1] || c[0] < n[0]) &&
(c[2] < n[2] || c[1] < n[1])
);
}
const cwd = process.cwd();
let args = process.argv.slice(2);
if(!fs.existsSync('version.json')) {
throw new Error('File version.json not found');
}
const folders = []
const lastVersion = JSON.parse(fs.readFileSync(path.join(cwd,'version.json')));
if(!lastVersion || !lastVersion.release || !canonical(lastVersion.release)) {
throw new Error('Wrong release value in version.json');
}
let argVersion;
while(args.length > 0) {
switch(args[0].trim()) {
case '--set':
if(args.length < 2) {
throw new Error('Version value wasn\'t been presented');
}
argVersion = args[1].trim();
args = args.slice(1);
break;
case '--release':
if(lastVersion.candidate && lastVersion.release) {
lastVersion.release = lastVersion.candidate;
lastVersion.candidate = '';
fs.writeFileSync(path.join(cwd, 'version.json'), JSON.stringify(lastVersion));
process.exit(0);
} else {
throw new Error(`Unable to set candidate as release\n${JSON.stringify(lastVersion)}`);
}
case '--decline':
if(lastVersion.release) {
lastVersion.candidate='';
fs.writeFileSync(path.join(cwd, 'version.json'), JSON.stringify(lastVersion));
process.exit(0);
} else {
throw new Error(`Unable to decline candidate\n${JSON.stringify(lastVersion)}`);
}
default :
item = path.join(cwd, ...args[0].split(/[\\/]/g));
if(fs.existsSync(item)) {
folders.push(item);
}
break;
}
args = args.slice(1);
}
const newVersion = argVersion && allowed(argVersion) ? canonical(argVersion) : (
lastVersion.candidate ? canonical(lastVersion.candidate) : increment(canonical(lastVersion.release))
);
console.log(newVersion.join('.'));
folders.forEach(item => setVersion(item, newVersion.join('.')));
if(!lastVersion.candidate) {
lastVersion.candidate = newVersion.join('.');
fs.writeFileSync(path.join(cwd, 'version.json'), JSON.stringify(lastVersion));
}