<commit_before>const fs = require('fs')
const pathResolve = require('path').resolve
const toposort = require('toposort')
const R = require('ramda')

const defaultConfig = {
  target: 'node',
  role: 'library',
}

// :: string -> string -> string
const resolveProjectPath = projectName => relativePath =>
  pathResolve(process.cwd(), `./projects/${projectName}`, relativePath)

// :: string -> Project
const getProject = (projectName) => {
  const thisProjectPath = resolveProjectPath(projectName)
  const packageJsonPath = thisProjectPath('./package.json')
  const constellateConfigPath = thisProjectPath('./constellate.js')
  const config = fs.existsSync(constellateConfigPath)
    ? // eslint-disable-next-line global-require, import/no-dynamic-require
      Object.assign({}, defaultConfig, require(constellateConfigPath))
    : defaultConfig
  return {
    name: projectName,
    config,
    paths: {
      root: thisProjectPath('./'),
      constellateConfig: constellateConfigPath,
      packageJson: packageJsonPath,
      nodeModules: thisProjectPath('./node_modules'),
      source: thisProjectPath('./modules'),
      sourceEntry: thisProjectPath('./modules/index.js'),
      dist: thisProjectPath('./dist'),
      distEntry: thisProjectPath('./dist/index.js'),
      webpackCache: thisProjectPath('./.webpackcache'),
    },
    packageJson: fs.existsSync(packageJsonPath)
      ? // eslint-disable-next-line global-require, import/no-dynamic-require
        JSON.parse(fs.readFileSync(packageJsonPath, { encoding: 'utf8' }))
      : {},
  }
}

// :: Array<Project> -> Array<Project>
function orderProjectsByDependencies(projects) {
  const packageDependencyGraph = project =>
    R.pipe(R.prop('dependencies'), R.map(dependency => [dependency, project.name]))(project)

  // :: Array<Project> -> Array<Array<string, string>>
  const dependencyGraph = R.chain(packageDependencyGraph)

  // :: Project -> bool
  const hasNoDependencies = ({ dependencies }) => dependencies.length === 0

  // :: Array<Project>
  const projectsWithNoDependencies = R.pipe(R.filter(hasNoDependencies), R.map(R.prop('name')))(
    projects
  )

  // :: string -> Project
  const findProjectByName = name => R.find(R.propEq('name', name), projects)

  return R.pipe(
    dependencyGraph,
    toposort,
    R.without(projectsWithNoDependencies),
    R.concat(projectsWithNoDependencies),
    R.map(findProjectByName)
  )(projects)
}

/**
 * Gets all the projects for the constellate application.
 *
 * The projects are ordered based on their dependency graph.
 * i.e. build them in order.
 *
 * @return {Array<Project>} The project meta object
 */
