-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
67 lines (55 loc) · 1.43 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
const fs = require('fs');
const path = require('path');
const exists = (file) => {
try {
fs.accessSync(file, fs.constants.F_OK);
return true;
} catch (e) {
return false;
}
}
const buildSassValue = (value) => {
if (Array.isArray(value)) {
return `(${value.reduce((prev, cur) => prev + `"${cur}",`, '')})`;
}
if (typeof value === "object") {
return `(${buildSassMap(value)})`;
}
return `"${value}"`;
}
const buildSassMap = (json) => {
return Object.keys(json).reduce((prev, cur) => {
return prev + `"${cur}": ${buildSassValue(json[cur])},`;
}, '');
}
const end = (done) => (value) => {
return done ? done(value) : value;
}
module.exports = function(url, prev, done) {
done = end(done);
if (!url) return done(null);
const parts = url.split('/');
const name = path.basename(parts.pop(), '.json');
const cwd = process.cwd();
const includePaths = this.options.includePaths.split(':')
.map(p => path.resolve(cwd, p));
try {
let resolved = path.join(cwd, url);
if (exists(resolved)) {
var json = require(resolved);
} else {
for (let i = 0; i < includePaths.length; i++ ) {
resolved = path.join(includePaths[i], url);
if (exists(resolved)) {
var json = require(resolved);
}
}
}
} catch(err) {
return done(err);
}
if (json) {
return done({ contents: `$${name}: (${buildSassMap(json)});` });
}
return done(null);
};