Skip to content

Commit 953c08e

Browse files
authored
Merge pull request #20 from constructive-io/fix/pkg2
Fix/pkg2
2 parents f354f08 + 21d32d1 commit 953c08e

File tree

5 files changed

+26
-11
lines changed

5 files changed

+26
-11
lines changed

packages/find-and-require-package-json/README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</a>
1414
</p>
1515

16-
This TypeScript module provides a function to locate, read, and parse the `package.json` file from the current directory or any of its parent directories.
16+
This TypeScript module provides a function to locate, read, and parse the `package.json` file starting from a specified directory and searching up through parent directories.
1717

1818
## install
1919

@@ -26,7 +26,21 @@ npm install find-and-require-package-json
2626
```js
2727
import { findAndRequirePackageJson } from 'find-and-require-package-json';
2828

29-
const packageJson = findAndRequirePackageJson();
29+
// Pass __dirname to find the package.json for your module
30+
const packageJson = findAndRequirePackageJson(__dirname);
3031
console.log('Package name:', packageJson.name);
3132
console.log('Version:', packageJson.version);
32-
```
33+
```
34+
35+
### ESM Usage
36+
37+
For ESM modules, you'll need to convert `import.meta.url` to a directory path:
38+
39+
```js
40+
import { findAndRequirePackageJson } from 'find-and-require-package-json';
41+
import { dirname } from 'path';
42+
import { fileURLToPath } from 'url';
43+
44+
const __dirname = dirname(fileURLToPath(import.meta.url));
45+
const packageJson = findAndRequirePackageJson(__dirname);
46+
```

packages/find-and-require-package-json/__tests__/find-pkg.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('findAndRequirePackageJson', () => {
3838

3939
jest.spyOn(process, 'cwd').mockReturnValue(mockCurrentDir);
4040

41-
const result = findAndRequirePackageJson();
41+
const result = findAndRequirePackageJson(mockCurrentDir);
4242

4343
expect(result).toEqual(mockPackageJson);
4444
expect(existsSync).toHaveBeenCalledWith(mockFilePath);
@@ -57,7 +57,7 @@ describe('findAndRequirePackageJson', () => {
5757

5858
jest.spyOn(process, 'cwd').mockReturnValue(mockCurrentDir);
5959

60-
expect(() => findAndRequirePackageJson()).toThrow(
60+
expect(() => findAndRequirePackageJson(mockCurrentDir)).toThrow(
6161
'package.json not found in any parent directory'
6262
);
6363
expect(existsSync).toHaveBeenCalledWith(mockFilePath);

packages/find-and-require-package-json/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "find-and-require-package-json",
3-
"version": "0.6.8",
3+
"version": "0.7.0",
44
"author": "Dan Lynch <[email protected]>",
55
"description": "Find the package.json file from within a build/package",
66
"main": "index.js",

packages/find-and-require-package-json/src/package.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ function _findPackageJson(currentDir: string): string {
3636
return _findPackageJson(parentDir);
3737
}
3838

39-
export function findAndRequirePackageJson(): PackageJson {
40-
// Start searching from the current directory
41-
const pkgPath = _findPackageJson(__dirname);
39+
export function findAndRequirePackageJson(callerDir: string): PackageJson {
40+
// Start searching from the caller's directory
41+
// The caller should pass __dirname (CJS) or dirname(fileURLToPath(import.meta.url)) (ESM)
42+
const pkgPath = _findPackageJson(callerDir);
4243

4344
// Read and parse the package.json
4445
const str = readFileSync(pkgPath, 'utf8');

packages/inquirerer/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { findAndRequirePackageJson } from "find-and-require-package-json";
33

44
// Function to display the version information
55
export function displayVersion(): any {
6-
const pkg = findAndRequirePackageJson();
6+
const pkg = findAndRequirePackageJson(__dirname);
77
console.log(green(`Name: ${pkg.name}`));
88
console.log(blue(`Version: ${pkg.version}`));
99
}
1010

1111

1212
export function getVersion(): string {
13-
const pkg = findAndRequirePackageJson();
13+
const pkg = findAndRequirePackageJson(__dirname);
1414
return pkg.version;
1515
}

0 commit comments

Comments
 (0)