load-plugin 6.0.1 → 6.0.3

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.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":"0BACa,OAAO,gBAAgB,EAAE,WAAW;6BACpC,OAAO,gBAAgB,EAAE,cAAc"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":"AAqGA;;;;;;;;;;;;;;;;;;GAkBG;AACH,iCAPW,MAAM,YAEN,SAAS,WAAW,CAAC,GAAG,IAAI,GAAG,SAAS,GAEtC,QAAQ,OAAO,CAAC,CAY5B;AAED;;;;;;;;;GASG;AACH,oCAPW,MAAM,YAEN,SAAS,cAAc,CAAC,GAAG,IAAI,GAAG,SAAS,GAEzC,QAAQ,MAAM,CAAC,CAyE3B;;;;0BAlNY,sBAAsB,GAAG,cAAc;;;;;;;;;;;;;UAKtC,OAAO,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS;;;;;;;;;;WAUnC,cAAc,SAAS,GAAG,CAAC,GAAG,MAAM,CAAC,GAAG,SAAS,GAAG,CAAC,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS;;;;;;;;;;;;;;aAGjF,OAAO,GAAG,IAAI,GAAG,SAAS;;;;aAY1B,MAAM,GAAG,IAAI,GAAG,SAAS"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":"AA4DA;;;;;;;;;;;;;;;;;;GAkBG;AACH,iCAPW,MAAM,YAEN,SAAS,WAAW,CAAC,GAAG,IAAI,GAAG,SAAS,GAEtC,QAAQ,OAAO,CAAC,CAY5B;AAED;;;;;;;;;GASG;AACH,oCAPW,MAAM,YAEN,SAAS,cAAc,CAAC,GAAG,IAAI,GAAG,SAAS,GAEzC,QAAQ,MAAM,CAAC,CA8F3B;;;;0BA9LY,sBAAsB,GAAG,cAAc;;;;;;;;;;;;;UAKtC,OAAO,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS;;;;;;;;;;WAUnC,cAAc,SAAS,GAAG,CAAC,GAAG,MAAM,CAAC,GAAG,SAAS,GAAG,CAAC,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS;;;;;;;;;;;;;;aAGjF,OAAO,GAAG,IAAI,GAAG,SAAS;;;;aAY1B,MAAM,GAAG,IAAI,GAAG,SAAS"}
package/lib/index.js CHANGED
@@ -33,71 +33,30 @@
33
33
  * Prefix to search for (optional).
34
34
  */
35
35
 
36
- import fs from 'node:fs/promises'
37
36
  import path from 'node:path'
38
37
  import process from 'node:process'
39
- import {fileURLToPath, pathToFileURL} from 'node:url'
38
+ import {pathToFileURL} from 'node:url'
40
39
  // @ts-expect-error: untyped
41
40
  import NpmConfig from '@npmcli/config'
41
+ // @ts-expect-error: untyped
42
+ import definitions from '@npmcli/config/lib/definitions/definitions.js'
42
43
  import {resolve} from 'import-meta-resolve'
43
44
 
44
45
  const electron = process.versions.electron !== undefined
45
- const windows = process.platform === 'win32'
46
-
47
- const argv = process.argv[1] || /* c8 ignore next -- windows */ ''
48
46
  const nvm = process.env.NVM_BIN
47
+ const windows = process.platform === 'win32'
48
+ /* c8 ignore next -- windows */
49
+ const argv = process.argv[1] || ''
50
+ /* c8 ignore next -- windows */
51
+ const nodeModules = windows ? 'node_modules' : 'lib/node_modules'
49
52
 
50
- const config = new NpmConfig({
51
- definitions: {},
52
- npmPath: fileURLToPath(new URL('.', import.meta.url))
53
- })
54
-
55
- config.loadGlobalPrefix()
56
-
57
- // Note: this is a file path.
58
- // If there is no prefix defined,
59
- // use the defaults.
60
- // See: <https://github.com/eush77/npm-prefix/blob/master/index.js>
61
- /* c8 ignore next 6 -- typically defined */
62
- /** @type {string} */
63
- const npmPrefix =
64
- config.globalPrefix ||
65
- (windows
66
- ? path.dirname(process.execPath)
67
- : path.resolve(process.execPath, '../..'))
68
-
69
- const defaultGlobal = electron || argv.startsWith(npmPrefix)
70
53
  /** @type {Readonly<LoadOptions>} */
71
54
  const defaultLoadOptions = {}
72
55
  /** @type {Readonly<ResolveOptions>} */
73
56
  const defaultResolveOptions = {}
74
57
 
