pr-check / src / userinput / getContext.ts
getContext.ts
Raw
import { readFile } from "../filesystem/readFile"
import { selectContext } from "./selectContext"

/**
 * Asks the user to select context files, and returns the text of the selected files.
 * @param contextFilesDirectory The directory in which the context files are located.
 * @returns The text of the selected context files.
 */
async function getContext(contextFilesDirectory: string): Promise<string> {
  const selectedFiles: string[] = await selectContext(contextFilesDirectory)
  
  const orderedContextFiles: string[] = []
  // The base context should be first, as it provides context for the AI
  // to understand it's task and understand the 'meta-context' for other context files.
  if (selectedFiles.includes("baseContext.md")) {
    orderedContextFiles.push("baseContext.md")
  }
  const otherFiles = selectedFiles.filter(file => file != "baseContext.md")
  const orderedOtherFiles = otherFiles.sort()
  orderedContextFiles.push(...orderedOtherFiles)

  const contextTexts: string[] = orderedContextFiles.map(file => readFile(file))
  const allContextText = contextTexts.join("\n")

  return allContextText
}

export { getContext }