Skip to content

Commit

Permalink
Support wrangler.json configuration file in adapter-cloudflare-workers (
Browse files Browse the repository at this point in the history
#13148)

* feat: support wrangler.json

* feat: Find the wrangler.json if the default config not found

* Update packages/adapter-cloudflare-workers/index.js

---------

Co-authored-by: Tee Ming <[email protected]>
  • Loading branch information
linnil1 and eltigerchino authored Dec 12, 2024
1 parent 8749894 commit 99babf9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ export default {

### config

Path to your custom `wrangler.toml` config file.
Path to your custom `wrangler.toml` or `wrangler.json` config file.

### platformProxy

Preferences for the emulated `platform.env` local bindings. See the [getPlatformProxy](https://developers.cloudflare.com/workers/wrangler/api/#syntax) Wrangler API documentation for a full list of options.

## Basic Configuration

This adapter expects to find a [wrangler.toml](https://developers.cloudflare.com/workers/platform/sites/configuration) file in the project root. It should look something like this:
This adapter expects to find a [wrangler.toml/wrangler.json](https://developers.cloudflare.com/workers/platform/sites/configuration) file in the project root. It should look something like this:

```toml
/// file: wrangler.toml
Expand Down
16 changes: 13 additions & 3 deletions packages/adapter-cloudflare-workers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,24 @@ export default function ({ config = 'wrangler.toml', platformProxy = {} } = {})
* @returns {WranglerConfig}
*/
function validate_config(builder, config_file) {
if (!existsSync(config_file) && config_file === 'wrangler.toml' && existsSync('wrangler.json')) {
builder.log.minor('Default wrangler.toml does not exist. Using wrangler.json.');
config_file = 'wrangler.json';
}
if (existsSync(config_file)) {
/** @type {WranglerConfig} */
let wrangler_config;

try {
wrangler_config = /** @type {WranglerConfig} */ (
toml.parse(readFileSync(config_file, 'utf-8'))
);
if (config_file.endsWith('.json')) {
wrangler_config = /** @type {WranglerConfig} */ (
JSON.parse(readFileSync(config_file, 'utf-8'))
);
} else {
wrangler_config = /** @type {WranglerConfig} */ (
toml.parse(readFileSync(config_file, 'utf-8'))
);
}
} catch (err) {
err.message = `Error parsing ${config_file}: ${err.message}`;
throw err;
Expand Down

0 comments on commit 99babf9

Please sign in to comment.