/
index.js
57 lines (46 loc) · 1.45 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
'use strict';
const Chalk = require('chalk').constructor;
const inspectWithKind = require('inspect-with-kind');
module.exports = function sumUp(pkgData, options) {
if (!pkgData || Array.isArray(pkgData) || typeof pkgData !== 'object') {
throw new TypeError(`Expected an object of package information \`{name: ... version: ..., description: ..., ...}\`, but got ${
inspectWithKind(pkgData)
}.`);
}
if (options) {
if (Array.isArray(options) || typeof options !== 'object') {
throw new TypeError(`The second argument of sum-up must be a plain object or undefined, but got ${
inspectWithKind(options)
}.`);
}
} else {
options = {};
}
const chalkOption = {};
if (options.color === true) {
chalkOption.enabled = true;
chalkOption.level = 1;
} else if (options.color === false) {
chalkOption.enabled = false;
chalkOption.level = 0;
} else if (options.color !== undefined) {
throw new TypeError(`Expected \`color\` option to be a Boolean value, but got ${
inspectWithKind(options.color)
}.`);
}
const chalk = new Chalk(chalkOption);
const lines = [];
const nameAndVersion = `${chalk.cyan(pkgData.name || '')}${
pkgData.version ? `${pkgData.name ? ' ' : ''}${chalk.gray(`v${pkgData.version}`)}` : ''
}`;
if (nameAndVersion) {
lines.push(nameAndVersion);
}
if (pkgData.homepage) {
lines.push(chalk.gray(pkgData.homepage));
}
if (pkgData.description) {
lines.push(pkgData.description);
}
return lines.join('\n');
};