-
-
Notifications
You must be signed in to change notification settings - Fork 412
/
getConfig.js
148 lines (128 loc) Β· 4.22 KB
/
getConfig.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* eslint no-prototype-builtins: 0 */
'use strict'
const chalk = require('chalk')
const format = require('stringify-object')
const intersection = require('lodash/intersection')
const defaultsDeep = require('lodash/defaultsDeep')
const isObject = require('lodash/isObject')
const validate = require('jest-validate').validate
const logValidationWarning = require('jest-validate').logValidationWarning
const unknownOptionWarning = require('jest-validate/build/warnings').unknownOptionWarning
const isGlob = require('is-glob')
/**
* Default config object
*
* @type {{concurrent: boolean, chunkSize: number, globOptions: {matchBase: boolean, dot: boolean}, linters: {}, subTaskConcurrency: number, renderer: string, verbose: boolean}}
*/
const defaultConfig = {
concurrent: true,
chunkSize: Number.MAX_SAFE_INTEGER,
globOptions: {
matchBase: true,
dot: true
},
linters: {},
subTaskConcurrency: 1,
renderer: 'update',
verbose: false
}
/**
* Check if the config is "simple" i.e. doesn't contains any of full config keys
*
* @param config
* @returns {boolean}
*/
function isSimple(config) {
return (
isObject(config) &&
!config.hasOwnProperty('linters') &&
intersection(Object.keys(defaultConfig), Object.keys(config)).length === 0
)
}
/**
* Custom jest-validate reporter for unknown options
* @param config
* @param example
* @param option
* @param options
* @returns {void}
*/
function unknownValidationReporter(config, example, option, options) {
/**
* If the unkonwn property is a glob this is probably
* a typical mistake of mixing simple and advanced configs
*/
if (isGlob(option)) {
// prettier-ignore
const message = ` Unknown option ${chalk.bold(`"${option}"`)} with value ${chalk.bold(
format(config[option], { inlineCharacterLimit: Number.POSITIVE_INFINITY })
)} was found in the config root.
You are probably trying to mix simple and advanced config formats. Adding
${chalk.bold(`"linters": {
"${option}": ${JSON.stringify(config[option])}
}`)}
will fix it and remove this message.`
const comment = options.comment
const name = (options.title && options.title.warning) || 'WARNING'
return logValidationWarning(name, message, comment)
}
// If it is not glob pattern, use default jest-validate reporter
return unknownOptionWarning(config, example, option, options)
}
/**
* For a given configuration object that we retrive from .lintstagedrc or package.json
* construct a full configuration with all options set.
*
* This is a bit tricky since we support 2 different syntxes: simple and full
* For simple config, only the `linters` configuration is provided.
*
* @param {Object} sourceConfig
* @returns {{
* concurrent: boolean, chunkSize: number, globOptions: {matchBase: boolean, dot: boolean}, linters: {}, subTaskConcurrency: number, renderer: string, verbose: boolean
* }}
*/
function getConfig(sourceConfig) {
const config = defaultsDeep(
{}, // Do not mutate sourceConfig!!!
isSimple(sourceConfig) ? { linters: sourceConfig } : sourceConfig,
defaultConfig
)
// Check if renderer is set in sourceConfig and if not, set accordingly to verbose
if (isObject(sourceConfig) && !sourceConfig.hasOwnProperty('renderer')) {
config.renderer = config.verbose ? 'verbose' : 'update'
}
return config
}
/**
* Runs config validation. Throws error if the config is not valid.
* @param config {Object}
* @returns config {Object}
*/
function validateConfig(config) {
const exampleConfig = Object.assign({}, defaultConfig, {
linters: {
'*.js': ['eslint --fix', 'git add'],
'*.css': 'stylelint'
}
})
const deprecatedConfig = {
gitDir: () => ` Option ${chalk.bold('gitDir')} was removed.
lint-staged now automatically resolves '.git' directory.
Please remove ${chalk.bold('gitDir')} from your configuration.`
}
const validation = validate(config, {
exampleConfig,
deprecatedConfig,
unknown: unknownValidationReporter,
comment:
'Please refer to https://github.com/okonet/lint-staged#configuration for more information...'
})
if (!validation.isValid) {
throw new Error('lint-staged config is invalid... Aborting.')
}
return config
}
module.exports = {
getConfig,
validateConfig
}