Kikobeats / finepack

Organizes and maintains readable your JSON files.
MIT License
19 stars 7 forks source link

Data gets lost when using JSON.stringify() to display messages #6

Closed pdehaan closed 9 years ago

pdehaan commented 9 years ago

I'm seeing an issue where if I use the finepack module API and try and do console.log(JSON.stringify(messages) then I get an empty array back instead of a formatted object with error, warning, success, and info keys.

Steps to reproduce:

Here's my index.js file:

var fs = require('fs');
var path     = require('path');
var finepack = require('finepack');

var filename = path.basename(filepath);
var filepath = path.resolve('./package.json');
var filedata = fs.readFileSync(filepath).toString();

var options = {
  filename: filename,
  lint: true
};

finepack(filedata, options, function(err, output, messages){
  if (err) {
    console.error('err', err);
  }
  console.log('\nraw:');
  console.log(messages);
  console.log('\nJSON.stringify():');
  console.log(JSON.stringify(messages, null, 2));
});

And I run it via the CLI (and have to specify --no-color otherwise the output gets chalk data in the messages):

$ node index --no-color
err true

raw:
[ error: [],
  warning: [ 'warning: missing \'bugs\'',
    'warning: missing \'keywords\'',
    'warning: missing \'homepage\'',
    'warning: missing \'repository\'' ],
  success: [],
  info: [ 'info: undefined is near to be fine. Check the file and run again.' ] ]

JSON.stringify():
[]

As you can see, when I use JSON.stringify() on the messages, I get back an empty array. As close as I can tell, this is a slight issue in ./lib/Logger.coffee:10 where the constructor method sets @messages to an array (@messages = []), where we probably want that to be an object ({}). Changing that locally in my ./node_modules/ directory seems to have solved the issue for me.

pdehaan commented 9 years ago

Unrelated, but not sure if specifying err as a Boolean instead of an Error instance is a bit non-standard and may be confusing. But I'm not sure if returning a custom err with nested error: [...] and warning: [...] is more obvious.