forked from arei/license-extractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
198 lines (157 loc) · 4.4 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
Recursively walk your project and find all license files.
Copyright 2014, Glen R. Goodwin
*/
"use strict";
var nlf = require("nlf");
var FindIt = require("findit");
var FS = require("fs");
var Rimraf = require("rimraf");
var Mkdirp = require("mkdirp");
var Path = require("path");
var args = require("minimist")(process.argv.slice(2),{
string: ["source","target","mode"],
boolean: ["overwrite","noheaders"],
default: {
source: ".",
target: "./LICENSES",
mode: "output",
overwrite: false,
noheaders: false
}
});
var LICENSE_FILE_MATCHERS = [
/LICENSE(-[\w.-_])?(\.TXT|\.MD|\.)?$/i
];
var source = args.source;
var mode = args.mode.toLowerCase();
var target = Path.resolve(args.target);
var licenses = [];
var deps = [];
if (mode!=="merge" && mode!=="collect" && mode!=="output") mode = "output";
var consume = function() {
licenses.forEach(function(file){
var title = file;
title = title.replace(/[\\\/]?node_modules[\\\/]?/g,"/");
title = title.replace(/[\\\/]/g," ");
title = title.replace(/\.TXT$|\.MD$|\.$/i,"");
if (mode==="output") consumeOutput(title,file);
if (mode==="collect") consumeCollect(title,file);
if (mode==="merge") consumeMerge(title,file);
});
};
var prepareOutput = function() {
// nothing to do... here for future sake.
};
var consumeOutput = function(title,file) {
var text = FS.readFileSync(file,{
encoding: "utf8"
});
if (!text) return;
console.log("\n\n");
if (!args.noheaders) console.log("--------------------------------------------------------------------------------");
if (!args.noheaders) console.log(title+" ("+file+")");
if (!args.noheaders) console.log("--------------------------------------------------------------------------------");
console.log(text);
if (!args.noheaders) console.log("--------------------------------------------------------------------------------");
if (!args.noheaders) console.log("\n");
};
var prepareMerge = function() {
if (FS.existsSync(target)) {
if (!args.overwrite) {
console.error("File/Directory already exists.");
process.reallyExit(1);
}
else {
Rimraf.sync(target);
}
}
FS.appendFileSync(target,"",{
encoding: "utf8"
});
};
var consumeMerge = function(title,file) {
var text = FS.readFileSync(file,{
encoding: "utf8"
});
if (!text) return;
var s = "";
s+= "\r\n\r\n";
if (!args.noheaders) s+= "--------------------------------------------------------------------------------\r\n";
if (!args.noheaders) s+= title+" ("+file+")\r\n";
if (!args.noheaders) s+= "--------------------------------------------------------------------------------\r\n";
s+= text+"\r\n";
if (!args.noheaders) s+= "--------------------------------------------------------------------------------\r\n";
if (!args.noheaders) s+= "\r\n";
FS.appendFileSync(target,s,{
encoding: "utf8"
});
console.log("Added "+title+" ("+file+")");
};
var prepareCollect = function() {
if (FS.existsSync(target)) {
if (!args.overwrite) {
console.error("File/Directory already exists.");
process.reallyExit(1);
}
else {
Rimraf.sync(target);
}
}
Mkdirp.sync(target);
};
var consumeCollect = function(title,file) {
var text = FS.readFileSync(file,{
encoding: "utf8"
});
if (!text) return;
var targetfile = title+".txt";
title = title.replace(/\s+/,".");
targetfile = Path.resolve(target,targetfile);
FS.writeFileSync(targetfile,text,{
encoding: "utf8"
});
console.log("Copied "+title+" to "+targetfile);
};
var prepare = function() {
if (mode==="output") prepareOutput();
if (mode==="collect") prepareCollect();
if (mode==="merge") prepareMerge();
};
var find = function() {
var findit = FindIt(source);
findit.on("file",function(file){
var resfile = Path.resolve(source,file);
if (resfile===target) return;
var match = LICENSE_FILE_MATCHERS.some(function(re){
return !!file.match(re);
});
if (!match) return;
// Only add dev dependencies
if(deps.some(function(dep) {
return dep.licenseSources.license.sources.length > 0 && dep.licenseSources.license.sources[0].filePath === resfile
})) {
licenses.push(file);
}
});
findit.on("directory",function(dir,stat,stop){
var resdir = Path.resolve(source,dir);
if (resdir===target) stop();
});
findit.on("end",function(){
consume();
});
};
nlf.find({
directory: source,
production: true
}, function (err, data) {
if (err) {
console.log(err);
return
}
deps = data;
console.log('Dev Deps: ' + deps.length)
prepare();
find();
});