pr-check / src / validation / validateState.ts
validateState.ts
Raw
import { doesFileExist } from "../filesystem/doesFileExist";
import { areThereNewCommitsOnThisBranch } from "../git/areThereUnmergedCommits";
import { doesBranchExist } from "../git/doesBranchExist";
import * as path from 'path';

/**
 * Validates that the state of the environment is valid for running the script.
 * 
 * @returns Nothing. Exits if the state is invalid.
 */
async function validateState() {
  // Ensure that the environment variable for the OpenAI API key is set
  const openAiApiKeyNotSpecified = process.env.OPENAI_API_KEY == undefined;
  if (openAiApiKeyNotSpecified) {
    process.stdout.write('No OpenAI API key specified. Specify this in the .env file. See `README.md` and `sample.env.md` for more details.\n');
    process.exit(1);
  }

  // Ensure that the environment variable for the OpenAI API key is not the default dummy value
  const openAiApiKeyHasDummyValue = process.env.OPENAI_API_KEY == "your-api-key-goes-here";
  if (openAiApiKeyHasDummyValue) {
    process.stdout.write('You have not set your OpenAI API key in the .env file. See `README.md` and `sample.env.md` for more details.\n');
    process.exit(1);
  }

  // Ensure that the environment variable for the OpenAI model ID is set
  const openAiModelIdSpecified = process.env.OPENAI_MODEL_ID != null;
  if (!openAiModelIdSpecified) {
    process.stdout.write('No OpenAI model ID specified. Specify this in the .env file. See `README.md` and `sample.env.md` for more details.\n');
    process.exit(1);
  }

  // Ensure that the environment variable for the default git branch is set
  const defaultGitBranchSpecified = process.env.NAME_OF_DEFAULT_GIT_BRANCH != null;
  if (!defaultGitBranchSpecified) {
    process.stdout.write('No default git branch specified. Specify this in the .env file. See README for more details.\n');
    process.exit(1);
  }

  // Ensure that a branch with the name of the default branch exists
  const defaultBranchExists = await doesBranchExist(process.env.NAME_OF_DEFAULT_GIT_BRANCH as string);
  if (!defaultBranchExists) {
    process.stdout.write(`The default branch '${process.env.NAME_OF_DEFAULT_GIT_BRANCH}' does not exist.\n`);
    process.exit(1);
  }

  // If there are no new commits on this branch, exit
  const thereAreUnmergedCommits = await areThereNewCommitsOnThisBranch();
  if (!thereAreUnmergedCommits) {
    process.stdout.write(
      `Your current branch has no commits that are not already on the 'master' branch.\nThis script works by reading commits on your current branch that are not on your 'master branch'.\nTherefore, there is nothing for this script to do in this state.\nSee README for more details on how to use this script.\n`
    );
    process.exit(1);
  }

  // If there are no default context files specified in the .env file, exit
  const defaultContextFilesNotSpecified = !process.env.DEFAULT_CONTEXT_FILES;
  if (defaultContextFilesNotSpecified) {
    process.stdout.write("You have not specified default context files in the .env file.\n");
    process.exit(1);
  }

  // Ensure that a base context exists
  const baseContextFileExists = doesFileExist(path.join(__dirname, '..', '..', 'prompt', 'context', 'baseContext.md'));
  if (!baseContextFileExists) {
    process.stdout.write("You must have a file `prompt/context/baseContext.md`. It can be empty, but it must exist. See README.\n");
    process.exit(1);
  }

  // Ensure that a final instruction exists
  const finalInstructionExists = doesFileExist(path.join(__dirname, '..', '..', 'prompt', 'instruction', 'finalInstruction.md'));
  if (!finalInstructionExists) {
    process.stdout.write("You must have a file `prompt/instruction/finalInstruction.md`. It can be empty, but it must exist. See README.\n");
    process.exit(1);
  }
}

export { validateState };