-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
84 lines (68 loc) · 2.06 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
74
75
76
77
78
79
80
81
82
83
84
"use strict"
const Stream = require("readable-stream")
const StreamQueue = require("streamqueue")
const parse = require("parse-sass-value")
const pkg = require("./package.json")
const log = require("fancy-log")
const ansiGreen = require("ansi-colors").green
const supportsColor = require("color-support")
const colorDetectionOptions = {
// If on Windows, ignore the isTTY check
// This is due to AppVeyor (and thus probably common Windows platforms?) failing the check
ignoreTTY: (process.platform === "win32")
}
const defaultOptions = {
verbose: true,
quotes: "single",
separator: "comma"
}
function green(msg) {
if (supportsColor(colorDetectionOptions)) {
return ansiGreen(msg)
}
return msg
}
function getStreamFromBuffer(string) {
const stream = new Stream.Readable()
stream._read = function() {
stream.push(Buffer.from(string))
stream._read = stream.push.bind(stream, null)
}
return stream
}
module.exports = (variables, options) => {
options = Object.assign({}, defaultOptions, options)
const statements = Object
.keys(variables)
.map(name => {
let value = "null"
try {
value = parse(variables[name])
} catch (error) {
if (options.verbose) {
log(`${pkg.name}: skipping var ${green(name)}.\n`
+ `${pkg.name}: ${error.message}`)
}
}
return `$${name}: ${value};`
})
if (options.verbose) {
log(`${pkg.name}: Injected ${green(statements.length)} variables to sass:\n`
+ `\t${statements.join("\n\t")}`)
}
const stream = new Stream.Transform({ objectMode: true })
stream._transform = function(file, enc, cb) {
if (file.isNull()) {
return cb(null, file)
}
const prependedBuffer = Buffer.from(statements.join("\n"))
if (file.isStream()) {
file.contents = new StreamQueue(getStreamFromBuffer(prependedBuffer), file.contents)
return cb(null, file)
}
file.contents = Buffer.concat([prependedBuffer, file.contents],
prependedBuffer.length + file.contents.length)
cb(null, file)
}
return stream
}