Skip to content

Commit

Permalink
properly classify json files as json
Browse files Browse the repository at this point in the history
Fix: #3
  • Loading branch information
isaacs committed Nov 28, 2023
1 parent cff7901 commit c72a9d5
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/classify-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ const readPJType = cachedMtime(
})
)

export const classifyModule = (fileName: string) => {
if (fileName.endsWith('.cts') || fileName.endsWith('.cjs')) {
export const classifyModule = (fileName: string): PackageJsonType | 'json' => {
if (fileName.endsWith('.json')) {
return 'json'
} if (fileName.endsWith('.cts') || fileName.endsWith('.cjs')) {
return 'commonjs'
} else if (fileName.endsWith('.mts') || fileName.endsWith('.mjs')) {
return 'module'
Expand Down
3 changes: 1 addition & 2 deletions src/service/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ export class DaemonServer extends SockDaemonServer<
if (target.startsWith('file://')) {
const tsFile = findTsFile(target)
if (tsFile) {
const ret = String(pathToFileURL(tsFile))
return { fileName: ret }
return { fileName: String(pathToFileURL(tsFile)) }
}
}
return {}
Expand Down
3 changes: 3 additions & 0 deletions tap-snapshots/test/classify-module.ts.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Object {
"index.cjs": "commonjs",
"index.cts": "commonjs",
"index.js": "commonjs",
"index.json": "json",
"index.jsx": "commonjs",
"index.mjs": "module",
"index.mts": "module",
Expand All @@ -23,6 +24,7 @@ Object {
"index.cjs": "commonjs",
"index.cts": "commonjs",
"index.js": "commonjs",
"index.json": "json",
"index.jsx": "commonjs",
"index.mjs": "module",
"index.mts": "module",
Expand All @@ -36,6 +38,7 @@ Object {
"index.cjs": "commonjs",
"index.cts": "commonjs",
"index.js": "module",
"index.json": "json",
"index.jsx": "module",
"index.mjs": "module",
"index.mts": "module",
Expand Down
2 changes: 2 additions & 0 deletions test/classify-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { classifyModule } from '../src/classify-module.js'

t.test('classify some modules', t => {
const files = {
'index.json': '',
'index.cjs': '',
'index.cts': '',
'index.mjs': '',
Expand Down Expand Up @@ -67,6 +68,7 @@ t.test('classify some modules', t => {
t.strictSame(
results,
{
'index.json': 'json',
'index.cjs': 'commonjs',
'index.cts': 'commonjs',
'index.mjs': 'module',
Expand Down

0 comments on commit c72a9d5

Please sign in to comment.