-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.gen.js
33 lines (26 loc) · 992 Bytes
/
version.gen.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
const fs = require('fs');
const path = require('path');
function generateVersion() {
const currentDate = new Date();
const year = currentDate.getFullYear();
const month = (currentDate.getMonth() + 1).toString().padStart(2, '0');
const versionFilePath = path.resolve(__dirname, 'src/version.js');
let version = '';
try {
version = fs.readFileSync(versionFilePath, 'utf8');
} catch (error) {
// If the version file doesn't exist, create it.
fs.writeFileSync(versionFilePath, '', 'utf8');
}
let res = version.match(new RegExp(/\d{6}\.\d+/))
if (!res) {
version = `${year}${month}.001`;
} else {
const parts = res[0].split('.');
const count = parseInt(parts[1]);
version = `${parts[0]}.${(count + 1).toString().padStart(3, '0')}`;
}
fs.writeFileSync(versionFilePath, `module.exports = '${version}';`, 'utf8');
console.log(`Generated version: ${version}`);
}
generateVersion();