-
Notifications
You must be signed in to change notification settings - Fork 55
/
index.js
56 lines (48 loc) · 1.24 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
import _ from 'lodash';
import {resolve} from 'path';
import isThere from 'is-there';
export default function(url, prev) {
if (/\.json$/.test(url)) {
let includePaths = this.options.includePaths ? this.options.includePaths.split(':') : [];
let paths = []
.concat(prev.slice(0, prev.lastIndexOf('/')))
.concat(includePaths);
let files = paths
.map(path => resolve(path, url))
.filter(isThere);
if (files.length === 0) {
return new Error(`Unable to find "${url}" from the following path(s): ${paths.join(', ')}. Check includePaths.`);
}
return {
contents: parseJSON(require(files[0]))
};
} else {
return {
file: url
};
}
}
function parseJSON(json) {
return Object.keys(json)
.map(key => `$${key}: ${parseValue(json[key])};`)
.join('\n');
}
function parseValue(value) {
if (_.isArray(value)) {
return parseList(value);
} else if (_.isPlainObject(value)) {
return parseMap(value);
} else {
return value;
}
}
function parseList(list) {
return `(${list
.map(value => parseValue(value))
.join(',')})`;
}
function parseMap(map) {
return `(${Object.keys(map)
.map(key => `${key}: ${parseValue(map[key])}`)
.join(',')})`;
}