From 6a4b046e2f6b0c3e1c657366f92c11f5be1f9ddd Mon Sep 17 00:00:00 2001 From: Jacob M-G Evans <27247160+JacobMGEvans@users.noreply.github.com> Date: Sun, 10 Apr 2022 18:00:11 -0500 Subject: [PATCH] refactor to `parseTOML` (#768) * polish: refactor `TOML.parse` to the custom `parseTOML` to improve output for users. * PR review suggestion: Remove `parseTOML` from tests --- packages/wrangler/src/index.tsx | 10 ++++++++-- packages/wrangler/src/parse.ts | 2 +- packages/wrangler/src/reporting.ts | 4 ++-- packages/wrangler/src/user.tsx | 4 ++-- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/packages/wrangler/src/index.tsx b/packages/wrangler/src/index.tsx index 02eb7b1d7305..f38da3135211 100644 --- a/packages/wrangler/src/index.tsx +++ b/packages/wrangler/src/index.tsx @@ -34,7 +34,13 @@ import { } from "./kv"; import { getPackageManager } from "./package-manager"; import { pages, pagesBetaWarning } from "./pages"; -import { formatMessage, ParseError, parseJSON, readFileSync } from "./parse"; +import { + formatMessage, + ParseError, + parseJSON, + parseTOML, + readFileSync, +} from "./parse"; import publish from "./publish"; import { createR2Bucket, deleteR2Bucket, listR2Buckets } from "./r2"; import { getAssetPaths } from "./sites"; @@ -469,7 +475,7 @@ export async function main(argv: string[]): Promise { ) { if (isCreatingWranglerToml) { // rewrite wrangler.toml with main = "path/to/script" - const parsedWranglerToml = TOML.parse( + const parsedWranglerToml = parseTOML( readFileSync(wranglerTomlDestination) ); fs.writeFileSync( diff --git a/packages/wrangler/src/parse.ts b/packages/wrangler/src/parse.ts index dcb46f282d8e..d22c69a97fc2 100644 --- a/packages/wrangler/src/parse.ts +++ b/packages/wrangler/src/parse.ts @@ -75,7 +75,7 @@ type TomlError = Error & { /** * A wrapper around `TOML.parse` that throws a `ParseError`. */ -export function parseTOML(input: string, file?: string): object { +export function parseTOML(input: string, file?: string): TOML.JsonMap | never { try { return TOML.parse(input); } catch (err) { diff --git a/packages/wrangler/src/reporting.ts b/packages/wrangler/src/reporting.ts index 9fa24b1982ce..09a2b848a223 100644 --- a/packages/wrangler/src/reporting.ts +++ b/packages/wrangler/src/reporting.ts @@ -2,7 +2,6 @@ import * as fs from "node:fs"; import { readFile, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; -import TOML from "@iarna/toml"; import { RewriteFrames } from "@sentry/integrations"; import { captureException, @@ -15,6 +14,7 @@ import { import { execaSync } from "execa"; import prompts from "prompts"; import * as pkj from "../package.json"; +import { parseTOML } from "./parse"; export function initReporting() { init({ @@ -118,7 +118,7 @@ async function reportingPermission() { ) return undefined; - const reportingTOML = TOML.parse( + const reportingTOML = parseTOML( await readFile(path.join(os.homedir(), ".wrangler/config/reporting.toml"), { encoding: "utf-8", }) diff --git a/packages/wrangler/src/user.tsx b/packages/wrangler/src/user.tsx index 27722552ea42..4a0df6e54acf 100644 --- a/packages/wrangler/src/user.tsx +++ b/packages/wrangler/src/user.tsx @@ -222,7 +222,7 @@ import { fetch } from "undici"; import { getCloudflareApiBaseUrl } from "./cfetch"; import { getEnvironmentVariableFactory } from "./environment-variables"; import openInBrowser from "./open-in-browser"; -import { readFileSync } from "./parse"; +import { parseTOML, readFileSync } from "./parse"; import type { Config } from "./config"; import type { Item as SelectInputItem } from "ink-select-input/build/SelectInput"; import type { ParsedUrlQuery } from "node:querystring"; @@ -862,7 +862,7 @@ export function writeAuthConfigFile(config: UserAuthConfig) { } export function readAuthConfigFile(): UserAuthConfig { - const toml = TOML.parse( + const toml = parseTOML( readFileSync(path.join(os.homedir(), USER_AUTH_CONFIG_FILE)) ); return toml;