'use strict';

const assert = require('assert');
const path = require('path');
const fse = require('fs-extra');

module.exports = dir => {
  if (!fse.existsSync(dir)) return {};

  const root = {};
  const paths = fse.readdirSync(dir, { withFileTypes: true }).filter(fd => fd.isFile());

  for (let fd of paths) {
    const { name } = fd;
    const fullPath = dir + path.sep + name;

    const ext = path.extname(name);
    const key = path.basename(name, ext);
    root[key] = loadPolicy(fullPath);
  }

  return root;
};

const loadPolicy = file => {
  try {
    const policy = require(file);

    assert(typeof policy === 'function', 'Policy must be a function.');

    return policy;
  } catch (error) {
    throw `Could not load policy ${file}: ${error.message}`;
  }
};

'use strict';

const assert = require('assert');
const _ = require('lodash');
const path = require('path');
const fse = require('fs-extra');

module.exports = dir => {
  if (!fse.existsSync(dir)) return {};

  const root = {};
  const paths = fse.readdirSync(dir, { withFileTypes: true }).filter(fd => fd.isFile());

  for (let fd of paths) {
    const { name } = fd;
    const fullPath = dir + path.sep + name;

    const ext = path.extname(name);
    const key = path.basename(name, ext);
    root[_.toLower(key)] = loadPolicy(fullPath);
  }

  return root;
};

const loadPolicy = file => {
  try {
    const policy = require(file);

    assert(typeof policy === 'function', 'Policy must be a function.');

    return policy;
  } catch (error) {
    throw `Could not load policy ${file}: ${error.message}`;
  }
};

Fix global policies case sensitive
Fix global policies case sensitive

Signed-off-by: Alexandre Bodin <70f0c7aa1e44ecfca5832512bee3743e3471816f@gmail.com>
