Search code examples
yeomanboweryeoman-generator

Conditional Dependencies in bower.json


I'm writing my own Yeoman generator, and I'm working on the dependencies using Bower at the moment. I want to have a prompt, asking the user which dependencies he/she requires for a project. The list looks something like this:

[?] What more would you like? (Press <space> to select)
‣⬡ jQuery
 ⬡ jQuery Mobile
 ⬡ Zepto
 ...

I'm having some trouble, however, using the user's import to modify my bower.json. The bower.json is as follows:

{
  "name": "<%= _.slugify(blogName) %>",
  "version": "0.0.0",
  "private": true,
  "dependencies": { 
    <% if (includeJQuery) { %>
      "jquery": "jquery/jquery",
    <% } if(includeZepto) { %>
      "zepto": "http://zeptojs.com/zepto.js",
    <% } %>
  }
}

where includeJQuery and includeZepto are set to true if the user selects the corresponding box in the prompt. When I run the yeoman generator without selecting any boxes, it runs fine. However, if I select that I want Zepto, an error occurs:

bower EMALFORMED    Failed to read bower.json

Additional error details:
Unexpected token }

I can't see where the problem in my configuration is, this only happens when I select at least one package. Furthermore, if I have only 1 dependency (so, let's say jQuery) I can toggle this fine; the problems only occur when I want to toggle multiple dependencies.


Solution

  • The problem is caused by trailing commas. The final dependency included cannot have a trailing comma. This is actually quite problematic for the generator set up I had in mind, but at least this problem has been solved.