load-plugin 2.2.0 → 2.3.1

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,112 @@
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 = path.resolve(windows ? /* istanbul ignore next */ '' : 'lib', 'node_modules');
17
- var globals = path.resolve(npmPrefix, prefix);
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 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
+ // If we’re in Electron, we’re running in a modified Node that cannot really
20
+ // install global node modules. To find the actual modules, the user has to
21
+ // either set `prefix` in their `.npmrc` (which is picked up by `npm-prefix`).
22
+ // Most people don’t do that, and some use NVM instead to manage different
23
+ // versions of Node. Luckily NVM leaks some environment variables that we can
24
+ // pick up on to try and detect the actual modules.
25
+ /* istanbul ignore next */
26
26
  if (electron && nvm && !fs.existsSync(globals)) {
27
- globals = path.resolve(nvm, '..', prefix);
27
+ globals = path.resolve(nvm, '..', prefix, 'node_modules')
28
28
  }
29
29
 
30
- /* Load the plug-in found using `resolvePlugin`. */
30
+ // Load the plug-in found using `resolvePlugin`.
31
31
  function loadPlugin(name, options) {
32
- return require(resolvePlugin(name, options) || name);
32
+ return require(resolvePlugin(name, options) || name)
33
33
  }
34
34
 
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). */
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 in each given
42
+ // `cwd` (and optionally the global `node_modules` directory).
43
+ //
44
+ // If a prefix is given and $name is not a path, `$prefix-$name` is also
45
+ // searched (preferring these over non-prefixed modules).
47
46
  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;
47
+ var settings = options || {}
48
+ var prefix = settings.prefix
49
+ var cwd = settings.cwd
50
+ var global = settings.global
51
+ var filePath
52
+ var sources
53
+ var length
54
+ var index
55
+ var plugin
56
+ var slash
57
+ var scope = ''
58
+
59
+ if (global === null || global === undefined) {
60
+ global = globally
61
+ }
56
62
 
57
63
  if (cwd && typeof cwd === 'object') {
58
- sources = cwd.concat();
64
+ sources = cwd.concat()
59
65
  } else {
60
- sources = [cwd || process.cwd()];
66
+ sources = [cwd || process.cwd()]
61
67
  }
62
68
 
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);
69
+ // Non-path.
70
+ if (name.charAt(0) !== '.') {
71
+ if (global) {
72
+ sources.push(globals)
67
73
  }
68
74
 
69
- /* Unprefix module. */
75
+ // Unprefix module.
70
76
  if (prefix) {
71
- prefix = prefix.charAt(prefix.length - 1) === '-' ? prefix : prefix + '-';
77
+ prefix = prefix.charAt(prefix.length - 1) === '-' ? prefix : prefix + '-'
78
+
79
+ // Scope?
80
+ if (name.charAt(0) === '@') {
81
+ slash = name.indexOf('/')
82
+
83
+ // Let’s keep the algorithm simple. No need to care if this is a
84
+ // “valid” scope (I think?). But we do check for the slash.
85
+ if (slash !== -1) {
86
+ scope = name.slice(0, slash + 1)
87
+ name = name.slice(slash + 1)
88
+ }
89
+ }
72
90
 
73
91
  if (name.slice(0, prefix.length) !== prefix) {
74
- plugin = prefix + name;
92
+ plugin = scope + prefix + name
75
93
  }
94
+
95
+ name = scope + name
76
96
  }
77
97
  }
78
98
 
79
- length = sources.length;
80
- index = -1;
99
+ length = sources.length
100
+ index = -1
81
101
 
82
102
  while (++index < length) {
83
- cwd = sources[index];
84
- filePath = (plugin && resolve(cwd, plugin)) || resolve(cwd, name);
103
+ cwd = sources[index]
104
+ filePath = (plugin && resolve(cwd, plugin)) || resolve(cwd, name)
85
105
 
86
106
  if (filePath) {
87
- return filePath;
107
+ return filePath
88
108
  }
89
109
  }
90
110
 
91
- return null;
111
+ return null
92
112
  }
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "load-plugin",
3
- "version": "2.2.0",
3
+ "version": "2.3.1",
4
4
  "description": "Load a submodule, plugin, or file",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -9,11 +9,11 @@
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
+ "author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
15
15
  "contributors": [
16
- "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)"
16
+ "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
17
17
  ],
18
18
  "main": "index.js",
19
19
  "browser": "browser.js",
@@ -23,32 +23,34 @@
23
23
  ],
24
24
  "dependencies": {
25
25
  "npm-prefix": "^1.2.0",
26
- "resolve-from": "^2.0.0"
26
+ "resolve-from": "^5.0.0"
27
27
  },
28
28
  "devDependencies": {
29
- "nyc": "^11.0.0",
30
- "remark-cli": "^3.0.0",
29
+ "nyc": "^14.0.0",
30
+ "prettier": "^1.12.0",
31
+ "remark-cli": "^6.0.0",
31
32
  "remark-lint": "^6.0.0",
32
- "remark-preset-wooorm": "^3.0.0",
33
+ "remark-preset-wooorm": "^4.0.0",
33
34
  "tape": "^4.0.0",
34
- "xo": "^0.18.0"
35
+ "xo": "^0.24.0"
35
36
  },
36
37
  "scripts": {
37
- "build-md": "remark . -qfo",
38
- "build": "npm run build-md",
39
- "lint": "xo",
38
+ "format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
40
39
  "test-api": "node test",
41
40
  "test-coverage": "nyc --reporter lcov tape test/index.js",
42
- "test": "npm run build && npm run lint && npm run test-coverage"
41
+ "test": "npm run format && npm run test-coverage"
42
+ },
43
+ "prettier": {
44
+ "tabWidth": 2,
45
+ "useTabs": false,
46
+ "singleQuote": true,
47
+ "bracketSpacing": false,
48
+ "semi": false,
49
+ "trailingComma": "none"
43
50
  },
44
51
  "xo": {
45
- "space": true,
46
- "esnext": false,
47
- "rules": {
48
- "import/no-dynamic-require": "off",
49
- "no-eq-null": "off",
50
- "eqeqeq": "off"
51
- }
52
+ "prettier": true,
53
+ "esnext": false
52
54
  },
53
55
  "nyc": {
54
56
  "check-coverage": true,
@@ -57,6 +59,8 @@
57
59
  "branches": 100
58
60
  },
59
61
  "remarkConfig": {
60
- "plugins": ["preset-wooorm"]
62
+ "plugins": [
63
+ "preset-wooorm"
64
+ ]
61
65
  }
62
66
  }
package/readme.md CHANGED
@@ -1,8 +1,12 @@
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
+
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.
6
10
 
7
11
  ## Installation
8
12
 
@@ -12,37 +16,40 @@ global too.
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
22
  ## Usage
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