bentonam / fakeit

Generates JSON documents based on models defined in YAML and adds them to a Couchbase Bucket
MIT License
86 stars 21 forks source link

Javascript is painful to work with #84

Closed mistersender closed 7 years ago

mistersender commented 7 years ago

I am making a lot of use of the custom javascript in just about every area it is possible to use it. It's been very challenging to work with for a few reasons:

tjbenton commented 7 years ago

@mistersender

no apparent way to console.log

All functions you can find in the node global should be available by default, so you should be able to use console.log like normal.

no good/helpful error handling

You're correct. I'm currently working on adding better error handling, but it's going to be difficult to show you where exactly the error is because we're generating the function from a string so we have to store information about the location of the function. It will show you the true error now instead of just the error message which should be more helpful.

comment support is intermittent at best...

When you have a multi line function don't use > to declare multiple lines because > tells the yaml parser that the following is just a really long string without line breaks.

For example if you had something like this.

build: >
  var exists = false;
  // find the matching document
  for (var i = 0; i < documents.Countries.length; i++) {
    if (documents.Countries[i].country_code === inputs.airlines[document_index].iso_country) {
      exists = true;
      break;
    }
  }
  return exists ? inputs.airlines[document_index].iso_country : null;

It will parse build into string like this, which isn't what you want.

var exists = false; // find the matching document for (var i = 0; i < documents.Countries.length; i++) {  if (documents.Countries[i].country_code === inputs.airlines[document_index].iso_country) {
    exists = true;
    break;
  }
} return exists ? inputs.airlines[document_index].iso_country : null;

Using | instead of > to declare multi line string will result in the following. Which is what you're looking for.

var exists = false;
// find the matching document
for (var i = 0; i < documents.Countries.length; i++) {
  if (documents.Countries[i].country_code === inputs.airlines[document_index].iso_country) {
    exists = true;
    break;
  }
}
return exists ? inputs.airlines[document_index].iso_country : null;

syntax is picky. sometimes it's upset about semicolons existing, sometimes not.

The paragraph above should explain why your js wasn't running correctly when you added in comments, or missed semi colons.

Sometimes it seems ok with es6, sometimes not, but either way, it fails awkwardly, or doesn't fail at all

After looking through the current code, this app does not support babel transpiling. At the point where we take the string you pass in (aka pre_build, post_build, etc.) and convert it into a real JS function we don't modify the code at all. So if the version of node you're running supports that part of es6 then it will work. If it doesn't support it then it will error out. I'm currently working on a branch for the 1.0.0 release that will enable the support for transpiling if a babel config exists in the .babelrc or package.json.