Skip to content

Commit

Permalink
feat(docz-core): add support for load custom themes
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Apr 15, 2018
1 parent b6e6d60 commit ade98ce
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/load-cfg/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const finds = (name: string): string[] => [
]

export const load = (name: string, defaultConfig: any = {}) => {
let file
let file = {}
const filepath = findup.sync(finds(name))

if (filepath) {
Expand Down
1 change: 1 addition & 0 deletions packages/playgrodd-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"babylon": "^6.18.0",
"express": "^4.16.3",
"fast-glob": "^2.2.0",
"load-cfg": "^0.0.1",
"mkdirp": "^0.5.1"
},
"devDependencies": {
Expand Down
22 changes: 14 additions & 8 deletions packages/playgrodd-core/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { load } from 'load-cfg'

import * as paths from './config/paths'
import { pick } from './utils/helpers'

import { Entries } from './Entries'
import { Bundler, BundlerFactory } from './Bundler'

Expand All @@ -18,13 +22,20 @@ export class Server {
private bundler: Bundler
private entries: Entries

constructor({ port, bundler, files: pattern, theme }: IConstructorParams) {
constructor(args: IConstructorParams) {
const initialArgs = this.getInitialArgs(args)
const { port, theme, files, bundler } = load('playgrodd', initialArgs)

this.port = port
this.theme = theme
this.entries = new Entries(pattern)
this.entries = new Entries(files)
this.bundler = this.getBundler(bundler).create({ port, paths })
}

private getInitialArgs(args: IConstructorParams) {
return pick(['port', 'theme', 'files', 'bundler'], args)
}

private getBundler(bundler: string): BundlerFactory {
try {
return require(`playgrodd-bundler-${bundler}`)
Expand All @@ -33,14 +44,9 @@ export class Server {
}
}

private getTheme() {
return `playgrodd-theme-${this.theme}`
}

public async start() {
const theme = this.getTheme()
const entries = this.entries.parse()
const compiler = await this.bundler.createCompiler(theme, entries)
const compiler = await this.bundler.createCompiler(this.theme, entries)
const server = await this.bundler.createServer(compiler)

server.listen(this.port)
Expand Down
24 changes: 24 additions & 0 deletions packages/playgrodd-core/src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@
const { keys } = Object

export const isFn = (value: any): boolean => typeof value === 'function'
export const prop = (key: string, obj: any): any => obj[key]

export function omit<R = object>(props: string[], obj: any): R {
const newObj = keys(obj)
.filter((key: string): boolean => props.indexOf(key) === -1)
.reduce(
(returnObj: any, key: string): R => ({ ...returnObj, [key]: obj[key] }),
{}
)

return newObj as R
}

export function pick<R = object>(props: string[], obj: any): R {
const newObj = keys(obj)
.filter((key: string): boolean => props.indexOf(key) > -1)
.reduce(
(returnObj: any, key: string): R => ({ ...returnObj, [key]: obj[key] }),
{}
)

return newObj as R
}
2 changes: 1 addition & 1 deletion packages/playgrodd/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ yargs
})
yargs.positional('theme', {
type: 'string',
default: 'default',
default: 'playgrodd-theme-default',
})
yargs.positional('bundler', {
type: 'string',
Expand Down

0 comments on commit ade98ce

Please sign in to comment.