load-plugin 4.0.1 → 5.0.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.
Files changed (4) hide show
  1. package/index.d.ts +6 -6
  2. package/index.js +17 -29
  3. package/package.json +8 -7
  4. package/readme.md +71 -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.<string>}
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[]} [cwd]
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
- /** @type {{prefix?: string}} */
29
- let builtinNpmConfig
27
+ const config = new NpmConfig({definitions: {}})
30
28
 
31
- // The prefix config defaults to the location where node is installed.
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
- * Note: `libnpmconfig` uses `figgy-pudding` which is slated for archival.
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 {Object.<string, unknown>} */
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.<string>}
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 && (await attempt(sources[index], 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": "4.0.1",
3
+ "version": "5.0.0",
4
4
  "description": "Load a submodule, plugin, or file",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -28,21 +28,21 @@
28
28
  "index.js"
29
29
  ],
30
30
  "dependencies": {
31
- "import-meta-resolve": "^1.0.0",
32
- "libnpmconfig": "^1.0.0"
31
+ "@npmcli/config": "^4.0.0",
32
+ "import-meta-resolve": "^2.0.0"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@types/tape": "^4.0.0",
36
36
  "c8": "^7.0.0",
37
37
  "prettier": "^2.0.0",
38
- "remark-cli": "^9.0.0",
39
- "remark-lint": "^8.0.0",
40
- "remark-preset-wooorm": "^8.0.0",
38
+ "remark-cli": "^10.0.0",
39
+ "remark-lint": "^9.0.0",
40
+ "remark-preset-wooorm": "^9.0.0",
41
41
  "rimraf": "^3.0.0",
42
42
  "tape": "^5.0.0",
43
43
  "type-coverage": "^2.0.0",
44
44
  "typescript": "^4.0.0",
45
- "xo": "^0.39.0"
45
+ "xo": "^0.49.0"
46
46
  },
47
47
  "scripts": {
48
48
  "prepack": "npm run build && npm run format",
@@ -63,6 +63,7 @@
63
63
  "xo": {
64
64
  "prettier": true,
65
65
  "rules": {
66
+ "unicorn/prefer-node-protocol": "off",
66
67
  "no-await-in-loop": "off"
67
68
  }
68
69
  },
package/readme.md CHANGED
@@ -4,16 +4,40 @@
4
4
  [![Coverage][coverage-badge]][coverage]
5
5
  [![Downloads][downloads-badge]][downloads]
6
6
 
7
- Load a submodule, plugin, or file.
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
- ## Install
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 [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
14
- Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
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
- [npm][]:
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+, or 18.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
- main()
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
- await resolvePlugin('@babel/function-name', {prefix: 'helper'})
36
- // => '/Users/tilde/projects/oss/load-plugin/node_modules/@babel/helper-function-name/lib/index.js'
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
- await resolvePlugin('./index.js', {prefix: 'remark'})
39
- // => '/Users/tilde/projects/oss/load-plugin/index.js'
59
+ console.log(await resolvePlugin('./index.js', {prefix: 'remark'}))
60
+ // => '/Users/tilde/projects/oss/load-plugin/index.js'
40
61
 
41
- await loadPlugin('lint', {prefix: 'remark'})
42
- // => [Function: lint]
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 following identifiers: `loadPlugin`, `resolvePlugin`.
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.<string>`, default:
93
+ Place or places to search from (`string`, `Array<string>`, default:
72
94
  `process.cwd()`).
73
95
 
74
96
  ###### `options.global`
@@ -92,19 +114,34 @@ used, and when `false` the whole module object is returned.
92
114
 
93
115
  ###### Returns
94
116
 
95
- `Promise.<unknown>` — Promise yielding the results of `require`ing the first
96
- path that exists.
97
- The promise rejects if `require`ing an existing path fails, or if no existing
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 path for `name` instead of importing
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+, 16.0+, and 18.0+.
139
+
140
+ ## Contribute
141
+
142
+ Yes please!
143
+ See [How to Contribute to Open Source][contribute].
144
+
108
145
  ## License
109
146
 
110
147
  [MIT][license] © [Titus Wormer][author]
@@ -129,9 +166,13 @@ Throws if `name` cannot be found.
129
166
 
130
167
  [author]: https://wooorm.com
131
168
 
132
- [global]: https://docs.npmjs.com/files/folders#node-modules
169
+ [esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
133
170
 
134
- [load-plugin]: #loadpluginname-options
171
+ [typescript]: https://www.typescriptlang.org
172
+
173
+ [contribute]: https://opensource.guide/how-to-contribute/
174
+
175
+ [global]: https://docs.npmjs.com/files/folders#node-modules
135
176
 
136
177
  [prefix]: https://docs.npmjs.com/misc/config#prefix
137
178
 
@@ -142,3 +183,5 @@ Throws if `name` cannot be found.
142
183
  [algo]: https://nodejs.org/api/esm.html#esm_resolution_algorithm
143
184
 
144
185
  [import-meta-resolve]: https://github.com/wooorm/import-meta-resolve
186
+
187
+ [load-plugin]: #loadpluginname-options