Skip to content

Commit

Permalink
Fix: Consider out option also when passed to programmatic interface
Browse files Browse the repository at this point in the history
  • Loading branch information
steffen-workpath committed May 12, 2023
1 parent f3755fa commit d7d069b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 20 deletions.
24 changes: 4 additions & 20 deletions bin/license-checker-rseidelsohn
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ const exitProcessOrWarnIfNeeded = require('../lib/exitProcessOrWarnIfNeeded');
const fs = require('fs');
const helpers = require('../lib/licenseCheckerHelpers');
const licenseCheckerMain = require('../lib/index');
const mkdirp = require('mkdirp');
const path = require('path');

const parsedArgs = args.parse();
const hasFailingArg = parsedArgs.failOn || parsedArgs.onlyAllow;
Expand All @@ -27,26 +25,12 @@ licenseCheckerMain.init(parsedArgs, function (err, foundLicensesJson) {
console.error(err);
}

if (helpers.shouldColorizeOutput(parsedArgs)) {
helpers.colorizeOutput(foundLicensesJson);
}

const formattedOutput = helpers.getFormattedOutput(foundLicensesJson, parsedArgs);

if (parsedArgs.files || parsedArgs.out) {
if (parsedArgs.files) {
licenseCheckerMain.asFiles(foundLicensesJson, parsedArgs.files);
}

if (parsedArgs.out) {
const dir = path.dirname(parsedArgs.out);

mkdirp.sync(dir);
fs.writeFileSync(parsedArgs.out, formattedOutput, 'utf8');
if (!parsedArgs.out) {
if (helpers.shouldColorizeOutput(parsedArgs)) {
helpers.colorizeOutput(foundLicensesJson);
}
}

if (!parsedArgs.out) {
const formattedOutput = helpers.getFormattedOutput(foundLicensesJson, parsedArgs);
console.log(formattedOutput);
}
});
26 changes: 26 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,9 @@ exports.init = function init(args, callback) {
if (err) {
debugError(err);
inputError = err;
} else {
// Output to files, if necessary
this.writeOutput(args, resultJson);
}

// Return the callback and variables nicely
Expand Down Expand Up @@ -964,3 +967,26 @@ const getCsvData = (sorted, customFormat, csvComponentPrefix) => {

const getOptionArray = (option) =>
(Array.isArray(option) && option) || (typeof option === 'string' && option.split(';')) || false;

/**
* Write output to a file, if indicated in parsedArgs.
*/
exports.writeOutput = function (parsedArgs, foundLicensesJson) {
if (parsedArgs.files || parsedArgs.out) {
// Import is in here to avoid a circular dependency
const licenseCheckerHelpers = require('./licenseCheckerHelpers');

const formattedOutput = licenseCheckerHelpers.getFormattedOutput(foundLicensesJson, parsedArgs);

if (parsedArgs.files) {
this.asFiles(foundLicensesJson, parsedArgs.files);
}

if (parsedArgs.out) {
const dir = path.dirname(parsedArgs.out);

mkdirp.sync(dir);
fs.writeFileSync(parsedArgs.out, formattedOutput, 'utf8');
}
}
};
32 changes: 32 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,38 @@ describe('main tests', function () {
});
});

describe('should write output to files in programmatic usage', function () {
const tmpFileName = path.join(__dirname, 'tmp_output.json');

before(function (done) {
checker.init(
{
start: path.join(__dirname, '../'),
json: true,
out: tmpFileName,
},
function (err, sorted) {
done();
},
);
});

after(() => {
if (fs.existsSync(tmpFileName)) {
fs.unlinkSync(tmpFileName);
}
});

it('and the file should contain parseable JSON', function () {
assert.ok(fs.existsSync(tmpFileName));

const outputTxt = fs.readFileSync(tmpFileName, 'utf8');
const outputJson = JSON.parse(outputTxt);

assert.equal(typeof outputJson, 'object');
});
});

function parseAndExclude(parsePath, licenses, result) {
return function (done) {
checker.init(
Expand Down

0 comments on commit d7d069b

Please sign in to comment.