forked from leoforfree/cz-customizable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
79 lines (64 loc) · 2.64 KB
/
index.js
File metadata and controls
79 lines (64 loc) · 2.64 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
69
70
71
72
73
74
75
76
77
78
79
'use strict';
// Inspired by: https://github.com/commitizen/cz-conventional-changelog and https://github.com/commitizen/cz-cli
var CZ_CONFIG_NAME = '.cz-config.js';
var CZ_CONFIG_EXAMPLE_LOCATION = './cz-config-EXAMPLE.js';
var findConfig = require('find-config');
var log = require('winston');
var editor = require('editor');
var temp = require('temp').track();
var fs = require('fs');
var path = require('path');
var buildCommit = require('./buildCommit');
/* istanbul ignore next */
function readConfigFile() {
// First try to find the .cz-config.js config file
var czConfig = findConfig.require(CZ_CONFIG_NAME, {home: false});
if (czConfig) {
return czConfig;
}
// fallback to locating it using the config block in the nearest package.json
var pkg = findConfig('package.json', {home: false});
if (pkg) {
var pkgDir = path.dirname(pkg);
pkg = require(pkg);
if (pkg.config && pkg.config['cz-customizable'] && pkg.config['cz-customizable'].config) {
// resolve relative to discovered package.json
var pkgPath = path.resolve(pkgDir, pkg.config['cz-customizable'].config);
console.info('>>> Using cz-customizable config specified in your package.json: ', pkgPath);
return require(pkgPath);
}
}
log.warn('Unable to find a configuration file. Please refer to documentation to learn how to ser up: https://github.com/leonardoanalista/cz-customizable#steps "');
}
module.exports = {
prompter: function(cz, commit) {
var config = readConfigFile();
var subjectLimit = config.subjectLimit || 100;
log.info('\n\nLine 1 will be cropped at ' + subjectLimit + ' characters. All other lines will be wrapped after 100 characters.\n');
var questions = require('./questions').getQuestions(config, cz);
cz.prompt(questions).then(function(answers) {
if (answers.confirmCommit === 'edit') {
temp.open(null, function(err, info) {
/* istanbul ignore else */
if (!err) {
fs.writeSync(info.fd, buildCommit(answers, config));
fs.close(info.fd, function(err) {
editor(info.path, function (code, sig) {
if (code === 0) {
var commitStr = fs.readFileSync(info.path, { encoding: 'utf8' });
commit(commitStr);
} else {
log.info('Editor returned non zero value. Commit message was:\n' + buildCommit(answers, config));
}
});
});
}
});
} else if (answers.confirmCommit === 'yes') {
commit(buildCommit(answers, config));
} else {
log.info('Commit has been canceled.');
}
});
}
};