load-plugin 2.2.1 → 3.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.
package/browser.js ADDED
@@ -0,0 +1,12 @@
1
+ 'use strict'
2
+
3
+ module.exports = loadPlugin
4
+ loadPlugin.resolve = resolvePlugin
5
+
6
+ function loadPlugin() {
7
+ throw new Error('Cannot require plugins in the browser')
8
+ }
9
+
10
+ function resolvePlugin() {
11
+ throw new Error('Cannot resolve plugins in the browser')
12
+ }
package/index.js CHANGED
@@ -1,92 +1,141 @@
1
- 'use strict';
2
-
3
- var fs = require('fs');
4
- var path = require('path');
5
- var resolve = require('resolve-from');
6
- var npmPrefix = require('npm-prefix')();
7
-
8
- module.exports = loadPlugin;
9
- loadPlugin.resolve = resolvePlugin;
10
-
11
- var electron = process.versions.electron !== undefined;
12
- var argv = process.argv[1] || /* istanbul ignore next */ '';
13
- var nvm = process.env.NVM_BIN;
14
- var globally = electron || argv.indexOf(npmPrefix) === 0;
15
- var windows = process.platform === 'win32';
16
- var prefix = windows ? /* istanbul ignore next */ '' : 'lib';
17
- var globals = path.resolve(npmPrefix, prefix, 'node_modules');
18
-
19
- /* istanbul ignore next - If we’re in Electron, we’re running in a modified
20
- * Node that cannot really install global node modules. To find the actual
21
- * modules, the user has to either set `prefix` in their `.npmrc` (which is
22
- * picked up by `npm-prefix`). Most people don’t do that, and some use NVM
23
- * instead to manage different versions of Node. Luckily NVM leaks some
24
- * environment variables that we can pick up on to try and detect the actual
25
- * modules. */
1
+ 'use strict'
2
+
3
+ var fs = require('fs')
4
+ var path = require('path')
5
+ var resolve = require('resolve-from').silent
6
+ var readNpmConfig = require('libnpmconfig').read
7
+
8
+ module.exports = loadPlugin
9
+ loadPlugin.resolve = resolvePlugin
10
+
11
+ var electron = process.versions.electron !== undefined
12
+ var windows = process.platform === 'win32'
13
+
14
+ var argv = process.argv[1] || /* istanbul ignore next */ ''
15
+ var nvm = process.env.NVM_BIN
16
+ var appData = process.env.APPDATA
17
+
18
+ /* istanbul ignore next */
19
+ var globalsLibrary = windows ? '' : 'lib'
20
+
21
+ var builtinNpmConfig
22
+
23
+ // The prefix config defaults to the location where node is installed.
24
+ // On Windows, this is in a place called `%AppData%`, which we have to
25
+ // pass to `libnpmconfig` explicitly:
26
+ /* istanbul ignore next */
27
+ if (windows && appData) {
28
+ builtinNpmConfig = {prefix: path.join(appData, 'npm')}
29
+ }
30
+
31
+ var npmPrefix = readNpmConfig(null, builtinNpmConfig).prefix
32
+
33
+ // If there is no prefix defined, use the defaults
34
+ // See: <https://github.com/eush77/npm-prefix/blob/master/index.js>
35
+ /* istanbul ignore next */
36
+ if (!npmPrefix) {
37
+ npmPrefix = windows
38
+ ? path.dirname(process.execPath)
39
+ : path.resolve(process.execPath, '../..')
40
+ }
41
+
42
+ var globally = electron || argv.indexOf(npmPrefix) === 0
43
+ var globals = path.resolve(npmPrefix, globalsLibrary, 'node_modules')
44
+
45
+ // If we’re in Electron, we’re running in a modified Node that cannot really
46
+ // install global node modules.
47
+ // To find the actual modules, the user has to set `prefix` somewhere in an
48
+ // `.npmrc` (which is picked up by `libnpmconfig`).
49
+ // Most people don’t do that, and some use NVM instead to manage different
50
+ // versions of Node.
51
+ // Luckily NVM leaks some environment variables that we can pick up on to try
52
+ // and detect the actual modules.
53
+ /* istanbul ignore next */
26
54
  if (electron && nvm && !fs.existsSync(globals)) {
27
- globals = path.resolve(nvm, '..', prefix, 'node_modules');
55
+ globals = path.resolve(nvm, '..', globalsLibrary, 'node_modules')
28
56
  }
29
57
 
