Skip to content

Commit

Permalink
feat(cli, eslint-config): add support for eslint command
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jun 15, 2020
1 parent e2b0c6a commit 7cdc0b1
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 50 deletions.
21 changes: 1 addition & 20 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
module.exports = {
root: true,
env: {
browser: false,
es6: true,
node: true,
'jest/globals': true,
},
parser: '@typescript-eslint/parser',
parserOptions: {
sourceType: 'module',
},
plugins: ['jest', '@typescript-eslint'],
rules: {
'prettier/prettier': [1, require('./prettier.config.js')],
'@typescript-eslint/no-inferrable-types': 1,
Expand All @@ -19,13 +8,5 @@ module.exports = {
'jest/valid-describe': 0,
'no-dupe-class-members': 0,
},
extends: [
'plugin:promise/recommended',
'plugin:@typescript-eslint/recommended',
'eslint:recommended',
'plugin:prettier/recommended',
'prettier',
'prettier/@typescript-eslint',
'plugin:jest/recommended',
],
extends: ['plugin:promise/recommended', '@siroc'],
}
81 changes: 55 additions & 26 deletions packages/cli/src/commands/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,71 @@ import { Package } from '@siroc/core'
import { bold, gray } from 'chalk'
import consola from 'consola'

interface RunCommandOptions {
packages: string[]
options?: Record<string, any>
const runJest = (pkg: Package) => {
let jestConfig
try {
jestConfig = join(
require.resolve('@siroc/jest-preset'),
'../jest.config.js'
)
const { stdout } = pkg.execInteractive(
'yarn',
`jest --passWithNoTests -c ${jestConfig}`
)
if (stdout) stdout.pipe(process.stdout)
} catch (e) {
if (!jestConfig) {
consola.error(`Couldn't resolve jest config.\n`, gray(e))
} else {
consola.error(`Error running ${bold('jest')} -c ${jestConfig}\n`, gray(e))
}
}
}

export async function test({ packages }: RunCommandOptions) {
function runCommand(pkg: Package) {
let jestConfig
try {
jestConfig = join(
require.resolve('@siroc/jest-preset'),
'../jest.config.js'
)
const { stdout } = pkg.execInteractive(
'yarn',
`jest --passWithNoTests -c ${jestConfig}`
const runEslint = (pkg: Package) => {
let eslintConfig
try {
eslintConfig = join(
require.resolve('@siroc/eslint-config'),
'../.eslintrc.js'
)
const { stdout } = pkg.execInteractive(
'yarn',
`eslint -c ${eslintConfig} --ext .js,.ts .`
)
if (stdout) stdout.pipe(process.stdout)
} catch (e) {
if (!eslintConfig) {
consola.error(`Couldn't resolve eslint config.\n`, gray(e))
} else {
consola.error(
`Error running ${bold('eslint')} -c ${eslintConfig}\n`,
gray(e)
)
if (stdout) stdout.pipe(process.stdout)
} catch (e) {
if (!jestConfig) {
consola.error(`Couldn't resolve jest config.\n`, gray(e))
} else {
consola.error(
`Error running ${bold('jest')} -c ${jestConfig}\n`,
gray(e)
)
}
}
}
}

const commands = {
jest: runJest,
eslint: runEslint,
} as const

export type Command = keyof typeof commands

interface CommandOptions {
command: Command
packages: string[]
options?: Record<string, any>
}

export async function test({ packages, command }: CommandOptions) {
const rootPackage = new Package()

if (packages.length) {
const workspacePackages = await rootPackage.getWorkspacePackages(packages)
workspacePackages.forEach(async pkg => runCommand(pkg))
workspacePackages.forEach(async pkg => commands[command](pkg))
} else {
runCommand(rootPackage)
commands[command](rootPackage)
}
}
16 changes: 12 additions & 4 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,18 @@ cli
)

cli
.command('test [...packages]', 'Run jest ')
.example(bin => ` ${bin} test`)
.example(bin => ` ${bin} test @siroc/cli`)
.action((packages, options) => run('testing', test, { packages, options }))
.command('jest [...packages]', 'Run jest ')
.example(bin => ` ${bin} jest`)
.example(bin => ` ${bin} jest @siroc/cli`)
.action(packages => run('starting jest', test, { packages, command: 'jest' }))

cli
.command('eslint [...packages]', 'Run eslint ')
.example(bin => ` ${bin} eslint`)
.example(bin => ` ${bin} eslint @siroc/cli`)
.action(packages =>
run('starting eslint', test, { packages, command: 'eslint' })
)

cli.version(version)
cli.help()
Expand Down
14 changes: 14 additions & 0 deletions packages/eslint-config/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// eslint-disable-next-line
const { join } = require('path')

let eslintConfig = {}
const eslintConfigPath = join(process.cwd(), '.eslintrc.js')

try {
eslintConfig = require(eslintConfigPath)
// eslint-disable-next-line
} catch {}

module.exports = eslintConfig || {
extends: '@siroc',
}
21 changes: 21 additions & 0 deletions packages/eslint-config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
env: {
browser: false,
es6: true,
node: true,
'jest/globals': true,
},
parser: '@typescript-eslint/parser',
parserOptions: {
sourceType: 'module',
},
plugins: ['jest', '@typescript-eslint'],
extends: [
'plugin:@typescript-eslint/recommended',
'eslint:recommended',
'plugin:prettier/recommended',
'prettier',
'prettier/@typescript-eslint',
'plugin:jest/recommended',
],
}
28 changes: 28 additions & 0 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@siroc/eslint-config",
"version": "0.0.1",
"description": "Zero-config build tooling for Node",
"keywords": [
"eslint",
"eslintconfig",
"node",
"nodejs",
"typescript",
"javascript"
],
"repository": "nuxt-contrib/siroc",
"license": "MIT",
"sideEffects": false,
"main": "index.js",
"files": [
"index.js",
".eslintrc.js"
],
"dependencies": {
"eslint": "^7.2.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-jest": "^23.13.2",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-promise": "^4.2.1"
}
}
3 changes: 3 additions & 0 deletions packages/eslint-config/siroc.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
build: false,
}

0 comments on commit 7cdc0b1

Please sign in to comment.