From c72a9d54d0e9ee1305a02b0293928a504b72275b Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 28 Nov 2023 08:03:31 -0800 Subject: [PATCH] properly classify json files as json Fix: #3 --- src/classify-module.ts | 6 ++++-- src/service/service.ts | 3 +-- tap-snapshots/test/classify-module.ts.test.cjs | 3 +++ test/classify-module.ts | 2 ++ 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/classify-module.ts b/src/classify-module.ts index e1201dc..291a647 100644 --- a/src/classify-module.ts +++ b/src/classify-module.ts @@ -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' diff --git a/src/service/service.ts b/src/service/service.ts index 9e0d561..13bcd24 100644 --- a/src/service/service.ts +++ b/src/service/service.ts @@ -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 {} diff --git a/tap-snapshots/test/classify-module.ts.test.cjs b/tap-snapshots/test/classify-module.ts.test.cjs index a6f7df7..b1251ed 100644 --- a/tap-snapshots/test/classify-module.ts.test.cjs +++ b/tap-snapshots/test/classify-module.ts.test.cjs @@ -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", @@ -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", @@ -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", diff --git a/test/classify-module.ts b/test/classify-module.ts index 0ec0de7..6a59cb4 100644 --- a/test/classify-module.ts +++ b/test/classify-module.ts @@ -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': '', @@ -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',