-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·68 lines (53 loc) · 1.96 KB
/
index.js
File metadata and controls
executable file
·68 lines (53 loc) · 1.96 KB
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
#!/usr/bin/env node
'use strict';
var fs = require('fs');
var execSync = require('child_process').execSync;
var program = require('commander');
var clc = require('cli-color');
var consoleError = clc.red.bold;
var commitMsgFile = process.argv[2] || './.git/COMMIT_EDITMSG';
var isNewCommit = (process.argv[3] === 'undefined');
var maxCharsLine = '#⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴⎴';
var scissorsLine = '# ---------- Message Formatting Convention ----------';
program
.option('-t, --template [file path]', 'Set file location of template for git commit message', '')
.parse(process.argv);
var msgTplFile = !program.template ? './default.tpl' : program.template;
try {
fs.accessSync(msgTplFile, fs.R_OK)
} catch (err) {
console.error(consoleError('Invalid path for template file: ' + msgTplFile));
process.exit(1);
}
var msgTpl = fs.readFileSync(msgTplFile).toString();
var doc = `
${scissorsLine}
${msgTpl}
`;
function getPrefilledMessage() {
if (!isNewCommit) {
return '';
}
var currentBranch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
return '(' + currentBranch + '): ';
}
function getMaxCharsLine() {
if (!isNewCommit) {
return '';
}
return '\n' + maxCharsLine;
}
function getContentWithDoc() {
if (content.indexOf(scissorsLine) !== -1) {
// git commit -v
content = content.replace(scissorsLine, doc + scissorsLine);
} else if (content.indexOf('#') !== -1) {
// other cases where there is a prompt to display doc
content = content + doc;
}
return content;
}
var content = fs.readFileSync(commitMsgFile).toString();
content = getPrefilledMessage() + getMaxCharsLine() + getContentWithDoc();
fs.writeFileSync(commitMsgFile, content);
process.exit(0);