Skip to content

Commit

Permalink
ESLint task refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartek Igielski authored and Igloczek committed Nov 7, 2021
1 parent cd1aab0 commit ebcc043
Show file tree
Hide file tree
Showing 6 changed files with 11,011 additions and 13,125 deletions.
108 changes: 78 additions & 30 deletions helpers/eslint.mjs
Original file line number Diff line number Diff line change
@@ -1,34 +1,82 @@
import path from 'path'
import gulp from 'gulp'
import { globbySync } from 'globby'
import eslint from 'gulp-eslint'
import plumber from 'gulp-plumber'
import gulpIf from 'gulp-if'
import notify from 'gulp-notify'
import logger from 'gulp-logger'

import { env, themes, projectPath } from './config.mjs'
import configLoader from './config-loader.mjs'

export default (name, file) => {
import fs from 'fs-extra'
import { globby } from 'globby'
import { ESLint } from 'eslint'
import progress from 'progress'
import log from 'fancy-log'
import colors from 'ansi-colors'

import configLoader from '../helpers/config-loader.mjs'
import { env, themes, projectPath } from '../helpers/config.mjs'

export default async (name, file) => {
const theme = themes[name]
const srcBase = path.join(projectPath, theme.dest)
const eslintConfig = configLoader('eslint.json')
const files = globbySync(srcBase + '/**/*.js')

return gulp.src(file ? file : files.length ? files : '.')
.pipe(gulpIf(
!env.ci,
plumber({
errorHandler: notify.onError('Error: <%= error.message %>')
const themePath = path.resolve(projectPath, theme.src) + '/'

configLoader('.browserslistrc')

const eslint = new ESLint({
cwd: themePath,
baseConfig: configLoader('.eslintrc.json'),
resolvePluginsRelativeTo: process.cwd(),
useEslintrc: false,
fix: env.fix
})

const formatter = await eslint.loadFormatter('stylish')

const paths = []

if (file) {
paths.push(file)
}
else {
paths.push('**/*.js', '!**/node_modules/**')

if (await fs.pathExists(themePath + '.eslintignore')) {
const ignore = await fs.readFile(themePath + '.eslintignore', 'utf8')
ignore.split('\n').forEach(path => {
if (path) {
paths.push('!' + path)
}
})
))
.pipe(eslint(eslintConfig))
.pipe(eslint.format())
.pipe(eslint.failAfterError())
.pipe(logger({
display : 'name',
beforeEach: 'Theme: ' + name + ' ' + 'File: ',
afterEach : ' - ESLint finished.'
}))
}
}

const files = await globby(paths, { absolute: true, cwd: themePath })

const progressBar = new progress(':percent | ETA: :etas | :current/:total | [:bar]', {
total: files.length,
clear: true
})

const resultText = []

await Promise.all(files.map(async filePath => {
const code = await fs.readFile(filePath, 'utf8')
const results = await eslint.lintText(code, { filePath })

progressBar.tick()

if (!results[0].messages.length) {
return
}

if (env.fix) {
await ESLint.outputFixes(results)
}

resultText.push(formatter.format(results))
}))

if (resultText.length) {
log(resultText.join('\n'))

if (env.ci) {
process.exit(1)
}
}
else {
log(colors.green('ESLint found no problems!'))
}
}
Loading

0 comments on commit ebcc043

Please sign in to comment.