1 | #!/usr/bin/env node
|
2 |
|
3 | const inquirer = require('inquirer')
|
4 | const fs = require('fs')
|
5 | const path = require('path')
|
6 |
|
7 | const defaults = {
|
8 | project: 'lib',
|
9 | env: 'browser',
|
10 | typeSystem: 'none',
|
11 | react: true,
|
12 | relay: true
|
13 | }
|
14 |
|
15 | const packagePath = path.resolve(process.cwd(), 'package.json')
|
16 | if (fs.existsSync(packagePath)) {
|
17 | const packageJSON = JSON.parse(fs.readFileSync(packagePath, 'utf8'))
|
18 | defaults.project = packageJSON.private ? 'app' : 'lib'
|
19 |
|
20 | const dependencies = Object.keys(packageJSON.dependencies || {})
|
21 | const devDependencies = Object.keys(packageJSON.devDependencies || {})
|
22 |
|
23 | defaults.react = dependencies.includes('react') || devDependencies.includes('react')
|
24 | defaults.relay = dependencies.includes('relay') || devDependencies.includes('relay')
|
25 |
|
26 | if (dependencies.includes('flow-bin') || devDependencies.includes('flow-bin')) {
|
27 | defaults.typeSystem = 'flow'
|
28 | }
|
29 | if (dependencies.includes('typescript') || devDependencies.includes('typescript')) {
|
30 | defaults.typeSystem = 'typescript'
|
31 | }
|
32 | }
|
33 |
|
34 | const questions = [
|
35 | {
|
36 | type: 'list',
|
37 | name: 'project',
|
38 | message: 'Is this project a web app or reuseable library?',
|
39 | choices: ['app', 'lib'],
|
40 | default: defaults.project
|
41 | },
|
42 | {
|
43 | type: 'list',
|
44 | name: 'env',
|
45 | message: 'Which environment does this library target?',
|
46 | choices: ['browser', 'node'],
|
47 | default: defaults.env
|
48 | },
|
49 | {
|
50 | type: 'list',
|
51 | name: 'typeSystem',
|
52 | message: 'What type system are you using?',
|
53 | choices: ['flow', 'typescript', 'none'],
|
54 | default: defaults.typeSystem
|
55 | },
|
56 | {
|
57 | type: 'confirm',
|
58 | name: 'relay',
|
59 | message: 'Are you using Relay?',
|
60 | default: defaults.relay
|
61 | },
|
62 | {
|
63 | type: 'confirm',
|
64 | name: 'react',
|
65 | message: 'Are you using React?',
|
66 | default: defaults.react,
|
67 | when: answers => answers.env === 'browser'
|
68 | }
|
69 | ]
|
70 |
|
71 | inquirer.prompt(questions).then(answers => {
|
72 | const eslintrc = {extends: ['plugin:github/es6']}
|
73 |
|
74 | if (answers.env === 'node') {
|
75 | eslintrc.extends.push('plugin:github/node')
|
76 | } else if (answers.project === 'app') {
|
77 | eslintrc.extends.push('plugin:github/app')
|
78 | } else if (answers.env === 'browser') {
|
79 | eslintrc.extends.push('plugin:github/browser')
|
80 | }
|
81 |
|
82 | if (answers.typeSystem === 'flow') eslintrc.extends.push('plugin:github/flow')
|
83 | if (answers.typeSystem === 'typescript') {
|
84 | eslintrc.extends.push('plugin:github/typescript')
|
85 | eslintrc.parser = '@typescript-eslint/parser'
|
86 |
|
87 |
|
88 | const tsconfigPath = path.resolve(process.cwd(), 'tsconfig.json')
|
89 | if (!fs.existsSync(tsconfigPath)) {
|
90 | const tsconfigDefaults = {
|
91 | compilerOptions: {
|
92 | target: 'es2015',
|
93 | module: 'esnext',
|
94 | lib: ['esnext', 'dom'],
|
95 | allowSyntheticDefaultImports: true,
|
96 | moduleResolution: 'node'
|
97 | }
|
98 | }
|
99 | if (answers.react) {
|
100 | tsconfigDefaults.compilerOptions.jsx = 'react'
|
101 | }
|
102 | fs.writeFileSync(tsconfigPath, JSON.stringify(tsconfigDefaults, null, ' '), 'utf8')
|
103 | }
|
104 | }
|
105 |
|
106 | if (answers.react) eslintrc.extends.push('plugin:github/react')
|
107 | if (answers.relay) eslintrc.extends.push('plugin:github/relay')
|
108 |
|
109 | fs.writeFileSync(path.resolve(process.cwd(), '.eslintrc.json'), JSON.stringify(eslintrc, null, ' '), 'utf8')
|
110 |
|
111 | const prettierConfig = []
|
112 | if (answers.typeSystem === 'flow') prettierConfig.push('/* @flow */')
|
113 |
|
114 | prettierConfig.push("module.exports = require('eslint-plugin-github/prettier.config')")
|
115 | prettierConfig.push('')
|
116 |
|
117 | fs.writeFileSync(path.resolve(process.cwd(), 'prettier.config.js'), prettierConfig.join('\n'), 'utf8')
|
118 | })
|