Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
feat: add eslint (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
believer authored Oct 11, 2019
1 parent c46e31b commit 3d2d2a9
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 9 deletions.
21 changes: 16 additions & 5 deletions src/commands/add.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { nvmrc, gitignore, jest, prettierrc, config, husky } from '../tools'
import {
config,
eslint,
gitignore,
husky,
jest,
nvmrc,
prettierrc,
} from '../tools'
import { CLIFlags } from '../'

export type Command =
| 'config'
| 'gitignore'
| 'eslint'
| 'git'
| 'gitignore'
| 'husky'
| 'jest'
| 'nvm'
Expand All @@ -12,9 +22,7 @@ export type Command =

interface AddProps {
command: Command
flags: {
javascript: boolean
}
flags: CLIFlags
}

export const add = ({ command, flags }: AddProps) => {
Expand All @@ -39,6 +47,9 @@ export const add = ({ command, flags }: AddProps) => {
case 'husky':
husky()
break
case 'eslint':
eslint({ node: flags.node, react: flags.react })
break
default:
console.log(`${command} is not a valid command`)
}
Expand Down
11 changes: 10 additions & 1 deletion src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { config, gitignore, jest, nvmrc, prettierrc, husky } from '../tools'
import {
config,
gitignore,
jest,
nvmrc,
prettierrc,
husky,
eslint,
} from '../tools'
import { CLIFlags } from '../'

interface InitProps {
Expand All @@ -12,4 +20,5 @@ export const init = async ({ flags }: InitProps) => {
await jest()
await nvmrc()
await husky()
await eslint({ node: flags.node, react: flags.react })
}
16 changes: 13 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import { snippets, SnippetLanguage, SnippetIDE } from './commands/snippets'
import { graphql } from './commands/graphql'

export interface CLIFlags {
javascript: boolean
examples?: boolean
ide?: string
javascript: boolean
language?: string
examples?: boolean
node?: boolean
react?: boolean
}

export interface CLIProps {
Expand Down Expand Up @@ -70,12 +72,14 @@ const cli = meow(
--ide IDE for snippets (snippets)
--language Language for snippets (snippets)
--examples GraphQL examples (examples)
--node ESLint node (add/init)
--react ESLint react (add/init)
`,
{
flags: {
javascript: {
type: 'boolean',
default: false,
type: 'boolean',
},
ide: {
type: 'string',
Expand All @@ -86,6 +90,12 @@ const cli = meow(
examples: {
type: 'boolean',
},
react: {
type: 'boolean',
},
node: {
type: 'boolean',
},
},
}
)
Expand Down
6 changes: 6 additions & 0 deletions src/templates/eslint/eslintrc.node.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": [
"@iteam/eslint-config-node",
]
}

23 changes: 23 additions & 0 deletions src/templates/eslint/eslintrc.react.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"extends": ["@iteam/eslint-config-react"],
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"parserOptions": {
"ecmaFeatures": {
"modules": true
},
"ecmaVersion": 8,
"sourceType": "module"
},
"overrides": [
{
"files": ["**/*.tsx", "**/*.ts"],
"rules": {
"no-undef": "off",
"no-unused-vars": "off",
"react/prop-types": "off"
}
}
]
}

28 changes: 28 additions & 0 deletions src/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,31 @@ export const husky = async (options: ToolProps = {}) => {
output: options.cwd ? `${options.cwd}/.huskyrc` : '.huskyrc',
})
}

interface EslintFlags extends ToolProps {
node?: boolean
react?: boolean
}

export const eslint = async (options: EslintFlags = {}) => {
await installPkg('eslint', options)

if (options.node) {
await installPkg('@iteam/eslint-config-node', options)

await create({
templateName: 'eslint/eslintrc.node',
output: options.cwd ? `${options.cwd}/.eslintrc` : '.eslintrc',
})
}

if (options.react) {
await installPkg('@iteam/eslint-config-react', options)
await installPkg('@typescript-eslint/eslint-plugin', options)

await create({
templateName: 'eslint/eslintrc.react',
output: options.cwd ? `${options.cwd}/.eslintrc` : '.eslintrc',
})
}
}
7 changes: 7 additions & 0 deletions test/commands/add.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
prettierrc,
config,
husky,
eslint,
} from '../../src/tools'

jest.mock('../../src/tools')
Expand Down Expand Up @@ -62,6 +63,12 @@ test('husky - setup', () => {
expect(husky).toHaveBeenCalled()
})

test('eslint - setup', () => {
add({ command: 'eslint', flags: { node: true } })

expect(eslint).toHaveBeenCalledWith({ node: true })
})

test('handles unknown command', () => {
add({ command: '__not_valid__', flags: { javascript: false } })

Expand Down
7 changes: 7 additions & 0 deletions test/commands/init.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
prettierrc,
nvmrc,
husky,
eslint,
} from '../../src/tools'

jest.mock('../../src/tools')
Expand Down Expand Up @@ -56,4 +57,10 @@ describe('#init', () => {

expect(husky).toHaveBeenCalled()
})

test('should init eslint', async () => {
await init({ flags: { node: true } })

expect(eslint).toHaveBeenCalledWith({ node: true })
})
})
80 changes: 80 additions & 0 deletions test/tools/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
nvmrc,
config,
husky,
eslint,
} from '../../src/tools'
import {
create,
Expand Down Expand Up @@ -322,6 +323,7 @@ describe('#husky', () => {
output: '.huskyrc',
})
})

test('creates a config in a sub folder', async () => {
await husky({ cwd: 'test' })

Expand All @@ -331,3 +333,81 @@ describe('#husky', () => {
})
})
})

describe('#eslint', () => {
test('installs eslint', async () => {
await eslint({ cwd: 'test' })

expect(installPkg).toHaveBeenCalledWith('eslint', { cwd: 'test' })
})

describe('Node', () => {
test('installs eslint node config', async () => {
await eslint({ cwd: 'test', node: true })

expect(installPkg).toHaveBeenCalledWith('@iteam/eslint-config-node', {
cwd: 'test',
node: true,
})
})

test('creates a config', async () => {
await eslint({ node: true })

expect(create).toHaveBeenCalledWith({
templateName: 'eslint/eslintrc.node',
output: '.eslintrc',
})
})

test('creates a config in a sub folder', async () => {
await eslint({ cwd: 'test', node: true })

expect(create).toHaveBeenCalledWith({
templateName: 'eslint/eslintrc.node',
output: 'test/.eslintrc',
})
})
})

describe('React', () => {
test('installs eslint react config', async () => {
await eslint({ cwd: 'test', react: true })

expect(installPkg).toHaveBeenCalledWith('@iteam/eslint-config-react', {
cwd: 'test',
react: true,
})
})

test('installs typescript eslint plugin', async () => {
await eslint({ cwd: 'test', react: true })

expect(installPkg).toHaveBeenCalledWith(
'@typescript-eslint/eslint-plugin',
{
cwd: 'test',
react: true,
}
)
})

test('creates a config', async () => {
await eslint({ react: true })

expect(create).toHaveBeenCalledWith({
templateName: 'eslint/eslintrc.react',
output: '.eslintrc',
})
})

test('creates a config in a sub folder', async () => {
await eslint({ cwd: 'test', react: true })

expect(create).toHaveBeenCalledWith({
templateName: 'eslint/eslintrc.react',
output: 'test/.eslintrc',
})
})
})
})

0 comments on commit 3d2d2a9

Please sign in to comment.