-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
65 lines (53 loc) · 1.68 KB
/
utils.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
const fs = require('fs');
const STYLE = require('./format.js');
const FEATHER_PACKAGE_NAME = 'arui-feather';
const VAR_RE = /(?:^|\n)\s+(--[-\w]+):\s*(.+?);/g;
const STYLE_RE = /^(\s+)([\w-]+):(.+)$/;
const BORDERS = {
' ': null,
';': null,
'undefined': null
};
function save(object, key, value) {
switch (typeof object[key]) {
case 'undefined':
object[key] = [value];
break;
default:
object[key].push(value);
break;
}
}
function getVarsFromCSS(...args) {
let regMatch;
const result = {};
for (let index = 0; index < args.length; ++index) {
try {
const gaps = fs.readFileSync(require.resolve(`${FEATHER_PACKAGE_NAME}/${args[index]}`)).toString();
regMatch = VAR_RE.exec(gaps);
while (regMatch) {
save(result, regMatch[2], regMatch[1]);
regMatch = VAR_RE.exec(gaps);
}
} catch (error) {
if (error.code === 'ENOENT' || error.code === 'MODULE_NOT_FOUND') {
process.stderr.write(
`${STYLE.GREEN}WARNING: ${STYLE.CLEAR}Module not found: ${STYLE.RED}${FEATHER_PACKAGE_NAME}/${args[index]}${STYLE.CLEAR}\n`
);
} else {
console.log(error);
}
}
}
return result;
}
function findInValue(haystack, needle, fromIndex) {
const index = haystack.indexOf(needle, fromIndex);
return (
index > -1
&& haystack[index - 1] in BORDERS
&& haystack[index + needle.length] in BORDERS
) && index;
}
module.exports.getVarsFromCSS = getVarsFromCSS;
module.exports.findInValue = findInValue;