-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
136 lines (108 loc) · 3.61 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const fs = require('fs');
const path = require('path');
const findup = require('find-up');
const semver = require('semver');
const defaults = {
optional: 'false',
dev: 'false',
peers: 'false',
production: 'false',
};
const npmPkgUrl = 'https://npmjs.org/package/';
function findPkg(dir) {
const pkgPath = findup.sync('package.json', { cwd: dir });
if (!pkgPath) throw new Error('No package.json file found');
return pkgPath;
}
function sanitizeSemver(version, maxLength = 10, truncateStr = '...') {
if (semver.valid(version)) return version;
const adjustedLength = maxLength - truncateStr.length;
return version.length > adjustedLength
? [version.substr(0, adjustedLength), truncateStr].join('')
: version;
}
function convertRepositoryToUrl(repository, name) {
let repo = (repository.url ? repository.url : repository).replace('.git', '');
if (repo.startsWith('http')) {
return repo;
} else if (repo.startsWith('git://')) {
return repo.replace('git://', 'https://');
} else if (repo.startsWith('git+ssh')) {
const [full, url] = repo.match(/^git\+ssh\:\/\/git\@(.*)$/);
return [`https://`, url].join('');
} else if (repo.startsWith('git@')) {
return repo.replace('git@', 'https://').replace(':', '/');
} else {
return ['https://github.com/', repo].join('');
}
return repo;
}
function getPkgUrl(pkg) {
const { name, repository, homepage, bugs } = pkg;
if (homepage) return homepage;
if (repository) return convertRepositoryToUrl(repository, name);
if (bugs) return bugs.url || bugs;
return `https://npmjs.org/package/${name}`;
}
function sanitizeLicense(license) {
return license ? license : 'UNLICENSED';
}
const readDependencies = (pkg) => (manifest, type) => {
const dependencyType = type || 'production';
let dependencies;
if (type === 'production') {
dependencies = pkg.dependencies;
} else {
dependencies = pkg[`${type}Dependencies`];
}
return manifest.concat(
Object.keys(dependencies || {}).map((name) => {
const localPkgPath = findup.sync(`node_modules/${name}/package.json`);
const localPkg = JSON.parse(fs.readFileSync(localPkgPath, 'utf8'));
const { description, homepage, version, repository, license } = localPkg;
return {
name,
semver: sanitizeSemver(dependencies[name]),
version,
description,
url: getPkgUrl(localPkg),
license: sanitizeLicense(license),
dependencyType,
};
})
);
};
function renderDependencies(dependency) {
const { name, semver, version, license, description, url, dependencyType } =
dependency;
return [
'',
`[${[name, semver].join('@')}](${url})`,
description,
version,
license,
dependencyType,
'',
].join(' | ');
}
module.exports = function DEPENDENCYTABLE(content, _options = {}, config) {
const options = Object.assign({}, defaults, _options);
let pkgPath;
if (options.pkg) {
pkgPath = path.resolve(path.dirname(config.originalPath), options.pkg);
} else {
pkgPath = findPkg(config.originalPath);
}
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
const headers = [
'| **Dependency** | **Description** | **Version** | **License** | **Type** |',
'| -------------- | --------------- | ----------- | ----------- | -------- |',
];
const types = ['production', 'peer', 'optional', 'dev'];
const declaredTypes = types.filter((type) => options[type] === 'true');
const deps = (declaredTypes.length ? declaredTypes : types)
.concat([''])
.reduce(readDependencies(pkg), [])
.map(renderDependencies);
return headers.concat(deps).join('\n');
};