https://github.com/artdecocode/fpj
Resolves The Location Of The Package.Json File By Traversing The File System Up Starting From Given Path.
https://github.com/artdecocode/fpj
Last synced: 11 months ago
JSON representation
Resolves The Location Of The Package.Json File By Traversing The File System Up Starting From Given Path.
- Host: GitHub
- URL: https://github.com/artdecocode/fpj
- Owner: artdecocode
- License: mit
- Created: 2019-02-08T16:36:19.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2020-02-23T00:13:33.000Z (almost 6 years ago)
- Last Synced: 2025-02-08T07:45:24.533Z (11 months ago)
- Language: JavaScript
- Homepage: https://www.artd.eco
- Size: 61.5 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# fpj: **Find Package Json**
[](https://www.npmjs.com/package/fpj)
`fpj` Resolves The Location Of The Package.Json File For The Given Dependency By Traversing The File System Up Starting From Given Path.
```sh
yarn add -E fpj
```
## Table Of Contents
- [Table Of Contents](#table-of-contents)
- [API](#api)
- [`async fpj(dirname: string, packageName: string, opts?: FPJConfig): FPJReturn`](#async-fpjdirname-stringpackagename-stringopts-fpjconfig-fpjreturn)
* [`_fpj.Config`](#type-_fpjconfig)
* [`_fpj.Return`](#type-_fpjreturn)
- [Fields](#fields)
- [Soft Mode](#soft-mode)
- [Copyright](#copyright)
## API
The package is available by importing its default function:
```js
import fpj from 'fpj'
```
## async fpj(
`dirname: string,`
`packageName: string,`
`opts?: FPJConfig,`
): FPJReturn
Returns the resolved entry point to the package. It will start checking for the presence of packages using Node's algorithm by resolving the `node_modules` folder first inside of the given _dirname_, then if not found, traverse up and repeat, until root of the OS is reached.
The preference of the `entry` output will be given to the `module` field specified in the _package.json_. If the `main` is found instead, it will be indicated with `hasMain` property on the returned object.
`_fpj.Config`: The options for `fpj`.
| Name | Type | Description | Default |
| ------ | ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| fields | !Array<string> | Any additional fields from `package.json` file to return. | - |
| soft | boolean | If the entry export (main or module) does not exist, `soft` mode will not throw an error, but add the `hasEntry` property to the output set to _false_. | `false` |
_For example, the `package.json` files and meta information for 2 packages can be fetched using the following example:_
```js
/* yarn example/ */
import fpj from 'fpj'
import { dirname } from 'path'
(async () => {
const zoroaster = await fpj(
dirname('example/example.js'),
'zoroaster'
)
console.log(zoroaster)
const read = await fpj(
dirname('example/example.js'),
'@wrote/read'
)
console.log(read)
})()
```
_FPJ gives preference to the `module` field and will report it as the entry if it exists. Otherwise, the `main` is used with the `hasMain` property set to true:_
```js
{
entry: 'node_modules\\zoroaster\\depack\\index.js',
packageJson: 'node_modules\\zoroaster\\package.json',
version: '4.3.0',
packageName: 'zoroaster',
hasMain: true
}
{
entry: 'node_modules\\@wrote\\read\\src\\index.js',
packageJson: 'node_modules\\@wrote\\read\\package.json',
version: '1.0.4',
packageName: '@wrote/read'
}
```
`_fpj.Return`: The return type of the program.
| Name | Type | Description |
| ---------------- | ---------------- | ---------------------------------------------------------------------------------------- |
| __entry*__ | string | The location of the package's entry file. The preference is given to the `module` field. |
| __packageJson*__ | string | The path to the package.json file itself. |
| __packageName*__ | string | The name of the resolved package. |
| version | string | The version of the package. |
| hasMain | boolean | Whether the entry is the `main` rather than `module`. |
| entryExists | boolean | In soft mode, will be set to `false` if the entry file does not exist. |
## Fields
Any additional fields from `package.json` that need to be present in the output can be specified in the `fields` property.
```js
/* yarn example/ */
import fpj from 'fpj'
import { dirname } from 'path'
(async () => {
const zoroaster = await fpj(
dirname('example/example.js'),
'zoroaster',
{ fields: ['license', 'bin'] },
)
console.log(zoroaster)
})()
```
```js
{
entry: 'node_modules\\zoroaster\\depack\\index.js',
packageJson: 'node_modules\\zoroaster\\package.json',
version: '4.3.0',
packageName: 'zoroaster',
hasMain: true,
license: 'AGPL-3.0',
bin: {
zoroaster: 'depack/bin/zoroaster.js',
'zoroaster-dev': 'src/bin/index.js'
}
}
```
## Soft Mode
When a package exports either a main or a module fields, `fpj` will check for their existence to resolve the path to the entry. When the entry does not exist, by default an error will be thrown. To disable the error, and add the `entryExists: false` to the output, the _Soft Mode_ can be activated.
```js
/* yarn example/ */
import fpj from 'fpj'
import { dirname } from 'path'
(async () => {
const zoroaster = await fpj(
dirname('example/example.js'),
'myPackage',
{ soft: true },
)
console.log(zoroaster)
})()
```
```js
{
entry: 'example\\node_modules\\myPackage\\index.js',
packageJson: 'example\\node_modules\\myPackage\\package.json',
version: '1.0.0',
packageName: 'myPackage',
hasMain: true,
entryExists: false
}
```
## Copyright
© Art Deco™ 2020