load-plugin 2.1.0 → 2.3.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,87 +1,107 @@
1
- 'use strict';
2
-
3
- /* Dependencies */
4
- var path = require('path');
5
- var resolve = require('resolve-from');
6
- var npmPrefix = require('npm-prefix')();
7
-
8
- /* Expose. */
9
- module.exports = loadPlugin;
10
- loadPlugin.resolve = resolvePlugin;
11
-
12
- /* Constants. */
13
- var isElectron = process.versions.electron !== undefined;
14
- var argv = process.argv[1] || /* istanbul ignore next */ '';
15
- var isGlobal = isElectron || argv.indexOf(npmPrefix) === 0;
16
- var isWindows = process.platform === 'win32';
17
- var prefix = isWindows ? /* istanbul ignore next */ '' : 'lib';
18
- var globals = path.resolve(npmPrefix, prefix, 'node_modules');
19
-
20
- /* Load the plug-in found using `resolvePlugin`. */
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
+ if (electron && nvm && !fs.existsSync(globals)) {
27
+ globals = path.resolve(nvm, '..', prefix, 'node_modules')
28
+ }
29
+
30
+ // Load the plug-in found using `resolvePlugin`.
21
31
  function loadPlugin(name, options) {
22
- return require(resolvePlugin(name, options) || name);
32
+ return require(resolvePlugin(name, options) || name)
23
33
  }
24
34
 
