Skip to content

Commit d9b811a

Browse files
committed
Merge pull request #91 from secdec/feature/outputfile
Add support for output to file
2 parents 4c4997d + 42b38a9 commit d9b811a

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

node/bin/retire

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,15 @@ program
4545
.option('--noderepo <path>', 'Local version of repo')
4646
.option('--proxy <url>', 'Proxy url (http://some.sever:8080)')
4747
.option('--outputformat <format>', 'Valid formats: text, json')
48+
.option('--outputpath <path>', 'File to which output should be written')
4849
.option('--ignore <paths>', 'Comma delimited list of paths to ignore')
4950
.option('--ignorefile <path>', 'Custom .retireignore file, defaults to .retireignore')
5051
.option('--exitwith', 'Custom exit code (default: 13) when vulnerabilities are found')
5152
.parse(process.argv);
5253

5354
var config = _.extend({ path: '.' }, _.pick(program, [
5455
'package', 'node', 'js', 'jspath', 'verbose', 'nodepath', 'path', 'jsrepo', 'noderepo',
55-
'dropexternal', 'nocache', 'proxy', 'ignore', 'ignorefile', 'outputformat', 'exitwith'
56+
'dropexternal', 'nocache', 'proxy', 'ignore', 'ignorefile', 'outputformat', 'outputpath', 'exitwith'
5657
]));
5758

5859
if (!config.nocache) {
@@ -74,8 +75,6 @@ if(config.ignorefile) {
7475
config.ignore = config.ignore.concat(ignored);
7576
}
7677

77-
78-
7978
events.on('load-js-repo', function() {
8079
(config.jsrepo
8180
? repo.loadrepositoryFromFile(config.jsrepo, config)
@@ -131,16 +130,28 @@ events.on('js-scanned', function() {
131130
});
132131

133132
events.on('scan-done', function() {
133+
var exit = function(exitCode) {
134+
exitCode = exitCode || 0;
135+
process.exit(vulnsFound ? (config.exitwith || 13) : exitCode);
136+
};
134137
if (config.outputformat === 'json') {
135-
(vulnsFound ? console.warn : console.log)(JSON.stringify(finalResults));
136-
}
137-
var exit = function() {
138-
process.exit(vulnsFound ? (config.exitwith || 13) : 0);
139-
}
140-
var stream = (vulnsFound ? process.stderr : process.stdout);
141-
if (! stream.write('', exit)) {
138+
if (config.fileOutput) {
139+
config.fileOutput.stream.on('drain', function() {
140+
fs.close(config.fileOutput.fileDescriptor);
141+
exit();
142+
});
143+
config.fileOutput.stream.write(JSON.stringify(finalResults));
144+
config.fileOutput.stream.end();
145+
} else {
146+
(vulnsFound ? console.warn : console.log)(JSON.stringify(finalResults));
147+
var stream = (vulnsFound ? process.stderr : process.stdout);
148+
if (! stream.write('', exit)) {
142149
stream.on('drain', exit);
150+
}
143151
}
152+
}
153+
154+
144155
});
145156

146157
process.on('uncaughtException', function (err) {
@@ -154,9 +165,26 @@ events.on('stop', function() {
154165
process.exit(1);
155166
});
156167

168+
if (typeof config.outputpath === 'string') {
169+
config.fileOutput = {
170+
fileDescriptor: fs.openSync(config.outputpath, "w")
171+
};
172+
if (config.fileOutput.fileDescriptor < 0) {
173+
console.error("Could not open " + config.outputpath + " for writing");
174+
process.exit(9);
175+
} else {
176+
config.fileOutput.stream = fs.createWriteStream('', {fd: config.fileOutput.fileDescriptor});
177+
config.writeToFile = function(message) {
178+
config.fileOutput.stream.write(message);
179+
config.fileOutput.stream.write('\n');
180+
};
181+
config.logger = config.writeToFile;
182+
}
183+
}
184+
157185
if (config.outputformat === 'json') {
158-
config.logger = function() {};
159-
config.warnlogger = function() {};
186+
config.logger = function () {};
187+
config.warnlogger = function () {};
160188
}
161189

162190
if (config.node) {

0 commit comments

Comments
 (0)