/
setup-eslint-rule-types.js
52 lines (45 loc) · 1.15 KB
/
setup-eslint-rule-types.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
/**
* @author Yosuke Ota
* See LICENSE file in root directory for full license.
*/
'use strict'
const fs = require('fs')
const path = require('path')
const eslint = require('eslint')
const ruleNames = new Set(new eslint.Linter().getRules().keys())
const TYPINGS_ESLINT_RULES_ROOT = path.resolve(
__dirname,
'../typings/eslint/lib/rules'
)
mkdirIfNotExists(TYPINGS_ESLINT_RULES_ROOT)
for (const ruleName of ruleNames) {
const filePath = path.join(TYPINGS_ESLINT_RULES_ROOT, `${ruleName}.d.ts`)
fs.writeFileSync(
filePath,
`import { Rule } from '../../index'
declare const rule: Rule.RuleModule
export = rule
`
)
}
fs.readdirSync(TYPINGS_ESLINT_RULES_ROOT)
.filter((file) => file.endsWith('.d.ts'))
.filter((file) => !ruleNames.has(file.slice(0, -5)))
.map((file) => path.join(TYPINGS_ESLINT_RULES_ROOT, file))
.forEach((filePath) => fs.unlinkSync(filePath))
function exists(file) {
try {
fs.statSync(file)
return true
} catch (err) {
if (err.code === 'ENOENT') return false
}
}
function mkdirIfNotExists(dir) {
if (exists(dir)) {
return
}
const p = path.dirname(dir)
mkdirIfNotExists(p)
fs.mkdirSync(dir)
}