Skip to content

Commit

Permalink
feat: drop read-package-json-fast (#2)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
43081j authored Mar 5, 2024
1 parent d1686d1 commit 2baaead
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
10 changes: 7 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -51,5 +50,10 @@ export async function findPackage(cwd: string): Promise<Package | null> {
return null;
}

return rpj(packagePath);
try {
const source = await readFile(packagePath, {encoding: 'utf8'});
return JSON.parse(source);
} catch (_err) {
return null;
}
}
9 changes: 9 additions & 0 deletions src/test/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
5 changes: 5 additions & 0 deletions test/fixtures/broken-package/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "broken-package",
"private": true,
"version": "1.0.0",
"description": "I'm totally missing a closing brace",

0 comments on commit 2baaead

Please sign in to comment.