Skip to content

Commit

Permalink
feat(docz-core): watch custom theme files
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Jul 6, 2019
1 parent c0b5c34 commit 5ee7abe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion core/docz-core/src/bundler/machine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const machine = Machine<ServerMachineCtx>({
states: {
watch: {
invoke: {
src: 'watchConfigFiles',
src: 'watchFiles',
},
},
server: {
Expand Down
2 changes: 1 addition & 1 deletion core/docz-core/src/bundler/machine/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export { createResources } from './create-resources'
export { ensureDirs } from './ensure-dirs'
export { execDevCommand } from './exec-dev-command'
export { installDeps } from './install-deps'
export { watchConfigFiles } from './watch-config-files'
export { watchFiles } from './watch-files'
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
import * as path from 'path'
import * as fs from 'fs-extra'
import { finds } from 'load-cfg'
import sh from 'shelljs'

import { Config } from '../../../config/argv'
import * as paths from '../../../config/paths'
import { createWatcher } from '../../../states/config'
import { ServerMachineCtx as Context } from '../context'

const watchGatsbyThemeFiles = (args: Config) => {
const watcher = createWatcher('src/gatsby-theme-**/**/*', args)
const copy = (filepath: string) => {
const src = path.resolve(paths.root, filepath)
const dest = path.resolve(paths.docz, filepath)
fs.copySync(src, dest)
}
const remove = (filepath: string) => {
fs.removeSync(path.resolve(paths.docz, filepath))
}

watcher
.on('add', copy)
.on('addDir', copy)
.on('change', copy)
.on('unlink', remove)
.on('unlinkDir', remove)

return () => watcher.close()
}

const createWatch = (args: Config) => (
glob: any,
src: string,
Expand All @@ -19,8 +40,8 @@ const createWatch = (args: Config) => (
custom ? src.replace('.js', '.custom.js') : src
)

const copyFile = () => sh.cp(srcPath, destPath)
const deleteFile = () => sh.rm(destPath)
const copyFile = () => fs.copySync(srcPath, destPath)
const deleteFile = () => fs.removeSync(destPath)

watcher
.on('add', copyFile)
Expand All @@ -30,19 +51,21 @@ const createWatch = (args: Config) => (
return () => watcher.close()
}

export const watchConfigFiles = ({ args }: Context) => () => {
export const watchFiles = ({ args }: Context) => () => {
const watch = createWatch(args)
const doczrc = watch(args.config || finds('docz'), 'doczrc.js')
const gatsbyBrowser = watch(paths.gatsbyBrowser, 'gatsby-browser.js')
const gatsbyNode = watch(paths.gatsbyNode, 'gatsby-node.js')
const gatsbySSR = watch(paths.gatsbySSR, 'gatsby-ssr.js')
const gatsbyConfig = watch(paths.gatsbyConfig, 'gatsby-config.js', true)
const themeFilesWatcher = watchGatsbyThemeFiles(args)

return () => {
doczrc()
gatsbyConfig()
gatsbyBrowser()
gatsbyNode()
gatsbySSR()
themeFilesWatcher()
}
}

0 comments on commit 5ee7abe

Please sign in to comment.