-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (91 loc) · 2.74 KB
/
index.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
#!/usr/bin/env node
const fs = require('fs')
const {prompt} = require('enquirer')
const semver = require('semver')
const chalk = require('chalk')
const getFilePath = async () => {
const files = [
'package.json',
...fs.readdirSync(process.cwd())
]
const cli = await prompt({
type: 'select',
name: 'file',
message: 'File config:',
choices: files.filter((element, index, array) => array.indexOf(element) === index)
})
return cli.file
}
const getNewVersion = async currentVersion => {
const versionTags = [
'major',
'minor',
'patch',
'custom'
// 'prerelease'
]
const cli = await prompt({
type: 'select',
name: 'version',
message: 'New Version:',
choices: versionTags
})
const {version} = cli
if (version === 'custom') {
const newVersion = await prompt({
type: 'input',
name: 'version',
message: 'Version Number:',
initial: currentVersion
})
return newVersion.version
}
return semver.inc(currentVersion, version)
}
const showFragment = (text, findString, highlighted) => {
const lines = text.split('\n')
const findIndex = lines.findIndex(emement => emement.match(findString))
const fragments = lines.slice(findIndex < 2 ? 0 : findIndex - 2, findIndex + 3) // Show 2 Line to top and 2 down
const fragmentText = fragments.map(element => {
const match = element.match(highlighted)
if (match) {
element = element.replace(highlighted, chalk.bold.white(highlighted))
}
return element
}).join('\n')
console.log(`${chalk.bold.bgBlack.greenBright(' Code: ')}\n${chalk.dim(fragmentText)}`)
}
const main = async () => {
const data = {}
data.file = await getFilePath()
if (!fs.existsSync(data.file)) {
console.log(chalk.bold.red('⬢ File not found!'))
process.exit(1)
}
const fileRaw = fs.readFileSync(data.file).toString().replace(/xml[\s]*version/, '') // Remove XML init tag
// Regex: https://regex101.com/r/Xhc3oV/5
const currentVersionMatch = fileRaw.match(/(?<full>["']*version["']*[\s:=]*["'](?<version>[\w.-]*)["'])/)
if (!currentVersionMatch) {
console.log(chalk.bold.red('⬢ Key \'version\' in file not found!'))
process.exit(1)
}
console.log(`${chalk.bold.white('▶ Current version')}: ${chalk.cyan(currentVersionMatch.groups.version)}`)
data.version = await getNewVersion(currentVersionMatch.groups.version)
const newVersion = currentVersionMatch.groups.full.replace(currentVersionMatch.groups.version, data.version)
const output = fileRaw.replace(currentVersionMatch.groups.full, newVersion)
showFragment(output, newVersion, data.version)
const done = await prompt({
type: 'confirm',
name: 'ok',
message: 'Is this OK?',
initial: true
})
if (done.ok) {
fs.writeFileSync(data.file, output)
}
}
main().catch(error => {
if (error !== '') {
console.log('Error:', error)
}
})