Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A better way for handle json in templates #10

Open
egoist opened this issue Dec 30, 2017 · 3 comments
Open

A better way for handle json in templates #10

egoist opened this issue Dec 30, 2017 · 3 comments

Comments

@egoist
Copy link
Member

egoist commented Dec 30, 2017

A json file will become a mess when there're too many conditions like:

"devDependencies": {<% if (unitTest) { %>
    "jest-cli": "^19.0.0"<% } %><% if (eslint === 'xo') { %><% if (unitTest) { %>,<% } %>
    "eslint-config-rem": "^3.0.0",
    "xo": "^0.18.0"<% } else if (eslint === 'standard') { %>,
    "standard": "^10.0.0"<% } %><% if (compile) { %><% if (eslint !== 'disable' || unitTest) { %>,<% } %>
    <%_ if (unitTest) { -%>
    "babel-preset-env": "^1.4.0",
    <%_ } -%>
    <%_ if (poi) { -%>
    "poi": "^9.1.4",
    <%_ } -%>
    "bili": "^1.3.1"<% } %>
  }<% if (eslint === 'xo') { %>,

Maybe we can use a special extension .json.js to generate json file, e.g. template/package.json.js that will be treat as template/package.json:

module.exports = ({ name, coverage }) => {
  const json = {
    name,
    version: '0.0.0',
    scripts: {}
  }

  if (coverage) {
    json.scripts.coverage = 'jest --coverage'
  }

  return json
}
@egoist
Copy link
Member Author

egoist commented Jan 6, 2018

I guess it could also support other markup languages like yaml toml etc...

{
  transformers: {
  	'**/*.json.js'(file, stream) {
  		stream.writeContents(file.path.replace(/\.js$/, ''), transform(file.content))
  		delete stream.files[file.path]
  	}
  } 
}

@kristianmandrup
Copy link

kristianmandrup commented Feb 28, 2018

Instead of using templates with json try using json/object transformer libs. Check npmjs ;)

Here one example: object-transform-stream

Many others out there for sure, but perhaps could be done via a general purpose merge template:

{
  type: 'json',
  base {
    // baseline object
  }, 
  parts: {
    author({author}) => author,
    repo({username, name}) {
       return {
         url: `...github/${username}${name}/...`
       }
    },
   // ...
  }
}

Then merge the result of each generated parts entry onto the base object then convert the end result to type. Not sure if this is much better or cleaner, but at least a novel approach.

@kristianmandrup
Copy link

Just implemented this idea using vm2 and it rocks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants