From 55eca193caf0d93a215f61509d9a15cf425b5096 Mon Sep 17 00:00:00 2001 From: Aiden Fujiwara Date: Tue, 26 Sep 2023 15:18:48 -0700 Subject: [PATCH] removed infinite loop --- src/configFile.ts | 126 +++++++++++++++++++++++----------------------- 1 file changed, 62 insertions(+), 64 deletions(-) diff --git a/src/configFile.ts b/src/configFile.ts index 7ad1c19..9824464 100644 --- a/src/configFile.ts +++ b/src/configFile.ts @@ -1,89 +1,87 @@ -import {join, dirname} from "path"; -import {parse} from "toml"; -import {readFile, existsSync} from "fs"; +import { join, dirname } from "path"; +import { parse } from "toml"; +import { readFile, existsSync } from "fs"; +import { dir } from "console"; interface foremanConfig { - tools: foremanTool[]; + tools: foremanTool[]; } interface foremanTool { - source?: string; - github?: string; - gitlab?: string; - version: string; + source?: string; + github?: string; + gitlab?: string; + version: string; } const MANIFEST = "foreman.toml"; function findManifestPath(): string | null { - let directory = __dirname; - while (true) { - if (!directory) { - break; + let directory = __dirname; + while (directory != "/") { + const configFilePath = join(directory, MANIFEST); + if (existsSync(configFilePath)) { + return configFilePath; + } else { + directory = dirname(directory); + } } - const configFilePath = join(directory, MANIFEST); - if (existsSync(configFilePath)) { - return configFilePath; - } else { - directory = dirname(directory); - } - } - return null; + return null; } function checkSameOrgToolSpecs(manifestContent: foremanConfig): boolean { - const tools = manifestContent.tools; - if (tools == null) { - throw new Error("Tools section in Foreman config not found"); - } - - const orgs: string[] = []; - for (const tool_name in tools) { - const tool_spec = tools[tool_name]; - let source = tool_spec["source"]; - if (source == null) { - source = tool_spec["github"]; - } - if (source == null) { - continue; + const tools = manifestContent.tools; + if (tools == null) { + throw new Error("Tools section in Foreman config not found"); } - const source_array = source.split("/"); - const org = source_array[0]; + const orgs: string[] = []; + for (const tool_name in tools) { + const tool_spec = tools[tool_name]; + let source = tool_spec["source"]; + if (source == null) { + source = tool_spec["github"]; + } + if (source == null) { + continue; + } + + const source_array = source.split("/"); + const org = source_array[0]; - if (org == null) { - throw new Error( - `Org not found in tool spec definition for: ${tool_name}` - ); + if (org == null) { + throw new Error( + `Org not found in tool spec definition for: ${tool_name}` + ); + } + orgs.push(org.toLowerCase()); } - orgs.push(org.toLowerCase()); - } - if (orgs.length == 0) { - return true; - } - return orgs.every(val => val === orgs[0]); + if (orgs.length == 0) { + return true; + } + return orgs.every(val => val === orgs[0]); } async function checkSameOrgInConfig(): Promise { - const manifestPath = findManifestPath(); - if (manifestPath == null) { - throw new Error("Foreman config file could not be found"); - } - - await readFile(manifestPath, "utf8", (err, data) => { - if (err) { - throw new Error("Could not read Foreman config file"); + const manifestPath = findManifestPath(); + if (manifestPath == null) { + throw new Error("Foreman config file could not be found"); } - { - const manifestContent = parse(data); - const sameGithubOrgSource = checkSameOrgToolSpecs(manifestContent); - if (sameGithubOrgSource == false) { - throw new Error("Not all GitHub orgs are the same"); - } - } - }); + + await readFile(manifestPath, "utf8", (err, data) => { + if (err) { + throw new Error("Could not read Foreman config file"); + } + { + const manifestContent = parse(data); + const sameGithubOrgSource = checkSameOrgToolSpecs(manifestContent); + if (sameGithubOrgSource == false) { + throw new Error("Not all GitHub orgs are the same"); + } + } + }); } export default { - checkSameOrgInConfig, - checkSameOrgToolSpecs + checkSameOrgInConfig, + checkSameOrgToolSpecs };