1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | const CZ_CONFIG_NAME = '.cz-config.js';
|
8 | const findConfig = require('find-config');
|
9 | const editor = require('editor');
|
10 | const temp = require('temp').track();
|
11 | const fs = require('fs');
|
12 | const path = require('path');
|
13 | const log = require('./logger');
|
14 | const buildCommit = require('./buildCommit');
|
15 |
|
16 |
|
17 | const readConfigFile = () => {
|
18 |
|
19 | const czConfig = findConfig.require(CZ_CONFIG_NAME, { home: false });
|
20 |
|
21 | if (czConfig) {
|
22 | return czConfig;
|
23 | }
|
24 |
|
25 |
|
26 | let pkg = findConfig('package.json', { home: false });
|
27 | if (pkg) {
|
28 | const pkgDir = path.dirname(pkg);
|
29 |
|
30 | pkg = require(pkg);
|
31 |
|
32 | if (pkg.config && pkg.config['cz-customizable'] && pkg.config['cz-customizable'].config) {
|
33 |
|
34 | const pkgPath = path.resolve(pkgDir, pkg.config['cz-customizable'].config);
|
35 |
|
36 | log.info('>>> Using cz-customizable config specified in your package.json: ', pkgPath);
|
37 |
|
38 | return require(pkgPath);
|
39 | }
|
40 | }
|
41 |
|
42 | log.warn(
|
43 | 'Unable to find a configuration file. Please refer to documentation to learn how to ser up: https://github.com/leonardoanalista/cz-customizable#steps "'
|
44 | );
|
45 | return null;
|
46 | };
|
47 |
|
48 | module.exports = {
|
49 | prompter(cz, commit) {
|
50 | const config = readConfigFile();
|
51 | config.subjectLimit = config.subjectLimit || 100;
|
52 | log.info('All lines except first will be wrapped after 100 characters.');
|
53 |
|
54 | const questions = require('./questions').getQuestions(config, cz);
|
55 |
|
56 | cz.prompt(questions).then(answers => {
|
57 | if (answers.confirmCommit === 'edit') {
|
58 | temp.open(null, (err, info) => {
|
59 |
|
60 | if (!err) {
|
61 | fs.writeSync(info.fd, buildCommit(answers, config));
|
62 | fs.close(info.fd, () => {
|
63 | editor(info.path, code => {
|
64 | if (code === 0) {
|
65 | const commitStr = fs.readFileSync(info.path, {
|
66 | encoding: 'utf8',
|
67 | });
|
68 | commit(commitStr);
|
69 | } else {
|
70 | log.info(`Editor returned non zero value. Commit message was:\n${buildCommit(answers, config)}`);
|
71 | }
|
72 | });
|
73 | });
|
74 | }
|
75 | });
|
76 | } else if (answers.confirmCommit === 'yes') {
|
77 | commit(buildCommit(answers, config));
|
78 | } else {
|
79 | log.info('Commit has been canceled.');
|
80 | }
|
81 | });
|
82 | },
|
83 | };
|