75
- /* c8 ignore next -- windows */
76
- const nodeModules = windows ? 'node_modules' : 'lib/node_modules'
77
-
78
- let globalFolder = new URL(nodeModules, pathToFileURL(npmPrefix) + '/')
79
-
80
- // If we’re in Electron,
81
- // we’re running in a modified Node that cannot really install global node
82
- // modules.
83
- // To find the actual modules,
84
- // the user has to set `prefix` somewhere in an `.npmrc` (which is picked up
85
- // by `@npmcli/config`).
86
- // Most people don’t do that,
87
- // and some use NVM instead to manage different versions of Node.
88
- // Luckily NVM leaks some environment variables that we can pick up on to try
89
- // and detect the actual modules.
90
- /* c8 ignore next 10 -- Electron. */
91
- if (electron && nvm) {
92
- try {
93
- await fs.stat(globalFolder)
94
- } catch (error) {
95
- const cause = /** @type {NodeJS.ErrnoException} */ (error)
96
- if (cause && cause.code === 'ENOENT') {
97
- globalFolder = new URL(nodeModules, pathToFileURL(nvm))
98
- }
99
- }
100
- }
58
+ /** @type {Promise<string> | string | undefined} */
59
+ let npmPrefix
101
60
 
102
61
  /**
103
62
  * Import `name` from `from` (and optionally the global `node_modules` directory).
@@ -125,8 +84,8 @@ export async function loadPlugin(name, options) {
125
84
  return typeof settings.key === 'string'
126
85
  ? module[settings.key]
127
86
  : settings.key === false
128
- ? module
129
- : module.default
87
+ ? module
88
+ : module.default
130
89
  }
131
90
 
132
91
  /**
@@ -146,11 +105,17 @@ export async function resolvePlugin(name, options) {
146
105
  : undefined
147
106
  const fromNonEmpty = settings.from || pathToFileURL(process.cwd() + '/')
148
107
  const from = /** @type {Array<Readonly<URL> | string>} */ (
149
- // type-coverage:ignore-next-line -- TS fails on readonly arrays.
150
108
  Array.isArray(fromNonEmpty) ? fromNonEmpty : [fromNonEmpty]
151
109
  )
110
+
111
+ if (!npmPrefix) npmPrefix = inferNpmPrefix()
112
+ if (typeof npmPrefix !== 'string') npmPrefix = await npmPrefix
113
+
152
114
  const globals =
153
- typeof settings.global === 'boolean' ? settings.global : defaultGlobal
115
+ typeof settings.global === 'boolean'
116
+ ? settings.global
117
+ : electron || argv.startsWith(npmPrefix)
118
+
154
119
  /** @type {string | undefined} */
155
120
  let plugin
156
121
  /** @type {Error | undefined} */
