Skip to content

Commit

Permalink
fix(docz-core): entries rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed May 15, 2018
1 parent b1a84f8 commit 986ba65
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 114 deletions.
2 changes: 2 additions & 0 deletions packages/docz-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"koa-static": "^4.0.2",
"load-cfg": "^0.0.1",
"lodash.get": "^4.4.2",
"lodash.template": "^4.4.0",
"prettier": "^1.12.0",
"react-hot-loader": "4.1.2",
"remark-frontmatter": "^1.2.0",
Expand Down Expand Up @@ -75,6 +76,7 @@
"@types/express": "^4.11.1",
"@types/html-webpack-plugin": "^2.30.3",
"@types/lodash.get": "^4.4.3",
"@types/lodash.template": "^4.4.3",
"@types/node": "10.0.4",
"@types/prettier": "^1.12.1",
"@types/resolve": "^0.0.7",
Expand Down
75 changes: 13 additions & 62 deletions packages/docz-core/src/Entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import * as glob from 'fast-glob'
import * as fs from 'fs'
import * as path from 'path'
import { test, mkdir } from 'shelljs'
import { compile } from 'art-template'
import template from 'lodash.template'

import * as paths from './config/paths'
import { propOf, omit } from './utils/helpers'
import { propOf } from './utils/helpers'
import { format } from './utils/format'

import { Entry, parseMdx } from './Entry'
Expand Down Expand Up @@ -35,7 +35,7 @@ const compiled = (file: string) =>
})

stream.on('data', chunk => (data += chunk))
stream.on('end', () => resolve(compile(data)))
stream.on('end', () => resolve(template(data)))
stream.on('error', err => reject(err))
})

