Skip to content

Commit fdf68f2

Browse files
committed
Add jsonc support for deno config.
1 parent 9e73471 commit fdf68f2

File tree

7 files changed

+168
-22
lines changed

7 files changed

+168
-22
lines changed

package-lock.json

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

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"semver-utils": "^1.1.4",
8181
"source-map-support": "^0.5.21",
8282
"spawn-please": "^2.0.1",
83+
"strip-json-comments": "^5.0.0",
8384
"untildify": "^4.0.0",
8485
"update-notifier": "^6.0.2",
8586
"yaml": "^2.2.1"

src/lib/findLockfile.ts

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export default async function findLockfile(
3131
return { directoryPath: currentPath, filename: 'pnpm-lock.yaml' }
3232
} else if (files.includes('deno.json')) {
3333
return { directoryPath: currentPath, filename: 'deno.json' }
34+
} else if (files.includes('deno.jsonc')) {
35+
return { directoryPath: currentPath, filename: 'deno.jsonc' }
3436
}
3537

3638
const pathParent = path.resolve(currentPath, '..')

src/lib/findPackage.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ async function findPackage(options: Options) {
7070
pkgData = data || getPackageDataFromFile(await pkgFile, pkgPath)
7171
} else {
7272
// find the closest package starting from the current working directory and going up to the root
73-
pkgFile = pkgPath ? findUp.sync(pkgPath, { cwd: options.cwd || process.cwd() }) : null
73+
pkgFile = pkgPath
74+
? findUp.sync(!options.packageFile && options.packageManager === 'deno' ? ['deno.json', 'deno.jsonc'] : pkgPath, {
75+
cwd: options.cwd || process.cwd(),
76+
})
77+
: null
7478
pkgData = getPackageDataFromFile(pkgFile, pkgPath)
7579
}
7680

src/lib/initOptions.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,7 @@ async function initOptions(runOptions: RunOptions, { cli }: { cli?: boolean } =
158158

159159
const resolvedOptions: Options = {
160160
...options,
161-
...(options.deep
162-
? { packageFile: '**/package.json' }
163-
: packageManager === 'deno' && !options.packageFile
164-
? { packageFile: 'deno.json' }
165-
: null),
161+
...(options.deep ? { packageFile: '**/package.json' } : null),
166162
...(packageManager === 'deno' && options.dep !== cliOptionsMap.dep.default ? { dep: ['imports'] } : null),
167163
...(options.format && options.format.length > 0 ? { format: options.format } : null),
168164
filter: args || filter,

src/lib/runLocal.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,16 @@ async function runLocal(
164164
print(options, '\nOptions:', 'verbose')
165165
print(options, sortOptions(options), 'verbose')
166166

167-
let pkg
167+
let pkg: PackageFile
168168

169169
try {
170170
if (!pkgData) {
171-
throw new Error('Missing pkgData: ' + pkgData)
171+
throw new Error('Missing pkgData')
172172
} else {
173-
pkg = jph.parse(pkgData)
173+
// strip comments from jsonc files
174+
const pkgDataStripped =
175+
pkgFile?.endsWith('.jsonc') && pkgData ? (await import('strip-json-comments')).default(pkgData) : pkgData
176+
pkg = jph.parse(pkgDataStripped)
174177
}
175178
} catch (e: any) {
176179
programError(

test/package-managers/deno/index.test.ts

+71-4
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ describe('deno', async function () {
2424
'ncu-test-v2': 'npm:ncu-test-v2@1.0.0',
2525
},
2626
}
27-
await fs.writeFile(pkgFile, JSON.stringify(pkg), 'utf-8')
27+
await fs.writeFile(pkgFile, JSON.stringify(pkg))
2828
try {
2929
const pkgData = await spawn(
3030
'node',
31-
[bin, '--jsonUpgraded', '--verbose', '--packageManager', 'deno', '--packageFile', pkgFile],
31+
[bin, '--jsonUpgraded', '--packageManager', 'deno', '--packageFile', pkgFile],
3232
undefined,
3333
)
3434
const pkg = jph.parse(pkgData)
@@ -46,9 +46,9 @@ describe('deno', async function () {
4646
'ncu-test-v2': 'npm:ncu-test-v2@1.0.0',
4747
},
4848
}
49-
await fs.writeFile(pkgFile, JSON.stringify(pkg), 'utf-8')
49+
await fs.writeFile(pkgFile, JSON.stringify(pkg))
5050
try {
51-
const pkgData = await spawn('node', [bin, '--jsonUpgraded', '--verbose'], undefined, {
51+
const pkgData = await spawn('node', [bin, '--jsonUpgraded'], undefined, {
5252
cwd: tempDir,
5353
})
5454
const pkg = jph.parse(pkgData)
@@ -57,4 +57,71 @@ describe('deno', async function () {
5757
await fs.rm(tempDir, { recursive: true, force: true })
5858
}
5959
})
60+
61+
it('rewrite deno.json', async () => {
62+
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
63+
const pkgFile = path.join(tempDir, 'deno.json')
64+
const pkg = {
65+
imports: {
66+
'ncu-test-v2': 'npm:ncu-test-v2@1.0.0',
67+
},
68+
}
69+
await fs.writeFile(pkgFile, JSON.stringify(pkg))
70+
try {
71+
await spawn('node', [bin, '-u'], undefined, { cwd: tempDir })
72+
const pkgDataNew = await fs.readFile(pkgFile, 'utf-8')
73+
const pkg = jph.parse(pkgDataNew)
74+
pkg.should.deep.equal({
75+
imports: {
76+
'ncu-test-v2': 'npm:ncu-test-v2@2.0.0',
77+
},
78+
})
79+
} finally {
80+
await fs.rm(tempDir, { recursive: true, force: true })
81+
}
82+
})
83+
84+
it('auto detect deno.jsonc', async () => {
85+
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
86+
const pkgFile = path.join(tempDir, 'deno.jsonc')
87+
const pkgString = `{
88+
"imports": {
89+
// this comment should be ignored in a jsonc file
90+
"ncu-test-v2": "npm:ncu-test-v2@1.0.0"
91+
}
92+
}`
93+
await fs.writeFile(pkgFile, pkgString)
94+
try {
95+
const pkgData = await spawn('node', [bin, '--jsonUpgraded'], undefined, {
96+
cwd: tempDir,
97+
})
98+
const pkg = jph.parse(pkgData)
99+
pkg.should.have.property('ncu-test-v2')
100+
} finally {
101+
await fs.rm(tempDir, { recursive: true, force: true })
102+
}
103+
})
104+
105+
it('rewrite deno.jsonc', async () => {
106+
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
107+
const pkgFile = path.join(tempDir, 'deno.jsonc')
108+
const pkg = {
109+
imports: {
110+
'ncu-test-v2': 'npm:ncu-test-v2@1.0.0',
111+
},
112+
}
113+
await fs.writeFile(pkgFile, JSON.stringify(pkg))
114+
try {
115+
await spawn('node', [bin, '-u'], undefined, { cwd: tempDir })
116+
const pkgDataNew = await fs.readFile(pkgFile, 'utf-8')
117+
const pkg = jph.parse(pkgDataNew)
118+
pkg.should.deep.equal({
119+
imports: {
120+
'ncu-test-v2': 'npm:ncu-test-v2@2.0.0',
121+
},
122+
})
123+
} finally {
124+
await fs.rm(tempDir, { recursive: true, force: true })
125+
}
126+
})
60127
})

0 commit comments

Comments
 (0)