30
- /* Load the plug-in found using `resolvePlugin`. */
58
+ // Load the plugin found using `resolvePlugin`.
31
59
  function loadPlugin(name, options) {
32
- return require(resolvePlugin(name, options) || name);
60
+ return require(resolvePlugin(name, options) || name)
33
61
  }
34
62
 
35
- /* Find a plugin.
36
- *
37
- * See also:
38
- * <https://docs.npmjs.com/files/folders#node-modules>
39
- * <https://github.com/sindresorhus/resolve-from>
40
- *
41
- * Uses the standard node module loading strategy to find $name
42
- * in each given `cwd` (and optionally the global node_modules
43
- * directory).
44
- *
45
- * If a prefix is given and $name is not a path, `$prefix-$name`
46
- * is also searched (preferring these over non-prefixed modules). */
63
+ // Find a plugin.
64
+ //
65
+ // See also:
66
+ // <https://docs.npmjs.com/files/folders#node-modules>
67
+ // <https://github.com/sindresorhus/resolve-from>
68
+ //
69
+ // Uses the standard node module loading strategy to find $name in each given
70
+ // `cwd` (and optionally the global `node_modules` directory).
71
+ //
72
+ // If a prefix is given and $name is not a path, `$prefix-$name` is also
73
+ // searched (preferring these over non-prefixed modules).
47
74
  function resolvePlugin(name, options) {
48
- var settings = options || {};
49
- var prefix = settings.prefix;
50
- var cwd = settings.cwd;
51
- var filePath;
52
- var sources;
53
- var length;
54
- var index;
55
- var plugin;
75
+ var settings = options || {}
76
+ var prefix = settings.prefix
77
+ var cwd = settings.cwd
78
+ var global = settings.global
79
+ var filePath
80
+ var sources
81
+ var length
82
+ var index
83
+ var plugin
84
+ var slash
85
+ var scope = ''
86
+
87
+ if (global === null || global === undefined) {
88
+ global = globally
89
+ }
56
90
 
57
91
  if (cwd && typeof cwd === 'object') {
58
- sources = cwd.concat();
92
+ sources = cwd.concat()
59
93
  } else {
60
- sources = [cwd || process.cwd()];
94
+ sources = [cwd || process.cwd()]
61
95
  }
62
96
 
63
- /* Non-path. */
64
- if (name.indexOf(path.sep) === -1 && name.charAt(0) !== '.') {
65
- if (settings.global == null ? globally : settings.global) {
66
- sources.push(globals);
97
+ // Non-path.
98
+ if (name.charAt(0) !== '.') {
99
+ if (global) {
100
+ sources.push(globals)
67
101
  }
68
102
 
69
- /* Unprefix module. */
103
+ // Unprefix module.
70
104
  if (prefix) {
71
- prefix = prefix.charAt(prefix.length - 1) === '-' ? prefix : prefix + '-';
105
+ prefix = prefix.charAt(prefix.length - 1) === '-' ? prefix : prefix + '-'
106
+
107
+ // Scope?
108
+ if (name.charAt(0) === '@') {
109
+ slash = name.indexOf('/')
110
+
111
+ // Let’s keep the algorithm simple.
112
+ // No need to care if this is a “valid” scope (I think?).
113
+ // But we do check for the slash.
114
+ if (slash !== -1) {
115
+ scope = name.slice(0, slash + 1)
116
+ name = name.slice(slash + 1)
117
+ }
118
+ }
72
119
 
73
120
  if (name.slice(0, prefix.length) !== prefix) {
74
- plugin = prefix + name;
121
+ plugin = scope + prefix + name
75
122
  }
123
+
124
+ name = scope + name
76
125
  }
77
126
  }
78
127
 
79
- length = sources.length;
80
- index = -1;
128
+ length = sources.length
129
+ index = -1
81
130
 
82
131
  while (++index < length) {
83
- cwd = sources[index];
84
- filePath = (plugin && resolve(cwd, plugin)) || resolve(cwd, name);
132
+ cwd = sources[index]
133
+ filePath = (plugin && resolve(cwd, plugin)) || resolve(cwd, name)
85
134
 
86
135
  if (filePath) {
87
- return filePath;
136
+ return filePath
88
137
  }
89
138
  }
90
139
 
91
- return null;
140
+ return null
92
141
  }
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "load-plugin",
3
- "version": "2.2.1",
3
+ "version": "3.0.0",
4
4
  "description": "Load a submodule, plugin, or file",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -9,11 +9,15 @@
9
9
  "package",
10
10
  "plugin"
11
11
  ],
12
- "repository": "https://github.com/wooorm/load-plugin.git",
12
+ "repository": "wooorm/load-plugin",
13
13
  "bugs": "https://github.com/wooorm/load-plugin/issues",
14
- "author": "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)",
14
+ "funding": {
15
+ "type": "github",
16
+ "url": "https://github.com/sponsors/wooorm"
17
+ },
18
+ "author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
15
19
  "contributors": [
16
- "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)"
20
+ "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
17
21
  ],
18
22
  "main": "index.js",
19
23
  "browser": "browser.js",
@@ -22,33 +26,35 @@
22
26
  "index.js"
23
27
  ],
