-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·161 lines (130 loc) · 4.03 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const program = require('commander');
const FileFormat = ['.eot', '.woff2', '.woff', '.ttf', '.otf'];
const FormatTypes = {
['eot']: 'embedded-opentype',
['woff2']: 'woff2',
['woff']: 'woff',
['ttf']: 'truetype',
['otf']: 'opentype',
};
function readFontFile(dirName) {
let fontFiles = {};
const dirs = fs.readdirSync(dirName);
for (let dir of dirs) {
if (fs.lstatSync(`${dirName}/${dir}`).isFile()) {
const fileFormat = path.extname(dir);
if (FileFormat.includes(fileFormat)) {
const [fontName] = dir.split(fileFormat);
fontFiles[fontName] = fontFiles[fontName]
? [...fontFiles[fontName], `/${dirName}/${dir}`]
: [`/${dirName}/${dir}`];
}
} else {
fontFiles = {
...fontFiles,
...readFontFile(`${dirName}/${dir}`),
};
}
}
return fontFiles;
}
function orderFontFiles(fontFiles) {
return Object.entries(fontFiles).reduce((acc, [fontName, fontFiles]) => {
return {
...acc,
[fontName]: fontFiles.sort((a, b) => {
const [aFormat] = a.split('.').slice(-1);
const [bFormat] = b.split('.').slice(-1);
return (
FileFormat.indexOf(`.${aFormat}`) - FileFormat.indexOf(`.${bFormat}`)
);
}),
};
}, {});
}
function getFontWeightCSS(fontStyles, configWeight = {}) {
let fontWeight;
const weightKeys = Object.keys(configWeight);
fontStyles.some((fontStyle) => {
if (weightKeys.includes(fontStyle)) {
fontWeight = fontStyle;
return true;
}
return false;
});
if (!fontWeight) {
return '\n font-weight: ' + `400;`;
}
return '\n font-weight: ' + `${configWeight[fontWeight]};`;
}
function getFontStyleCSS(configStyle = {}) {
const configStyleArr = Object.entries(configStyle);
if (configStyleArr.length === 0) {
return '';
}
return configStyleArr.reduce((acc, [key, value]) => {
return `${acc}` + `\n ${key}: ${value};`;
}, '');
}
function makeFontFamilyCSS(fontName, fontFiles, prefix, config) {
const [pureFontName, ...rest] = fontName.split('-');
let fontFamilyStyleString =
'@font-face {\n font-family: ' + `'${pureFontName}'` + ';\n src:';
for (let fontFile of fontFiles) {
const [format] = fontFile.split('.').slice(-1);
let url = fontFile;
if (prefix) {
if (prefix.endsWith('/')) {
url = `${prefix}${fontName}.${format}`
} else {
url = `${prefix}/${fontName}.${format}`;
}
}
fontFamilyStyleString = `${fontFamilyStyleString} url('${url}') format('${FormatTypes[format]}'),`;
}
fontFamilyStyleString = `${fontFamilyStyleString.slice(0, -1)}`;
return (
`${fontFamilyStyleString};` +
getFontWeightCSS(rest, config?.weight) +
getFontStyleCSS(config?.style) +
'\n}'
);
}
function main(options) {
if (options.help) {
return;
}
const configDir = options.config || './config.json';
let config;
try {
config = JSON.parse(fs.readFileSync(configDir));
} catch {
// If can not find config file, pass
}
const fontFamilyCSS = [];
const parsingDir = options.dir || config?.dir || 'fonts';
const outputDir = options.output || config?.output || parsingDir;
const prefix = options.prefix || config?.prefix;
const fontFiles = readFontFile(parsingDir);
const orderedFontFiles = orderFontFiles(fontFiles);
Object.entries(orderedFontFiles).forEach(([fontName, fontFiles]) => {
fontFamilyCSS.push(makeFontFamilyCSS(fontName, fontFiles, prefix, config));
});
const fontFamilyCSSString = fontFamilyCSS.join('\n');
fs.mkdirSync(outputDir, { recursive: true });
fs.writeFileSync(`${outputDir}/fonts.css`, fontFamilyCSSString);
}
program
.option('-c, --config <dir>', 'Config dir')
.option('-p, --prefix <prefix>', 'Prefix for font files')
.option('-d, --dir <dir>', 'Parsing dir')
.option('-o, --output <dir>', 'Output dir')
.action(() => {
const options = program.opts();
main(options);
});
// eslint-disable-next-line no-undef
program.parse(process.argv);