forked from jenslind/drupal-breakpoints-scss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (56 loc) · 1.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict'
var YAML = require('yamljs')
var fs = require('fs')
var stream = require('stream')
var through = require('through2')
var drupalBreakpointsScss = {}
var defaultOpts = {
vars: true,
map: false,
mapName: 'drupal-breakpoints',
varsPrefix: ''
}
function jsonToScssVars (obj, opts) {
var scssVars = ''
for (var i in obj) {
scssVars += '$' + opts.varsPrefix + obj[i].label + ': \'' + obj[i].mediaQuery + '\';\n'
}
return scssVars
}
function jsonToScssMap (obj, opts) {
var scssMap = '$' + opts.mapName + ': (\n'
for (var i in obj) {
scssMap += ' ' + obj[i].label + ': \'' + obj[i].mediaQuery + '\',\n'
}
scssMap += ');'
return scssMap
}
function generateScss (breakpoints, opts) {
var opts = Object.assign(defaultOpts, opts)
var output = ''
if (opts.vars) output += jsonToScssVars(breakpoints, opts) + '\n'
if (opts.map) output += jsonToScssMap(breakpoints, opts) + '\n'
return output
}
drupalBreakpointsScss.read = function (path, opts = {}) {
var rs = stream.Readable()
var breakpoints = YAML.load(path)
rs._read = function () {
rs.push(generateScss(breakpoints, opts))
rs.push(null)
}
return rs
}
drupalBreakpointsScss.write = function (path) {
var scssFile = fs.createWriteStream(path)
return scssFile
}
drupalBreakpointsScss.ymlToScss = function (opts = {}) {
return through.obj(function (file, enc, cb) {
var content = file.contents.toString('utf8')
var breakpoints = YAML.parse(content)
file.contents = new Buffer(String(generateScss(breakpoints, opts)))
cb(null, file)
})
}
module.exports = drupalBreakpointsScss