Skip to content

Commit

Permalink
feat(docz-core): ensure files before running machine
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Jul 11, 2019
1 parent 4ba6eaf commit 1876540
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
34 changes: 26 additions & 8 deletions core/docz-core/src/bundler/machine/actions.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
import * as path from 'path'
import * as fs from 'fs-extra'
import { assign, Event } from 'xstate'
import { get, assoc } from 'lodash/fp'
import { assoc } from 'lodash/fp'
import glob from 'fast-glob'
import log from 'signale'
import sh from 'shelljs'

import * as paths from '../../config/paths'
import { openBrowser as open } from '../../utils/open-browser'
import { ServerMachineCtx } from './context'

const ensureFile = (filename: string, toDelete?: string) => {
const ghost = path.resolve(paths.docz, toDelete || filename)
const original = path.resolve(paths.root, filename)
if (fs.pathExistsSync(ghost) && !fs.pathExistsSync(original)) {
fs.removeSync(ghost)
}
}

export const ensureFiles = () => {
const themeDirs = glob.sync('src/gatsby-theme-**', {
cwd: paths.docz,
onlyDirectories: true,
})

ensureFile('doczrc.js')
ensureFile('gatsby-browser.js')
ensureFile('gatsby-ssr.js')
ensureFile('gatsby-node.js')
ensureFile('gatsby-config.js', 'gatsby-config.custom.js')

themeDirs.forEach(dir => ensureFile(dir))
}

export const assignFirstInstall = assign((ctx: ServerMachineCtx) => {
const firstInstall = !sh.test('-e', paths.docz)
return assoc('firstInstall', firstInstall, ctx)
Expand All @@ -18,12 +42,6 @@ export const checkIsDoczRepo = assign((ctx: ServerMachineCtx) => {
return assoc('isDoczRepo', isDoczRepo, ctx)
})

export const openBrowser = ({ args }: ServerMachineCtx) => {
if (get('openBrowser', args) || get('o', args)) {
open(`http://${args.host}:${args.port}`)
}
}

export const logError = (ctx: ServerMachineCtx, ev: Event<any>) => {
log.fatal(ev.data)
sh.exit(0)
Expand Down
1 change: 1 addition & 0 deletions core/docz-core/src/bundler/machine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const machine = Machine<ServerMachineCtx>({
type: 'parallel',
states: {
watch: {
onEntry: 'ensureFiles',
invoke: {
src: 'watchFiles',
},
Expand Down

0 comments on commit 1876540

Please sign in to comment.