forked from alibaba-fusion/next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorder-var.js
66 lines (58 loc) · 1.8 KB
/
order-var.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
const fs = require('fs');
const path = require('path');
const { logger, checkComponentName } = require('./utils');
const cwd = process.cwd();
const componentName = checkComponentName();
const varFilePath = path.join(
cwd,
'src',
componentName,
'scss',
'variable.scss'
);
if (!fs.existsSync(varFilePath)) {
throw new Error(`Can not find the variable.scss: ${varFilePath}`);
}
const varContent = fs.readFileSync(varFilePath, 'utf8');
const nsReg = /\/\/\/\s+@namespace\s+(.+)/g;
const nsMap = {};
let nsMatched;
// eslint-disable-next-line
while ((nsMatched = nsReg.exec(varContent))) {
nsMap[nsMatched[1]] = true;
}
const nsNewMap = {};
Object.keys(nsMap).forEach((ns) => {
const parts = ns.split('/').filter((_) => _);
parts.reduce((ret, part) => {
ret[part] = ret[part] || {};
return ret[part];
}, nsNewMap);
});
const order = {};
const loop = (nsMap, prefix, preValue) => {
Object.keys(nsMap).forEach((nsPart, index) => {
const ns = prefix ? `${prefix}/${nsPart}` : nsPart;
const value = prefix ? preValue * 10 + index : 1;
order[ns] = prefix ? value : index + 1;
loop(nsMap[nsPart], ns, value);
});
};
loop(nsNewMap, '', 1);
delete order.size;
delete order.statement;
let newVarContent;
const orderReg = /(\/\/\/\s+@order\s+)(\{.*\})/;
if (orderReg.test(varContent)) {
newVarContent = varContent.replace(orderReg, function(...args) {
return args[1] + JSON.stringify(order);
});
} else {
const metaReg = /(\/\/\/\/(\s|.)+?)(\/\/\/\/)/;
newVarContent = varContent.replace(metaReg, function(...args) {
return `${args[1]}/// @order ${JSON.stringify(order)}\n${args[3]}`;
});
}
fs.writeFileSync(varFilePath, newVarContent);
logger.success('Add order to variable file successfully: ');
logger.info(order);