'use strict';

const path = require('path');
const consola = require('consola');
const defu = require('defu');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

const consola__default = /*#__PURE__*/_interopDefaultLegacy(consola);

var name = "@nuxtjs/eslint-module";
var version = "3.1.0";

function moduleExists(name) {
  try {
    return require.resolve(name);
  } catch (e) {
    return false;
  }
}

const logger = consola__default["default"].withTag("nuxt:eslint");
const CONFIG_KEY = "eslint";
const nuxtModule = function(moduleOptions) {
  if (!this.options.dev) {
    return;
  }
  const DEFAULTS = {
    context: this.options.srcDir,
    eslintPath: "eslint",
    extensions: ["js", "ts", "vue"],
    cache: true,
    lintDirtyModulesOnly: true
  };
  const options = defu.defu(this.options[CONFIG_KEY], moduleOptions, DEFAULTS);
  if (!moduleExists(options.eslintPath)) {
    logger.warn(`The dependency \`${options.eslintPath}\` not found.`, "Please run `yarn add eslint --dev` or `npm install eslint --save-dev`");
    return;
  }
  const filesToWatch = [
    ".eslintrc",
    ".eslintrc.json",
    ".eslintrc.yaml",
    ".eslintrc.yml",
    ".eslintrc.js"
  ];
  this.options.watch = this.options.watch || [];
  this.options.watch.push(...filesToWatch.map((file) => path.resolve(this.options.rootDir, file)));
  this.extendBuild((config, { isDev, isClient }) => {
    if (isDev && isClient) {
      const EslintPlugin = require("eslint-webpack-plugin");
      config.plugins.push(new EslintPlugin(options));
    }
  });
};
nuxtModule.meta = { name, version };

module.exports = nuxtModule;
