diff --git a/backend/appInfo.test.ts b/backend/appInfo.test.ts index 7c1807b..719be7d 100644 --- a/backend/appInfo.test.ts +++ b/backend/appInfo.test.ts @@ -181,7 +181,7 @@ test("url app info with manifest without name", async () => { expect(appInfo.location).toEqual(location); expect(appInfo.manifest).toEqual({ - name: "No entry in manifest.toml (running from URL)", + name: "Missing name entry in manifest.toml", sourceCodeUrl: "http://example.com", manifestFound: true, }); @@ -205,18 +205,12 @@ test("url app info with broken manifest", async () => { fetch.isRedirect = () => false; const location = getLocation("http://localhost:3000") as UrlLocation; - try { - await getAppInfoUrl(location, fetch); - } catch (e) { - if (e instanceof AppInfoError) { - expect(e.message).toEqual( - "Invalid manifest.toml, please check the format", - ); - } else { - throw e; - } - } - expect.assertions(1); + const appInfo = await getAppInfoUrl(location, fetch); + expect(appInfo.manifest).toEqual({ + name: "", + sourceCodeUrl: undefined, + manifestFound: false, + }); }); test("url app info with manifest and source code URL", async () => { diff --git a/backend/appInfo.ts b/backend/appInfo.ts index 75987b8..8e118df 100644 --- a/backend/appInfo.ts +++ b/backend/appInfo.ts @@ -30,6 +30,12 @@ export type AppInfo = { export class AppInfoError extends Error {} +const MISSING_MANIFEST = { + name: undefined, + sourceCodeUrl: undefined, + manifestFound: false, +}; + export async function getAppInfo(location: Location): Promise { if (location.type === "url") { try { @@ -72,19 +78,10 @@ async function getManifestInfoFromUrl( } const response = await fetch(url + "manifest.toml"); if (!response.ok) { - return { - name: "Unknown (running from URL)", - sourceCodeUrl: undefined, - manifestFound: false, - }; + console.error("Missing manifest.toml (from URL)"); + return { ...MISSING_MANIFEST, name: "Unknown (running from URL)" }; } - const body = await response.text(); - const parsed = tomlParse(body); - return { - name: parsed.name || "No entry in manifest.toml (running from URL)", - sourceCodeUrl: parsed.source_code_url, - manifestFound: true, - }; + return tomlParse(await response.text()); } async function getIconInfoFromUrl( @@ -117,26 +114,24 @@ function getManifestInfoFromDir( ): ManifestInfo { const tomlBuffer = readFileBuffer(path.join(dir, "manifest.toml")); if (tomlBuffer === null) { - return { - name: fallbackName, - sourceCodeUrl: undefined, - manifestFound: false, - }; + console.error("Missing manifest.toml (from DIR)"); + return { ...MISSING_MANIFEST, name: fallbackName }; } - const parsed = tomlParse(tomlBuffer.toString()); - const name = parsed.name || fallbackName; - return { - name, - sourceCodeUrl: parsed.source_code_url, - manifestFound: true, - }; + return tomlParse(tomlBuffer.toString(), fallbackName); } -function tomlParse(s: string): any { +function tomlParse(s: string, fallbackName: string = ""): any { try { - return toml.parse(s); + const parsed = toml.parse(s); + return { + name: + parsed.name || fallbackName || "Missing name entry in manifest.toml", + sourceCodeUrl: parsed.source_code_url || undefined, + manifestFound: true, + }; } catch (e) { - throw new AppInfoError("Invalid manifest.toml, please check the format"); + console.error("Failed to parse manifest.toml, please check the format!"); + return { ...MISSING_MANIFEST, name: fallbackName }; } } diff --git a/package.json b/package.json index cdf9fe9..ad94877 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "fix": "prettier --write .", "check": "prettier --check .", "cli": "node dist/backend/cli.js", - "test": "jest", + "test": "jest --silent", "typecheck": "tsc --noEmit", "build-backend": "tsc --project tsconfig-backend.json", "build-frontend": "webpack --config webpack.prod.js",