-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathimport-from.mjs
117 lines (98 loc) · 3.19 KB
/
import-from.mjs
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
106
107
108
109
110
111
112
113
114
115
116
117
import fs from 'fs';
import path from 'path';
import * as postcss from 'postcss';
import { parse } from 'postcss-values-parser';
async function getCustomPropertiesFromCSSFile(from) {
const css = await readFile(from); // eslint-disable-next-line no-unused-vars
postcss.parse(css, {
from,
});
throw new Error('Importing from CSS files not supported yet'); //return getCustomPropertiesFromRoot(root, { preserve: true });
}
/* Get Custom Properties from Object
/* ========================================================================== */
function getCustomPropertiesFromObject(object) {
const customProperties = Object.assign(
{},
Object(object).customProperties,
Object(object)['custom-properties'],
);
for (const key in customProperties) {
customProperties[key] = parse(String(customProperties[key])).nodes;
}
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);
}
export default function getCustomPropertiesFromImports(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),
);
}, {});
}
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));
//# sourceMappingURL=import-from.js.map