-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
43 lines (36 loc) · 997 Bytes
/
index.js
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
import fs from 'node:fs/promises'
import path from 'node:path'
import applicationConfigPath from 'application-config-path'
import { loadJsonFile } from 'load-json-file'
import { writeJsonFile } from 'write-json-file'
class ApplicationConfig {
constructor (name) {
this.filePath = path.join(applicationConfigPath(name), 'config.json')
}
async read () {
try {
return await loadJsonFile(this.filePath)
} catch (err) {
if (err.code === 'ENOENT') return {}
throw err
}
}
async write (data) {
if (typeof data !== 'object' || data === null) {
throw new TypeError('data is not an object')
}
await writeJsonFile(this.filePath, data)
}
async trash () {
try {
await fs.unlink(this.filePath)
await fs.rmdir(path.dirname(this.filePath))
} catch (err) {
if (err.code === 'ENOENT') return
throw err
}
}
}
export default function createApplicationConfig (name) {
return new ApplicationConfig(name)
}