Skip to content

Commit

Permalink
lint, optimizations, update deps
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert committed Dec 1, 2015
1 parent f03ea07 commit 616dd52
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 54 deletions.
62 changes: 27 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
'use strict';

var path = require('path');
var colors = require('ansi-colors');
var utils = require('./utils');

module.exports = function (options) {
module.exports = function(options) {
options = options || {};
var configProp = options.configProp || 'reflinks';

Expand All @@ -26,15 +27,15 @@ module.exports = function (options) {
*/

if (typeof config.pkg === 'undefined') {
config.pkg = require('load-pkg');
config.pkg = require('load-pkg')(process.cwd());
}

/**
* Get keys from `dependencies`
*/

if (typeof config.keys === 'undefined') {
config.keys = keys(config.pkg.dependencies || {});
config.keys = utils.keys(config.pkg.dependencies || {});
}

/**
Expand All @@ -53,37 +54,40 @@ module.exports = function (options) {

function reflinks(repos, opts, cb) {
if (typeof repos === 'function') {
cb = repos;
opts = {};
repos = null;
return reflinks(null, {}, repos);
}

if (typeof opts === 'function') {
cb = opts;
opts = {};
return reflinks(repos, {}, opts);
}

repos = repos || [];
opts = utils.extend({}, options, opts);

if (this && this.options) {
opts = utils.extend({}, this.options, opts);
}

// allow a prop-string to be passed: eg: `related("a.b.c")`,
// so that `get()` can resolve the value from the context
if (this && this.context && typeof repos === 'string') {
opts = utils.extend({}, this.options, opts);
var res = utils.get(this.context, [configProp, repos].join('.'));
if (res) repos = res;
}

opts.remove = utils.arrayify(opts.remove);
if (opts.remove.length) {
utils.remove(repos, opts.remove);
}

var deps = reflinks.sync(repos, opts);

if (!repos || !repos.length) {
return typeof cb === 'function'
? cb(null, deps)
: deps;
return typeof cb === 'function' ? cb(null, deps) : deps;
}

// generate reflinks from npm packages
getRepos(arrayify(repos), opts, function (err, res) {
getRepos(utils.arrayify(repos), opts, function(err, res) {
if (err) return cb(err);
if (opts.node_modules === true) {
res += '\n' + deps;
Expand All @@ -102,11 +106,11 @@ module.exports = function (options) {
* @return {String}
*/

reflinks.sync = function (repos, opts) {
reflinks.sync = function(repos, opts) {
message('node_modules', opts);

if (!config.keys.length) return '';
repos = repos ? arrayify(repos) : null;
repos = repos ? utils.arrayify(repos) : null;
var keys = [];

var len = repos && repos.length;
Expand Down Expand Up @@ -142,21 +146,21 @@ module.exports = function (options) {
opts = opts || {};
message('npm', opts);

utils.getPkgs(repos, function (err, pkgs) {
utils.getPkgs(repos, function(err, pkgs) {
if (err) {
console.error(utils.red('helper-reflinks: %j'), err);
console.error(colors.red('helper-reflinks: %j'), err);
return cb(err);
}

pkgs = pkgs.sort(function (a, b) {
pkgs = pkgs.sort(function(a, b) {
return a.name.localeCompare(b.name);
});

utils.reduce(pkgs, [], function (acc, pkg, next) {
var link = utils.mdu.reference(pkg.name, pkg.homepage);
utils.reduce(pkgs, [], function(acc, pkg, next) {
var link = utils.referenceLink(pkg.name, pkg.homepage);
link = link.replace(/#readme$/, '');
next(null, acc.concat(link));
}, function (err, arr) {
}, function(err, arr) {
if (err) return cb(err);
cb(null, arr.join('\n'));
});
Expand All @@ -179,7 +183,7 @@ module.exports = function (options) {
var ele = node_modules(dep);
var ref = homepage(ele);
if (ref) {
res += utils.mdu.reference(ref.repo, ref.url) + '\n';
res += utils.referenceLink(ref.repo, ref.url) + '\n';
}
}
return res;
Expand Down Expand Up @@ -229,22 +233,10 @@ module.exports = function (options) {
function message(origin, opts) {
if (opts && opts.silent !== true) {
var msg = ' helper-reflinks: generating reflinks from ' + origin + ' info.';
console.log(' ' + utils.green(utils.success) + utils.gray(msg));
console.log(' ' + colors.green(utils.success) + colors.gray(msg));
}
}

/**
* Utils
*/

function keys(o) {
return Object.keys(o).sort();
}

function arrayify(val) {
return Array.isArray(val) ? val : [val];
}

/**
* Expose `reflinks`
*/
Expand Down
27 changes: 12 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,26 @@
"test": "mocha"
},
"dependencies": {
"ansi-gray": "^0.1.1",
"ansi-green": "^0.1.1",
"ansi-red": "^0.1.1",
"ansi-colors": "^0.1.0",
"async-array-reduce": "^0.1.0",
"extend-shallow": "^2.0.1",
"get-pkgs": "^0.2.2",
"get-value": "^1.2.1",
"lazy-cache": "^0.2.3",
"load-pkg": "^1.3.0",
"markdown-utils": "^0.7.1",
"get-pkgs": "^0.3.0",
"get-value": "^2.0.0",
"lazy-cache": "^0.2.4",
"load-pkg": "^2.0.1",
"markdown-reference": "^0.1.0",
"parse-github-url": "^0.2.1",
"stringify-github-url": "^0.1.0",
"success-symbol": "^0.1.0"
},
"devDependencies": {
"assemble-core": "^0.1.1",
"assemble-core": "^0.3.0",
"engine-base": "^0.1.2",
"engine-handlebars": "^0.8.0",
"handlebars": "^3.0.3",
"lodash": "^3.10.0",
"mocha": "*",
"should": "*",
"verb": "^0.8.0"
"handlebars": "^4.0.5",
"lodash": "^3.10.1",
"mocha": "^2.3.4",
"should": "^7.1.1"
},
"keywords": [
"docs",
Expand Down Expand Up @@ -71,4 +68,4 @@
"description": ""
}
}
}
}
28 changes: 24 additions & 4 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,39 @@ var utils = require('lazy-cache')(require);
var fn = require;

require = utils;
require('ansi-red', 'red');
require('ansi-gray', 'gray');
require('ansi-green', 'green');
require('success-symbol', 'success');
require('parse-github-url', 'parse');
require('stringify-github-url', 'stringify');
require('async-array-reduce', 'reduce');
require('extend-shallow', 'extend');
require('markdown-utils', 'mdu');
require('markdown-reference', 'referenceLink');
require('get-value', 'get');
require('get-pkgs');
require = fn;

/**
* Utils
*/

utils.keys = function (o) {
return Object.keys(o).sort();
};

utils.arrayify = function (val) {
if (!val) return [];
return Array.isArray(val) ? val : [val];
};

utils.remove = function (arr, items) {
var len = arr.length;
while (len--) {
if (items.indexOf(arr[len]) > -1) {
arr.splice(1, len);
}
}
return arr;
};

/**
* Expose utils
*/
Expand Down

0 comments on commit 616dd52

Please # to comment.