export function gitClone()

in lib/util/utils.ts [532:593]


export function gitClone(directory: string, url: string, branch: string | undefined): void {
  if (
    url === null ||
    url === undefined ||
    typeof url.valueOf() !== "string" ||
    !url.trim().length
  ) {
    throw new Error("url is a required parameter of type string and it cannot be an empty string.");
  }

  if (
    directory === null ||
    directory === undefined ||
    typeof directory.valueOf() !== "string" ||
    !directory.trim().length
  ) {
    throw new Error(
      "directory is a required parameter of type string and it cannot be an empty string."
    );
  }

  // If the directory exists then we assume that the repo to be cloned is already present.
  if (fs.existsSync(directory)) {
    if (fs.lstatSync(directory).isDirectory()) {
      try {
        removeDirSync(directory);
      } catch (err) {
        const text = util.inspect(err, { depth: null });
        throw new Error(`An error occurred while deleting directory ${directory}: ${text}.`);
      }
    } else {
      try {
        fs.unlinkSync(directory);
      } catch (err) {
        const text = util.inspect(err, { depth: null });
        throw new Error(`An error occurred while deleting file ${directory}: ${text}.`);
      }
    }
  }

  try {
    fs.mkdirSync(directory);
  } catch (err) {
    const text = util.inspect(err, { depth: null });
    throw new Error(`An error occurred while creating directory ${directory}: ${text}.`);
  }

  try {
    const isBranchDefined =
      branch !== null && branch !== undefined && typeof branch.valueOf() === "string";
    const cmd = isBranchDefined
      ? `git clone --depth=1 --branch ${branch} ${url} ${directory}`
      : `git clone --depth=1 ${url} ${directory}`;
    execSync(cmd, { encoding: "utf8" });
  } catch (err) {
    throw new Error(
      `An error occurred while cloning git repository: ${util.inspect(err, {
        depth: null,
      })}.`
    );
  }
}