@@ -159,7 +124,22 @@ export async function resolvePlugin(name, options) {
159
124
  // Bare specifier.
160
125
  if (name.charAt(0) !== '.') {
161
126
  if (globals) {
162
- from.push(globalFolder)
127
+ from.push(new URL(nodeModules, pathToFileURL(npmPrefix) + '/'))
128
+
129
+ // If we’re in Electron,
130
+ // we’re running in a modified Node that cannot really install global node
131
+ // modules.
132
+ // To find the actual modules,
133
+ // the user has to set `prefix` somewhere in an `.npmrc` (which is picked up
134
+ // by `@npmcli/config`).
135
+ // Most people don’t do that,
136
+ // and some use NVM instead to manage different versions of Node.
137
+ // Luckily NVM leaks some environment variables that we can pick up on to try
138
+ // and detect the actual modules.
139
+ /* c8 ignore next 3 -- Electron. */
140
+ if (electron && nvm) {
141
+ from.push(new URL(nodeModules, pathToFileURL(nvm)))
142
+ }
163
143
  }
164
144
 
165
145
  let scope = ''
@@ -210,3 +190,26 @@ export async function resolvePlugin(name, options) {
210
190
 
211
191
  throw lastError
212
192
  }
193
+
194
+ /**
195
+ * Find npm prefix.
196
+ *
197
+ * @returns {Promise<string>}
198
+ */
199
+ async function inferNpmPrefix() {
200
+ const config = new NpmConfig({
201
+ argv: [],
202
+ definitions,
203
+ npmPath: ''
204
+ })
205
+
206
+ await config.load()
207
+
208
+ /* c8 ignore next 6 -- typically defined */
209
+ return (
210
+ config.globalPrefix ||
211
+ (windows
212
+ ? path.dirname(process.execPath)
213
+ : path.resolve(process.execPath, '../..'))
214
+ )
215
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "load-plugin",
3
- "version": "6.0.1",
3
+ "version": "6.0.3",
4
4
  "description": "Load a submodule, plugin, or file",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -24,9 +24,9 @@
24
24
  "exports": "./index.js",
25
25
  "files": [
26
26
  "lib/",
27
+ "index.d.ts.map",
27
28
  "index.d.ts",
28
- "index.js",
29
- "index.map"
29
+ "index.js"
30
30
  ],
31
31
  "dependencies": {
32
32
  "@npmcli/config": "^8.0.0",
@@ -34,14 +34,15 @@
34
34
  },
35
35
  "devDependencies": {
36
36
  "@types/node": "^20.0.0",
37
- "c8": "^8.0.0",
37
+ "c8": "^9.0.0",
38
+ "github-slugger": "^2.0.0",
38
39
  "prettier": "^3.0.0",
39
- "remark-cli": "^11.0.0",
40
- "remark-lint": "^9.0.0",
41
- "remark-preset-wooorm": "^9.0.0",
40
+ "remark-cli": "^12.0.0",
41
+ "remark-lint": "^10.0.0",
42
+ "remark-preset-wooorm": "^10.0.0",
42
43
  "type-coverage": "^2.0.0",
43
44
  "typescript": "^5.0.0",
44
- "xo": "^0.56.0"
45
+ "xo": "^0.58.0"
45
46
  },
46
47
  "scripts": {
47
48
  "build": "tsc --build --clean && tsc --build && type-coverage",
@@ -71,6 +72,10 @@
71
72
  "strict": true
72
73
  },
73
74
  "xo": {
74
- "prettier": true
75
+ "prettier": true,
76
+ "rules": {
77
+ "complexity": "off",
78
+ "logical-assignment-operators": "off"
79
+ }
75
80
  }
76
81
  }
package/readme.md CHANGED
@@ -8,19 +8,19 @@ Load a submodule, plugin, or file.
8
8
 
9
9
  ## Contents
10
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
- * [`LoadOptions`](#loadoptions)
19
- * [`ResolveOptions`](#resolveoptions)
20
- * [Compatibility](#compatibility)
21
- * [Security](#security)
22
- * [Contribute](#contribute)
23
- * [License](#license)
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
+ * [`LoadOptions`](#loadoptions)
19
+ * [`ResolveOptions`](#resolveoptions)
20
+ * [Compatibility](#compatibility)
21
+ * [Security](#security)
22
+ * [Contribute](#contribute)
23
+ * [License](#license)
24
24
 
25
25
  ## What is this?
26
26
 
@@ -95,10 +95,10 @@ the prefix is applied after it: `@scope/$prefix-name`.
95
95
 
96
96
  ###### Parameters
97
97
 
98
- * `name` (`string`)
99
- — specifier
100
- * `options` ([`LoadOptions`][api-load-options], optional)
101
- — configuration
98
+ * `name` (`string`)
99
+ — specifier
100
+ * `options` ([`LoadOptions`][api-load-options], optional)
101
+ — configuration
102
102
 
103
103
  ###### Returns
104
104
 
@@ -110,10 +110,10 @@ Resolve `name` from `from`.
110
110
 
111
111
  ###### Parameters
112
112
 
113
- * `name` (`string`)
114
- — specifier
115
- * `options` ([`ResolveOptions`][api-resolve-options], optional)
116
- — configuration
113
+ * `name` (`string`)
114
+ — specifier
115
+ * `options` ([`ResolveOptions`][api-resolve-options], optional)
116
+ — configuration
117
117
 
118
118
  ###### Returns
119
119
 
@@ -127,13 +127,13 @@ This type extends `ResolveOptions` and adds:
127
127
 
128
128
  ###### Fields
129
129
 
130
- * `key` (`boolean` or `string`, default: `'default'`)
131
- — identifier to take from the exports;
132
- for example when given `'x'`,
133
- the value of `export const x = 1` will be returned;
134
- when given `'default'`,
135
- the value of `export default …` is used,
136
- and when `false` the whole module object is returned
130
+ * `key` (`boolean` or `string`, default: `'default'`)
131
+ — identifier to take from the exports;
132
+ for example when given `'x'`,
133
+ the value of `export const x = 1` will be returned;
134
+ when given `'default'`,
135
+ the value of `export default …` is used,
136
+ and when `false` the whole module object is returned
137
137
 
138
138
  ### `ResolveOptions`
139
139
 
@@ -141,21 +141,21 @@ Configuration for `resolvePlugin` (TypeScript type).
141
141
 
142
142
  ###### Fields
143
143
 
144
- * `from` (`Array<URL | string> | URL | string`, optional)
145
- — place or places to search from;
146
- defaults to the current working directory
147
- * `global` (`boolean`, default: whether global is detected)
148
- — whether to look for `name` in [global places][npm-node-modules];
149
- if this is nullish,
150
- `load-plugin` will detect if it’s currently running in global mode: either
151
- because it’s in Electron or because a globally installed package is running
152
- it;
153
- note that Electron runs its own version of Node instead of your system Node,
154
- meaning global packages cannot be found,
155
- unless you’ve set-up a [`prefix`][npm-prefix] in your `.npmrc` or are using
156
- [nvm][github-nvm] to manage your system node
157
- * `prefix` (`string`, optional)
158
- — prefix to search for
144
+ * `from` (`Array<URL | string> | URL | string`, optional)
145
+ — place or places to search from;
146
+ defaults to the current working directory
147
+ * `global` (`boolean`, default: whether global is detected)
148
+ — whether to look for `name` in [global places][npm-node-modules];
149
+ if this is nullish,
150
+ `load-plugin` will detect if it’s currently running in global mode: either
151
+ because it’s in Electron or because a globally installed package is running
152
+ it;
153
+ note that Electron runs its own version of Node instead of your system Node,
154
+ meaning global packages cannot be found,
155
+ unless you’ve set-up a [`prefix`][npm-prefix] in your `.npmrc` or are using
156
+ [nvm][github-nvm] to manage your system node
157
+ * `prefix` (`string`, optional)
158
+ — prefix to search for
159
159
 
160
160
  ## Compatibility
161
161