-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·81 lines (66 loc) · 2.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* depdoc
* @author: Zander Martineau
*
* USAGE:
* depdoc package.json ✔
* depdoc https://raw.githubusercontent.com/mrmartineau/depdoc/master/package.json ✔
*
* TODO:
* - Error handling:
* - if the registry is down
* - if the path/filename is incorrect/doesn't exist
*/
var fs = require('fs');
var _ = require('lodash');
var registryUrl = require('registry-url')();
var request = require('sync-request');
var Mustache = require('mustache');
var repo = require('gepo');
var banner = '# Project dependencies\n\n';
var credit = 'Documentation created with [depdoc](https://github.com/mrmartineau/depdoc/)'
function depdoc(input) {
var result;
var input = arguments[0];
var type = arguments[1] === undefined ? undefined : arguments[1];
if (type === 'json') {
result = getPackageInformation(input);
} else if (input.indexOf('github') > 0) {
var newPackageUrl = input.replace('raw.githubusercontent.com', 'rawgit.com');
var res = request('GET', newPackageUrl);
var data = res.getBody('utf-8').toString();
result = getPackageInformation(JSON.parse(data));
} else {
var readFile = fs.readFileSync(input);
result = getPackageInformation(JSON.parse(readFile));
}
return result;
}
/**
* Get package.json contents
*/
function getPackageInformation(data) {
var deps = data.dependencies;
var result = '';
if (_.isEmpty(deps)) {
return 'This package has no dependencies :(';
}
_.forEach(deps, function(value, key){
var newKey = key.replace('/', '%2F');
var res = request('GET', registryUrl + newKey);
var data = res.getBody('utf-8');
var packageInfo = JSON.parse(data);
var packageInfoData = {
name: packageInfo.name,
description: typeof packageInfo.description !== 'undefined' ? packageInfo.description : '',
homepage: typeof packageInfo.homepage !== 'undefined' ? packageInfo.homepage : '',
repo: typeof packageInfo.repository !== 'undefined' ? repo(packageInfo.repository.url) : '',
issues: typeof packageInfo.bugs !== 'undefined' ? packageInfo.bugs.url : '',
license: typeof packageInfo.license !== 'undefined' ? packageInfo.license : ''
}
var template = fs.readFileSync(__dirname + '/template.mst');
result += Mustache.render(template.toString(), packageInfoData);
});
return banner + result + credit;
}
module.exports = depdoc;