Add support for global externals when running globally

Closes GH-38.
Closes GH-39.
This commit is contained in:
YJ Yang 2016-01-12 21:35:29 -05:00 committed by Titus Wormer
parent 83b9f2228d
commit 589b4d7c18
4 changed files with 29 additions and 1 deletions

View File

@ -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

View File

@ -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 were 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;
}

View File

@ -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"

View File

@ -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"
}
}