-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (140 loc) Β· 4.23 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
'use strict';
const path = require('path');
const chalk = require('chalk');
const fs = require('fs');
const {
askType,
askScope,
askSubject,
askChanges,
askLabels
} = require('./lib/prompts.js');
const {
executeScripts,
buildEnvsFromObject
} = require('./lib/scripts.js');
const {
preppendChangelog
} = require('./lib/changelog.js');
const {
loadTemplate
} = require('./lib/template.js');
const config = {
changelogFilename:'CHANGELOG.md',
changelogTemplate:'default',
commitTemplate:'karma',
types:'karma',
scopes:[],
labels:[
'action',
'version'
],
beforeCommit:[],
afterCommit:[]
};
try {
const localConfig = fs.readFileSync(path.join(process.cwd(), '.gitchangelog')).toString('utf8');
Object.assign(config, JSON.parse(localConfig));
} catch (error) {}
if (config.types === 'karma') {
config.types = {
feat: 'new feature for the user, not a new feature for build script',
fix: 'bug fix for the user, not a fix to a build script',
docs: 'changes to the documentation',
style: 'formatting, missing semi colons, etc; no production code change',
refactor: 'refactoring production code, eg. renaming a variable',
test: 'adding missing tests, refactoring tests; no production code change',
chore: 'updating grunt tasks etc; no production code change'
};
} else if (config.types === 'atom') {
config.types = {
'π¨ - code improvement': 'when improving the format/structure of the code',
'π - performance improvement': 'when improving performance',
'π± - memory leak': 'when plugging memory leaks',
'π - docs': 'when writing docs',
'π§ - linux fix': 'when fixing something on Linux',
'π - macos fix': 'when fixing something on macOS',
'π - windows fix': 'when fixing something on Windows',
'π - bug fix': 'when fixing a bug',
'π₯ - removing code/files': 'when removing code or files',
'π - ci build fix': 'when fixing the CI build',
'β
- adding tests': 'when adding tests',
'π - security changes': 'when dealing with security',
'β¬οΈ - upgrading dependencies': 'when upgrading dependencies',
'β¬οΈ - downgrading dependencies': 'when downgrading dependencies',
'π - linter compliance': 'when removing linter warnings',
};
}
const commitTemplate = loadTemplate(
config.commitTemplate,
__dirname,
'commit'
);
const changelogTemplate = loadTemplate(
config.changelogTemplate,
__dirname,
'changelog'
);
const run = async () => {
const entry = {
type:null,
scope:null,
subject:null,
body:null,
labels:null
};
entry.type = await askType(Object.keys(config.types));
entry.scope = await askScope(config.scopes);
entry.subject = await askSubject();
entry.body = await askChanges();
entry.labels = await askLabels(config.labels);
const commitMessage = commitTemplate(entry);
const changelogMessage = changelogTemplate(entry);
const scriptsEnvs = buildEnvsFromObject(entry);
if (config.beforeCommit.length > 0) {
console.log(chalk.cyanBright.bold('executing scripts before commmit...'));
executeScripts(config.beforeCommit, scriptsEnvs);
}
console.log(chalk.cyanBright.bold('the commit message:'));
console.log(commitMessage);
executeScripts([[
'git',
'commit',
...process.argv.slice(2),
'-m',
commitMessage
]], scriptsEnvs);
console.log(chalk.cyanBright.bold('the changelog message:'));
console.log(changelogMessage);
const changelogPath = preppendChangelog(changelogMessage, config.changelogFilename);
executeScripts([
[
'git',
'add',
changelogPath
],
[
'git',
'commit',
'-m',
`Changelog update: ${new Date().toISOString()}`
]
], scriptsEnvs);
if (config.afterCommit.length > 0) {
console.log(chalk.cyanBright.bold('executing scripts after commmit...'));
executeScripts(config.afterCommit, scriptsEnvs);
}
console.log(chalk.greenBright.bold('committed'));
};
if (process.argv[2] === '-h' || process.argv[2] === '--help') {
console.log('\nUsage: git changelog [git commit options]\n');
console.log('Allowed types:');
for (const type in config.types)
console.log(` ${chalk.bold(type)}: ${config.types[type]}`);
process.exit(0);
} else if (process.argv[2] === '-i' || process.argv[2] === '--install') {
let path = '!' +process.argv[0];
executeScripts([`git config --global alias.changelog '${path}'`]);
process.exit(0);
}
run();