1 | #!/usr/bin/env node
|
2 |
|
3 | const fs = require('fs')
|
4 | const config = {
|
5 | plugins: [],
|
6 | }
|
7 | const kPluginName = Symbol('kPluginName')
|
8 |
|
9 | function loadConfig(extender) {
|
10 | if (extender.startsWith('plugin:')) {
|
11 | extender = extender.substr('plugin:'.length)
|
12 | const [pluginName, configName] = extender.split('/')
|
13 | const plugin = require(`eslint-plugin-${pluginName}`)
|
14 | const pluginConfig = plugin.configs[configName]
|
15 | pluginConfig[kPluginName] = pluginName
|
16 | console.log(`Loaded plugin: ${pluginName}`)
|
17 | return pluginConfig
|
18 | }
|
19 |
|
20 | return require(`eslint-config-${extender}`)
|
21 | }
|
22 |
|
23 | function deepAssign(target, source) {
|
24 | for (const key in source) {
|
25 | if (Reflect.has(source, key)) {
|
26 | if (key === 'plugins') {
|
27 | target[key] = target[key] || []
|
28 | target[key].push(...source[key])
|
29 | } else if (
|
30 | Array.isArray(source[key]) ||
|
31 | typeof source[key] !== 'object' ||
|
32 | !Reflect.has(target, key)
|
33 | ) {
|
34 | target[key] = source[key]
|
35 | } else {
|
36 | deepAssign(target[key], source[key])
|
37 | }
|
38 | }
|
39 | }
|
40 | }
|
41 |
|
42 | function extendConfig(extender) {
|
43 | const extended = (
|
44 | typeof extender === 'string' ?
|
45 | loadConfig(extender) :
|
46 | extender
|
47 | )
|
48 |
|
49 | if (extended[kPluginName] && !config.plugins.includes(extended[kPluginName])) {
|
50 | config.plugins.push(extended[kPluginName])
|
51 | }
|
52 |
|
53 | if (Reflect.has(extended, 'extends')) {
|
54 | for (const child of extended.extends) {
|
55 | console.log(`Extending via ${child} (asked for by ${extender})`)
|
56 | extendConfig(child)
|
57 | }
|
58 | }
|
59 |
|
60 | deepAssign(config, extended)
|
61 | }
|
62 |
|
63 | extendConfig(require('../.eslintrc.js'))
|
64 |
|
65 | config.envs = Object.keys(config.env)
|
66 | config.globals = Object.keys(config.globals)
|
67 | delete config.extends
|
68 | delete config.env
|
69 |
|
70 |
|
71 |
|
72 | config.plugins = [
|
73 | "import",
|
74 | "promise",
|
75 | "standard",
|
76 | "prettier",
|
77 | 'node',
|
78 | ]
|
79 |
|
80 | console.log('Writing: .eslintrc.dist.js')
|
81 | fs.writeFile(__dirname + '/../.eslintrc.dist.js', 'module.exports = ' + JSON.stringify(config, null, '\t'), err => {
|
82 | if (err) {
|
83 | console.error(err.stack)
|
84 | process.exit(1)
|
85 | }
|
86 | })
|