Skip to content

Instantly share code, notes, and snippets.

@kelly
Last active January 1, 2016 13:29
Show Gist options
  • Save kelly/8151346 to your computer and use it in GitHub Desktop.
Save kelly/8151346 to your computer and use it in GitHub Desktop.
sanitized json to plist. Converts all keys to camelCase and allows for prefixed keys.
_ = require 'underscore'
fs = require 'fs'
plist = require 'plist'
ccase = require 'change-case'
stdio = require 'stdio'
opts = stdio.getopt
'input':
description: 'input',
mandatory: true,
args: 1
,
'output':
description: 'output',
args: 1
,
'prefix':
description: 'prefix all keys',
args: 1
sanitize = (obj, prefix) ->
newObj = {}
_.each obj, (val, key, list) ->
key = ccase.camelCase key
if prefix
key = prefix + key.charAt(0).toUpperCase() + key.slice(1);
newObj[key] = if (typeof val == 'object') && !(val instanceof Array) then sanitize(val, prefix) else val
return newObj
jsonToPlist = (opts) ->
file = fs.readFileSync opts.input
json = JSON.parse file
jsonS = sanitize json, opts.prefix
plist = plist.build(jsonS).toString()
if opts.output
fs.writeFile opts.output, plist, (err) ->
if err then console.log err
else
console.log plist
jsonToPlist(opts)
module.exports = jsonToPlist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment