Skip to content

Commit

Permalink
fix(docz-core): data server rewriting files
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Sep 20, 2018
1 parent 9c6cb80 commit 66e6248
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
7 changes: 6 additions & 1 deletion packages/docz-core/src/DataServer.ts
Expand Up @@ -60,7 +60,7 @@ export class DataServer {
)
)

await touch(paths.db, JSON.stringify(this.state, null, 2))
this.updateStateFile()
}

public async listen(): Promise<void> {
Expand Down Expand Up @@ -97,6 +97,11 @@ export class DataServer {
return (key: string, val: any): void => {
this.state[key] = val
send(`state.${key}`, val)
this.updateStateFile()
}
}

private async updateStateFile(): Promise<void> {
await touch(paths.db, JSON.stringify(this.state, null, 2))
}
}
19 changes: 7 additions & 12 deletions packages/docz-core/src/states/config.ts
@@ -1,7 +1,6 @@
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'
Expand Down Expand Up @@ -36,29 +35,25 @@ const getInitialConfig = (config: Config): Payload => {
}
}

const updateConfig = (config: Config) => async (p: Params) => {
const old = p.state.config
const newConfig = load('docz', getInitialConfig(config), true, false)

if (newConfig && !equal(old, newConfig)) {
p.setState('config', newConfig)
}
}
const updateConfig = (config: Config) => async ({ setState }: Params) =>
setState('config', load('docz', getInitialConfig(config), true, false))

export const state = (config: Config): State => {
const watcher = chokidar.watch(finds('docz'), {
cwd: paths.root,
ignored: /(^|[\/\\])\../,
persistent: true,
})

return {
init: updateConfig(config),
update: async params => {
const update = updateConfig(config)
const fn = async () => update(params)

watcher.on('add', async () => update(params))
watcher.on('change', async () => update(params))
watcher.on('unlink', async () => update(params))
watcher.on('add', fn)
watcher.on('change', fn)
watcher.on('unlink', fn)

return () => watcher.close()
},
Expand Down
2 changes: 1 addition & 1 deletion packages/rehype-docz/package.json
Expand Up @@ -22,7 +22,7 @@
"tslint": "tslint --project ."
},
"dependencies": {
"@babel/generator": "^7.1.0",
"@babel/generator": "^7.0.0",
"@babel/parser": "^7.1.0",
"@babel/traverse": "^7.1.0",
"codesandboxer-fs": "^0.3.1",
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Expand Up @@ -27,7 +27,7 @@
semver "^5.4.1"
source-map "^0.5.0"

"@babel/generator@^7.0.0", "@babel/generator@^7.1.0":
"@babel/generator@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.0.0.tgz#1efd58bffa951dc846449e58ce3a1d7f02d393aa"
dependencies:
Expand Down

0 comments on commit 66e6248

Please sign in to comment.