1

I'm experimenting with the license-checker library to filter and throw on certain license types. In the README, there is an example of how to interrogate the JSON data with some unexpected characters:

var checker = require('license-checker');

checker.init({
    start: '/path/to/start/looking'
}, function(json, err) {
    if (err) {
        //Handle error
    } else {
        //The sorted json data
    }
});

However, when I look at the format of that JSON, I'm not sure how I would pull it a part to evaluate the licenses. Here's an example of the structure:

 { 'ansi-styles@1.1.0': 
     { licenses: 'MIT',
       repository: 'https://github.com/sindresorhus/ansi-styles' },
    'ansi-styles@2.1.0': 
     { licenses: 'MIT',
       repository: 'git+https://github.com/chalk/ansi-styles',
       licenseFile: '...' },
    'ansi-wrap@0.1.0': 
     { licenses: 'MIT',
       repository: 'git+https://github.com/jonschlinkert/ansi-wrap',
       licenseFile: '...' },
    ...

How can I examine that json variable passed into the checker function to compare the licenses property against a license whitelist array?

Brandon Linton
  • 4,373
  • 5
  • 42
  • 63

1 Answers1

1

The escape sequences at the start of your object \u001b[34m look suspiciously like ANSI escape sequences used to tell a terminal to render things in color. See this for example: How to print color in console using System.out.println?. Your code correctly dumped the license JSON when I tried it thus:

var checker = require('license-checker');

checker.init({ start: '.' }, function(json, err) {
    if (err) {
        //Handle error
    } else {
        console.log (JSON.stringify (json))
    }
});

So how would you work with the resulting JSON? Object.keys on any object extracts the (necessarily) distinct keys in the object into an array. Then a simple filter expression on the array will allow you to capture whatever is of interest to you. So for example, if you want to retain all the MIT licensed packages, you could do:

var keys = Object.keys (json)
var okPackages = keys.filter (function (e) {
        return json.hasOwnProperty (e) && (json[e].licenses === "MIT")
    });
Community
  • 1
  • 1
Ram Rajamony
  • 1,717
  • 15
  • 17
  • 1
    Thanks Ram - I was using gutil to capture the output, which to your point was emitting the color sequences. I'm still unsure how to iterate that object appropriately though now that I have it. I've updated my JSON snippet accordingly. – Brandon Linton Sep 17 '15 at 17:51
  • I hope the snippet pointing out how to filter helped; let me know if you have more questions. – Ram Rajamony Sep 17 '15 at 18:47