From 2baaead671c380aaeadd0c455e1386dfe374a0df Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Tue, 5 Mar 2024 21:30:27 +0000 Subject: [PATCH] feat: drop read-package-json-fast (#2) This removes the `read-package-json-fast` dependency since we don't actually need the normalisation it provides. It is soon being merged into npm's CLI package too, meaning we will have a rather heavy dependency for what's basically the task of reading a file. Makes sense to drop it. --- package.json | 1 - src/main.ts | 10 +++++++--- src/test/main_test.ts | 9 +++++++++ test/fixtures/broken-package/package.json | 5 +++++ 4 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/broken-package/package.json diff --git a/package.json b/package.json index 65d1fe0..2cbd064 100644 --- a/package.json +++ b/package.json @@ -75,7 +75,6 @@ "main": "./dist/commonjs/main.js", "types": "./dist/commonjs/main.d.ts", "dependencies": { - "read-package-json-fast": "^3.0.2", "walk-up-path": "^3.0.1" } } diff --git a/src/main.ts b/src/main.ts index 3f14adf..875b194 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,7 +1,6 @@ import {walkUp} from 'walk-up-path'; import {resolve} from 'node:path'; -import {stat} from 'node:fs/promises'; -import rpj from 'read-package-json-fast'; +import {stat, readFile} from 'node:fs/promises'; /** * Determines if a file exists or not @@ -51,5 +50,10 @@ export async function findPackage(cwd: string): Promise { return null; } - return rpj(packagePath); + try { + const source = await readFile(packagePath, {encoding: 'utf8'}); + return JSON.parse(source); + } catch (_err) { + return null; + } } diff --git a/src/test/main_test.ts b/src/test/main_test.ts index 72459d4..5aa9c78 100644 --- a/src/test/main_test.ts +++ b/src/test/main_test.ts @@ -61,4 +61,13 @@ test('findPackage', async (t) => { assert.equal(result!.name, 'simple-package'); }); + + await t.test('null for non-existent packages', async () => { + assert.equal(await findPackage('/'), null); + }); + + await t.test('null for invalid package.json', async () => { + const fixturePath = joinPath(currentDir, 'test/fixtures/broken-package'); + assert.equal(await findPackage(fixturePath), null); + }); }); diff --git a/test/fixtures/broken-package/package.json b/test/fixtures/broken-package/package.json new file mode 100644 index 0000000..89114fa --- /dev/null +++ b/test/fixtures/broken-package/package.json @@ -0,0 +1,5 @@ +{ + "name": "broken-package", + "private": true, + "version": "1.0.0", + "description": "I'm totally missing a closing brace",