Skip to content

Commit

Permalink
refactor to parseTOML (#768)
Browse files Browse the repository at this point in the history
* polish: refactor `TOML.parse` to the custom `parseTOML` to improve output for users.

* PR review suggestion: Remove `parseTOML` from tests
  • Loading branch information
JacobMGEvans authored Apr 10, 2022
1 parent 6d253a1 commit 6a4b046
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
10 changes: 8 additions & 2 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -469,7 +475,7 @@ export async function main(argv: string[]): Promise<void> {
) {
if (isCreatingWranglerToml) {
// rewrite wrangler.toml with main = "path/to/script"
const parsedWranglerToml = TOML.parse(
const parsedWranglerToml = parseTOML(
readFileSync(wranglerTomlDestination)
);
fs.writeFileSync(
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/wrangler/src/reporting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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({
Expand Down Expand Up @@ -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",
})
Expand Down
4 changes: 2 additions & 2 deletions packages/wrangler/src/user.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 6a4b046

Please sign in to comment.