Expand Down Expand Up @@ -80,7 +80,7 @@ const writeDataAndImports = async (
const imports = await compiled('imports.tpl.js')

const rawImportsJs = imports({
entries: Object.values(entries),
entries,
})

const rawData = stringify({
Expand All @@ -97,18 +97,17 @@ const writeDataAndImports = async (
export type EntryMap = Record<string, Entry>

export class Entries {
public static write(config: Config): (entries: EntryMap) => Promise<void> {
return async (entries: EntryMap) => {
mkd(paths.docz)
await writeStructuredFiles(config)
await writeDataAndImports(entries, config)
}
public static async write(config: Config, entries: EntryMap): Promise<void> {
mkd(paths.docz)
await writeStructuredFiles(config)
await writeDataAndImports(entries, config)
}

public static rewrite(config: Config): (entries: EntryMap) => Promise<void> {
return async (entries: EntryMap) => {
await writeDataAndImports(entries, config)
}
public static async rewrite(config: Config): Promise<void> {
const entries = new Entries(config)
const map = await entries.getMap()

await writeDataAndImports(map, config)
}

public config: Config
Expand All @@ -125,34 +124,6 @@ export class Entries {
}
}

public remove(file: string): EntryMap {
return omit([this.entryFilepath(file)], this.all)
}

public async update(file: string): Promise<EntryMap> {
const merge = this.mergeEntriesWithNewEntry(this.all, this.config)
return (await Entry.check(file)) ? merge(file) : this.all
}

public async clean(dir: string): Promise<EntryMap> {
if (test('-d', dir)) {
return this.get(this.config)
}

const { paths } = this.config
const src = path.resolve(paths.root, this.config.src)
let entries = this.all

for (const file of Object.keys(this.all)) {
const filepath = path.join(src, file)
if (!test('-f', filepath)) {
entries = this.remove(filepath)
}
}

return entries
}

private async get(config: Config): Promise<EntryMap> {
const { files: pattern } = config

Expand All @@ -175,24 +146,4 @@ export class Entries {

return filesToReduce.reduce(reducer, {})
}

private entryFilepath(file: string): string {
const srcPath = path.resolve(paths.root, this.config.src)
return path.relative(srcPath, file)
}

private mergeEntriesWithNewEntry(
entries: EntryMap,
config: Config
): (file: string) => Promise<EntryMap> {
return async (file: string) => {
const ast = await parseMdx(file)
const entry = new Entry(ast, file, this.config.src)

return {
...entries,
[entry.filepath]: entry,
}
}
}
}
54 changes: 14 additions & 40 deletions packages/docz-core/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,33 @@ import chokidar from 'chokidar'
import * as paths from '../config/paths'
import { Config } from './args'

import { Entries, EntryMap } from '../Entries'
import { Entries } from '../Entries'
import { webpack } from '../bundlers'

process.env.BABEL_ENV = process.env.BABEL_ENV || 'development'
process.env.NODE_ENV = process.env.NODE_ENV || 'development'

export type Rewrite = (entries: EntryMap) => Promise<void>

const handleUpdate = (rewrite: Rewrite) => (entries: Entries) => async (
file: string
) => {
const newEntries = await entries.update(file)
await rewrite(newEntries)
}

const handleRemove = (rewrite: Rewrite) => (entries: Entries) => async (
file: string
) => {
const newEntries = entries.remove(file)
await rewrite(newEntries)
}

const handleRemoveDir = (rewrite: Rewrite) => (entries: Entries) => async (
event: string,
path: string,
details: any
) => {
if (details.event === 'moved' && details.type === 'directory') {
const newEntries = await entries.clean(details.path)
await rewrite(newEntries)
}
}

const writeEntriesAndWatch = async (config: Config) => {
const instance = new Entries(config)
const entries = await instance.getMap()

const write = Entries.write(config)
const rewrite = Entries.rewrite(config)
const onChange = handleUpdate(rewrite)
const onUnlinkDir = handleRemove(rewrite)
const onRaw = handleRemoveDir(rewrite)
const entries = new Entries(config)
const map = await entries.getMap()

const handleUpdate = async () => Entries.rewrite(config)
const handleRaw = async (event: string, path: string, details: any) => {
if (details.event === 'moved' && details.type === 'directory') {
handleUpdate()
}
}

const watcher = chokidar.watch(config.files, {
ignored: /(^|[\/\\])\../,
})

watcher
.on('change', onChange(instance))
.on('unlink', onUnlinkDir(instance))
.on('raw', onRaw(instance))
.on('change', handleUpdate)
.on('unlink', handleUpdate)
.on('raw', handleRaw)

await write(entries)
await Entries.write(config, map)
}

const INITIAL_CONFIG = {
Expand Down
2 changes: 1 addition & 1 deletion packages/docz-core/templates/imports.tpl.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const imports = {
<% entries.forEach(function(entry) { %>'<%- entry.filepath %>': () =>
<% Object.values(entries).forEach(function(entry) { %>'<%- entry.filepath %>': () =>
import(/* webpackPrefetch: true, webpackChunkName: "<%- entry.slug %>" */ '<%- entry.filepath %>'),<% }) %>
}
26 changes: 15 additions & 11 deletions packages/docz/src/components/Docs.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import * as React from 'react'
import { Children } from 'react'

import { dataContext, Entry, EntryMap } from '../theme'
import { dataContext, Entry } from '../theme'

export const isFn = (value: any): boolean => typeof value === 'function'

const getDocsFromEntries = (entries: EntryMap) =>
Object.values(entries).sort((entryA, entryB) => entryB.order - entryA.order)
const sortAlphabetically = (a: Entry, b: Entry) => {
if (a.name < b.name) return -1
if (a.name > b.name) return 1
return 0
}

const getDocsFromEntries = (entries: Entry[]) =>
entries.sort((a, b) => b.order - a.order)

const getCategoriesFromEntries = (entries: EntryMap) =>
const getCategoriesFromEntries = (entries: Entry[]) =>
Array.from(
new Set([
...Object.values(entries)
.map(entry => entry.menu)
.filter(c => Boolean(c)),
])
new Set([...entries.map(entry => entry.menu).filter(c => Boolean(c))])
)

export interface DocsRenderProps {
Expand All @@ -36,8 +38,10 @@ export const Docs: React.SFC<DocsProps> = ({ children }) => (
)
}

const docs = getDocsFromEntries(data.entries)
const menus = getCategoriesFromEntries(data.entries)
const sortedEntries = Object.values(data.entries).sort(sortAlphabetically)

const docs = getDocsFromEntries(sortedEntries)
const menus = getCategoriesFromEntries(sortedEntries)

return Children.only(
children({
Expand Down
6 changes: 6 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,12 @@
dependencies:
"@types/lodash" "*"

"@types/lodash.template@^4.4.3":
version "4.4.3"
resolved "https://registry.npmjs.org/@types/lodash.template/-/lodash.template-4.4.3.tgz#3c6df2eb7e964cd56b12ce55ac0e6669fef6216f"
dependencies:
"@types/lodash" "*"

"@types/lodash@*":
version "4.14.108"
resolved "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.108.tgz#02656af3add2e5b3174f830862c47421c00ef817"
Expand Down

0 comments on commit 986ba65

Please sign in to comment.