-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·169 lines (143 loc) · 3.94 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#! /usr/bin/env node
/**
* Create changelog using keep a changelog https://keepachangelog.com
* Release Behavior:
* - each release have a version number, which obtained from package.json version.
* - each logs from the CLI question, will generated the changes by type to the Changelog Instance.
* - when release is added, we created a changelog file with group by minor version in changelogs/ directory.
*
* @author Andikha Dian.
*/
const { Changelog, Release, parser } = require('keep-a-changelog')
const fs = require('fs')
const readline = require('readline')
const { version } = JSON.parse(fs.readFileSync('package.json', 'utf8'))
const bump = {
version: version,
date: new Date(),
logs: [],
}
const supportedChangeTypes = [
{
key: 1,
label: 'Added',
methodName: 'added',
},
{
key: 2,
label: 'Changed',
methodName: 'changed',
},
{
key: 3,
label: 'Removed',
methodName: 'removed',
},
{
key: 4,
label: 'Fixed',
methodName: 'fixed',
},
{
key: 5,
label: 'Deprecated',
methodName: 'deprecated',
},
{
key: 6,
label: 'Security',
methodName: 'security',
},
]
/* ------------------------------ CLI Questions ----------------------------- */
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
const question = (str) => new Promise((resolve) => rl.question(str, resolve))
const steps = {
start: async () => {
console.log('Changelog Generation\n')
return steps.chooseChangeType()
},
chooseChangeType: async () => {
console.log('Change Types: ')
supportedChangeTypes.forEach((item) =>
console.log(`${item.key}. ${item.label}`)
)
const answer = await question('Choose change type, Please type number: ')
const targetChangeType = supportedChangeTypes.find(
(item) => item.key === Number(answer)
)
if (!targetChangeType) {
console.log('Wrong log type!')
return steps.chooseChangeType()
}
return steps.describeLogChange(targetChangeType)
},
describeLogChange: async (changeType) => {
const answer = await question('Describe the change (eg: New Home UI): ')
bump.logs.push({
changeType,
change: answer,
})
console.log('OK!')
return steps.newLog()
},
newLog: async () => {
const answer = await question('Add more log? Please type [y/n]: ')
switch (answer.toLowerCase()) {
case 'y':
return steps.chooseChangeType()
default:
return steps.end()
}
},
end: async () => {
createChangelog()
return rl.close()
},
}
/* --------------------------- Generate Changelog --------------------------- */
function createChangelog() {
if (!bump.logs.length) {
return
}
const ROOT_DIR = 'changelogs/'
const [major, minor] = String(bump.version).split('.')
const targetFile = `${ROOT_DIR}${major}.${minor}.x.md`
let changelog = new Changelog(
'Changelog',
`All notable changes to ${major}.${minor}.x version will be documented in this file.`
)
if (!fs.existsSync(ROOT_DIR)) {
fs.mkdir(ROOT_DIR, (err) => {
if (err) throw new Error(`Error write file: ${err.message}`)
})
}
if (fs.existsSync(targetFile)) {
changelog = parser(fs.readFileSync(targetFile, 'utf-8'))
}
const release = new Release(bump.version, bump.date)
for (let idx = 0; idx < bump.logs.length; idx++) {
const log = bump.logs[idx]
try {
release[log.changeType.methodName](log.change)
} catch (error) {
console.log(`❌ Failed to call release item: ${log}`)
console.log(`Error: ${error}`)
}
}
changelog.addRelease(release)
fs.writeFile(
targetFile,
changelog.toString(),
{ encoding: 'utf-8' },
(error) => {
if (error) throw new Error(`Error write file: ${error.message}`)
console.log(`✅ Changelog from ${version} has been created!`)
}
)
}
/* --------------------------------- Runner --------------------------------- */
steps.start()