-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
config.ts
66 lines (56 loc) · 1.67 KB
/
config.ts
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import * as fs from 'fs-extra'
import { load, finds } from 'load-cfg'
import chokidar from 'chokidar'
import equal from 'fast-deep-equal'
import get from 'lodash.get'
import { Params, State } from '../DataServer'
import { Config, RootMenuConfig, ThemeConfig } from '../commands/args'
import { getRepoUrl } from '../utils/repo-info'
import * as paths from '../config/paths'
interface Payload {
title: string
description: string
ordering: string
menu: RootMenuConfig
themeConfig: ThemeConfig
version: string | null
repository: string | null
native: boolean
}
const getInitialConfig = (config: Config): Payload => {
const pkg = fs.readJsonSync(paths.appPackageJson, { throws: false })
const repoUrl = getRepoUrl()
return {
title: config.title,
description: config.description,
themeConfig: config.themeConfig,
menu: config.menu,
ordering: config.ordering,
version: get(pkg, 'version'),
repository: repoUrl,
native: config.native,
}
}
const updateConfig = (config: Config) => async (p: Params) => {
const old = p.state.config
const newConfig = load('docz', getInitialConfig(config), true)
if (newConfig && !equal(old, newConfig)) {
p.setState('config', newConfig)
}
}
export const state = (config: Config): State => {
const watcher = chokidar.watch(finds('docz'), {
cwd: paths.root,
persistent: true,
})
return {
init: updateConfig(config),
update: async params => {
const update = updateConfig(config)
watcher.on('add', async () => update(params))
watcher.on('change', async () => update(params))
watcher.on('unlink', async () => update(params))
return () => watcher.close()
},
}
}