Skip to content

Commit

Permalink
feat(docz-core): watch gatsby config files
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Jul 6, 2019
1 parent 4caa47a commit 1539378
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 53 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: 'watchConfig',
src: 'watchConfigFiles',
},
},
server: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from 'path'
import * as fs from 'fs-extra'
import { finds } from 'load-cfg'
import { getOr, fromPairs } from 'lodash/fp'
import { omit, getOr, fromPairs } from 'lodash/fp'
import latestVersion from 'latest-version'
import findUp from 'find-up'
import sh from 'shelljs'
Expand Down Expand Up @@ -107,47 +107,48 @@ export const writeNotFound = async () => {
await outputFileFromTemplate('404.tpl.js', outputPath, {})
}

const writeConfigFile = async ({ args, isDoczRepo }: ServerMachineCtx) => {
const writeGatsbyConfig = async ({ args, isDoczRepo }: ServerMachineCtx) => {
const outputPath = path.join(paths.docz, 'gatsby-config.js')

const config = omit(['plugins'], args)
const newConfig = JSON.stringify({ ...config, root: paths.docz })
await outputFileFromTemplate('gatsby-config.tpl.js', outputPath, {
isDoczRepo,
config: JSON.stringify({ ...args, root: paths.docz }),
config: newConfig,
})
}

const writeGatsbyNode = async (ctx: ServerMachineCtx) => {
const outputPath = path.join(paths.docz, 'gatsby-node.js')
await outputFileFromTemplate('gatsby-node.tpl.js', outputPath, {
keys: Object.keys(ctx.args.gatsbyNode),
})
const copyGatsbyConfigFile = async (from: string, to: string) => {
const filepath = path.join(paths.root, from)
const dest = path.join(paths.docz, to)
if (fs.pathExistsSync(filepath)) {
await fs.copy(filepath, dest)
}
}

const writeGatsbySSR = async (ctx: ServerMachineCtx) => {
const outputPath = path.join(paths.docz, 'gatsby-ssr.js')
await outputFileFromTemplate('gatsby-ssr.tpl.js', outputPath, {
keys: Object.keys(ctx.args.gatsbySSR),
})
}
const writeGatsbyConfigCustom = async () =>
copyGatsbyConfigFile('gatsby-config.js', 'gatsby-config.custom.js')

const writeGatsbyBrowser = async (ctx: ServerMachineCtx) => {
const outputPath = path.join(paths.docz, 'gatsby-browser.js')
await outputFileFromTemplate('gatsby-browser.tpl.js', outputPath, {
keys: Object.keys(ctx.args.gatsbyBrowser),
})
}
const writeGatsbyNode = async () =>
copyGatsbyConfigFile('gatsby-node.js', 'gatsby-node.js')

const writeGatsbySSR = async () =>
copyGatsbyConfigFile('gatsby-ssr.js', 'gatsby-ssr.js')

const writeGatsbyBrowser = async () =>
copyGatsbyConfigFile('gatsby-browser.js', 'gatsby-browser.js')

export const createResources = async (ctx: ServerMachineCtx) => {
try {
copyPkgJSON()
await copyDoczRc()
await copyAndModifyPkgJson(ctx)
await writeEslintRc(ctx)
await writeConfigFile(ctx)
await writeNotFound()
await writeGatsbyBrowser(ctx)
await writeGatsbyNode(ctx)
await writeGatsbySSR(ctx)
await writeGatsbyConfig(ctx)
await writeGatsbyConfigCustom()
await writeGatsbyNode()
await writeGatsbyBrowser()
await writeGatsbySSR()
} catch (err) {
console.log(err)
}
Expand Down
10 changes: 5 additions & 5 deletions core/docz-core/src/bundler/machine/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { createResources } from './createResources'
export { ensureDirs } from './ensureDirs'
export { execDevCommand } from './execDevCommand'
export { installDeps } from './installDeps'
export { watchConfig } from './watchConfig'
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'
48 changes: 48 additions & 0 deletions core/docz-core/src/bundler/machine/services/watch-config-files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as path from 'path'
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 createWatch = (args: Config) => (
glob: any,
src: string,
custom?: boolean
) => {
const watcher = createWatcher(glob, args)
const srcPath = path.join(paths.root, src)
const destPath = path.join(
paths.docz,
custom ? src.replace('.js', '.custom.js') : src
)

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

watcher
.on('add', copyFile)
.on('change', copyFile)
.on('unlink', deleteFile)

return () => watcher.close()
}

export const watchConfigFiles = ({ 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)

return () => {
doczrc()
gatsbyConfig()
gatsbyBrowser()
gatsbyNode()
gatsbySSR()
}
}
19 changes: 0 additions & 19 deletions core/docz-core/src/bundler/machine/services/watchConfig.ts

This file was deleted.

8 changes: 8 additions & 0 deletions core/docz-core/src/config/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export interface Paths {
appNodeModules: string
appPackageJson: string
appYarnLock: string
gatsbyConfig: string
gatsbyBrowser: string
gatsbyNode: string
gatsbySSR: string
ownNodeModules: string

checkIsDoczProject: (config: any) => boolean
Expand Down Expand Up @@ -69,6 +73,10 @@ export const appNodeModules = resolveApp('node_modules')
export const appPackageJson = resolveApp('package.json')
export const appYarnLock = resolveOwn('yarn.lock')
export const ownNodeModules = resolveOwn('node_modules')
export const gatsbyConfig = resolveApp('gatsby-config.js')
export const gatsbyBrowser = resolveApp('gatsby-browser.js')
export const gatsbyNode = resolveApp('gatsby-node.js')
export const gatsbySSR = resolveApp('gatsby-ssr.js')

export const getDist = (dest: string) => path.join(root, dest)
export const distPublic = (dest: string) => path.join(dest, 'public/')
Expand Down
6 changes: 3 additions & 3 deletions core/docz-core/src/states/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ const update = async (params: Params, initial: Payload, { config }: Config) => {

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

export const configWatcher = (config: Config) => {
const glob = config.config || finds('docz')
export const createWatcher = (glob: any, config: Config) => {
const ignored = config.watchIgnore || WATCH_IGNORE
const watcher = chokidar.watch(glob, {
ignored,
Expand All @@ -62,8 +61,9 @@ export const configWatcher = (config: Config) => {
}

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

return {
id: 'config',
Expand Down

0 comments on commit 1539378

Please sign in to comment.