25
- /**
26
- * Find a plugin.
27
- *
28
- * Uses the standard node module loading strategy to find $name
29
- * in each given `cwd` (and optionally the global node_modules
30
- * directory).
31
- *
32
- * If a prefix is given and $name is not a path, `$prefix-$name`
33
- * is also searched (preferring these over non-prefixed modules).
34
- *
35
- * @see https://docs.npmjs.com/files/folders#node-modules
36
- * @see https://github.com/sindresorhus/resolve-from
37
- *
38
- * @param {string} name - Reference to plugin.
39
- * @param {Object?} [options] - Configuration,
40
- * @return {string?} - Path to `name`, if found.
41
- */
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).
42
46
  function resolvePlugin(name, options) {
43
- var settings = options || {};
44
- var prefix = settings.prefix;
45
- var cwd = settings.cwd;
46
- var filePath;
47
- var sources;
48
- var length;
49
- var index;
50
- var plugin;
47
+ var settings = options || {}
48
+ var prefix = settings.prefix
49
+ var cwd = settings.cwd
50
+ var filePath
51
+ var sources
52
+ var length
53
+ var index
54
+ var plugin
55
+ var slash
56
+ var scope = ''
51
57
 
52
58
  if (cwd && typeof cwd === 'object') {
53
- sources = cwd.concat();
59
+ sources = cwd.concat()
54
60
  } else {
55
- sources = [cwd || process.cwd()];
61
+ sources = [cwd || process.cwd()]
56
62
  }
57
63
 
58
- /* Non-path. */
59
- if (name.indexOf(path.sep) === -1 && name.charAt(0) !== '.') {
60
- if (settings.global == null ? isGlobal : settings.global) {
61
- sources.push(globals);
64
+ // Non-path.
65
+ if (name.charAt(0) !== '.') {
66
+ if (settings.global == null ? globally : settings.global) {
67
+ sources.push(globals)
62
68
  }
63
69
 
64
- /* Unprefix module. */
70
+ // Unprefix module.
65
71
  if (prefix) {
66
- prefix = prefix.charAt(prefix.length - 1) === '-' ? prefix : prefix + '-';
72
+ prefix = prefix.charAt(prefix.length - 1) === '-' ? prefix : prefix + '-'
73
+
74
+ // Scope?
75
+ if (name.charAt(0) === '@') {
76
+ slash = name.indexOf('/')
77
+
78
+ // Let’s keep the algorithm simple. No need to care if this is a
79
+ // “valid” scope (I think?). But we do check for the slash.
80
+ if (slash !== -1) {
81
+ scope = name.slice(0, slash + 1)
82
+ name = name.slice(slash + 1)
83
+ }
84
+ }
67
85
 
68
86
  if (name.slice(0, prefix.length) !== prefix) {
69
- plugin = prefix + name;
87
+ plugin = scope + prefix + name
70
88
  }
89
+
90
+ name = scope + name
71
91
  }
72
92
  }
73
93
 
74
- length = sources.length;
75
- index = -1;
94
+ length = sources.length
95
+ index = -1
76
96
 
77
97
  while (++index < length) {
78
- cwd = sources[index];
79
- filePath = (plugin && resolve(cwd, plugin)) || resolve(cwd, name);
98
+ cwd = sources[index]
99
+ filePath = (plugin && resolve(cwd, plugin)) || resolve(cwd, name)
80
100
 
81
101
  if (filePath) {
82
- return filePath;
102
+ return filePath
83
103
  }
84
104
  }
85
105
 
86
- return null;
106
+ return null
87
107
  }
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "load-plugin",
3
- "version": "2.1.0",
3
+ "version": "2.3.0",
4
4
  "description": "Load a submodule, plugin, or file",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -9,42 +9,49 @@
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
14
  "author": "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)",
15
15
  "contributors": [
16
16
  "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)"
17
17
  ],
18
- "dependencies": {
19
- "npm-prefix": "^1.2.0",
20
- "resolve-from": "^2.0.0"
21
- },
22
18
  "main": "index.js",
23
19
  "browser": "browser.js",
24
20
  "files": [
25
21
  "browser",
26
22
  "index.js"
27
23
  ],
24
+ "dependencies": {
25
+ "npm-prefix": "^1.2.0",
26
+ "resolve-from": "^4.0.0"
27
+ },
28
28
  "devDependencies": {
29
- "nyc": "^10.0.0",
30
- "remark-cli": "^2.1.0",
31
- "remark-lint": "^5.2.0",
32
- "remark-preset-wooorm": "^1.0.0",
29
+ "nyc": "^13.0.0",
30
+ "prettier": "^1.12.0",
31
+ "remark-cli": "^6.0.0",
32
+ "remark-lint": "^6.0.0",
33
+ "remark-preset-wooorm": "^4.0.0",
33
34
  "tape": "^4.0.0",
34
- "xo": "^0.17.0"
35
+ "xo": "^0.23.0"
35
36
  },
36
37
  "scripts": {
37
- "build-md": "remark . --quiet --frail --output",
38
- "build": "npm run build-md",
39
- "lint": "xo",
40
- "test-api": "node test/index.js",
38
+ "format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
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,
52
+ "prettier": true,
53
+ "esnext": false,
46
54
  "rules": {
47
- "import/no-dynamic-require": "off",
48
55
  "no-eq-null": "off",
49
56
  "eqeqeq": "off"
50
57
  }
@@ -56,6 +63,8 @@
56
63
  "branches": 100
57
64
  },
58
65
  "remarkConfig": {
59
- "presets": "wooorm"
66
+ "plugins": [
67
+ "preset-wooorm"
68
+ ]
60
69
  }
61
70
  }
package/readme.md CHANGED
@@ -1,60 +1,74 @@
1
1
  # load-plugin [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov]
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
+ Load a submodule, plugin, or file.
4
+ Like Node’s `require` and `require.resolve`, but from one or more places, and
5
+ optionally global too.
6
6
 
7
7
  ## Installation
8
8
 
9
- [npm][npm-install]:
9
+ [npm][]:
10
10
 
11
11
  ```bash
12
12
  npm install load-plugin
13
13
  ```
14
14
 
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.
15
+ When bundled for the browser, a small [file][browser] is included to warn that,
16
+ when any of the below functions are invoked, the action is unsupported.
18
17
 
19
18
  ## Usage
20
19
 
21
20
  Say we’re in this project (with dependencies installed):
22
21
 
23
22
  ```javascript
24
- var load = require('load-plugin');
23
+ var load = require('load-plugin')
25
24
 
26
- load.resolve('lint', {prefix: 'remark'});
27
- // '/Users/tilde/projects/oss/load-plugin/node_modules/remark-lint/index.js'
25
+ load.resolve('lint', {prefix: 'remark'})
26
+ // => '/Users/tilde/projects/oss/load-plugin/node_modules/remark-lint/index.js'
28
27
 
29
- load.resolve('./index.js', {prefix: 'remark'});
30
- // '/Users/tilde/projects/oss/load-plugin/index.js'
28
+ load.resolve('@babel/function-name', {prefix: 'helper'})
29
+ // => '/Users/tilde/projects/oss/load-plugin/node_modules/@babel/helper-function-name/index.js'
31
30
 
32
- load.require('lint', {prefix: 'remark'});
33
- // [Function: lint]
31
+ load.resolve('./index.js', {prefix: 'remark'})
32
+ // => '/Users/tilde/projects/oss/load-plugin/index.js'
33
+
34
+ load.require('lint', {prefix: 'remark'})
35
+ // => [Function: lint]
34
36
  ```
35
37
 
36
38
  ## API
37
39
 
38
40
  ### `loadPlugin(name[, options])`
39
41
 
40
- Uses the standard node module loading strategy to require `name`
41
- in each given `cwd` (and optionally the global `node_modules`
42
- directory).
42
+ Uses the standard node module loading strategy to require `name` in each given
43
+ `cwd` (and optionally the global `node_modules` directory).
44
+
45
+ If a prefix is given and `name` is not a path, `prefix-name` is also searched
46
+ (preferring these over non-prefixed modules).
47
+ If name starts with a scope (`@scope/name`), the prefix is applied after it:
48
+ `@scope/prefix-name`.
49
+
50
+ ##### `options`
51
+
52
+ ###### `options.prefix`
53
+
54
+ Prefix to search for (`string`, optional).
43
55
 
44
- If a prefix is given and `name` is not a path, `prefix-name`
45
- is also searched (preferring these over non-prefixed modules).
56
+ ###### `options.cwd`
46
57
 
47
- ###### `options`
58
+ Place or places to search from (`string`, `Array.<string>`, default:
59
+ `process.cwd()`).
48
60
 
49
- * `prefix` (`string`, optional)
50
- — Prefix to search for;
51
- * `cwd` (`string`, `Array.<string>`, default: `process.cwd()`)
52
- — Place or places to search from;
53
- * `global` (`boolean`, optional, defaults to whether global is detected)
54
- — Whether to look for `name` in [global places][global].
55
- If this is nully, `load-plugin` will detect if it’s currently
56
- running in global mode: either because it’s in Electron, or because
57
- a globally installed package is running it.
61
+ ###### `options.global`
62
+
63
+ Whether to look for `name` in [global places][global] (`boolean`, optional,
64
+ defaults to whether global is detected).
65
+ If this is nully, `load-plugin` will detect if it’s currently running in global
66
+ mode: either because it’s in Electron, or because a globally installed package
67
+ is running it.
68
+
69
+ Note: Electron runs its own version of Node instead of your system Node.
70
+ That means global packages cannot be found, unless you’ve [set-up][] a [`prefix`
71
+ in your `.npmrc`][prefix] or are using [nvm][] to manage your system node.
58
72
 
59
73
  ###### Returns
60
74
 
@@ -66,9 +80,10 @@ If `require`ing an existing path fails, or if no existing path exists.
66
80
 
67
81
  ### `loadPlugin.resolve(name[, options])`
68
82
 
69
- Search for `name`. Accepts the same parameters as [`loadPlugin`][load-plugin]
70
- but returns an absolute path for `name` instead of requiring it,
71
- and `null` if it cannot be found.
83
+ Search for `name`.
84
+ Accepts the same parameters as [`loadPlugin`][load-plugin] but returns an
85
+ absolute path for `name` instead of requiring it, and `null` if it cannot be
86
+ found.
72
87
 
73
88
  ## License
74
89
 
@@ -84,9 +99,9 @@ and `null` if it cannot be found.
84
99
 
85
100
  [codecov]: https://codecov.io/github/wooorm/load-plugin
86
101
 
87
- [npm-install]: https://docs.npmjs.com/cli/install
102
+ [npm]: https://docs.npmjs.com/cli/install
88
103
 
89
- [license]: LICENSE
104
+ [license]: license
90
105
 
91
106
  [author]: http://wooorm.com
92
107
 
@@ -95,3 +110,9 @@ and `null` if it cannot be found.
95
110
  [load-plugin]: #loadpluginname-options
96
111
 
97
112
  [browser]: browser.js
113
+
114
+ [prefix]: https://docs.npmjs.com/misc/config#prefix
115
+
116
+ [set-up]: https://github.com/sindresorhus/guides/blob/master/npm-global-without-sudo.md
117
+
118
+ [nvm]: https://github.com/creationix/nvm