UNPKG

1.27 kBJavaScriptView Raw
1/* eslint-disable import/no-dynamic-require */
2/* eslint-disable global-require */
3
4const CZ_CONFIG_NAME = '.cz-config.js';
5const findConfig = require('find-config');
6const path = require('path');
7const log = require('./logger');
8
9// TODO: write unit tests
10const readConfigFile = () => {
11 // First try to find the .cz-config.js config file
12 const czConfig = findConfig.require(CZ_CONFIG_NAME, { home: false });
13
14 if (czConfig) {
15 return czConfig;
16 }
17
18 // fallback to locating it using the config block in the nearest package.json
19 let pkg = findConfig('package.json', { home: false });
20 if (pkg) {
21 const pkgDir = path.dirname(pkg);
22
23 pkg = require(pkg);
24
25 if (pkg.config && pkg.config['cz-customizable'] && pkg.config['cz-customizable'].config) {
26 // resolve relative to discovered package.json
27 const pkgPath = path.resolve(pkgDir, pkg.config['cz-customizable'].config);
28
29 log.info('>>> Using cz-customizable config specified in your package.json: ', pkgPath);
30
31 return require(pkgPath);
32 }
33 }
34
35 log.warn(
36 'Unable to find a configuration file. Please refer to documentation to learn how to set up: https://github.com/leonardoanalista/cz-customizable#steps "'
37 );
38 return null;
39};
40
41module.exports = readConfigFile;