Skip to content

Commit

Permalink
chore(docz-core): use entry properties in just one level
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Jun 10, 2018
1 parent 1e7e4ae commit 7b92a75
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 47 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"packages:build": "lerna run build --ignore docz-example-*",
"prerelease": "yarn run packages",
"release": "lerna publish --conventional-commits",
"release:beta": "yarn release --npm-tag=beta --cd-version=prerelease --preid=beta"
"release:beta": "yarn release --npm-tag=beta --preid=beta"
},
"devDependencies": {
"del": "^3.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/docz-core/src/DataServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class DataServer {

const handleConnection = async (socket: WS) => {
const update = this.updateEntries(entries, socket)
const map = await entries.getMap()
const map = await entries.get()

watcher.on('change', async () => update(this.config))
watcher.on('unlink', async () => update(this.config))
Expand Down Expand Up @@ -89,7 +89,7 @@ export class DataServer {
): (config: Config) => Promise<void> {
return async config => {
if (isSocketOpened(socket)) {
const map = await entries.getMap()
const map = await entries.get()

await Entries.writeImports(map)
socket.send(this.entriesData(map))
Expand Down
54 changes: 34 additions & 20 deletions packages/docz-core/src/Entries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as path from 'path'

import * as paths from './config/paths'
import { touch, compiled } from './utils/fs'
import { mapToObj } from './utils/helpers'

import { Entry, parseMdx } from './Entry'
import { Plugin } from './Plugin'
Expand Down Expand Up @@ -49,7 +50,9 @@ const writeAppFiles = async (config: Config, dev: boolean): Promise<void> => {

const writeImports = async (map: EntryMap): Promise<void> => {
const imports = await compiled(fromTemplates('imports.tpl.js'))
await touch(paths.importsJs, imports({ entries: Object.values(map) }))
const rawImportsJs = imports({ entries: Object.values(map) })

await touch(paths.importsJs, rawImportsJs)
}

const writeData = async (map: EntryMap, config: Config): Promise<void> => {
Expand All @@ -63,7 +66,18 @@ const writeData = async (map: EntryMap, config: Config): Promise<void> => {
await touch(paths.configJson, JSON.stringify(configObj, null, 2))
}

export type EntryMap = Record<string, Entry>
export interface EntryObj {
id: string
filepath: string
slug: string
name: string
route: string
order: number
menu: string | null
[key: string]: any
}

export type EntryMap = Record<string, EntryObj>

export class Entries {
public static async writeApp(config: Config, dev?: boolean): Promise<void> {
Expand All @@ -79,19 +93,15 @@ export class Entries {
await writeData(map, config)
}

public all: EntryMap
public getMap: () => Promise<EntryMap>
public all: Map<string, EntryObj>
public get: () => Promise<EntryMap>

constructor(config: Config) {
this.all = {}

this.getMap = async () => {
this.all = await this.get(config)
return this.all
}
this.all = new Map()
this.get = async () => this.getMap(config)
}

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

const ignoreGlob = '!node_modules'
Expand All @@ -103,18 +113,22 @@ export class Entries {

const createEntry = async (file: string) => {
const ast = await parseMdx(file)
return new Entry(ast, file, src)
const { settings, ...entry } = new Entry(ast, file, src)

return {
...settings,
...entry,
}
}

const filesToReduce = await Promise.all(
files.filter(isEntry).map(createEntry)
)
const map = new Map()
const entries = await Promise.all(files.filter(isEntry).map(createEntry))

const reducer = (obj: EntryMap, entry: Entry) => ({
...obj,
[entry.filepath]: entry,
})
for (const entry of entries) {
map.set(entry.filepath, entry)
}

return filesToReduce.reduce(reducer, {})
this.all = map
return mapToObj(map)
}
}
34 changes: 16 additions & 18 deletions packages/docz-core/src/Entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,43 @@ export const parseMdx = (file: string): Promise<string> => {
return parser.run(parser.parse(raw))
}

const getFromParsedData = (value: string) => (ast: any) => {
const getParsedData = (ast: any) => {
const node = find(ast, (node: any) => is('yaml', node))
return get(node, `data.parsedValue.${value}`)
return get(node, `data.parsedValue`)
}

const getName = getFromParsedData('name')
const getRoute = getFromParsedData('route')
const getMenu = getFromParsedData('menu')
const getOrder = getFromParsedData('order')
const getSettings = getFromParsedData('settings')

export class Entry {
readonly [key: string]: any

public static async check(file: string): Promise<boolean | null> {
const ast = await parseMdx(file)
return Boolean(getName(ast))
const parsed = getParsedData(ast)
return Boolean(parsed && parsed.name)
}

public id: string
public filepath: string
public slug: string
public route: string
public name: string
public menu: string | null
public order: number
public settings: any
public menu: string | null
public settings: {
[key: string]: any
}

constructor(ast: any, file: string, src: string) {
const filepath = this.getFilepath(file, src)
const parsed = getParsedData(ast)

this.id = ulid()
this.filepath = filepath
this.slug = this.slugify(filepath)
this.route = this.getRoute(ast)
this.name = getName(ast)
this.menu = getMenu(ast)
this.order = parseInt(getOrder(ast), 10) || 0
this.settings = getSettings(ast) || {}
this.route = this.getRoute(parsed)
this.name = parsed.name
this.order = parsed.order || 0
this.menu = parsed.menu
this.settings = parsed
}

private getFilepath(file: string, src: string): string {
Expand All @@ -75,7 +73,7 @@ export class Entry {
return slugify(fileWithoutExt)
}

private getRoute(ast: any): string {
return getRoute(ast) || `/${this.slug}`
private getRoute(parsed: any): string {
return parsed && parsed.route ? parsed.route : `/${this.slug}`
}
}
2 changes: 1 addition & 1 deletion packages/docz-core/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const build = async (args: Config) => {
const config = loadConfig(args)
const bundler = webpack(config, 'production')
const entries = new Entries(config)
const map = await entries.getMap()
const map = await entries.get()
const run = Plugin.runPluginsMethod(config.plugins)

await fs.remove(paths.app)
Expand Down
2 changes: 1 addition & 1 deletion packages/docz-core/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const dev = async (args: Config) => {
const config = loadConfig(args)
const bundler = webpack(config, 'development')
const entries = new Entries(config)
const map = await entries.getMap()
const map = await entries.get()
const server = await bundler.createServer(bundler.getConfig())
const app = await server.start()

Expand Down
6 changes: 6 additions & 0 deletions packages/docz-core/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ export function isArrEqual(arr: any[] | null, to: any[] | null): boolean {

return equals
}

export const mapToObj = (map: Map<any, any>) =>
Array.from(map.entries()).reduce(
(obj, [key, value]) => ({ ...obj, [`${key}`]: value }),
{}
)
1 change: 1 addition & 0 deletions packages/docz/src/components/Docs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const Docs: React.SFC<DocsProps> = ({ children }) => {
const menus = getMenusFromEntries(entriesArr).sort((a, b) =>
sortBy(a, b)
)

const docs = entriesArr
.sort((a, b) => sortBy(a.name, b.name))
.sort((a, b) => b.order - a.order)
Expand Down
6 changes: 2 additions & 4 deletions packages/docz/src/theme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ export interface Entry {
slug: string
route: string
name: string
menu: string | null
order: number
settings: {
[key: string]: any
}
menu: string | null
[key: string]: any
}

export interface ThemeConfig {
Expand Down

0 comments on commit 7b92a75

Please sign in to comment.