-
Notifications
You must be signed in to change notification settings - Fork 1
/
auto_version_patch.js
55 lines (38 loc) · 1.62 KB
/
auto_version_patch.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
/* eslint no-console: 0 */
const fs = require('fs')
const { execSync, exec } = require('child_process')
const packageJSON = fs.readFileSync('package.json')
const json = JSON.parse(packageJSON)
const packageName = json.name
const packageVersion = json.version
let registryVersion = packageVersion
console.log(`Package name: ${packageName}`)
console.log(`Package version: ${packageVersion}`)
try {
const npmViewCommand = execSync(`npm view ${packageName} version`)
registryVersion = npmViewCommand.toString().trim()
}
catch (error) {
console.warn('Failed to get registry version')
}
console.log(`Registry version: ${registryVersion}`)
// Increment package patch version
// DOCS: https://docs.npmjs.com/about-semantic-versioning
const packageVersionParsed = packageVersion.split('.')
const registryPackageVersionParsed = registryVersion.split('.')
const majorVer = packageVersionParsed[0]
const minorVer = packageVersionParsed[1]
const majorVerReg = registryPackageVersionParsed[0]
const minorVerReg = registryPackageVersionParsed[1]
const patchVerReg = registryPackageVersionParsed[2]
let patchVersion = 0
if (majorVer === majorVerReg && minorVer === minorVerReg) {
patchVersion = parseInt(patchVerReg) + 1
}
const newVersion = `${packageVersionParsed[0]}.${packageVersionParsed[1]}.${patchVersion}`
console.log(`\nNew package version 🤖: ${newVersion}\n`)
json.version = newVersion
console.log('Write version to ./package.json')
fs.writeFileSync('./package.json', JSON.stringify(json, null, 2))
console.log('Write version to ./src/js/PluginVersion.js')
fs.writeFileSync('./src/js/PluginVersion.js', `module.exports = '${newVersion}'\n`)