import { SwitchFile , CheckedSwitchFile , SwitchRule , OrPromise } from './types' ; import { RailsWorkspaceCache } from './rails-workspace' ; import { RailsFile } from './rails-file' ; import * as fs from 'fs-extra' ; import { rules } from './rules' ; function existsFilter ( switchFile ) { return Boolean ( switchFile . checkedExists ) ; } /**
 * Given a list of switch files, return the ones that exist on the filesystem
 */ export async function checkSwitchFiles ( switchFiles ) { return ( await Promise . all ( ( await Promise . resolve ( switchFiles ) ) . map ( checkSwitchFile ) ) ) . filter ( existsFilter ) as CheckedSwitchFile [ ] ; } /**
 * Get the switch files (that exist) from a given rule
 */ export function getSwitchesFromRule ( rule ) ; export function getSwitchesFromRule ( rule , railsFile ) ; export function getSwitchesFromRule ( rule , railsFile ? ) { const fn = async function ( railsFile ) { const workspace = await RailsWorkspaceCache . fetch ( railsFile . railsRoot ) ; const switchFiles = rule ( railsFile , workspace ) ; return checkSwitchFiles ( switchFiles ) ; } ; if ( railsFile ) { return fn ( railsFile ) ; } return fn ; } /**
 * Get the switch files (that exist) from a set of rules
 */ export async function getSwitchesFromRules ( rules , railsFile ) ; export async function getSwitchesFromRules ( rules , railsFile , checked ) ; export async function getSwitchesFromRules ( rules , railsFile , checked ) ; export async function getSwitchesFromRules ( rules , railsFile , checked = true ) { const workspace = await RailsWorkspaceCache . fetch ( railsFile . railsRoot ) ; const switchFiles = Promise . all ( rules . map ( rule => rule ( railsFile , workspace ) ) ) . then ( switches => [ ] . concat ( ... switches ) ) ; return checked ? checkSwitchFiles ( switchFiles ) : switchFiles ; } /**
 * Check to see if a switch file exists. If it has already been checked return
 * true, otherwise look to the filesystem.
 */ async function switchFileExists ( switchFile ) { return switchFile . checkedExists || fs . pathExists ( switchFile . filename ) ; } /**
 * Check to see if a switch file exists and return the switch file with
 * `checkedExists` filled in.
 */ async function checkSwitchFile ( switchFile ) { const resolvedSwitchFile = await Promise . resolve ( switchFile ) ; return { ... resolvedSwitchFile , checkedExists : await switchFileExists ( resolvedSwitchFile ) , } ; } /**
 * Return all available switch files using the preset rules.
 */ export async function getCheckedSwitches ( railsFile ) { return await getSwitchesFromRules ( rules , railsFile ) ; }