Skip to content

Commit

Permalink
feat(docz-core): add doczrc config watcher on dev
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed May 24, 2019
1 parent 18e6c55 commit 5aa0455
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 5 deletions.
10 changes: 9 additions & 1 deletion core/docz-core/src/bundler/server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { interpret } from 'xstate'
import { finds } from 'load-cfg'
import findUp from 'find-up'

import { Config as Args } from '../config/argv'
import { ServerHooks as Hooks } from '../lib/Bundler'
import { devServerMachine } from '../machines/devServer'

export const server = (args: Args) => async (config: any, hooks: Hooks) => ({
start: async () => {
const machine = devServerMachine.withContext({ args, config })
const doczrcFilepath = await findUp(finds('docz'))
const machine = devServerMachine.withContext({
args,
config,
doczrcFilepath,
})

const service = interpret(machine).onTransition(state => {
args.debug && console.log(state.value)
})
Expand Down
1 change: 1 addition & 0 deletions core/docz-core/src/machines/devServer/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Config } from '../../config/argv'
export interface ServerMachineCtx {
args: Config
config: any
doczrcFilepath: string
firstInstall?: boolean
isDoczRepo?: boolean
}
5 changes: 5 additions & 0 deletions core/docz-core/src/machines/devServer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ const machine = Machine<ServerMachineCtx>({
id: 'devServer',
type: 'parallel',
states: {
watch: {
invoke: {
src: 'watchConfig',
},
},
server: {
initial: 'idle',
states: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const copyPkgJSON = () => {
sh.cp(pkgJSON, paths.docz)
}

const copyDoczRc = async () => {
export const copyDoczRc = async () => {
const filepath = await findUp(finds('docz'))
sh.cp(filepath, paths.docz)
}
Expand Down
1 change: 1 addition & 0 deletions core/docz-core/src/machines/devServer/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { createResources } from './createResources'
export { ensureDirs } from './ensureDirs'
export { execDevCommand } from './execDevCommand'
export { installDeps } from './installDeps'
export { watchConfig } from './watchConfig'
19 changes: 19 additions & 0 deletions core/docz-core/src/machines/devServer/services/watchConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as path from 'path'
import sh from 'shelljs'

import { ServerMachineCtx as Context } from '../context'
import { configWatcher } from '../../../states/config'
import * as paths from '../../../config/paths'

export const watchConfig = (ctx: Context) => () => {
const watcher = configWatcher(ctx.args)
const copyDoczrc = () => sh.cp(ctx.doczrcFilepath, paths.docz)
const deleteDoczrc = () => sh.rm(path.join(paths.docz, 'doczrc.js'))

watcher
.on('add', copyDoczrc)
.on('change', copyDoczrc)
.on('unlink', deleteDoczrc)

return () => watcher.close()
}
10 changes: 7 additions & 3 deletions core/docz-core/src/states/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,22 @@ const update = async (params: Params, initial: Payload, { config }: Config) => {

export const WATCH_IGNORE = /(((^|[\/\\])\.((?!docz)(.+)))|(node_modules))/

export const state = (config: Config, dev?: boolean): State => {
const initial = getInitialConfig(config)
export const configWatcher = (config: Config) => {
const glob = config.config || finds('docz')
const ignored = config.watchIgnore || WATCH_IGNORE

const watcher = chokidar.watch(glob, {
ignored,
cwd: paths.root,
persistent: true,
})

watcher.setMaxListeners(Infinity)
return watcher
}

export const state = (config: Config, dev?: boolean): State => {
const initial = getInitialConfig(config)
const watcher = configWatcher(config)

return {
id: 'config',
Expand Down

0 comments on commit 5aa0455

Please sign in to comment.