Skip to content

Commit

Permalink
Add jsonc support for deno config.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Feb 10, 2023
1 parent 9e73471 commit fdf68f2
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 22 deletions.
91 changes: 82 additions & 9 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 @@ -80,6 +80,7 @@
"semver-utils": "^1.1.4",
"source-map-support": "^0.5.21",
"spawn-please": "^2.0.1",
"strip-json-comments": "^5.0.0",
"untildify": "^4.0.0",
"update-notifier": "^6.0.2",
"yaml": "^2.2.1"
Expand Down
2 changes: 2 additions & 0 deletions src/lib/findLockfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export default async function findLockfile(
return { directoryPath: currentPath, filename: 'pnpm-lock.yaml' }
} else if (files.includes('deno.json')) {
return { directoryPath: currentPath, filename: 'deno.json' }
} else if (files.includes('deno.jsonc')) {
return { directoryPath: currentPath, filename: 'deno.jsonc' }
}

const pathParent = path.resolve(currentPath, '..')
Expand Down
6 changes: 5 additions & 1 deletion src/lib/findPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ async function findPackage(options: Options) {
pkgData = data || getPackageDataFromFile(await pkgFile, pkgPath)
} else {
// find the closest package starting from the current working directory and going up to the root
pkgFile = pkgPath ? findUp.sync(pkgPath, { cwd: options.cwd || process.cwd() }) : null
pkgFile = pkgPath
? findUp.sync(!options.packageFile && options.packageManager === 'deno' ? ['deno.json', 'deno.jsonc'] : pkgPath, {
cwd: options.cwd || process.cwd(),
})
: null
pkgData = getPackageDataFromFile(pkgFile, pkgPath)
}

Expand Down
6 changes: 1 addition & 5 deletions src/lib/initOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,7 @@ async function initOptions(runOptions: RunOptions, { cli }: { cli?: boolean } =

const resolvedOptions: Options = {
...options,
...(options.deep
? { packageFile: '**/package.json' }
: packageManager === 'deno' && !options.packageFile
? { packageFile: 'deno.json' }
: null),
...(options.deep ? { packageFile: '**/package.json' } : null),
...(packageManager === 'deno' && options.dep !== cliOptionsMap.dep.default ? { dep: ['imports'] } : null),
...(options.format && options.format.length > 0 ? { format: options.format } : null),
filter: args || filter,
Expand Down
9 changes: 6 additions & 3 deletions src/lib/runLocal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,16 @@ async function runLocal(
print(options, '\nOptions:', 'verbose')
print(options, sortOptions(options), 'verbose')

let pkg
let pkg: PackageFile

try {
if (!pkgData) {
throw new Error('Missing pkgData: ' + pkgData)
throw new Error('Missing pkgData')
} else {
pkg = jph.parse(pkgData)
// strip comments from jsonc files
const pkgDataStripped =
pkgFile?.endsWith('.jsonc') && pkgData ? (await import('strip-json-comments')).default(pkgData) : pkgData
pkg = jph.parse(pkgDataStripped)
}
} catch (e: any) {
programError(
Expand Down
75 changes: 71 additions & 4 deletions test/package-managers/deno/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ describe('deno', async function () {
'ncu-test-v2': 'npm:ncu-test-v2@1.0.0',
},
}
await fs.writeFile(pkgFile, JSON.stringify(pkg), 'utf-8')
await fs.writeFile(pkgFile, JSON.stringify(pkg))
try {
const pkgData = await spawn(
'node',
[bin, '--jsonUpgraded', '--verbose', '--packageManager', 'deno', '--packageFile', pkgFile],
[bin, '--jsonUpgraded', '--packageManager', 'deno', '--packageFile', pkgFile],
undefined,
)
const pkg = jph.parse(pkgData)
Expand All @@ -46,9 +46,9 @@ describe('deno', async function () {
'ncu-test-v2': 'npm:ncu-test-v2@1.0.0',
},
}
await fs.writeFile(pkgFile, JSON.stringify(pkg), 'utf-8')
await fs.writeFile(pkgFile, JSON.stringify(pkg))
try {
const pkgData = await spawn('node', [bin, '--jsonUpgraded', '--verbose'], undefined, {
const pkgData = await spawn('node', [bin, '--jsonUpgraded'], undefined, {
cwd: tempDir,
})
const pkg = jph.parse(pkgData)
Expand All @@ -57,4 +57,71 @@ describe('deno', async function () {
await fs.rm(tempDir, { recursive: true, force: true })
}
})

it('rewrite deno.json', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const pkgFile = path.join(tempDir, 'deno.json')
const pkg = {
imports: {
'ncu-test-v2': 'npm:ncu-test-v2@1.0.0',
},
}
await fs.writeFile(pkgFile, JSON.stringify(pkg))
try {
await spawn('node', [bin, '-u'], undefined, { cwd: tempDir })
const pkgDataNew = await fs.readFile(pkgFile, 'utf-8')
const pkg = jph.parse(pkgDataNew)
pkg.should.deep.equal({
imports: {
'ncu-test-v2': 'npm:ncu-test-v2@2.0.0',
},
})
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
}
})

it('auto detect deno.jsonc', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const pkgFile = path.join(tempDir, 'deno.jsonc')
const pkgString = `{
"imports": {
// this comment should be ignored in a jsonc file
"ncu-test-v2": "npm:ncu-test-v2@1.0.0"
}
}`
await fs.writeFile(pkgFile, pkgString)
try {
const pkgData = await spawn('node', [bin, '--jsonUpgraded'], undefined, {
cwd: tempDir,
})
const pkg = jph.parse(pkgData)
pkg.should.have.property('ncu-test-v2')
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
}
})

it('rewrite deno.jsonc', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-'))
const pkgFile = path.join(tempDir, 'deno.jsonc')
const pkg = {
imports: {
'ncu-test-v2': 'npm:ncu-test-v2@1.0.0',
},
}
await fs.writeFile(pkgFile, JSON.stringify(pkg))
try {
await spawn('node', [bin, '-u'], undefined, { cwd: tempDir })
const pkgDataNew = await fs.readFile(pkgFile, 'utf-8')
const pkg = jph.parse(pkgDataNew)
pkg.should.deep.equal({
imports: {
'ncu-test-v2': 'npm:ncu-test-v2@2.0.0',
},
})
} finally {
await fs.rm(tempDir, { recursive: true, force: true })
}
})
})

0 comments on commit fdf68f2

Please sign in to comment.