-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathgetConfig.ts
More file actions
48 lines (37 loc) · 1.21 KB
/
getConfig.ts
File metadata and controls
48 lines (37 loc) · 1.21 KB
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
import path from 'path';
import fs from 'fs';
import colors from 'colors';
import defaultConfig from './iconfont.json';
export interface Config {
symbol_url: string;
use_typescript: boolean;
save_dir: string;
trim_icon_prefix: string;
unit: string;
default_icon_size: number;
default_style: Record<string, string>;
}
let cacheConfig: Config;
export const getConfig = () => {
if (cacheConfig) {
return cacheConfig;
}
const targetFile = path.resolve('iconfont.json');
if (!fs.existsSync(targetFile)) {
console.warn(colors.red('File "iconfont.json" doesn\'t exist, did you forget to generate it?'));
process.exit(1);
}
const config = require(targetFile) as Config;
if (!config.symbol_url || !/^(https?:)?\/\//.test(config.symbol_url)) {
console.warn(colors.red('You are required to provide symbol_url'));
process.exit(1);
}
if (config.symbol_url.indexOf('//') === 0) {
config.symbol_url = 'http:' + config.symbol_url;
}
config.save_dir = config.save_dir || defaultConfig.save_dir;
config.default_icon_size = config.default_icon_size || defaultConfig.default_icon_size;
config.unit = config.unit || defaultConfig.unit;
cacheConfig = config;
return config;
};