find-and-require-package-json 0.6.8 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -2
- package/esm/package.js +4 -3
- package/package.d.ts +1 -1
- package/package.js +4 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
</a>
|
|
14
14
|
</p>
|
|
15
15
|
|
|
16
|
-
This TypeScript module provides a function to locate, read, and parse the `package.json` file from
|
|
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.
|
|
17
17
|
|
|
18
18
|
## install
|
|
19
19
|
|
|
@@ -26,11 +26,25 @@ npm install find-and-require-package-json
|
|
|
26
26
|
```js
|
|
27
27
|
import { findAndRequirePackageJson } from 'find-and-require-package-json';
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
// Pass __dirname to find the package.json for your module
|
|
30
|
+
const packageJson = findAndRequirePackageJson(__dirname);
|
|
30
31
|
console.log('Package name:', packageJson.name);
|
|
31
32
|
console.log('Version:', packageJson.version);
|
|
32
33
|
```
|
|
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
|
+
```
|
|
47
|
+
|
|
34
48
|
---
|
|
35
49
|
|
|
36
50
|
## Development
|
package/esm/package.js
CHANGED
|
@@ -16,9 +16,10 @@ function _findPackageJson(currentDir) {
|
|
|
16
16
|
// Recursively look in the parent directory
|
|
17
17
|
return _findPackageJson(parentDir);
|
|
18
18
|
}
|
|
19
|
-
export function findAndRequirePackageJson() {
|
|
20
|
-
// Start searching from the
|
|
21
|
-
|
|
19
|
+
export function findAndRequirePackageJson(callerDir) {
|
|
20
|
+
// Start searching from the caller's directory
|
|
21
|
+
// The caller should pass __dirname (CJS) or dirname(fileURLToPath(import.meta.url)) (ESM)
|
|
22
|
+
const pkgPath = _findPackageJson(callerDir);
|
|
22
23
|
// Read and parse the package.json
|
|
23
24
|
const str = readFileSync(pkgPath, 'utf8');
|
|
24
25
|
const pkg = JSON.parse(str);
|
package/package.d.ts
CHANGED
package/package.js
CHANGED
|
@@ -19,9 +19,10 @@ function _findPackageJson(currentDir) {
|
|
|
19
19
|
// Recursively look in the parent directory
|
|
20
20
|
return _findPackageJson(parentDir);
|
|
21
21
|
}
|
|
22
|
-
function findAndRequirePackageJson() {
|
|
23
|
-
// Start searching from the
|
|
24
|
-
|
|
22
|
+
function findAndRequirePackageJson(callerDir) {
|
|
23
|
+
// Start searching from the caller's directory
|
|
24
|
+
// The caller should pass __dirname (CJS) or dirname(fileURLToPath(import.meta.url)) (ESM)
|
|
25
|
+
const pkgPath = _findPackageJson(callerDir);
|
|
25
26
|
// Read and parse the package.json
|
|
26
27
|
const str = (0, fs_1.readFileSync)(pkgPath, 'utf8');
|
|
27
28
|
const pkg = JSON.parse(str);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "find-and-require-package-json",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
5
5
|
"description": "Find the package.json file from within a build/package",
|
|
6
6
|
"main": "index.js",
|
|
@@ -32,5 +32,5 @@
|
|
|
32
32
|
"makage": "0.1.8"
|
|
33
33
|
},
|
|
34
34
|
"keywords": [],
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "a704f3ec0e53747e4fba85c2018138439120f309"
|
|
36
36
|
}
|