-
Notifications
You must be signed in to change notification settings - Fork 32
support for configs in jsonc or json #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mrhut10
wants to merge
1
commit into
zpg6:main
Choose a base branch
from
mrhut10:feat/support-wrangler-config-formats
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -177,6 +177,152 @@ export function parseWranglerToml(tomlContent: string): { | |
| }; | ||
| } | ||
|
|
||
| export type WranglerConfigFormat = "toml" | "json" | "jsonc"; | ||
|
|
||
| export interface WranglerConfigResult { | ||
| path: string; | ||
| content: string; | ||
| format: WranglerConfigFormat; | ||
| } | ||
|
|
||
| export function findWranglerConfig(cwd: string = process.cwd()): WranglerConfigResult | null { | ||
| const { existsSync } = require("fs"); | ||
| const { join } = require("path"); | ||
|
|
||
| const configFiles: Array<{ name: string; format: WranglerConfigFormat }> = [ | ||
| { name: "wrangler.json", format: "json" }, | ||
| { name: "wrangler.jsonc", format: "jsonc" }, | ||
| { name: "wrangler.toml", format: "toml" }, | ||
| ]; | ||
|
|
||
| for (const configFile of configFiles) { | ||
| const configPath = join(cwd, configFile.name); | ||
| if (existsSync(configPath)) { | ||
| const { readFileSync } = require("fs"); | ||
| const content = readFileSync(configPath, "utf8"); | ||
| return { | ||
| path: configPath, | ||
| content, | ||
| format: configFile.format, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| export function findWranglerConfigWithExplicitPath(configPath: string): WranglerConfigResult | null { | ||
| const { existsSync, readFileSync } = require("fs"); | ||
| const { extname, resolve } = require("path"); | ||
|
|
||
| const resolvedPath = resolve(configPath); | ||
|
|
||
| if (!existsSync(resolvedPath)) { | ||
| return null; | ||
| } | ||
|
|
||
| const ext = extname(configPath).toLowerCase(); | ||
| let format: WranglerConfigFormat; | ||
|
|
||
| switch (ext) { | ||
| case ".json": | ||
| format = "json"; | ||
| break; | ||
| case ".jsonc": | ||
| format = "jsonc"; | ||
| break; | ||
| case ".toml": | ||
| format = "toml"; | ||
| break; | ||
| default: | ||
| return null; | ||
| } | ||
|
|
||
| const content = readFileSync(resolvedPath, "utf8"); | ||
| return { | ||
| path: resolvedPath, | ||
| content, | ||
| format, | ||
| }; | ||
| } | ||
|
|
||
| export function parseWranglerConfig( | ||
| content: string, | ||
| format: WranglerConfigFormat | ||
| ): { | ||
| databases: DatabaseConfig[]; | ||
| hasMultipleDatabases: boolean; | ||
| } { | ||
| const databases: DatabaseConfig[] = []; | ||
|
|
||
|
Comment on lines
+256
to
+257
|
||
| if (format === "toml") { | ||
| const toml = require("@iarna/toml"); | ||
| const parsed = toml.parse(content); | ||
| return parseWranglerConfigObject(parsed); | ||
| } | ||
|
|
||
| const jsonc = require("jsonc-simple-parser"); | ||
| const parsed = jsonc.parse(content); | ||
| return parseWranglerConfigObject(parsed); | ||
| } | ||
|
|
||
| function parseWranglerConfigObject(parsed: Record<string, unknown>): { | ||
| databases: DatabaseConfig[]; | ||
| hasMultipleDatabases: boolean; | ||
| } { | ||
| const databases: DatabaseConfig[] = []; | ||
|
|
||
| const d1Databases = parsed.d1_databases as Array<Record<string, unknown>> | undefined; | ||
| if (d1Databases && Array.isArray(d1Databases)) { | ||
|
Comment on lines
+269
to
+276
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer something more typesafe, but obviously let's not concern ourselves with irrelevant wrangler fields |
||
| for (const db of d1Databases) { | ||
| const binding = db.binding as string | undefined; | ||
| if (binding) { | ||
| databases.push({ | ||
| type: "d1", | ||
| binding, | ||
| name: db.database_name as string | undefined, | ||
| id: db.database_id as string | undefined, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const hyperdriveConfigs = parsed.hyperdrive as Record<string, unknown> | undefined; | ||
| if (hyperdriveConfigs) { | ||
| if (Array.isArray(hyperdriveConfigs)) { | ||
| for (const hd of hyperdriveConfigs) { | ||
| const binding = hd.binding as string | undefined; | ||
| if (binding) { | ||
| databases.push({ | ||
| type: "hyperdrive", | ||
| binding, | ||
| id: hd.id as string | undefined, | ||
| }); | ||
| } | ||
| } | ||
| } else if (typeof hyperdriveConfigs === "object") { | ||
| for (const hd of Object.values(hyperdriveConfigs)) { | ||
| if (typeof hd === "object" && hd !== null) { | ||
| const hdObj = hd as Record<string, unknown>; | ||
| const binding = hdObj.binding as string | undefined; | ||
| if (binding) { | ||
| databases.push({ | ||
| type: "hyperdrive", | ||
| binding, | ||
| id: hdObj.id as string | undefined, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| databases, | ||
| hasMultipleDatabases: databases.length > 1, | ||
| }; | ||
| } | ||
|
|
||
| // Functions to extract IDs from wrangler command responses | ||
| export function extractD1DatabaseId(wranglerOutput: string): string | null { | ||
| try { | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, and I prefer the CLI generate use toml by default still