function getAllProjects() {
  // :: Array<Project>
  const projects = fs.readdirSync(pathResolve(process.cwd(), './projects')).map(getProject)

  // :: Array<string>
  const projectNames = R.map(R.prop('name'), projects)

  // :: Project -> Array<String>
  const getDependencies = (project) => {
    // :: (string, Project) -> Array<string>
    const readPackageDependencies = dependenciesType =>
      R.pipe(R.path(['packageJson', dependenciesType]), R.defaultTo({}), R.keys)(project)
    const combinedDependencies = R.concat(
      readPackageDependencies('dependencies'),
      readPackageDependencies('devDependencies')
    )
    // :: string -> boolean
    const isContellateProject = R.contains(R.__, projectNames)
    // :: Array<string> -> Array<string>
    const filterToConstellateProjects = R.pipe(R.uniq, R.filter(isContellateProject))
    return filterToConstellateProjects(combinedDependencies)
  }

  // :: Project -> Array<string>
  const getDependants = (project) => {
    // We will recursively resolve the dependants for the project.
    const loop = (targetProject) => {
      const dependants = projects.filter(x => R.contains(targetProject.name, x.dependencies))<commit_msg>Fixes project resolver to only include directories.<commit_after>const fs = require('fs')
const path = require('path')
const toposort = require('toposort')
const R = require('ramda')

const defaultConfig = {
  target: 'node',
  role: 'library',
}

// :: string -> string -> string
const resolveProjectPath = projectName => relativePath =>
  path.resolve(process.cwd(), `./projects/${projectName}`, relativePath)

// :: string -> Project
const toProject = (projectName) => {
  const thisProjectPath = resolveProjectPath(projectName)
  const packageJsonPath = thisProjectPath('./package.json')
  const constellateConfigPath = thisProjectPath('./constellate.js')
  const config = fs.existsSync(constellateConfigPath)
    ? // eslint-disable-next-line global-require, import/no-dynamic-require
      Object.assign({}, defaultConfig, require(constellateConfigPath))
    : defaultConfig
  return {
    name: projectName,
    config,
    paths: {
      root: thisProjectPath('./'),
      constellateConfig: constellateConfigPath,
      packageJson: packageJsonPath,
      nodeModules: thisProjectPath('./node_modules'),
      source: thisProjectPath('./modules'),
      sourceEntry: thisProjectPath('./modules/index.js'),
      dist: thisProjectPath('./dist'),
      distEntry: thisProjectPath('./dist/index.js'),
      webpackCache: thisProjectPath('./.webpackcache'),
    },
    packageJson: fs.existsSync(packageJsonPath)
      ? // eslint-disable-next-line global-require, import/no-dynamic-require
        JSON.parse(fs.readFileSync(packageJsonPath, { encoding: 'utf8' }))
      : {},
  }
}

// :: Array<Project> -> Array<Project>
function orderProjectsByDependencies(projects) {
  const packageDependencyGraph = project =>
    R.pipe(R.prop('dependencies'), R.map(dependency => [dependency, project.name]))(project)

  // :: Array<Project> -> Array<Array<string, string>>
  const dependencyGraph = R.chain(packageDependencyGraph)

  // :: Project -> bool
  const hasNoDependencies = ({ dependencies }) => dependencies.length === 0

  // :: Array<Project>
  const projectsWithNoDependencies = R.pipe(R.filter(hasNoDependencies), R.map(R.prop('name')))(
    projects
  )

  // :: string -> Project
  const findProjectByName = name => R.find(R.propEq('name', name), projects)

  return R.pipe(
    dependencyGraph,
    toposort,
    R.without(projectsWithNoDependencies),
    R.concat(projectsWithNoDependencies),
    R.map(findProjectByName)
  )(projects)
}

/**
 * Gets all the projects for the constellate application.
 *
 * The projects are ordered based on their dependency graph.
 * i.e. build them in order.
 *
 * @return {Array<Project>} The project meta object
 */
function getAllProjects() {
  const projectsRoot = path.resolve(process.cwd(), './projects')

  // :: Array<Project>
  const projects = fs
    .readdirSync(projectsRoot)
    // only include directories
    .filter(file => fs.lstatSync(path.join(projectsRoot, file)).isDirectory())
    // convert into a Project
    .map(toProject)

  // :: Array<string>
  const projectNames = R.map(R.prop('name'), projects)

  // :: Project -> Array<String>
  const getDependencies = (project) => {
    // :: (string, Project) -> Array<string>
    const readPackageDependencies = dependenciesType =>
      R.pipe(R.path(['packageJson', dependenciesType]), R.defaultTo({}), R.keys)(project)
    const combinedDependencies = R.concat(
      readPackageDependencies('dependencies'),
      readPackageDependencies('devDependencies')
    )
    // :: string -> boolean
    const isContellateProject = R.contains(R.__, projectNames)
    // :: Array<string> -> Array<string>
    const filterToConstellateProjects = R.pipe(R.uniq, R.filter(isContellateProject))
    return filterToConstellateProjects(combinedDependencies)
  }

  // :: Project -> Array<string>
  const getDependants = (project) => {
    // We will recursively resolve the dependants for the project.
    const loop = (targetProject) => {
      const dependants = projects.filter(x => R.contains(targetProject.name, x.dependencies))
{'fraction_non_alphanumeric': 0.14269391900469722, 'fraction_numerical': 0.000507807540941983, 'mean_word_length': 3.9640831758034025, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 80, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2644159', 'n_tokens_mistral': 2280, 'n_tokens_neox': 2208, 'n_words': 470}