diff --git a/.eslintrc.js b/.eslintrc.js index 6babd7b..1225b47 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -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, @@ -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'], } diff --git a/packages/cli/src/commands/testing.ts b/packages/cli/src/commands/testing.ts index bcba6c6..11bdffb 100644 --- a/packages/cli/src/commands/testing.ts +++ b/packages/cli/src/commands/testing.ts @@ -4,42 +4,71 @@ import { Package } from '@siroc/core' import { bold, gray } from 'chalk' import consola from 'consola' -interface RunCommandOptions { - packages: string[] - options?: Record +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 +} +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) } } diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 0c04d3d..b17d2d6 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -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() diff --git a/packages/eslint-config/.eslintrc.js b/packages/eslint-config/.eslintrc.js new file mode 100644 index 0000000..0f1d4d3 --- /dev/null +++ b/packages/eslint-config/.eslintrc.js @@ -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', +} diff --git a/packages/eslint-config/index.js b/packages/eslint-config/index.js new file mode 100644 index 0000000..8b402a4 --- /dev/null +++ b/packages/eslint-config/index.js @@ -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', + ], +} diff --git a/packages/eslint-config/package.json b/packages/eslint-config/package.json new file mode 100644 index 0000000..107aa57 --- /dev/null +++ b/packages/eslint-config/package.json @@ -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" + } +} diff --git a/packages/eslint-config/siroc.config.js b/packages/eslint-config/siroc.config.js new file mode 100644 index 0000000..b429509 --- /dev/null +++ b/packages/eslint-config/siroc.config.js @@ -0,0 +1,3 @@ +export default { + build: false, +}