From 5ee7abe81263df5b8e2e164f395bb5a108565f8f Mon Sep 17 00:00:00 2001 From: Pedro Nauck Date: Sat, 6 Jul 2019 16:35:17 -0300 Subject: [PATCH] feat(docz-core): watch custom theme files --- core/docz-core/src/bundler/machine/index.ts | 2 +- .../src/bundler/machine/services/index.ts | 2 +- .../{watch-config-files.ts => watch-files.ts} | 31 ++++++++++++++++--- 3 files changed, 29 insertions(+), 6 deletions(-) rename core/docz-core/src/bundler/machine/services/{watch-config-files.ts => watch-files.ts} (58%) diff --git a/core/docz-core/src/bundler/machine/index.ts b/core/docz-core/src/bundler/machine/index.ts index 30b9e2994..a0937fd02 100644 --- a/core/docz-core/src/bundler/machine/index.ts +++ b/core/docz-core/src/bundler/machine/index.ts @@ -33,7 +33,7 @@ const machine = Machine({ states: { watch: { invoke: { - src: 'watchConfigFiles', + src: 'watchFiles', }, }, server: { diff --git a/core/docz-core/src/bundler/machine/services/index.ts b/core/docz-core/src/bundler/machine/services/index.ts index 28393301b..b47564565 100644 --- a/core/docz-core/src/bundler/machine/services/index.ts +++ b/core/docz-core/src/bundler/machine/services/index.ts @@ -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' diff --git a/core/docz-core/src/bundler/machine/services/watch-config-files.ts b/core/docz-core/src/bundler/machine/services/watch-files.ts similarity index 58% rename from core/docz-core/src/bundler/machine/services/watch-config-files.ts rename to core/docz-core/src/bundler/machine/services/watch-files.ts index fff62a254..a5dbbcc18 100644 --- a/core/docz-core/src/bundler/machine/services/watch-config-files.ts +++ b/core/docz-core/src/bundler/machine/services/watch-files.ts @@ -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, @@ -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) @@ -30,13 +51,14 @@ 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() @@ -44,5 +66,6 @@ export const watchConfigFiles = ({ args }: Context) => () => { gatsbyBrowser() gatsbyNode() gatsbySSR() + themeFilesWatcher() } }