load-plugin
Advanced tools
Comparing version 6.0.2 to 6.0.3
104
lib/index.js
@@ -36,36 +36,19 @@ /** | ||
import fs from 'node:fs' | ||
import path from 'node:path' | ||
import process from 'node:process' | ||
import {fileURLToPath, pathToFileURL} from 'node:url' | ||
import {pathToFileURL} from 'node:url' | ||
// @ts-expect-error: untyped | ||
import NpmConfig from '@npmcli/config' | ||
// @ts-expect-error: untyped | ||
import definitions from '@npmcli/config/lib/definitions/definitions.js' | ||
import {resolve} from 'import-meta-resolve' | ||
const electron = process.versions.electron !== undefined | ||
const nvm = process.env.NVM_BIN | ||
const windows = process.platform === 'win32' | ||
/* c8 ignore next -- windows */ | ||
const argv = process.argv[1] || '' | ||
/* c8 ignore next -- windows */ | ||
const nodeModules = windows ? 'node_modules' : 'lib/node_modules' | ||
const argv = process.argv[1] || /* c8 ignore next -- windows */ '' | ||
const nvm = process.env.NVM_BIN | ||
const config = new NpmConfig({ | ||
definitions: {}, | ||
npmPath: fileURLToPath(new URL('.', import.meta.url)) | ||
}) | ||
config.loadGlobalPrefix() | ||
// Note: this is a file path. | ||
// If there is no prefix defined, | ||
// use the defaults. | ||
// See: <https://github.com/eush77/npm-prefix/blob/master/index.js> | ||
/* c8 ignore next 6 -- typically defined */ | ||
/** @type {string} */ | ||
const npmPrefix = | ||
config.globalPrefix || | ||
(windows | ||
? path.dirname(process.execPath) | ||
: path.resolve(process.execPath, '../..')) | ||
const defaultGlobal = electron || argv.startsWith(npmPrefix) | ||
/** @type {Readonly<LoadOptions>} */ | ||
@@ -76,22 +59,5 @@ const defaultLoadOptions = {} | ||
/* c8 ignore next -- windows */ | ||
const nodeModules = windows ? 'node_modules' : 'lib/node_modules' | ||
/** @type {Promise<string> | string | undefined} */ | ||
let npmPrefix | ||
let globalFolder = new URL(nodeModules, pathToFileURL(npmPrefix) + '/') | ||
// If we’re in Electron, | ||
// we’re running in a modified Node that cannot really install global node | ||
// modules. | ||
// To find the actual modules, | ||
// the user has to set `prefix` somewhere in an `.npmrc` (which is picked up | ||
// by `@npmcli/config`). | ||
// Most people don’t do that, | ||
// and some use NVM instead to manage different versions of Node. | ||
// Luckily NVM leaks some environment variables that we can pick up on to try | ||
// and detect the actual modules. | ||
/* c8 ignore next 10 -- Electron. */ | ||
if (electron && nvm && !fs.existsSync(globalFolder)) { | ||
globalFolder = new URL(nodeModules, pathToFileURL(nvm)) | ||
} | ||
/** | ||
@@ -144,7 +110,13 @@ * Import `name` from `from` (and optionally the global `node_modules` directory). | ||
const from = /** @type {Array<Readonly<URL> | string>} */ ( | ||
// type-coverage:ignore-next-line -- TS fails on readonly arrays. | ||
Array.isArray(fromNonEmpty) ? fromNonEmpty : [fromNonEmpty] | ||
) | ||
if (!npmPrefix) npmPrefix = inferNpmPrefix() | ||
if (typeof npmPrefix !== 'string') npmPrefix = await npmPrefix | ||
const globals = | ||
typeof settings.global === 'boolean' ? settings.global : defaultGlobal | ||
typeof settings.global === 'boolean' | ||
? settings.global | ||
: electron || argv.startsWith(npmPrefix) | ||
/** @type {string | undefined} */ | ||
@@ -158,3 +130,18 @@ let plugin | ||
if (globals) { | ||
from.push(globalFolder) | ||
from.push(new URL(nodeModules, pathToFileURL(npmPrefix) + '/')) | ||
// If we’re in Electron, | ||
// we’re running in a modified Node that cannot really install global node | ||
// modules. | ||
// To find the actual modules, | ||
// the user has to set `prefix` somewhere in an `.npmrc` (which is picked up | ||
// by `@npmcli/config`). | ||
// Most people don’t do that, | ||
// and some use NVM instead to manage different versions of Node. | ||
// Luckily NVM leaks some environment variables that we can pick up on to try | ||
// and detect the actual modules. | ||
/* c8 ignore next 3 -- Electron. */ | ||
if (electron && nvm) { | ||
from.push(new URL(nodeModules, pathToFileURL(nvm))) | ||
} | ||
} | ||
@@ -210,1 +197,24 @@ | ||
} | ||
/** | ||
* Find npm prefix. | ||
* | ||
* @returns {Promise<string>} | ||
*/ | ||
async function inferNpmPrefix() { | ||
const config = new NpmConfig({ | ||
argv: [], | ||
definitions, | ||
npmPath: '' | ||
}) | ||
await config.load() | ||
/* c8 ignore next 6 -- typically defined */ | ||
return ( | ||
config.globalPrefix || | ||
(windows | ||
? path.dirname(process.execPath) | ||
: path.resolve(process.execPath, '../..')) | ||
) | ||
} |
{ | ||
"name": "load-plugin", | ||
"version": "6.0.2", | ||
"version": "6.0.3", | ||
"description": "Load a submodule, plugin, or file", | ||
@@ -27,5 +27,5 @@ "license": "MIT", | ||
"lib/", | ||
"index.d.ts.map", | ||
"index.d.ts", | ||
"index.js", | ||
"index.map" | ||
"index.js" | ||
], | ||
@@ -39,9 +39,10 @@ "dependencies": { | ||
"c8": "^9.0.0", | ||
"github-slugger": "^2.0.0", | ||
"prettier": "^3.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-lint": "^9.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"remark-cli": "^12.0.0", | ||
"remark-lint": "^10.0.0", | ||
"remark-preset-wooorm": "^10.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^5.0.0", | ||
"xo": "^0.56.0" | ||
"xo": "^0.58.0" | ||
}, | ||
@@ -76,4 +77,8 @@ "scripts": { | ||
"xo": { | ||
"prettier": true | ||
"prettier": true, | ||
"rules": { | ||
"complexity": "off", | ||
"logical-assignment-operators": "off" | ||
} | ||
} | ||
} |
@@ -11,15 +11,15 @@ # load-plugin | ||
* [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) | ||
* [`LoadOptions`](#loadoptions) | ||
* [`ResolveOptions`](#resolveoptions) | ||
* [Compatibility](#compatibility) | ||
* [Security](#security) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
* [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) | ||
* [`LoadOptions`](#loadoptions) | ||
* [`ResolveOptions`](#resolveoptions) | ||
* [Compatibility](#compatibility) | ||
* [Security](#security) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
@@ -99,6 +99,6 @@ ## What is this? | ||
* `name` (`string`) | ||
— specifier | ||
* `options` ([`LoadOptions`][api-load-options], optional) | ||
— configuration | ||
* `name` (`string`) | ||
— specifier | ||
* `options` ([`LoadOptions`][api-load-options], optional) | ||
— configuration | ||
@@ -115,6 +115,6 @@ ###### Returns | ||
* `name` (`string`) | ||
— specifier | ||
* `options` ([`ResolveOptions`][api-resolve-options], optional) | ||
— configuration | ||
* `name` (`string`) | ||
— specifier | ||
* `options` ([`ResolveOptions`][api-resolve-options], optional) | ||
— configuration | ||
@@ -133,9 +133,9 @@ ###### Returns | ||
* `key` (`boolean` or `string`, default: `'default'`) | ||
— identifier to take from the exports; | ||
for example when given `'x'`, | ||
the value of `export const x = 1` will be returned; | ||
when given `'default'`, | ||
the value of `export default …` is used, | ||
and when `false` the whole module object is returned | ||
* `key` (`boolean` or `string`, default: `'default'`) | ||
— identifier to take from the exports; | ||
for example when given `'x'`, | ||
the value of `export const x = 1` will be returned; | ||
when given `'default'`, | ||
the value of `export default …` is used, | ||
and when `false` the whole module object is returned | ||
@@ -148,17 +148,17 @@ ### `ResolveOptions` | ||
* `from` (`Array<URL | string> | URL | string`, optional) | ||
— place or places to search from; | ||
defaults to the current working directory | ||
* `global` (`boolean`, default: whether global is detected) | ||
— whether to look for `name` in [global places][npm-node-modules]; | ||
if this is nullish, | ||
`load-plugin` will detect if it’s currently running in global mode: either | ||
because it’s in Electron or because a globally installed package is running | ||
it; | ||
note that Electron runs its own version of Node instead of your system Node, | ||
meaning global packages cannot be found, | ||
unless you’ve set-up a [`prefix`][npm-prefix] in your `.npmrc` or are using | ||
[nvm][github-nvm] to manage your system node | ||
* `prefix` (`string`, optional) | ||
— prefix to search for | ||
* `from` (`Array<URL | string> | URL | string`, optional) | ||
— place or places to search from; | ||
defaults to the current working directory | ||
* `global` (`boolean`, default: whether global is detected) | ||
— whether to look for `name` in [global places][npm-node-modules]; | ||
if this is nullish, | ||
`load-plugin` will detect if it’s currently running in global mode: either | ||
because it’s in Electron or because a globally installed package is running | ||
it; | ||
note that Electron runs its own version of Node instead of your system Node, | ||
meaning global packages cannot be found, | ||
unless you’ve set-up a [`prefix`][npm-prefix] in your `.npmrc` or are using | ||
[nvm][github-nvm] to manage your system node | ||
* `prefix` (`string`, optional) | ||
— prefix to search for | ||
@@ -165,0 +165,0 @@ ## Compatibility |
Sorry, the diff of this file is not supported yet
20008
9
276
10