UNPKG

2.01 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const fs = require('fs')
4const config = {
5 plugins: [],
6}
7const kPluginName = Symbol('kPluginName')
8
9function 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
23function 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
42function 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
63extendConfig(require('../.eslintrc.js'))
64
65config.envs = Object.keys(config.env)
66config.globals = Object.keys(config.globals)
67delete config.extends
68delete config.env
69
70// TODO: This must be a smarter way to figure out
71// which plugins need to be here
72config.plugins = [
73 "import",
74 "promise",
75 "standard",
76 "prettier",
77 'node',
78]
79
80console.log('Writing: .eslintrc.dist.js')
81fs.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})