24
28
  "dependencies": {
25
- "npm-prefix": "^1.2.0",
26
- "resolve-from": "^2.0.0"
29
+ "libnpmconfig": "^1.0.0",
30
+ "resolve-from": "^5.0.0"
27
31
  },
28
32
  "devDependencies": {
29
- "nyc": "^11.0.0",
30
- "remark-cli": "^3.0.0",
33
+ "nyc": "^15.0.0",
34
+ "prettier": "^1.0.0",
35
+ "remark-cli": "^7.0.0",
31
36
  "remark-lint": "^6.0.0",
32
- "remark-preset-wooorm": "^3.0.0",
37
+ "remark-preset-wooorm": "^6.0.0",
33
38
  "tape": "^4.0.0",
34
- "xo": "^0.18.0"
39
+ "xo": "^0.25.0"
35
40
  },
36
41
  "scripts": {
37
- "build-md": "remark . -qfo",
38
- "build": "npm run build-md",
39
- "lint": "xo",
42
+ "format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
40
43
  "test-api": "node test",
41
44
  "test-coverage": "nyc --reporter lcov tape test/index.js",
42
- "test": "npm run build && npm run lint && npm run test-coverage"
45
+ "test": "npm run format && npm run test-coverage"
46
+ },
47
+ "prettier": {
48
+ "tabWidth": 2,
49
+ "useTabs": false,
50
+ "singleQuote": true,
51
+ "bracketSpacing": false,
52
+ "semi": false,
53
+ "trailingComma": "none"
43
54
  },
44
55
  "xo": {
45
- "space": true,
46
- "esnext": false,
47
- "rules": {
48
- "import/no-dynamic-require": "off",
49
- "no-eq-null": "off",
50
- "eqeqeq": "off"
51
- }
56
+ "prettier": true,
57
+ "esnext": false
52
58
  },
53
59
  "nyc": {
54
60
  "check-coverage": true,
@@ -57,6 +63,8 @@
57
63
  "branches": 100
58
64
  },
59
65
  "remarkConfig": {
60
- "plugins": ["preset-wooorm"]
66
+ "plugins": [
67
+ "preset-wooorm"
68
+ ]
61
69
  }
62
70
  }
package/readme.md CHANGED
@@ -1,48 +1,55 @@
1
- # load-plugin [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov]
1
+ # load-plugin
2
2
 
3
- Load a submodule, plugin, or file. Like Node’s `require` and
4
- `require.resolve`, but from one or more places, and optionally
5
- global too.
3
+ [![Build][build-badge]][build]
4
+ [![Coverage][coverage-badge]][coverage]
5
+ [![Downloads][downloads-badge]][downloads]
6
6
 
7
- ## Installation
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.
10
+
11
+ ## Install
8
12
 
9
13
  [npm][]:
10
14
 
11
- ```bash
15
+ ```sh
12
16
  npm install load-plugin
13
17
  ```
14
18
 
15
- When bundled for the browser, a small [file][browser] is included to
16
- warn that, when any of the below functions are invoked, the action is
17
- unsupported.
19
+ When bundled for the browser, a small [file][browser] is included to warn that,
20
+ when any of the below functions are invoked, the action is unsupported.
18
21
 
19
- ## Usage
22
+ ## Use
20
23
 
21
24
  Say we’re in this project (with dependencies installed):
22
25
 
23
26
  ```javascript
24
- var load = require('load-plugin');
27
+ var load = require('load-plugin')
28
+
29
+ load.resolve('lint', {prefix: 'remark'})
30
+ // => '/Users/tilde/projects/oss/load-plugin/node_modules/remark-lint/index.js'
25
31
 
