-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse-css-classes.js
105 lines (79 loc) · 3.22 KB
/
parse-css-classes.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import fs from 'fs';
import path from 'path';
import postcss from 'postcss';
import getCustomPropertiesFromRoot from './get-custom-properties-from-root';
/* Get Custom Properties from CSS File
/* ========================================================================== */
async function getCustomPropertiesFromCSSFile(from) {
const css = await readFile(from);
const root = postcss.parse(css, { from });
return await getCustomPropertiesFromRoot(root);
}
/* Get Custom Properties from Object
/* ========================================================================== */
function getCustomPropertiesFromObject(object) {
const customProperties = Object.assign(
{},
Object(object).customProperties,
Object(object)['custom-properties']
);
return customProperties;
}
/* Get Custom Properties from JSON file
/* ========================================================================== */
async function getCustomPropertiesFromJSONFile(from) {
const object = await readJSON(from);
return getCustomPropertiesFromObject(object);
}
/* Get Custom Properties from JS file
/* ========================================================================== */
async function getCustomPropertiesFromJSFile(from) {
const object = await import(from);
return getCustomPropertiesFromObject(object);
}
/* Get Custom Properties from Sources
/* ========================================================================== */
export default function getCustomPropertiesFromSources(sources) {
return sources.map(source => {
if (source instanceof Promise) {
return source;
} else if (source instanceof Function) {
return source();
}
// read the source as an object
const opts = source === Object(source) ? source : { from: String(source) };
// skip objects with Custom Properties
if (opts.customProperties || opts['custom-properties']) {
return opts
}
// source pathname
const from = path.resolve(String(opts.from || ''));
// type of file being read from
const type = (opts.type || path.extname(from).slice(1)).toLowerCase();
return { type, from };
}).reduce(async (customProperties, source) => {
const { type, from } = await source;
if (type === 'css') {
return Object.assign(await customProperties, await getCustomPropertiesFromCSSFile(from));
}
if (type === 'js') {
return Object.assign(await customProperties, await getCustomPropertiesFromJSFile(from));
}
if (type === 'json') {
return Object.assign(await customProperties, await getCustomPropertiesFromJSONFile(from));
}
return Object.assign(await customProperties, await getCustomPropertiesFromObject(await source));
}, {});
}
/* Promise-ified utilities
/* ========================================================================== */
const readFile = from => new Promise((resolve, reject) => {
fs.readFile(from, 'utf8', (error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
const readJSON = async from => JSON.parse(await readFile(from));