load-plugin 4.0.1 → 5.1.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/index.d.ts +6 -6
- package/index.js +17 -29
- package/package.json +12 -13
- package/readme.md +72 -28
package/index.d.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
export function loadPlugin(
|
|
9
9
|
name: string,
|
|
10
|
-
options?: LoadOptions
|
|
10
|
+
options?: LoadOptions | undefined
|
|
11
11
|
): Promise<unknown>
|
|
12
12
|
/**
|
|
13
13
|
* Find a plugin.
|
|
@@ -24,16 +24,16 @@ export function loadPlugin(
|
|
|
24
24
|
*
|
|
25
25
|
* @param {string} name
|
|
26
26
|
* @param {ResolveOptions} [options]
|
|
27
|
-
* @returns {Promise
|
|
27
|
+
* @returns {Promise<string>}
|
|
28
28
|
*/
|
|
29
29
|
export function resolvePlugin(
|
|
30
30
|
name: string,
|
|
31
|
-
options?: ResolveOptions
|
|
31
|
+
options?: ResolveOptions | undefined
|
|
32
32
|
): Promise<string>
|
|
33
33
|
export type ResolveOptions = {
|
|
34
|
-
prefix?: string
|
|
35
|
-
cwd?: string | string[]
|
|
36
|
-
global?: boolean
|
|
34
|
+
prefix?: string | undefined
|
|
35
|
+
cwd?: string | string[] | undefined
|
|
36
|
+
global?: boolean | undefined
|
|
37
37
|
}
|
|
38
38
|
export type LoadOptions = ResolveOptions & {
|
|
39
39
|
key?: string | false
|
package/index.js
CHANGED
|
@@ -1,47 +1,35 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @typedef ResolveOptions
|
|
3
3
|
* @property {string} [prefix]
|
|
4
|
-
* @property {string|string
|
|
4
|
+
* @property {string|Array<string>} [cwd]
|
|
5
5
|
* @property {boolean} [global]
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
/**
|
|
6
|
+
*
|
|
9
7
|
* @typedef {ResolveOptions & {key?: string|false}} LoadOptions
|
|
10
8
|
*/
|
|
11
9
|
|
|
12
10
|
import fs from 'fs'
|
|
11
|
+
import process from 'process'
|
|
13
12
|
import {pathToFileURL, fileURLToPath} from 'url'
|
|
14
13
|
import path from 'path'
|
|
14
|
+
// @ts-expect-error: untyped
|
|
15
|
+
import NpmConfig from '@npmcli/config'
|
|
15
16
|
import {resolve as esmResolve} from 'import-meta-resolve'
|
|
16
|
-
import libNpmConfig from 'libnpmconfig'
|
|
17
17
|
|
|
18
18
|
const electron = process.versions.electron !== undefined
|
|
19
19
|
const windows = process.platform === 'win32'
|
|
20
20
|
|
|
21
21
|
const argv = process.argv[1] || /* c8 ignore next */ ''
|
|
22
22
|
const nvm = process.env.NVM_BIN
|
|
23
|
-
const appData = process.env.APPDATA
|
|
24
23
|
|
|
25
24
|
/* c8 ignore next */
|
|
26
25
|
const globalsLibrary = windows ? '' : 'lib'
|
|
27
26
|
|
|
28
|
-
|
|
29
|
-
let builtinNpmConfig
|
|
27
|
+
const config = new NpmConfig({definitions: {}})
|
|
30
28
|
|
|
31
|
-
|
|
32
|
-
// On Windows, this is in a place called `%AppData%`, which we have to
|
|
33
|
-
// pass to `libnpmconfig` explicitly:
|
|
34
|
-
/* c8 ignore next 4 */
|
|
35
|
-
if (windows && appData) {
|
|
36
|
-
builtinNpmConfig = {prefix: path.join(appData, 'npm')}
|
|
37
|
-
}
|
|
29
|
+
config.loadGlobalPrefix()
|
|
38
30
|
|
|
39
|
-
/**
|
|
40
|
-
|
|
41
|
-
* Either `libnpmconfig` will switch to an alternative or we’ll have to.
|
|
42
|
-
* @type {string}
|
|
43
|
-
*/
|
|
44
|
-
let npmPrefix = libNpmConfig.read(null, builtinNpmConfig).prefix
|
|
31
|
+
/** @type {string} */
|
|
32
|
+
let npmPrefix = config.globalPrefix
|
|
45
33
|
|
|
46
34
|
// If there is no prefix defined, use the defaults
|
|
47
35
|
// See: <https://github.com/eush77/npm-prefix/blob/master/index.js>
|
|
@@ -78,7 +66,7 @@ if (electron && nvm && !fs.existsSync(globalDir)) {
|
|
|
78
66
|
export async function loadPlugin(name, options = {}) {
|
|
79
67
|
const {key = 'default', ...rest} = options
|
|
80
68
|
const fp = await resolvePlugin(name, rest)
|
|
81
|
-
/** @type {
|
|
69
|
+
/** @type {Record<string, unknown>} */
|
|
82
70
|
// Bug with coverage on Node@12.
|
|
83
71
|
/* c8 ignore next 3 */
|
|
84
72
|
const mod = await import(pathToFileURL(fp).href)
|
|
@@ -100,7 +88,7 @@ export async function loadPlugin(name, options = {}) {
|
|
|
100
88
|
*
|
|
101
89
|
* @param {string} name
|
|
102
90
|
* @param {ResolveOptions} [options]
|
|
103
|
-
* @returns {Promise
|
|
91
|
+
* @returns {Promise<string>}
|
|
104
92
|
*/
|
|
105
93
|
export async function resolvePlugin(name, options = {}) {
|
|
106
94
|
const prefix = options.prefix
|
|
@@ -113,9 +101,9 @@ export async function resolvePlugin(name, options = {}) {
|
|
|
113
101
|
? globalsDefault
|
|
114
102
|
: options.global
|
|
115
103
|
const sources = Array.isArray(cwd) ? cwd.concat() : [cwd || process.cwd()]
|
|
116
|
-
/** @type {string} */
|
|
104
|
+
/** @type {string|undefined} */
|
|
117
105
|
let plugin
|
|
118
|
-
/** @type {Error} */
|
|
106
|
+
/** @type {Error|undefined} */
|
|
119
107
|
let lastError
|
|
120
108
|
|
|
121
109
|
// Non-path.
|
|
@@ -150,11 +138,11 @@ export async function resolvePlugin(name, options = {}) {
|
|
|
150
138
|
}
|
|
151
139
|
|
|
152
140
|
let index = -1
|
|
153
|
-
/** @type {string} */
|
|
141
|
+
/** @type {string|undefined} */
|
|
154
142
|
let fp
|
|
155
143
|
|
|
156
144
|
while (++index < sources.length) {
|
|
157
|
-
fp = plugin
|
|
145
|
+
fp = plugin ? await attempt(sources[index], plugin) : undefined
|
|
158
146
|
if (fp) return fp
|
|
159
147
|
|
|
160
148
|
fp = await attempt(sources[index], name)
|
|
@@ -169,7 +157,7 @@ export async function resolvePlugin(name, options = {}) {
|
|
|
169
157
|
/**
|
|
170
158
|
* @param {string} base
|
|
171
159
|
* @param {string} name
|
|
172
|
-
* @returns {Promise<string>}
|
|
160
|
+
* @returns {Promise<string|undefined>}
|
|
173
161
|
*/
|
|
174
162
|
async function attempt(base, name) {
|
|
175
163
|
try {
|
|
@@ -181,7 +169,7 @@ export async function resolvePlugin(name, options = {}) {
|
|
|
181
169
|
// Bug with coverage on Node@12.
|
|
182
170
|
/* c8 ignore next 1 */
|
|
183
171
|
} catch (error) {
|
|
184
|
-
lastError = error
|
|
172
|
+
lastError = /** @type {Error} */ (error)
|
|
185
173
|
}
|
|
186
174
|
}
|
|
187
175
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "load-plugin",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"description": "Load a submodule, plugin, or file",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
@@ -28,28 +28,26 @@
|
|
|
28
28
|
"index.js"
|
|
29
29
|
],
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"
|
|
32
|
-
"
|
|
31
|
+
"@npmcli/config": "^6.0.0",
|
|
32
|
+
"import-meta-resolve": "^2.0.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@types/
|
|
35
|
+
"@types/node": "^18.0.0",
|
|
36
36
|
"c8": "^7.0.0",
|
|
37
37
|
"prettier": "^2.0.0",
|
|
38
|
-
"remark-cli": "^
|
|
39
|
-
"remark-lint": "^
|
|
40
|
-
"remark-preset-wooorm": "^
|
|
41
|
-
"rimraf": "^3.0.0",
|
|
42
|
-
"tape": "^5.0.0",
|
|
38
|
+
"remark-cli": "^11.0.0",
|
|
39
|
+
"remark-lint": "^9.0.0",
|
|
40
|
+
"remark-preset-wooorm": "^9.0.0",
|
|
43
41
|
"type-coverage": "^2.0.0",
|
|
44
42
|
"typescript": "^4.0.0",
|
|
45
|
-
"xo": "^0.
|
|
43
|
+
"xo": "^0.53.0"
|
|
46
44
|
},
|
|
47
45
|
"scripts": {
|
|
48
46
|
"prepack": "npm run build && npm run format",
|
|
49
|
-
"build": "
|
|
47
|
+
"build": "tsc --build --clean && tsc --build && type-coverage",
|
|
50
48
|
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
|
|
51
|
-
"test-api": "node test/index.js",
|
|
52
|
-
"test-coverage": "c8 --check-coverage --
|
|
49
|
+
"test-api": "node --conditions development test/index.js",
|
|
50
|
+
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api",
|
|
53
51
|
"test": "npm run build && npm run format && npm run test-coverage"
|
|
54
52
|
},
|
|
55
53
|
"prettier": {
|
|
@@ -63,6 +61,7 @@
|
|
|
63
61
|
"xo": {
|
|
64
62
|
"prettier": true,
|
|
65
63
|
"rules": {
|
|
64
|
+
"unicorn/prefer-node-protocol": "off",
|
|
66
65
|
"no-await-in-loop": "off"
|
|
67
66
|
}
|
|
68
67
|
},
|
package/readme.md
CHANGED
|
@@ -4,16 +4,40 @@
|
|
|
4
4
|
[![Coverage][coverage-badge]][coverage]
|
|
5
5
|
[![Downloads][downloads-badge]][downloads]
|
|
6
6
|
|
|
7
|
-
Load
|
|
8
|
-
Like Node’s `require` and `require.resolve`, but from one or more places, and
|
|
9
|
-
optionally global too.
|
|
7
|
+
Load submodules, plugins, or files.
|
|
10
8
|
|
|
11
|
-
##
|
|
9
|
+
## Contents
|
|
10
|
+
|
|
11
|
+
* [What is this?](#what-is-this)
|
|
12
|
+
* [When to use this?](#when-to-use-this)
|
|
13
|
+
* [Install](#install)
|
|
14
|
+
* [Use](#use)
|
|
15
|
+
* [API](#api)
|
|
16
|
+
* [`loadPlugin(name[, options])`](#loadpluginname-options)
|
|
17
|
+
* [`resolvePlugin(name[, options])`](#resolvepluginname-options)
|
|
18
|
+
* [Types](#types)
|
|
19
|
+
* [Compatibility](#compatibility)
|
|
20
|
+
* [Contribute](#contribute)
|
|
21
|
+
* [License](#license)
|
|
22
|
+
|
|
23
|
+
## What is this?
|
|
12
24
|
|
|
13
|
-
This package is
|
|
14
|
-
|
|
25
|
+
This package is useful when you want to load plugins.
|
|
26
|
+
It resolves things like Node.js does, but supports a prefix (e.g., when given a
|
|
27
|
+
prefix `remark` and the user provided value `gfm`, it can find `remark-gfm`),
|
|
28
|
+
can load from several places, and optionally global too.
|
|
15
29
|
|
|
16
|
-
|
|
30
|
+
## When to use this?
|
|
31
|
+
|
|
32
|
+
This package is particularly useful when you want users to configure something
|
|
33
|
+
with plugins.
|
|
34
|
+
One example is `remark-cli` which can load remark plugins from configuration
|
|
35
|
+
files.
|
|
36
|
+
|
|
37
|
+
## Install
|
|
38
|
+
|
|
39
|
+
This package is [ESM only][esm].
|
|
40
|
+
In Node.js (version 14.14+, 16.0+), install with [npm][]:
|
|
17
41
|
|
|
18
42
|
```sh
|
|
19
43
|
npm install load-plugin
|
|
@@ -26,26 +50,22 @@ Say we’re in this project (with dependencies installed):
|
|
|
26
50
|
```js
|
|
27
51
|
import {loadPlugin, resolvePlugin} from 'load-plugin'
|
|
28
52
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
async function main() {
|
|
32
|
-
await resolvePlugin('lint', {prefix: 'remark'})
|
|
33
|
-
// => '/Users/tilde/projects/oss/load-plugin/node_modules/remark-lint/index.js'
|
|
53
|
+
console.log(await resolvePlugin('lint', {prefix: 'remark'}))
|
|
54
|
+
// => '/Users/tilde/projects/oss/load-plugin/node_modules/remark-lint/index.js'
|
|
34
55
|
|
|
35
|
-
|
|
36
|
-
|
|
56
|
+
console.log(await resolvePlugin('validator-identifier', {prefix: '@babel/helper'}))
|
|
57
|
+
// => '/Users/tilde/Projects/oss/load-plugin/node_modules/@babel/helper-validator-identifier/lib/index.js'
|
|
37
58
|
|
|
38
|
-
|
|
39
|
-
|
|
59
|
+
console.log(await resolvePlugin('./index.js', {prefix: 'remark'}))
|
|
60
|
+
// => '/Users/tilde/projects/oss/load-plugin/index.js'
|
|
40
61
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
62
|
+
console.log(await loadPlugin('lint', {prefix: 'remark'}))
|
|
63
|
+
// => [Function: remarkLint]
|
|
44
64
|
```
|
|
45
65
|
|
|
46
66
|
## API
|
|
47
67
|
|
|
48
|
-
This package exports the
|
|
68
|
+
This package exports the identifiers `loadPlugin` and `resolvePlugin`.
|
|
49
69
|
There is no default export.
|
|
50
70
|
|
|
51
71
|
### `loadPlugin(name[, options])`
|
|
@@ -62,13 +82,15 @@ If `name` starts with a scope (`@scope/name`), the prefix is applied after it:
|
|
|
62
82
|
|
|
63
83
|
##### `options`
|
|
64
84
|
|
|
85
|
+
Configuration (optional).
|
|
86
|
+
|
|
65
87
|
###### `options.prefix`
|
|
66
88
|
|
|
67
89
|
Prefix to search for (`string`, optional).
|
|
68
90
|
|
|
69
91
|
###### `options.cwd`
|
|
70
92
|
|
|
71
|
-
Place or places to search from (`string`, `Array
|
|
93
|
+
Place or places to search from (`string`, `Array<string>`, default:
|
|
72
94
|
`process.cwd()`).
|
|
73
95
|
|
|
74
96
|
###### `options.global`
|
|
@@ -92,19 +114,35 @@ used, and when `false` the whole module object is returned.
|
|
|
92
114
|
|
|
93
115
|
###### Returns
|
|
94
116
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
The promise rejects if
|
|
117
|
+
Promise yielding the results of importing the first path that exists
|
|
118
|
+
(`Promise<unknown>`).
|
|
119
|
+
The promise rejects if importing an existing path fails, or if no existing
|
|
98
120
|
path exists.
|
|
99
121
|
|
|
100
122
|
### `resolvePlugin(name[, options])`
|
|
101
123
|
|
|
102
124
|
Search for `name`.
|
|
103
125
|
Accepts the same parameters as [`loadPlugin`][load-plugin] (except `key`) but
|
|
104
|
-
returns a promise resolving to an absolute
|
|
105
|
-
it.
|
|
126
|
+
returns a promise resolving to an absolute URL (`string`) for `name` instead of
|
|
127
|
+
importing it.
|
|
106
128
|
Throws if `name` cannot be found.
|
|
107
129
|
|
|
130
|
+
## Types
|
|
131
|
+
|
|
132
|
+
This package is fully typed with [TypeScript][].
|
|
133
|
+
It exports the additional types `ResolveOptions` and `LoadOptions`.
|
|
134
|
+
|
|
135
|
+
## Compatibility
|
|
136
|
+
|
|
137
|
+
This package is at least compatible with all maintained versions of Node.js.
|
|
138
|
+
As of now, that is Node.js 14.14+ and 16.0+.
|
|
139
|
+
It also works in Deno and modern browsers.
|
|
140
|
+
|
|
141
|
+
## Contribute
|
|
142
|
+
|
|
143
|
+
Yes please!
|
|
144
|
+
See [How to Contribute to Open Source][contribute].
|
|
145
|
+
|
|
108
146
|
## License
|
|
109
147
|
|
|
110
148
|
[MIT][license] © [Titus Wormer][author]
|
|
@@ -129,9 +167,13 @@ Throws if `name` cannot be found.
|
|
|
129
167
|
|
|
130
168
|
[author]: https://wooorm.com
|
|
131
169
|
|
|
132
|
-
[
|
|
170
|
+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
|
|
133
171
|
|
|
134
|
-
[
|
|
172
|
+
[typescript]: https://www.typescriptlang.org
|
|
173
|
+
|
|
174
|
+
[contribute]: https://opensource.guide/how-to-contribute/
|
|
175
|
+
|
|
176
|
+
[global]: https://docs.npmjs.com/files/folders#node-modules
|
|
135
177
|
|
|
136
178
|
[prefix]: https://docs.npmjs.com/misc/config#prefix
|
|
137
179
|
|
|
@@ -142,3 +184,5 @@ Throws if `name` cannot be found.
|
|
|
142
184
|
[algo]: https://nodejs.org/api/esm.html#esm_resolution_algorithm
|
|
143
185
|
|
|
144
186
|
[import-meta-resolve]: https://github.com/wooorm/import-meta-resolve
|
|
187
|
+
|
|
188
|
+
[load-plugin]: #loadpluginname-options
|