UNPKG

2.73 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3/* eslint-disable import/no-dynamic-require */
4/* eslint-disable global-require */
5// Inspired by: https://github.com/commitizen/cz-conventional-changelog and https://github.com/commitizen/cz-cli
6
7const CZ_CONFIG_NAME = '.cz-config.js';
8const findConfig = require('find-config');
9const editor = require('editor');
10const temp = require('temp').track();
11const fs = require('fs');
12const path = require('path');
13const log = require('./logger');
14const buildCommit = require('./buildCommit');
15
16/* istanbul ignore next */
17const readConfigFile = () => {
18 // First try to find the .cz-config.js config file
19 const czConfig = findConfig.require(CZ_CONFIG_NAME, { home: false });
20
21 if (czConfig) {
22 return czConfig;
23 }
24
25 // fallback to locating it using the config block in the nearest package.json
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 // resolve relative to discovered package.json
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
48module.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 /* istanbul ignore else */
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};