Skip to content

Commit 1499880

Browse files
committed
refactor(grabber): extract config function as a module
1 parent 4a50a34 commit 1499880

File tree

4 files changed

+64
-50
lines changed

4 files changed

+64
-50
lines changed

src/config.ts

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

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import fs from 'fs-extra'
22
import path from 'path'
33

4-
import Utils, {loadConfig} from './utils'
4+
import loadConfig from './config'
5+
import Utils from './utils'
56
import {confirm, writeJSON} from './helper'
67
import * as logger from './logger'
78
import ProgressBar from './progressbar'

src/utils.ts

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,14 @@
33

44
import fs from 'fs-extra'
55
import path from 'path'
6-
import yaml from 'js-yaml'
76
import axios from 'axios'
87
import NBT from 'mcnbt'
98

9+
import loadConfig from './config'
1010
import {defaultSkin, delay, download, mergeStats, writeJSON} from './helper'
1111
import * as logger from './logger'
1212

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()
6014

6115
export default class Utils {
6216
apiLimited: boolean

tests/config.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'path'
22

3-
import {loadConfig} from '../src/utils'
3+
import loadConfig from '../src/config'
44

55
describe('config', () => {
66
const configPath = path.resolve(__dirname, './mocks/config.yml')
@@ -21,4 +21,8 @@ describe('config', () => {
2121
expect(config.resolve('../file')).toBe(path.resolve(configPath, '../../file'))
2222
expect(config.resolve('/file')).toBe('/file')
2323
})
24+
25+
test('should return the same config object when calling loadConfig() again with same configPath', () => {
26+
expect(loadConfig(configPath)).toBe(config)
27+
})
2428
})

0 commit comments

Comments
 (0)