Skip to content

Commit dbcfe98

Browse files
authored
fix: Support comments in JSON (jsonc) (#12)
1 parent e9d1c09 commit dbcfe98

File tree

6 files changed

+65
-16
lines changed

6 files changed

+65
-16
lines changed

package-lock.json

Lines changed: 47 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"deepmerge": "^4.3.1",
3535
"fs-extra": "^11.3.0",
3636
"glob": "^11.0.0",
37+
"jsonc": "^2.0.0",
3738
"parse-imports": "^2.2.1",
3839
"smol-toml": "^1.3.1"
3940
},

src/utilities/locales.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { jsonc } from 'jsonc'
12
import fs, { mkdtempSync, rmSync } from 'node:fs'
23
import { tmpdir } from 'node:os'
34
import path from 'node:path'
@@ -10,6 +11,8 @@ import { CleanOptions, LocaleContent, LocaleDiff, LocaleOptions, SyncOptions, Tr
1011
const SCHEMA_DIRS = ['config', 'blocks', 'sections'] as const
1112
const STOREFRONT_DIRS = ['blocks', 'layout', 'sections', 'snippets', 'templates'] as const
1213

14+
15+
1316
export async function getLocalesSource(source: string): Promise<LocaleContent> {
1417
if (isUrl(source)) {
1518
return fetchRemoteLocales(source)
@@ -45,7 +48,8 @@ function loadLocalLocales(dir: string): LocaleContent {
4548
const filePath = path.join(dir, file)
4649

4750
try {
48-
content[file] = JSON.parse(fs.readFileSync(filePath, 'utf8'))
51+
const fileContent = fs.readFileSync(filePath, 'utf8')
52+
content[file] = jsonc.parse(fileContent, { stripComments: true }) as Record<string, unknown>
4953
} catch (error) {
5054
throw new Error(`Failed to parse JSON file ${file}: ${error instanceof Error ? error.message : String(error)}`)
5155
}
@@ -267,7 +271,8 @@ export async function removeUnreferencedStorefrontTranslations(themeDir: string,
267271
function removeUnreferencedKeysFromFile(filePath: string, usedKeys: Set<string>, options?: LocaleOptions): void {
268272
if (!fs.existsSync(filePath)) return
269273

270-
const content = JSON.parse(fs.readFileSync(filePath, 'utf8'))
274+
const fileContent = fs.readFileSync(filePath, 'utf8')
275+
const content = jsonc.parse(fileContent, { stripComments: true }) as Record<string, unknown>
271276
if (!content || typeof content !== 'object') return
272277

273278
const flattenedContent = flattenObject(content)
@@ -330,7 +335,8 @@ export async function mergeLocaleFiles(
330335
continue
331336
}
332337

333-
const targetContent = JSON.parse(fs.readFileSync(targetPath, 'utf8'))
338+
const targetFileContent = fs.readFileSync(targetPath, 'utf8')
339+
const targetContent = jsonc.parse(targetFileContent, { stripComments: true }) as Record<string, unknown>
334340
const diff = compareLocales(sourceContent, targetContent)
335341

336342
const mergedContent = mode === 'replace-existing'

src/utilities/manifest.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { jsonc } from 'jsonc'
12
import * as fs from 'node:fs'
23

34
import logger from './logger.js'
@@ -9,7 +10,7 @@ export function getManifest(path: string): Manifest {
910

1011
if (fs.existsSync(path)) {
1112
const manifestContent = fs.readFileSync(path, 'utf8')
12-
const parsedContent = JSON.parse(manifestContent)
13+
const parsedContent = jsonc.parse(manifestContent, { stripComments: true })
1314
data.collections = parsedContent.collections || {}
1415
data.files.assets = parsedContent.files?.assets || {}
1516
data.files.snippets = parsedContent.files?.snippets || {}

src/utilities/package-json.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import { jsonc } from 'jsonc'
12
import fs from "node:fs";
23
import path from "node:path";
34

45
export function getNameFromPackageJson(dir: string): string|undefined {
56
const pkgPath = path.join(dir, 'package.json');
67
let name;
78
if (fs.existsSync(pkgPath)) {
8-
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
9+
const pkg = jsonc.parse(fs.readFileSync(pkgPath, 'utf8'), { stripComments: true });
910
name = pkg.name;
1011
}
1112

@@ -16,7 +17,7 @@ export function getVersionFromPackageJson(dir: string): string|undefined {
1617
const pkgPath = path.join(dir, 'package.json');
1718
let version;
1819
if (fs.existsSync(pkgPath)) {
19-
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
20+
const pkg = jsonc.parse(fs.readFileSync(pkgPath, 'utf8'), { stripComments: true });
2021
version = pkg.version;
2122
}
2223

src/utilities/setup.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { jsonc } from 'jsonc'
12
import path from 'node:path'
23

34
import { copyFileIfChanged, writeFileIfChanged } from './files.js'
@@ -57,7 +58,7 @@ export async function processSettingsSchema(
5758
}
5859

5960
try {
60-
const schema = JSON.parse(node.body)
61+
const schema = jsonc.parse(node.body, { stripComments: true })
6162
if (!Array.isArray(schema)) {
6263
logger.warn(`Invalid schema format in ${setupFile}: Expected an array`)
6364
return []
@@ -79,7 +80,7 @@ export async function processSettingsData(
7980
}
8081

8182
try {
82-
const data = JSON.parse(node.body)
83+
const data = jsonc.parse(node.body, { stripComments: true })
8384
if (typeof data !== 'object' || data === null) {
8485
logger.warn(`Invalid settings data format in ${setupFile}: Expected an object`)
8586
return {}

0 commit comments

Comments
 (0)