mirror of
https://github.com/remarkjs/remark-lint.git
synced 2024-09-11 19:57:15 +03:00
Add support for global externals when running globally
Closes GH-38. Closes GH-39.
This commit is contained in:
parent
83b9f2228d
commit
589b4d7c18
@ -93,6 +93,9 @@ These are, or refer to, an object mapping `ruleId`s to rules.
|
||||
Note that in node.js, a string can be given (a module
|
||||
name or a file), but in the browser an object must be passed in.
|
||||
|
||||
When using a globally installed remark-lint, globally installed external
|
||||
rules are also loaded.
|
||||
|
||||
### reset
|
||||
|
||||
````md
|
||||
|
24
lib/index.js
24
lib/index.js
@ -21,6 +21,7 @@ var range = require('remark-range');
|
||||
var zone = require('mdast-zone');
|
||||
var internals = require('./rules');
|
||||
var filter = require('./filter');
|
||||
var npmPrefix = require('npm-prefix')();
|
||||
|
||||
/*
|
||||
* Needed for plug-in resolving.
|
||||
@ -30,12 +31,24 @@ var path = require('path');
|
||||
var fs = require('fs');
|
||||
var exists = fs && fs.existsSync;
|
||||
var resolve = path && path.resolve;
|
||||
var isWindows;
|
||||
var isGlobal;
|
||||
var globals;
|
||||
var cwd;
|
||||
|
||||
var MODULES = 'node_modules';
|
||||
|
||||
/* istanbul ignore else */
|
||||
if (typeof global !== 'undefined') {
|
||||
/* global global */
|
||||
cwd = global.process.cwd();
|
||||
|
||||
/* Detect whether we’re running as a globally installed package. */
|
||||
isWindows = global.process.platform === 'win32';
|
||||
isGlobal = global.process.argv[1].indexOf(npmPrefix) === 0;
|
||||
|
||||
/* istanbul ignore next */
|
||||
globals = resolve(npmPrefix, isWindows ? '' : 'lib', MODULES);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -117,6 +130,14 @@ function attachFactory(id, rule, options) {
|
||||
*
|
||||
* Where `$cwd` is the current working directory.
|
||||
*
|
||||
* When using a globally installed executable, the
|
||||
* following are also included:
|
||||
*
|
||||
* - `$globals/$pathlike`.
|
||||
*
|
||||
* Where `$globals` is the directory of globally installed
|
||||
* npm packages.
|
||||
*
|
||||
* @example
|
||||
* var plugin = findPlugin('foo');
|
||||
*
|
||||
@ -128,6 +149,7 @@ function attachFactory(id, rule, options) {
|
||||
function loadExternal(pathlike) {
|
||||
var local = resolve(cwd, pathlike);
|
||||
var current = resolve(cwd, 'node_modules', pathlike);
|
||||
var globalPath = resolve(globals, pathlike);
|
||||
var plugin;
|
||||
|
||||
if (exists(local) || exists(local + '.js')) {
|
||||
@ -135,6 +157,8 @@ function loadExternal(pathlike) {
|
||||
/* istanbul ignore else - for globals */
|
||||
} else if (exists(current)) {
|
||||
plugin = current;
|
||||
} else if (isGlobal && exists(globalPath)) {
|
||||
plugin = globalPath;
|
||||
} else {
|
||||
plugin = pathlike;
|
||||
}
|
||||
|
@ -18,6 +18,7 @@
|
||||
"mdast-util-position": "^1.0.0",
|
||||
"mdast-util-to-string": "^1.0.0",
|
||||
"mdast-zone": "^2.0.0",
|
||||
"npm-prefix": "^1.1.1",
|
||||
"plur": "^2.0.0",
|
||||
"unist-util-visit": "^1.0.0",
|
||||
"vfile-sort": "^1.0.0"
|
||||
|
@ -4,7 +4,7 @@
|
||||
"example": "<!-- Explicitly activate rules: -->\n```json\n{\n \"reset\": true,\n \"final-newline\": true\n}\n```\n"
|
||||
},
|
||||
"external": {
|
||||
"description": "External contains a list of extra rules to load.\nThese are, or refer to, an object mapping `ruleId`s to rules.\n\nNote that in node.js, a string can be given (a module\nname or a file), but in the browser an object must be passed in.",
|
||||
"description": "External contains a list of extra rules to load.\nThese are, or refer to, an object mapping `ruleId`s to rules.\n\nNote that in node.js, a string can be given (a module\nname or a file), but in the browser an object must be passed in.\n\nWhen using a globally installed remark-lint, globally installed external\nrules are also loaded.",
|
||||
"example": "<!-- Load more rules -->\n```json\n{\n \"external\": [\"foo\", \"bar\", \"baz\"]\n}\n```\n"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user