-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathconfig.ts
More file actions
45 lines (34 loc) · 1017 Bytes
/
config.ts
File metadata and controls
45 lines (34 loc) · 1017 Bytes
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
import * as fs from 'fs-extra'
import defaults from 'lodash.defaults'
import {getConfigFilePath} from './utils'
const configFilePath = getConfigFilePath()
const defaultConfig = {
hackmdAPIEndpointURL: 'https://api.hackmd.io/v1',
}
const envConfig = {
accessToken: process.env.HMD_API_ACCESS_TOKEN,
hackmdAPIEndpointURL: process.env.HMD_API_ENDPOINT_URL,
}
// look for a readable config file; we can merge it with the env.
let hasExistingConfigFile = false
try {
fs.accessSync(configFilePath, fs.constants.R_OK)
hasExistingConfigFile = true
// tslint:disable-next-line: no-unused
} catch {
// noop
}
let readConfig = {}
if (hasExistingConfigFile) {
try {
readConfig = JSON.parse(fs.readFileSync(configFilePath, 'utf8'))
} catch (error) {
throw new Error(`
Could not read JSON config file at ${configFilePath}. Encountered exception:
${error}
`)
}
}
// prefer environment config over file config
const config = defaults(envConfig, readConfig, defaultConfig)
export default config