|
3 | 3 |
|
4 | 4 | import fs from 'fs-extra'
|
5 | 5 | import path from 'path'
|
6 |
| -import yaml from 'js-yaml' |
7 | 6 | import axios from 'axios'
|
8 | 7 | import NBT from 'mcnbt'
|
9 | 8 |
|
| 9 | +import loadConfig from './config' |
10 | 10 | import {defaultSkin, delay, download, mergeStats, writeJSON} from './helper'
|
11 | 11 | import * as logger from './logger'
|
12 | 12 |
|
13 |
| -interface Config extends NSConfig { |
14 |
| - __filename: string |
15 |
| - __dirname: string |
16 |
| - |
17 |
| - resolve (filepath: string): string |
18 |
| - |
19 |
| - get<T> (keyPath: string | string[]): T |
20 |
| -} |
21 |
| - |
22 |
| -let config: Config |
23 |
| - |
24 |
| -export function loadConfig (configPath = './config.yml'): Config { |
25 |
| - const filename = path.resolve(configPath) |
26 |
| - |
27 |
| - if (config?.__filename === filename) { |
28 |
| - return config |
29 |
| - } |
30 |
| - |
31 |
| - let _config: NSConfig |
32 |
| - try { |
33 |
| - // TODO: `.yaml` support |
34 |
| - _config = yaml.safeLoad(fs.readFileSync(configPath, 'utf-8')) as NSConfig |
35 |
| - } catch (err) /* istanbul ignore next */ { |
36 |
| - logger.Config.error('Error occurred while reading config') |
37 |
| - logger.Config.error(err) |
38 |
| - process.exit(1) |
39 |
| - } |
40 |
| - |
41 |
| - const dirname = path.parse(filename).dir |
42 |
| - config = Object.assign(_config, { |
43 |
| - __filename: filename, |
44 |
| - __dirname: dirname, |
45 |
| - resolve (filepath: string): string { |
46 |
| - return path.resolve(dirname, filepath) |
47 |
| - }, |
48 |
| - get<T = unknown> (keyPath: string | string[]): T { |
49 |
| - if (typeof keyPath === 'string') { |
50 |
| - keyPath = keyPath.split('.') |
51 |
| - } |
52 |
| - // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
53 |
| - // @ts-ignore |
54 |
| - // FIXME: Better typing |
55 |
| - return keyPath.reduce((value, key) => value[key], config) |
56 |
| - }, |
57 |
| - }) |
58 |
| - return config |
59 |
| -} |
| 13 | +const config = loadConfig() |
60 | 14 |
|
61 | 15 | export default class Utils {
|
62 | 16 | apiLimited: boolean
|
|
0 commit comments