load-plugin
Advanced tools
Comparing version 4.0.1 to 5.0.0
@@ -10,3 +10,3 @@ /** | ||
name: string, | ||
options?: LoadOptions | ||
options?: LoadOptions | undefined | ||
): Promise<unknown> | ||
@@ -28,12 +28,12 @@ /** | ||
* @param {ResolveOptions} [options] | ||
* @returns {Promise.<string>} | ||
* @returns {Promise<string>} | ||
*/ | ||
export function resolvePlugin( | ||
name: string, | ||
options?: ResolveOptions | ||
options?: ResolveOptions | undefined | ||
): Promise<string> | ||
export type ResolveOptions = { | ||
prefix?: string | ||
cwd?: string | string[] | ||
global?: boolean | ||
prefix?: string | undefined | ||
cwd?: string | string[] | undefined | ||
global?: boolean | undefined | ||
} | ||
@@ -40,0 +40,0 @@ export type LoadOptions = ResolveOptions & { |
46
index.js
/** | ||
* @typedef ResolveOptions | ||
* @property {string} [prefix] | ||
* @property {string|string[]} [cwd] | ||
* @property {string|Array<string>} [cwd] | ||
* @property {boolean} [global] | ||
*/ | ||
/** | ||
* | ||
* @typedef {ResolveOptions & {key?: string|false}} LoadOptions | ||
@@ -13,6 +11,8 @@ */ | ||
import fs from 'fs' | ||
import process from 'process' | ||
import {pathToFileURL, fileURLToPath} from 'url' | ||
import path from 'path' | ||
// @ts-expect-error: untyped | ||
import NpmConfig from '@npmcli/config' | ||
import {resolve as esmResolve} from 'import-meta-resolve' | ||
import libNpmConfig from 'libnpmconfig' | ||
@@ -24,3 +24,2 @@ const electron = process.versions.electron !== undefined | ||
const nvm = process.env.NVM_BIN | ||
const appData = process.env.APPDATA | ||
@@ -30,19 +29,8 @@ /* c8 ignore next */ | ||
/** @type {{prefix?: string}} */ | ||
let builtinNpmConfig | ||
const config = new NpmConfig({definitions: {}}) | ||
// The prefix config defaults to the location where node is installed. | ||
// On Windows, this is in a place called `%AppData%`, which we have to | ||
// pass to `libnpmconfig` explicitly: | ||
/* c8 ignore next 4 */ | ||
if (windows && appData) { | ||
builtinNpmConfig = {prefix: path.join(appData, 'npm')} | ||
} | ||
config.loadGlobalPrefix() | ||
/** | ||
* Note: `libnpmconfig` uses `figgy-pudding` which is slated for archival. | ||
* Either `libnpmconfig` will switch to an alternative or we’ll have to. | ||
* @type {string} | ||
*/ | ||
let npmPrefix = libNpmConfig.read(null, builtinNpmConfig).prefix | ||
/** @type {string} */ | ||
let npmPrefix = config.globalPrefix | ||
@@ -84,3 +72,3 @@ // If there is no prefix defined, use the defaults | ||
const fp = await resolvePlugin(name, rest) | ||
/** @type {Object.<string, unknown>} */ | ||
/** @type {Record<string, unknown>} */ | ||
// Bug with coverage on Node@12. | ||
@@ -107,3 +95,3 @@ /* c8 ignore next 3 */ | ||
* @param {ResolveOptions} [options] | ||
* @returns {Promise.<string>} | ||
* @returns {Promise<string>} | ||
*/ | ||
@@ -121,5 +109,5 @@ export async function resolvePlugin(name, options = {}) { | ||
const sources = Array.isArray(cwd) ? cwd.concat() : [cwd || process.cwd()] | ||
/** @type {string} */ | ||
/** @type {string|undefined} */ | ||
let plugin | ||
/** @type {Error} */ | ||
/** @type {Error|undefined} */ | ||
let lastError | ||
@@ -159,7 +147,7 @@ | ||
let index = -1 | ||
/** @type {string} */ | ||
/** @type {string|undefined} */ | ||
let fp | ||
while (++index < sources.length) { | ||
fp = plugin && (await attempt(sources[index], plugin)) | ||
fp = plugin ? await attempt(sources[index], plugin) : undefined | ||
if (fp) return fp | ||
@@ -179,3 +167,3 @@ | ||
* @param {string} name | ||
* @returns {Promise<string>} | ||
* @returns {Promise<string|undefined>} | ||
*/ | ||
@@ -192,5 +180,5 @@ async function attempt(base, name) { | ||
} catch (error) { | ||
lastError = error | ||
lastError = /** @type {Error} */ (error) | ||
} | ||
} | ||
} |
{ | ||
"name": "load-plugin", | ||
"version": "4.0.1", | ||
"version": "5.0.0", | ||
"description": "Load a submodule, plugin, or file", | ||
@@ -31,4 +31,4 @@ "license": "MIT", | ||
"dependencies": { | ||
"import-meta-resolve": "^1.0.0", | ||
"libnpmconfig": "^1.0.0" | ||
"@npmcli/config": "^4.0.0", | ||
"import-meta-resolve": "^2.0.0" | ||
}, | ||
@@ -39,5 +39,5 @@ "devDependencies": { | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-lint": "^8.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"remark-cli": "^10.0.0", | ||
"remark-lint": "^9.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"rimraf": "^3.0.0", | ||
@@ -47,3 +47,3 @@ "tape": "^5.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.39.0" | ||
"xo": "^0.49.0" | ||
}, | ||
@@ -69,2 +69,3 @@ "scripts": { | ||
"rules": { | ||
"unicorn/prefer-node-protocol": "off", | ||
"no-await-in-loop": "off" | ||
@@ -71,0 +72,0 @@ } |
@@ -7,13 +7,37 @@ # load-plugin | ||
Load a submodule, plugin, or file. | ||
Like Node’s `require` and `require.resolve`, but from one or more places, and | ||
optionally global too. | ||
Load submodules, plugins, or files. | ||
## Contents | ||
* [What is this?](#what-is-this) | ||
* [When to use this?](#when-to-use-this) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`loadPlugin(name[, options])`](#loadpluginname-options) | ||
* [`resolvePlugin(name[, options])`](#resolvepluginname-options) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
## What is this? | ||
This package is useful when you want to load plugins. | ||
It resolves things like Node.js does, but supports a prefix (e.g., when given a | ||
prefix `remark` and the user provided value `gfm`, it can find `remark-gfm`), | ||
can load from several places, and optionally global too. | ||
## When to use this? | ||
This package is particularly useful when you want users to configure something | ||
with plugins. | ||
One example is `remark-cli` which can load remark plugins from configuration | ||
files. | ||
## Install | ||
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): | ||
Node 12+ is needed to use it and it must be `import`ed instead of `require`d. | ||
This package is [ESM only][esm]. | ||
In Node.js (version 14.14+, 16.0+, or 18.0+), install with [npm][]: | ||
[npm][]: | ||
```sh | ||
@@ -30,17 +54,13 @@ npm install load-plugin | ||
main() | ||
console.log(await resolvePlugin('lint', {prefix: 'remark'})) | ||
// => '/Users/tilde/projects/oss/load-plugin/node_modules/remark-lint/index.js' | ||
async function main() { | ||
await resolvePlugin('lint', {prefix: 'remark'}) | ||
// => '/Users/tilde/projects/oss/load-plugin/node_modules/remark-lint/index.js' | ||
console.log(await resolvePlugin('validator-identifier', {prefix: '@babel/helper'})) | ||
// => '/Users/tilde/Projects/oss/load-plugin/node_modules/@babel/helper-validator-identifier/lib/index.js' | ||
await resolvePlugin('@babel/function-name', {prefix: 'helper'}) | ||
// => '/Users/tilde/projects/oss/load-plugin/node_modules/@babel/helper-function-name/lib/index.js' | ||
console.log(await resolvePlugin('./index.js', {prefix: 'remark'})) | ||
// => '/Users/tilde/projects/oss/load-plugin/index.js' | ||
await resolvePlugin('./index.js', {prefix: 'remark'}) | ||
// => '/Users/tilde/projects/oss/load-plugin/index.js' | ||
await loadPlugin('lint', {prefix: 'remark'}) | ||
// => [Function: lint] | ||
} | ||
console.log(await loadPlugin('lint', {prefix: 'remark'})) | ||
// => [Function: remarkLint] | ||
``` | ||
@@ -50,3 +70,3 @@ | ||
This package exports the following identifiers: `loadPlugin`, `resolvePlugin`. | ||
This package exports the identifiers `loadPlugin` and `resolvePlugin`. | ||
There is no default export. | ||
@@ -68,2 +88,4 @@ | ||
Configuration (optional). | ||
###### `options.prefix` | ||
@@ -75,3 +97,3 @@ | ||
Place or places to search from (`string`, `Array.<string>`, default: | ||
Place or places to search from (`string`, `Array<string>`, default: | ||
`process.cwd()`). | ||
@@ -100,5 +122,5 @@ | ||
`Promise.<unknown>` — Promise yielding the results of `require`ing the first | ||
path that exists. | ||
The promise rejects if `require`ing an existing path fails, or if no existing | ||
Promise yielding the results of importing the first path that exists | ||
(`Promise<unknown>`). | ||
The promise rejects if importing an existing path fails, or if no existing | ||
path exists. | ||
@@ -110,6 +132,21 @@ | ||
Accepts the same parameters as [`loadPlugin`][load-plugin] (except `key`) but | ||
returns a promise resolving to an absolute path for `name` instead of importing | ||
it. | ||
returns a promise resolving to an absolute URL (`string`) for `name` instead of | ||
importing it. | ||
Throws if `name` cannot be found. | ||
## Types | ||
This package is fully typed with [TypeScript][]. | ||
It exports the additional types `ResolveOptions` and `LoadOptions`. | ||
## Compatibility | ||
This package is at least compatible with all maintained versions of Node.js. | ||
As of now, that is Node.js 14.14+, 16.0+, and 18.0+. | ||
## Contribute | ||
Yes please! | ||
See [How to Contribute to Open Source][contribute]. | ||
## License | ||
@@ -139,6 +176,10 @@ | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[typescript]: https://www.typescriptlang.org | ||
[contribute]: https://opensource.guide/how-to-contribute/ | ||
[global]: https://docs.npmjs.com/files/folders#node-modules | ||
[load-plugin]: #loadpluginname-options | ||
[prefix]: https://docs.npmjs.com/misc/config#prefix | ||
@@ -153,1 +194,3 @@ | ||
[import-meta-resolve]: https://github.com/wooorm/import-meta-resolve | ||
[load-plugin]: #loadpluginname-options |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
14843
188
3
192
+ Added@npmcli/config@^4.0.0
+ Added@npmcli/config@4.2.2(transitive)
+ Added@npmcli/map-workspaces@2.0.4(transitive)
+ Added@npmcli/name-from-folder@1.0.1(transitive)
+ Addedabbrev@1.1.1(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@2.0.1(transitive)
+ Addedchownr@2.0.0(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedglob@8.1.0(transitive)
+ Addedimport-meta-resolve@2.2.2(transitive)
+ Addedinfer-owner@1.0.4(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedini@3.0.1(transitive)
+ Addedjson-parse-even-better-errors@2.3.1(transitive)
+ Addedminimatch@5.1.6(transitive)
+ Addedmkdirp@1.0.4(transitive)
+ Addedmkdirp-infer-owner@2.0.0(transitive)
+ Addednopt@6.0.0(transitive)
+ Addednpm-normalize-package-bin@1.0.1(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedproc-log@2.0.1(transitive)
+ Addedread-package-json-fast@2.0.3(transitive)
+ Addedwalk-up-path@1.0.0(transitive)
+ Addedwrappy@1.0.2(transitive)
- Removedlibnpmconfig@^1.0.0
- Removedbuiltins@4.1.0(transitive)
- Removedfiggy-pudding@3.5.2(transitive)
- Removedfind-up@3.0.0(transitive)
- Removedimport-meta-resolve@1.1.1(transitive)
- Removedini@1.3.8(transitive)
- Removedlibnpmconfig@1.2.1(transitive)
- Removedlocate-path@3.0.0(transitive)
- Removedp-limit@2.3.0(transitive)
- Removedp-locate@3.0.0(transitive)
- Removedp-try@2.2.0(transitive)
- Removedpath-exists@3.0.0(transitive)
Updatedimport-meta-resolve@^2.0.0