diff --git a/core/docz-core/src/bundler/server.ts b/core/docz-core/src/bundler/server.ts index 139a1af08..4625d52b8 100644 --- a/core/docz-core/src/bundler/server.ts +++ b/core/docz-core/src/bundler/server.ts @@ -1,4 +1,6 @@ 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' @@ -6,7 +8,13 @@ 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) }) diff --git a/core/docz-core/src/machines/devServer/context.ts b/core/docz-core/src/machines/devServer/context.ts index ad68b78fb..949cc8a50 100644 --- a/core/docz-core/src/machines/devServer/context.ts +++ b/core/docz-core/src/machines/devServer/context.ts @@ -3,6 +3,7 @@ import { Config } from '../../config/argv' export interface ServerMachineCtx { args: Config config: any + doczrcFilepath: string firstInstall?: boolean isDoczRepo?: boolean } diff --git a/core/docz-core/src/machines/devServer/index.ts b/core/docz-core/src/machines/devServer/index.ts index 622286854..012624612 100644 --- a/core/docz-core/src/machines/devServer/index.ts +++ b/core/docz-core/src/machines/devServer/index.ts @@ -8,6 +8,11 @@ const machine = Machine({ id: 'devServer', type: 'parallel', states: { + watch: { + invoke: { + src: 'watchConfig', + }, + }, server: { initial: 'idle', states: { diff --git a/core/docz-core/src/machines/devServer/services/createResources.ts b/core/docz-core/src/machines/devServer/services/createResources.ts index 1e62c559c..1d2df5700 100644 --- a/core/docz-core/src/machines/devServer/services/createResources.ts +++ b/core/docz-core/src/machines/devServer/services/createResources.ts @@ -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) } diff --git a/core/docz-core/src/machines/devServer/services/index.ts b/core/docz-core/src/machines/devServer/services/index.ts index 143391bbc..ab3fb303f 100644 --- a/core/docz-core/src/machines/devServer/services/index.ts +++ b/core/docz-core/src/machines/devServer/services/index.ts @@ -2,3 +2,4 @@ export { createResources } from './createResources' export { ensureDirs } from './ensureDirs' export { execDevCommand } from './execDevCommand' export { installDeps } from './installDeps' +export { watchConfig } from './watchConfig' diff --git a/core/docz-core/src/machines/devServer/services/watchConfig.ts b/core/docz-core/src/machines/devServer/services/watchConfig.ts new file mode 100644 index 000000000..c83f8cf42 --- /dev/null +++ b/core/docz-core/src/machines/devServer/services/watchConfig.ts @@ -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() +} diff --git a/core/docz-core/src/states/config.ts b/core/docz-core/src/states/config.ts index 62d39cffb..398824b65 100644 --- a/core/docz-core/src/states/config.ts +++ b/core/docz-core/src/states/config.ts @@ -48,11 +48,9 @@ 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, @@ -60,6 +58,12 @@ export const state = (config: Config, dev?: boolean): State => { }) watcher.setMaxListeners(Infinity) + return watcher +} + +export const state = (config: Config, dev?: boolean): State => { + const initial = getInitialConfig(config) + const watcher = configWatcher(config) return { id: 'config',