package com.freeleaps.devops.lint

import com.freeleaps.devops.enums.CodeLinterTypes
import com.freeleaps.devops.lint.LinterBase

class ESLint extends LinterBase {
  def eslintVersion
  def plugins

  ESLint(steps, workspace, configs, eslintVersion, plugins) {
    super(steps, workspace, configs, CodeLinterTypes.ESLINT)
    this.eslintVersion = eslintVersion
    this.plugins = plugins
  }

  def doLint() {
    steps.dir(workspace) {
      steps.log.info("${linterType.linter}", "Install eslint with version: ${eslintVersion}")
      steps.sh("npm install -g eslint@${eslintVersion}")
      steps.log.info("${linterType.linter}", "Install eslint dependencies...")
      steps.sh("npm install -g ${plugins.join(' ')}")
      // check if given config file is not end with .json
      if (!configs.endsWith('.js')) {
        steps.log.warn("${linterType.linter}", "Invalid eslint configuration file, must be a JS file, convert file as valid JS file...")
        steps.sh("mv ${configs} ${configs}.js")
        configs = "${configs}.js"
      }
      steps.log.info("${linterType.linter}", "Running eslint...")
      steps.sh("eslint --config ${configs} ${workspace}")
    }
  }
}