-
Notifications
You must be signed in to change notification settings - Fork 51
/
eslint-github-init.js
executable file
·88 lines (75 loc) · 2.53 KB
/
eslint-github-init.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
#!/usr/bin/env node
const inquirer = require('inquirer')
const fs = require('fs')
const path = require('path')
const defaults = {
project: 'lib',
env: 'browser',
typeSystem: 'none'
}
const packagePath = path.resolve(process.cwd(), 'package.json')
if (fs.existsSync(packagePath)) {
const packageJSON = JSON.parse(fs.readFileSync(packagePath, 'utf8'))
defaults.project = packageJSON.private ? 'app' : 'lib'
const dependencies = Object.keys(packageJSON.dependencies || {})
const devDependencies = Object.keys(packageJSON.devDependencies || {})
if (dependencies.includes('typescript') || devDependencies.includes('typescript')) {
defaults.typeSystem = 'typescript'
}
}
const questions = [
{
type: 'list',
name: 'project',
message: 'Is this project a web app or reuseable library?',
choices: ['app', 'lib'],
default: defaults.project
},
{
type: 'list',
name: 'env',
message: 'Which environment does this library target?',
choices: ['browser', 'node'],
default: defaults.env
},
{
type: 'list',
name: 'typeSystem',
message: 'What type system are you using?',
choices: ['typescript', 'none'],
default: defaults.typeSystem
}
]
inquirer.prompt(questions).then(answers => {
const eslintrc = {extends: ['plugin:github/es6']}
if (answers.env === 'node') {
eslintrc.extends.push('plugin:github/node')
} else if (answers.project === 'app') {
eslintrc.extends.push('plugin:github/app')
} else if (answers.env === 'browser') {
eslintrc.extends.push('plugin:github/browser')
}
if (answers.typeSystem === 'typescript') {
eslintrc.extends.push('plugin:github/typescript')
eslintrc.parser = '@typescript-eslint/parser'
// Create a `tsconfig.json`.
const tsconfigPath = path.resolve(process.cwd(), 'tsconfig.json')
if (!fs.existsSync(tsconfigPath)) {
const tsconfigDefaults = {
compilerOptions: {
target: 'es2015',
module: 'esnext',
lib: ['esnext', 'dom'],
allowSyntheticDefaultImports: true,
moduleResolution: 'node'
}
}
fs.writeFileSync(tsconfigPath, JSON.stringify(tsconfigDefaults, null, ' '), 'utf8')
}
}
fs.writeFileSync(path.resolve(process.cwd(), '.eslintrc.json'), JSON.stringify(eslintrc, null, ' '), 'utf8')
const prettierConfig = []
prettierConfig.push("module.exports = require('eslint-plugin-github/prettier.config')")
prettierConfig.push('')
fs.writeFileSync(path.resolve(process.cwd(), 'prettier.config.js'), prettierConfig.join('\n'), 'utf8')
})