Skip to content
This repository has been archived by the owner on Oct 6, 2022. It is now read-only.

Commit

Permalink
feat: add support for .jsonc files (#26)
Browse files Browse the repository at this point in the history
Add support for `.jsonc` files, aka. json files with comments.
  • Loading branch information
boywithkeyboard authored Jul 25, 2022
1 parent d1d71f1 commit 56c9884
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .changeset/nice-rice-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@darkflare/wjson": patch
---

feat: add support for `.jsonc` files

Add support for `.jsonc` files, aka. json files with comments.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ wjson
### Automatically Detectable Files

- `wrangler.json`
- `wrangler.jsonc`
- `wrangler.js`
- `wrangler.mjs`
- `wrangler.cjs`
Expand Down Expand Up @@ -51,7 +52,7 @@ await generateConfig(config)

## Config Files

`json`
`json/jsonc`

```jsonc
{
Expand Down
53 changes: 46 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"js-yaml": "^4.1.0",
"minimist": "^1.2.6",
"rimraf": "^3.0.2",
"strip-json-comments": "^5.0.0",
"typescript": "^4.7.4"
},
"engines": {
Expand Down
9 changes: 9 additions & 0 deletions src/modules/parseConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { join } from 'node:path'
import chalk from 'chalk'
import { build } from 'esbuild'
import yaml from 'js-yaml'
import stripJsonComments from 'strip-json-comments'
import type { WranglerConfig } from '../type'

const getStats = async (path: string) => {
Expand Down Expand Up @@ -76,6 +77,10 @@ export const parseConfig = async (path?: string) => {
const c = await require(path)

config = c
} else if (path.endsWith('.jsonc')) {
const c = await readFile(path, { encoding: 'utf-8' })

config = JSON.parse(stripJsonComments(c))
} else if (path.endsWith('.js')) {
await buildConfig(path)

Expand Down Expand Up @@ -112,6 +117,10 @@ export const parseConfig = async (path?: string) => {
const c = await require(join(path, './wrangler.json'))

config = c
} else if ((await getStats(join(path, './wrangler.jsonc')))?.isFile()) {
const c = await readFile(join(path, './wrangler.jsonc'), { encoding: 'utf-8' })

config = JSON.parse(stripJsonComments(c))
} else if ((await getStats(join(path, './wrangler.js')))?.isFile()) {
await buildConfig(join(path, './wrangler.js'))

Expand Down

0 comments on commit 56c9884

Please sign in to comment.