This repository has been archived by the owner on Jun 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheckrules.js
executable file
·250 lines (214 loc) · 5.91 KB
/
checkrules.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env node
/**
* checkrules.js: Lookup ESLint github repository for new rules.
*
* Copyright 2016, 2017 Sudaraka Wijesinghe <[email protected]>
*
* This program comes with ABSOLUTELY NO WARRANTY;
* This is free software, and you are welcome to redistribute it and/or modify
* it under the terms of the BSD 2-clause License. See the LICENSE file for more
* details.
*
*/
const
rc = require('rc'),
axios = require('axios'),
chalk = require('chalk'),
yargs = require('yargs'),
npm = rc('npm', { 'registry': 'https://registry.npmjs.org/' }),
{ argv } = yargs
.usage('USAGE: ./$0 [-t]')
.option(
'tags',
{
'alias': 't',
'boolean': true,
'default': false,
'describe': 'Process configured version tags'
}
)
.help(),
processReactPlugin = data => {
const
objectString = (data.match(/.*allRules\s*=\s*([^;]+)/)[1] || '{}')
.replace(/'/g, '"')
.replace(/require\(/g, '')
.replace(/\)/g, '')
try {
return Object.keys(JSON.parse(objectString))
}
catch(_) {
return []
}
},
RULE_SOURCES = [
{
'package': 'eslint',
'url': '/lib/rules/?meta',
'docs': {
'latest': 'http://eslint.org/docs/rules/{rule}',
'next': 'https://github.com/eslint/eslint/blob/master/docs/rules/{rule}.md'
},
'file': 'default.js',
'tags': [
'latest',
'next'
]
},
{
'package': 'eslint-plugin-react',
'docs': 'https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/{rule}.md',
'file': 'react.js',
'namespace': 'react/',
'prefixes': [ 'jsx' ],
'processRemoteData': processReactPlugin
}
],
readLocalRules = ruleSource => {
let
localRules = []
try {
const
config = require(`./${ruleSource.file}`) // eslint-disable-line global-require
localRules = Object.keys(config.rules)
.filter(r => !ruleSource.namespace || null !== r.match(ruleSource.namespace))
}
catch(_) {
// noop
}
return Object.assign({}, ruleSource, { localRules })
},
processCore = ({ files }, ruleSource) => {
if(!Array.isArray(files)) {
const
err = new Error('Remote location does not seem to contain valid rule set')
err.ruleSource = ruleSource
throw err
}
return files
.filter(f => 'application/javascript' === f.contentType)
.map(f => f.path.split('/').pop().replace(/\.js$/, ''))
},
loadRemoteRules = rulePromise => rulePromise.then(
ruleSource => axios
.get(ruleSource.url)
.then(({ data }) => {
const
remoteRules = 'function' === typeof ruleSource.processRemoteData
? ruleSource.processRemoteData(data)
: processCore(data, ruleSource)
return Object.assign({ 'prefixes': [] }, ruleSource, { remoteRules })
})
),
applyPackageInfo = rulePromise => rulePromise.then(
ruleSource => axios
.get(`${npm.registry}${ruleSource.package}`)
.then(result => {
const
{ 'data': pkg } = result,
name = `${chalk.white.bold(pkg.name)} ${chalk.magenta(`v${pkg['dist-tags'][ruleSource.tag]}`)}${chalk.dim(` - ${pkg.description}`)}`,
url = `https://unpkg.com/${pkg.name}@${pkg['dist-tags'][ruleSource.tag]}${ruleSource.url || ''}`
return Object.assign({}, ruleSource, {
name,
url
})
})
),
findNewRules = ruleSource => {
const
newRules = ruleSource.remoteRules
.filter(r => {
if(ruleSource.localRules.includes(`${ruleSource.namespace || ''}${r}`)) {
return false
}
return !ruleSource.prefixes
.some(prefix => ruleSource.localRules.includes(`${ruleSource.namespace || ''}${prefix}-${r}`))
})
.map(r => ` ${chalk.green('+')} \
${chalk.cyan(ruleSource.docs.replace(/{rule}/, chalk.bold(r)))}`)
return Object.assign({}, ruleSource, { newRules })
},
findRemovedRules = ruleSource => {
const
removedRules = ruleSource.localRules
.filter(r => !ruleSource.remoteRules.includes(r.replace(ruleSource.namespace || '', '')))
.map(r => ` ${chalk.red.dim('-')} ${chalk.gray(r)}`)
return Object.assign({}, ruleSource, { removedRules })
},
formatSource = ruleSource => {
const
rules = [
...ruleSource.newRules,
...ruleSource.removedRules
],
name = [
'',
chalk.bold(0 < rules.length ? chalk.yellow('!') : chalk.green('✓')),
ruleSource.name
]
return [
name.join(' '),
rules.sort().join('\n'),
''
].join('\n').replace(/\n\n/, '\n')
},
formatError = ({ message, ruleSource }) => {
const
name = [
'',
chalk.bold.red('⨯'),
ruleSource.name
]
return [
name.join(' '),
` ${message}`,
''
].join('\n').replace(/\n\n/, '\n')
},
expandTags = tagsWanted => (list, ruleSource) => {
let
{ tags } = tagsWanted ? ruleSource : {}
if(!Array.isArray(tags)) {
tags = [ 'latest' ]
}
const
source = tags.map(
tag => Object.assign({}, ruleSource, {
'tags': null,
tag
})
)
return [
...list,
...source
]
},
selectDocsUrl = ruleSource => {
if('object' !== typeof ruleSource.docs) {
return ruleSource
}
return {
...ruleSource,
'docs': ruleSource.docs[ruleSource.tag]
}
}
console.log('')
RULE_SOURCES
.reduce(expandTags(argv.tags), [])
.map(selectDocsUrl)
.map(s => Promise.resolve(s))
.map(p => p.then(readLocalRules))
.map(applyPackageInfo)
.map(loadRemoteRules)
.map(p => p.then(findNewRules))
.map(p => p.then(findRemovedRules))
.map(
p => p
.then(formatSource)
.catch(formatError)
)
.map(
s => s
.then(console.log)
.catch(console.error)
)