26
- load.resolve('lint', {prefix: 'remark'});
27
- //=> '/Users/tilde/projects/oss/load-plugin/node_modules/remark-lint/index.js'
32
+ load.resolve('@babel/function-name', {prefix: 'helper'})
33
+ // => '/Users/tilde/projects/oss/load-plugin/node_modules/@babel/helper-function-name/index.js'
28
34
 
29
- load.resolve('./index.js', {prefix: 'remark'});
30
- //=> '/Users/tilde/projects/oss/load-plugin/index.js'
35
+ load.resolve('./index.js', {prefix: 'remark'})
36
+ // => '/Users/tilde/projects/oss/load-plugin/index.js'
31
37
 
32
- load.require('lint', {prefix: 'remark'});
33
- //=> [Function: lint]
38
+ load.require('lint', {prefix: 'remark'})
39
+ // => [Function: lint]
34
40
  ```
35
41
 
36
42
  ## API
37
43
 
38
44
  ### `loadPlugin(name[, options])`
39
45
 
40
- Uses the standard node module loading strategy to require `name`
41
- in each given `cwd` (and optionally the global `node_modules`
42
- directory).
46
+ Uses the standard node module loading strategy to require `name` in each given
47
+ `cwd` (and optionally the global `node_modules` directory).
43
48
 
44
- If a prefix is given and `name` is not a path, `prefix-name`
45
- is also searched (preferring these over non-prefixed modules).
49
+ If a prefix is given and `name` is not a path, `prefix-name` is also searched
50
+ (preferring these over non-prefixed modules).
51
+ If name starts with a scope (`@scope/name`), the prefix is applied after it:
52
+ `@scope/prefix-name`.
46
53
 
47
54
  ##### `options`
48
55
 
@@ -58,9 +65,10 @@ Place or places to search from (`string`, `Array.<string>`, default:
58
65
  ###### `options.global`
59
66
 
60
67
  Whether to look for `name` in [global places][global] (`boolean`, optional,
61
- defaults to whether global is detected). If this is nully, `load-plugin`
62
- will detect if it’s currently running in global mode: either because it’s
63
- in Electron, or because a globally installed package is running it.
68
+ defaults to whether global is detected).
69
+ If this is nully, `load-plugin` will detect if it’s currently running in global
70
+ mode: either because it’s in Electron, or because a globally installed package
71
+ is running it.
64
72
 
65
73
  Note: Electron runs its own version of Node instead of your system Node.
66
74
  That means global packages cannot be found, unless you’ve [set-up][] a [`prefix`
@@ -76,9 +84,10 @@ If `require`ing an existing path fails, or if no existing path exists.
76
84
 
77
85
  ### `loadPlugin.resolve(name[, options])`
78
86
 
79
- Search for `name`. Accepts the same parameters as [`loadPlugin`][load-plugin]
80
- but returns an absolute path for `name` instead of requiring it,
81
- and `null` if it cannot be found.
87
+ Search for `name`.
88
+ Accepts the same parameters as [`loadPlugin`][load-plugin] but returns an
89
+ absolute path for `name` instead of requiring it, and `null` if it cannot be
90
+ found.
82
91
 
83
92
  ## License
84
93
 
@@ -86,19 +95,23 @@ and `null` if it cannot be found.
86
95
 
87
96
  <!-- Definitions -->
88
97
 
89
- [travis-badge]: https://img.shields.io/travis/wooorm/load-plugin.svg
98
+ [build-badge]: https://img.shields.io/travis/wooorm/load-plugin.svg
99
+
100
+ [build]: https://travis-ci.org/wooorm/load-plugin
101
+
102
+ [coverage-badge]: https://img.shields.io/codecov/c/github/wooorm/load-plugin.svg
90
103
 
91
- [travis]: https://travis-ci.org/wooorm/load-plugin
104
+ [coverage]: https://codecov.io/github/wooorm/load-plugin
92
105
 
93
- [codecov-badge]: https://img.shields.io/codecov/c/github/wooorm/load-plugin.svg
106
+ [downloads-badge]: https://img.shields.io/npm/dm/load-plugin.svg
94
107
 
95
- [codecov]: https://codecov.io/github/wooorm/load-plugin
108
+ [downloads]: https://www.npmjs.com/package/load-plugin
96
109
 
97
110
  [npm]: https://docs.npmjs.com/cli/install
98
111
 
99
- [license]: LICENSE
112
+ [license]: license
100
113
 
101
- [author]: http://wooorm.com
114
+ [author]: https://wooorm.com
102
115
 
103
116
  [global]: https://docs.npmjs.com/files/folders#node-modules
104
117