All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.configs.eslint.helpers.index.js Maven / Gradle / Ivy

Go to download

A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.

There is a newer version: 4.12.3
Show newest version
const fs = require("fs");
const path = require("path");

const { mergeDeepLeft, mergeLeft, keys } = require("ramda");

const loadJS = jsPath => {
  try {
    return require(jsPath);
  } catch {
    return {};
  }
};

const doesFileExist = filePath =>
  fs.existsSync(filePath) && fs.lstatSync(filePath).isFile();

const generateAllPathsInsideDir = dirPath => {
  const files = fs.readdirSync(dirPath, { withFileTypes: true });

  return files.flatMap(file => {
    const filePath = path.join(dirPath, file.name);

    return file.isDirectory() ? generateAllPathsInsideDir(filePath) : filePath;
  });
};

const generatePathGroupsFromAssets = assetsPath => {
  if (!fs.existsSync(assetsPath)) return [];

  return generateAllPathsInsideDir(assetsPath).map(absPath => ({
    pattern: absPath.replace(assetsPath, "").split(".")[0],
    group: "internal",
  }));
};

const buildPathGroupsBasedOnWebpackAliases = ({
  customAliasPath = "config/webpack/resolve.js",
  commonAliasPath = "node_modules/@bigbinary/neeto-commons-frontend/configs/webpack/resolve.js",
}) => {
  const rootOfProject = path.join(__dirname, "../../../../../../");
  const projectResolve = loadJS(path.join(rootOfProject, customAliasPath));
  const commonResolve = loadJS(path.join(rootOfProject, commonAliasPath));

  const { dependencies = [], devDependencies = [] } = loadJS(
    path.join(rootOfProject, "package.json")
  );
  const packages = keys(mergeLeft(dependencies, devDependencies));
  const { alias = {}, extensions = [] } = mergeDeepLeft(
    projectResolve,
    commonResolve
  );

  const pathGroups = Object.entries(alias).flatMap(([pattern, aliasPath]) => {
    const group = packages.includes(aliasPath) ? "external" : "internal";
    const isFile = ["", ...extensions].some(extension =>
      doesFileExist(`${aliasPath}${extension}`)
    );

    if (isFile) return { pattern, group };

    return [
      { pattern, group },
      { pattern: `${pattern}/**`, group },
    ];
  });

  const pathGroupsFromAssets = generatePathGroupsFromAssets(
    path.join(rootOfProject, "app/assets/")
  );

  return pathGroups.concat(pathGroupsFromAssets);
};

module.exports = { buildPathGroupsBasedOnWebpackAliases };




© 2015 - 2024 Weber Informatics LLC | Privacy Policy