Skip to content

Commit

Permalink
Add support for global externals when running globally
Browse files Browse the repository at this point in the history
Closes GH-38.
Closes GH-39.
  • Loading branch information
YJ Yang authored and wooorm committed Jan 13, 2016
1 parent 83b9f22 commit 589b4d7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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');
*
Expand All @@ -128,13 +149,16 @@ 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')) {
plugin = local;
/* istanbul ignore else - for globals */
} else if (exists(current)) {
plugin = current;
} else if (isGlobal && exists(globalPath)) {
plugin = globalPath;
} else {
plugin = pathlike;
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion script/additional.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit 589b4d7

Please sign in to comment.