Skip to content

Instantly share code, notes, and snippets.

@kelly
Last active January 1, 2016 08:29
Show Gist options
  • Save kelly/8118725 to your computer and use it in GitHub Desktop.
Save kelly/8118725 to your computer and use it in GitHub Desktop.
Store your colors, or other variables in json. This will convert to sass variables. usage: "coffee sassyJSON.coffee --input file.json"
_ = require 'underscore'
beautify = require('js-beautify').css
stdio = require 'stdio'
fs = require 'fs'
opts = stdio.getopt
'input':
description: 'input',
mandatory: true,
args: 1,
'output':
description: 'output',
args: 1
parseVars = (obj) ->
str = ''
_.each obj, (val, key, list) ->
if typeof val == 'string'
str += sassify key, val
else
str += sassifyMixin(key, parseVars(val))
str
sassify = (key, val) ->
"$#{key}: #{val};"
sassifyMixin = (key, str) ->
"@mixin #{key} { #{str} }"
sassyJSON = (opts) ->
file = fs.readFileSync opts.input
json = JSON.parse(file)
sass = parseVars json
res = beautify(sass, indent_size: 2)
if opts.output
fs.writeFile opts.output, res, (err) ->
if err then console.log err
else
console.log res
sassyJSON(opts)
module.exports = sassyJSON
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment