1 | const path = require('path');
|
2 | const rcNodeBack = require('cli-rc');
|
3 | const denodeify = require('denodeify');
|
4 | const entries = require('core-js/fn/object/entries');
|
5 | const merge = require('lodash').merge;
|
6 |
|
7 | const rc = denodeify(rcNodeBack);
|
8 |
|
9 | const loaders = [
|
10 | {
|
11 | name: '.jsonlintrc',
|
12 | process: config => config
|
13 | },
|
14 | {
|
15 | name: '.jsonlintignore',
|
16 | type: 'ini',
|
17 | process: config => {
|
18 | return {
|
19 | ignore: entries(config)
|
20 | .filter(item => item[1])
|
21 | .map(item => item[0])
|
22 | };
|
23 | }
|
24 | }
|
25 | ];
|
26 |
|
27 | function getPaths(directory) {
|
28 | return directory
|
29 | .split(path.sep)
|
30 | .map((_, index) => {
|
31 | const args = [directory].concat(Array(index).fill('..'));
|
32 | return path.resolve.apply(null, args);
|
33 | });
|
34 | }
|
35 |
|
36 | function load(directory) {
|
37 | return Promise.all(
|
38 | loaders
|
39 | .map(load => {
|
40 | const paths = getPaths(directory);
|
41 | const loader = merge({}, load, {
|
42 | path: [directory],
|
43 | prepend: paths
|
44 | });
|
45 | return rc(loader)
|
46 | .then(result => {
|
47 | return result;
|
48 | })
|
49 | .then(loader.process);
|
50 | })
|
51 | ).then(configurations => {
|
52 | return configurations
|
53 | .reduce((registry, config) => {
|
54 | return merge({}, registry, config);
|
55 | }, {});
|
56 | });
|
57 | }
|
58 |
|
59 | module.exports = inputs => {
|
60 | return inputs
|
61 | .map(input => {
|
62 | input.configuration = load(input.directory);
|
63 | return input;
|
64 | });
|
65 | };
|