-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
97 lines (86 loc) · 3.26 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
#!/usr/bin/env node
const program = require('commander');
const nlf = require('nlf');
const path = require('path');
const fs = require('fs');
function appender(xs) {
xs = xs || [];
return function(x) {
xs.push(x);
return xs;
}
}
program
.name('lcdoc')
.version('0.0.1')
.option('-o, --out <path>', 'write the data to a specific file.')
.option('-i, --ignore <pattern>', 'write the path to ignore.', appender(), [])
.option('-d, --depth <number>', 'how deep to traverse packages where 0 is the current package.json only');
program
.command('make [path]')
.action(function(argv) {
const ignorePaths = program.ignore;
let result = [];
let workingPath = argv || process.cwd();
const option = {
directory: workingPath,
};
let depth = program.depth;
if(depth) {
option.depth = Number(depth);
}
nlf.find(option, function(err, items = []) {
items.forEach((item) => {
let textList = [];
if (item.id) {
textList.push(`Package: ${item.id}`);
}
const licenseSources = item.licenseSources;
if (licenseSources && licenseSources.license) {
const package = licenseSources.package || {};
const license = licenseSources.license;
const pSource = package.sources || [];
const lSource = license.sources || [];
let licenseUrl = '';
pSource.forEach((p) => {
textList.push(`License: ${p.license}`);
if (p.url) {
textList.push(`License Url: ${p.url}`);
}
});
lSource.forEach((l) => {
if(ignorePaths && ignorePaths.some((i)=> {
var a = path.resolve(workingPath, i);
if(l.filePath.indexOf(a) > -1) {
return true;
}
})) {
return;
}
if (l.filePath) {
const parser = path.parse(l.filePath);
textList.push(`License Source: ${parser.base}`);
}
if (l.text) {
textList.push(`Source Text:\n\n${l.text}`);
}
});
}
result.push(textList.join('\n'));
});
const outputPath = program.out;
const outputText = result.join('\n\n-------------------------------------------------------------------------\n\n');
if (outputPath) {
fs.writeFile(outputPath, outputText, 'utf8', (err) => {
if (err) {
console.log(err);
} else {
console.log(`Successfully created file '${outputPath}'.`);
}
});
} else {
console.log(outputText);
}
});
});
program.parse(process.argv);