1 |
|
2 |
|
3 | var retire = require('./retire');
|
4 | var utils = require('./utils');
|
5 | var fs = require('fs');
|
6 | var crypto = require('crypto');
|
7 |
|
8 | var loggers = {
|
9 | console : require("./reporters/console"),
|
10 | text : require("./reporters/console"),
|
11 | json : require("./reporters/json"),
|
12 | depcheck : require("./reporters/depcheck"),
|
13 | cyclonedx: require("./reporters/cyclonedx")
|
14 | };
|
15 | loggers.clean = loggers.console;
|
16 | loggers.jsonsimple = loggers.json;
|
17 |
|
18 |
|
19 | var colorwarn = function(x) { return x; };
|
20 |
|
21 | var verbose = false;
|
22 |
|
23 | function hashContent(hash, content) {
|
24 | var h = crypto.createHash(hash);
|
25 | h.update(content);
|
26 | return h.digest('hex');
|
27 | }
|
28 |
|
29 | var hash = {
|
30 | md5: (file) => hashContent('md5', file),
|
31 | sha1: (file) => hashContent('sha1', file),
|
32 | sha256: (file) => hashContent('sha256', file),
|
33 | sha512: (file) => hashContent('sha512', file),
|
34 | };
|
35 |
|
36 |
|
37 |
|
38 | var writer = {
|
39 | out: console.log,
|
40 | err: function(x) { console.warn(colorwarn(x)); },
|
41 | close : function() { }
|
42 | };
|
43 |
|
44 | var logger = {
|
45 | info : function(x) { writer.out(x); },
|
46 | debug : function(x) { if (verbose) writer.out(x); },
|
47 | warn : function(x) { writer.err(x); },
|
48 | error : function(x) { writer.err(x); },
|
49 |
|
50 | logDependency : function(finding) { },
|
51 | logVulnerableDependency: function(finding) { },
|
52 | close: function() { writer.close(); }
|
53 | };
|
54 |
|
55 |
|
56 |
|
57 | function configureFileWriter(config) {
|
58 | var fileOutput = {
|
59 | fileDescriptor: fs.openSync(config.outputpath, "w")
|
60 | };
|
61 | if (fileOutput.fileDescriptor < 0) {
|
62 | console.error("Could not open " + config.outputpath + " for writing");
|
63 | process.exit(9);
|
64 | }
|
65 | fileOutput.stream = fs.createWriteStream('', {fd: fileOutput.fileDescriptor, autoClose: false});
|
66 | var writeToFile = function(message) {
|
67 | fileOutput.stream.write(message);
|
68 | fileOutput.stream.write('\n');
|
69 | };
|
70 | writer.out = writer.err = writeToFile;
|
71 | writer.close = function() {
|
72 | fileOutput.stream.on('finish', function() {
|
73 | fs.closeSync(fileOutput.fileDescriptor);
|
74 | });
|
75 | fileOutput.stream.end();
|
76 | };
|
77 | }
|
78 |
|
79 | exports.open = function(config) {
|
80 | verbose = config.verbose;
|
81 | if (config.colors) colorwarn = config.colorwarn;
|
82 | var format = config.outputformat || "console";
|
83 | if (Object.keys(loggers).indexOf(format) == -1) {
|
84 | console.warn("Invalid outputformat: " + format);
|
85 | process.exit(1);
|
86 | }
|
87 | loggers[format].configure(logger, writer, config, hash);
|
88 |
|
89 | if (typeof config.outputpath === 'string') {
|
90 | configureFileWriter(config);
|
91 | }
|
92 | return logger;
|
93 | };
|