Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions packages/find-and-require-package-json/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</a>
</p>

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.
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.

## install

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

const packageJson = findAndRequirePackageJson();
// Pass __dirname to find the package.json for your module
const packageJson = findAndRequirePackageJson(__dirname);
console.log('Package name:', packageJson.name);
console.log('Version:', packageJson.version);
```
```

### ESM Usage

For ESM modules, you'll need to convert `import.meta.url` to a directory path:

```js
import { findAndRequirePackageJson } from 'find-and-require-package-json';
import { dirname } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url));
const packageJson = findAndRequirePackageJson(__dirname);
```
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('findAndRequirePackageJson', () => {

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

const result = findAndRequirePackageJson();
const result = findAndRequirePackageJson(mockCurrentDir);

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

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

expect(() => findAndRequirePackageJson()).toThrow(
expect(() => findAndRequirePackageJson(mockCurrentDir)).toThrow(
'package.json not found in any parent directory'
);
expect(existsSync).toHaveBeenCalledWith(mockFilePath);
Expand Down
2 changes: 1 addition & 1 deletion packages/find-and-require-package-json/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "find-and-require-package-json",
"version": "0.6.8",
"version": "0.7.0",
"author": "Dan Lynch <[email protected]>",
"description": "Find the package.json file from within a build/package",
"main": "index.js",
Expand Down
7 changes: 4 additions & 3 deletions packages/find-and-require-package-json/src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ function _findPackageJson(currentDir: string): string {
return _findPackageJson(parentDir);
}

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

// Read and parse the package.json
const str = readFileSync(pkgPath, 'utf8');
Expand Down
4 changes: 2 additions & 2 deletions packages/inquirerer/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { findAndRequirePackageJson } from "find-and-require-package-json";

// Function to display the version information
export function displayVersion(): any {
const pkg = findAndRequirePackageJson();
const pkg = findAndRequirePackageJson(__dirname);
console.log(green(`Name: ${pkg.name}`));
console.log(blue(`Version: ${pkg.version}`));
}


export function getVersion(): string {
const pkg = findAndRequirePackageJson();
const pkg = findAndRequirePackageJson(__dirname);
return pkg.version;
}
Loading