/* eslint-env node */
'use strict';

var path = require('path');

var rules = requireUp('eslint-local-rules.js');

module.exports = {
  rules: rules,
};

// Attempt to require a file, recursively checking parent directories until found
// Similar to native `require` behavior, but doesn't check in `node_modules` folders
// Based on https://github.com/js-cli/node-findup-sync
function requireUp(filename, cwd) {
  cwd = cwd || process.cwd();
  var filepath = path.resolve(cwd, filename);

  try {
    return require(filepath);
  } catch(error) {
    // Ignore (will recurse to parent directory)
  }

  var dir = path.dirname(cwd);
  if (dir === cwd) {
    return undefined;
  }

  return requireUp(filename, dir);
}

/* eslint-env node */
'use strict';

var path = require('path');

var rules = requireUp('eslint-local-rules.js', __dirname);

if (!rules) {
  throw new Error(
    'eslint-plugin-local-rules: ' +
    'Cannot find "eslint-local-rules.js" ' +
    '(looking up from "' + __dirname + '").'
  );
}

module.exports = {
  rules: rules,
};

// Attempt to require a file, recursively checking parent directories until found
// Similar to native `require` behavior, but doesn't check in `node_modules` folders
// Based on https://github.com/js-cli/node-findup-sync
function requireUp(filename, cwd) {
  var filepath = path.resolve(cwd, filename);

  try {
    return require(filepath);
  } catch(error) {
    // Ignore (will recurse to parent directory)
  }

  var dir = path.dirname(cwd);
  if (dir === cwd) {
    return undefined;
  }

  return requireUp(filename, dir);
}

Fix VS Code integration when ESLint project outside VS Code project
Fix VS Code integration when ESLint project outside VS Code project

When an ESLint project is not in the root directory, e.g.

+ our-project
|--+ client
|  |--- src/
|  |--- package.json
|  |--- eslint-local-rules.js
|--+ server
|  |--- stuff

Some editor plugins does not `cd` into `client` directory when invoking
ESLint. Therefore `process.cwd()` is "our-project", which means `requireUp`
will not find our "eslint-local-rules.js".

This problem is fixed by making this plugin require from `__dirname` instead
of `process.cwd()`. Intuitively, the behavior of the plugin should not depend
on where it is run.

This commit also makes `eslint-plugin-local-rules` fail loudly if the rules
file is not found.