UNPKG

3.31 kBPlain TextView Raw
1import fs from 'fs';
2import path from 'path';
3import importFresh from 'import-fresh';
4import stripComments from 'strip-json-comments';
5import yaml from 'js-yaml';
6
7/*
8 * @description 保证文件一定存在,文件不存在则创建文件
9 * @param filePath 文件路径
10 * */
11export function fileExit(filePath: string) {
12 try {
13 fs.readFileSync(filePath, 'utf-8');
14 } catch (_) {
15 fs.appendFileSync(filePath, '', 'utf-8');
16 }
17}
18// 文件中读取json对象的某个value
19export const getKeyFormFile = (file: string, key: string) => {
20 try {
21 const jsonString = fs.readFileSync(file, 'utf-8');
22 if (jsonString) {
23 const jsonData = JSON.parse(jsonString);
24 return jsonData[key];
25 } else {
26 return '';
27 }
28 } catch (e) {
29 console.log('getKeyFormCache error =>', e);
30 }
31};
32// 文件中写入一个json字符串
33export const setKeyToFile = (file: string, key: string, value: any): any => {
34 try {
35 const jsonString = fs.readFileSync(file, 'utf-8');
36 let jsonData;
37 if (jsonString) {
38 jsonData = JSON.parse(jsonString);
39 jsonData[key] = value;
40 } else {
41 jsonData = {
42 [key]: value
43 };
44 }
45 fs.writeFileSync(file, JSON.stringify(jsonData, null, 4), 'utf-8');
46 } catch (e) {
47 console.log('setKeyToCache error =>', e);
48 }
49};
50
51export class Config {
52 static loadConfigFile(filePath: string) {
53 switch (path.extname(filePath)) {
54 case '.js':
55 return Config.loadJSConfigFile(filePath);
56
57 case '.json':
58 if (path.basename(filePath) === 'package.json') {
59 return Config.loadPackageJSONConfigFile(filePath);
60 }
61 return Config.loadJSONConfigFile(filePath);
62
63 case '.yaml':
64 case '.yml':
65 return Config.loadYAMLConfigFile(filePath);
66
67 default:
68 return Config.loadLegacyConfigFile(filePath);
69 }
70 }
71
72 static loadJSConfigFile(filePath: string) {
73 try {
74 return importFresh(filePath);
75 } catch (e) {
76 e.message = `Cannot read config file: ${filePath}\nError: ${e.message}`;
77 throw e;
78 }
79 }
80
81 static loadPackageJSONConfigFile(filePath: string) {
82 try {
83 return Config.loadJSONConfigFile(filePath);;
84 } catch (e) {
85 e.message = `Cannot read config file: ${filePath}\nError: ${e.message}`;
86 throw e;
87 }
88 }
89
90 static loadJSONConfigFile(filePath: string) {
91 try {
92 return JSON.parse(stripComments(Config.readFile(filePath)));
93 } catch (e) {
94 e.message = `Cannot read config file: ${filePath}\nError: ${e.message}`;
95 e.messageTemplate = 'failed-to-read-json';
96 e.messageData = {
97 path: filePath,
98 message: e.message
99 };
100 throw e;
101 }
102 }
103
104 static readFile(filePath: string) {
105 return fs.readFileSync(filePath, 'utf8').replace(/^\ufeff/u, '');
106 }
107
108 static loadYAMLConfigFile(filePath: string) {
109 try {
110 return yaml.safeLoad(Config.readFile(filePath)) || {};
111 } catch (e) {
112 e.message = `Cannot read config file: ${filePath}\nError: ${e.message}`;
113 throw e;
114 }
115 }
116
117 static loadLegacyConfigFile(filePath: string) {
118 try {
119 return yaml.safeLoad(stripComments(this.readFile(filePath))) || {};
120 } catch (e) {
121 e.message = `Cannot read config file: ${filePath}\nError: ${e.message}`;
122 throw e;
123 }
124 }
125}