Skip to content

Commit 40441c3

Browse files
authoredAug 12, 2024
feat: jsonc support (#38)
1 parent 9a21056 commit 40441c3

File tree

6 files changed

+43
-48
lines changed

6 files changed

+43
-48
lines changed
 

‎package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"escalade": "^3.1.2",
6666
"fast-glob": "^3.3.2",
6767
"js-yaml": "^4.1.0",
68+
"jsonc-parser": "^3.3.1",
6869
"prompts": "^2.4.2",
6970
"semver": "^7.6.3"
7071
},
@@ -74,8 +75,6 @@
7475
"@types/node": "^22.0.2",
7576
"@types/prompts": "^2.4.9",
7677
"@types/semver": "^7.5.8",
77-
"detect-indent": "^7.0.1",
78-
"detect-newline": "^4.0.1",
7978
"eslint": "^9.8.0",
8079
"esno": "^4.7.0",
8180
"log-symbols": "^6.0.0",

‎pnpm-lock.yaml

+8-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/fs.ts

+23-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import fs from 'node:fs'
22
import path from 'node:path'
3-
import detectIndent from 'detect-indent'
4-
import { detectNewline } from 'detect-newline'
3+
import * as jsonc from 'jsonc-parser'
54

65
/**
76
* Describes a plain-text file.
@@ -11,38 +10,43 @@ export interface TextFile {
1110
data: string
1211
}
1312

13+
/**
14+
* Modifies a JSON file.
15+
*/
16+
type ModifyUnion = [jsonc.JSONPath, unknown]
17+
1418
/**
1519
* Describes a JSON file.
1620
*/
1721
interface JsonFile {
18-
path: string
19-
data: unknown
20-
indent: string
21-
newline: string | undefined
22+
path: Readonly<string>
23+
text: Readonly<string>
24+
data: Readonly<unknown>
25+
modified: ModifyUnion[]
2226
}
2327

2428
/**
25-
* Reads a JSON file and returns the parsed data.
29+
* Reads a JSON/JSONC file and returns the parsed data.
2630
*/
27-
export async function readJsonFile(name: string, cwd: string): Promise<JsonFile> {
31+
export async function readJsoncFile(name: string, cwd: string): Promise<JsonFile> {
2832
const file = await readTextFile(name, cwd)
29-
const data = JSON.parse(file.data) as unknown
30-
const indent = detectIndent(file.data).indent
31-
const newline = detectNewline(file.data)
33+
const data = jsonc.parse(file.data)
34+
const modified: ModifyUnion[] = []
3235

33-
return { ...file, data, indent, newline }
36+
return { ...file, data, modified, text: file.data }
3437
}
3538

3639
/**
37-
* Writes the given data to the specified JSON file.
40+
* Writes the given data to the specified JSON/JSONC file.
3841
*/
39-
export async function writeJsonFile(file: JsonFile): Promise<void> {
40-
let json = JSON.stringify(file.data, undefined, file.indent)
41-
42-
if (file.newline)
43-
json += file.newline
42+
export async function writeJsoncFile(file: JsonFile): Promise<void> {
43+
let newJSON = file.text
44+
for (const [key, value] of file.modified) {
45+
const edit = (jsonc.modify(file.text, key, value, {}))
46+
newJSON = jsonc.applyEdits(newJSON, edit)
47+
}
4448

45-
return writeTextFile({ ...file, data: json })
49+
return writeTextFile({ ...file, data: newJSON })
4650
}
4751

4852
/**

‎src/get-current-version.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { valid as isValidVersion } from 'semver'
2-
import { readJsonFile } from './fs'
2+
import { readJsoncFile } from './fs'
33
import { isManifest } from './manifest'
44
import type { Operation } from './operation'
55

@@ -50,7 +50,7 @@ export async function getCurrentVersion(operation: Operation): Promise<Operation
5050
*/
5151
async function readVersion(file: string, cwd: string): Promise<string | undefined> {
5252
try {
53-
const { data: manifest } = await readJsonFile(file, cwd)
53+
const { data: manifest } = await readJsoncFile(file, cwd)
5454

5555
if (isManifest(manifest)) {
5656
if (isValidVersion(manifest.version))

‎src/run-npm-script.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as ezSpawn from '@jsdevtools/ez-spawn'
2-
import { readJsonFile } from './fs'
2+
import { readJsoncFile } from './fs'
33
import type { Manifest } from './manifest'
44
import { isManifest } from './manifest'
55
import type { Operation } from './operation'
@@ -13,7 +13,7 @@ export async function runNpmScript(script: NpmScript, operation: Operation): Pro
1313
const { cwd, ignoreScripts } = operation.options
1414

1515
if (!ignoreScripts) {
16-
const { data: manifest } = await readJsonFile('package.json', cwd)
16+
const { data: manifest } = await readJsoncFile('package.json', cwd)
1717

1818
if (isManifest(manifest) && hasScript(manifest, script)) {
1919
await ezSpawn.async('npm', ['run', script, '--silent'], { stdio: 'inherit' })

‎src/update-files.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as path from 'node:path'
2-
import { readJsonFile, readTextFile, writeJsonFile, writeTextFile } from './fs'
2+
import { readJsoncFile, readTextFile, writeJsoncFile, writeTextFile } from './fs'
33
import { isManifest, isPackageLockManifest } from './manifest'
44
import type { Operation } from './operation'
55
import { ProgressEvent } from './types/version-bump-progress'
@@ -44,7 +44,9 @@ async function updateFile(relPath: string, operation: Operation): Promise<boolea
4444
case 'bower.json':
4545
case 'component.json':
4646
case 'jsr.json':
47+
case 'jsr.jsonc':
4748
case 'deno.json':
49+
case 'deno.jsonc':
4850
return updateManifestFile(relPath, operation)
4951

5052
default:
@@ -65,14 +67,14 @@ async function updateManifestFile(relPath: string, operation: Operation): Promis
6567
const { newVersion } = operation.state
6668
let modified = false
6769

68-
const file = await readJsonFile(relPath, cwd)
70+
const file = await readJsoncFile(relPath, cwd)
6971

7072
if (isManifest(file.data) && file.data.version !== newVersion) {
71-
file.data.version = newVersion
73+
file.modified.push([['version'], newVersion])
7274
if (isPackageLockManifest(file.data))
73-
file.data.packages[''].version = newVersion
75+
file.modified.push([['packages', '', 'version'], newVersion])
7476

75-
await writeJsonFile(file)
77+
await writeJsoncFile(file)
7678
modified = true
7779
}
7880

0 commit comments

Comments
 (0)
Please sign in to comment.