Skip to content

Commit

Permalink
fix(docz-core): prevent watch in production
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Apr 25, 2019
1 parent 4d062de commit d673262
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 22 deletions.
7 changes: 5 additions & 2 deletions core/docz-core/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ export const dev = async (args: Arguments<any>) => {
const dataServer = new DataServer()
const socket = new Socket(server, websocketHost, websocketPort)

if (config.propsParser) dataServer.register([states.props(config)])
dataServer.register([states.config(config), states.entries(entries, config)])
if (config.propsParser) dataServer.register([states.props(config, true)])
dataServer.register([
states.config(config, true),
states.entries(entries, config, true),
])

try {
await dataServer.start()
Expand Down
11 changes: 7 additions & 4 deletions core/docz-core/src/states/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const update = async (params: Params, initial: Payload, { config }: Config) => {
params.setState('config', next)
}

export const state = (config: Config): State => {
export const state = (config: Config, dev?: boolean): State => {
const initial = getInitialConfig(config)
const glob = config.config || finds('docz')
const ignored = config.watchIgnore || /(((^|[\/\\])\..+)|(node_modules))/
Expand All @@ -64,9 +64,12 @@ export const state = (config: Config): State => {
start: async params => {
const fn = async () => update(params, initial, config)
await update(params, initial, config)
watcher.on('add', fn)
watcher.on('change', fn)
watcher.on('unlink', fn)

if (dev) {
watcher.on('add', fn)
watcher.on('change', fn)
watcher.on('unlink', fn)
}
},
close: () => {
watcher.close()
Expand Down
26 changes: 16 additions & 10 deletions core/docz-core/src/states/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ const updateEntries = (entries: Entries) => async (p: Params) => {
}
}

export const state = (entries: Entries, config: Config): State => {
export const state = (
entries: Entries,
config: Config,
dev?: boolean
): State => {
const src = path.relative(paths.root, config.src)
const files = Array.isArray(config.files)
? config.files.map(filePath => path.join(src, filePath))
Expand All @@ -42,16 +46,18 @@ export const state = (entries: Entries, config: Config): State => {
id: 'entries',
start: async params => {
const update = updateEntries(entries)

await update(params)
watcher.on('add', async () => update(params))
watcher.on('change', async () => update(params))
watcher.on('unlink', async () => update(params))
watcher.on('raw', async (event: string, path: string, details: any) => {
if (details.event === 'moved' && details.type === 'directory') {
await update(params)
}
})

if (dev) {
watcher.on('add', async () => update(params))
watcher.on('change', async () => update(params))
watcher.on('unlink', async () => update(params))
watcher.on('raw', async (event: string, path: string, details: any) => {
if (details.event === 'moved' && details.type === 'directory') {
await update(params)
}
})
}
},
close: () => {
watcher.close()
Expand Down
9 changes: 6 additions & 3 deletions core/docz-core/src/states/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const remove = (p: Params) => async (filepath: string) => {
p.setState('props', next)
}

export const state = (config: Config): State => {
export const state = (config: Config, dev?: boolean): State => {
const pattern = getPattern(config)
const ignored = config.watchIgnore || /(((^|[\/\\])\..+)|(node_modules))/
const watcher = chokidar.watch(pattern, {
Expand All @@ -61,8 +61,11 @@ export const state = (config: Config): State => {
start: async params => {
const addInitial = initial(config, pattern)
await addInitial(params)
watcher.on('change', change(params, config))
watcher.on('unlink', remove(params))

if (dev) {
watcher.on('change', change(params, config))
watcher.on('unlink', remove(params))
}
},
close: () => {
watcher.close()
Expand Down
2 changes: 1 addition & 1 deletion core/docz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"dependencies": {
"@loadable/component": "^5.9.0",
"@mdx-js/react": "^1.0.6",
"@mdx-js/react": "^1.0.15",
"@reach/router": "^1.2.1",
"array-sort": "^1.0.0",
"capitalize": "^2.0.0",
Expand Down
10 changes: 8 additions & 2 deletions core/gatsby-theme-docz/src/node/sourceNodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const fs = require('fs-extra')
const { Entries, DataServer, states } = require('docz-core')
const { parseConfig } = require('../utils/parseConfig')

const NODE_ENV = process.env.GATSBY_ACTIVE_ENV || process.env.NODE_ENV
const IS_DEV = NODE_ENV === 'development'

const digest = str =>
crypto
.createHash('md5')
Expand All @@ -15,8 +18,11 @@ module.exports = async ({ actions, createNodeId }, opts) => {
const entries = new Entries(config)
const dataServer = new DataServer()

if (config.propsParser) dataServer.register([states.props(config)])
dataServer.register([states.config(config), states.entries(entries, config)])
if (config.propsParser) dataServer.register([states.props(config, IS_DEV)])
dataServer.register([
states.config(config, IS_DEV),
states.entries(entries, config, IS_DEV),
])

try {
await dataServer.start()
Expand Down

0 comments on commit d673262

Please sign in to comment.