Skip to content

Commit

Permalink
feat: rewrite dest path for themeDir override
Browse files Browse the repository at this point in the history
  • Loading branch information
dangoo authored and rakannimer committed Sep 4, 2019
1 parent bb62c6b commit 7418e55
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
29 changes: 22 additions & 7 deletions core/docz-core/__tests__/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import * as path from 'path'
import { setArgs } from '../src/config/argv'
import { getBaseConfig } from '../src/config/docz'
import { getThemeDir } from '../src/config/paths'
import yargs from 'yargs'

describe('config.argv & config.docz', () => {
test('works', () => {
const yargs = {
argv: {},
option: jest.fn().mockImplementation((key, value) => {
yargs.argv[value.alias ? value.alias : key] = value
return yargs
}),
}
const { argv } = setArgs(yargs)
// expect(argv).toMatchInlineSnapshot(`undefined`)
//@ts-ignore
getBaseConfig(argv, {})
// expect(c)
})
})

describe('getThemeDir', () => {
test('returns src/ for default config', () => {
const { argv } = setArgs(yargs)
//@ts-ignore
const config = getBaseConfig(argv, {})

expect(getThemeDir(config)).toBe(path.join(config.root, '/src'))
})

test('returns correct path for custom themesDir entry', () => {
const { argv } = setArgs(yargs)
//@ts-ignore
const config = getBaseConfig(argv, { themesDir: 'theme' })

expect(getThemeDir(config)).toBe(path.join(config.root, '/theme'))
})
})
16 changes: 15 additions & 1 deletion core/docz-core/src/bundler/machine/services/watch-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,28 @@ import * as paths from '../../../config/paths'
import { createWatcher } from '../../../states/config'
import { ServerMachineCtx as Context } from '../context'

const replaceThemeDir = (filepath: string, args: Config) => {
// Make the path to a given absolute`filepath` relative:
const relFilePath = path.relative(filepath, paths.getThemeDir(args))
// => '/gatsby-theme-docz/**/index.tsx'

// Prefix with `src`
const newRelFilePath = path.join('src', relFilePath)
// => 'src/gatsby-theme-docz/**/index.tsx'

// Finally make filepath absolute again
return path.resolve(paths.root, newRelFilePath)
// => '/Users/.../theme/a/docz-project/src/gatsby-theme-docz/**/index.tsx'
}

const watchGatsbyThemeFiles = (args: Config) => {
const watcher = createWatcher(
path.join(args.themesDir, 'gatsby-theme-**/**/*'),
args
)
const copy = (filepath: string) => {
const src = path.resolve(paths.root, filepath)
const dest = path.resolve(paths.docz, filepath)
const dest = path.resolve(paths.docz, replaceThemeDir(filepath, args))
fs.copySync(src, dest)
}
const remove = (filepath: string) => {
Expand Down
5 changes: 5 additions & 0 deletions core/docz-core/src/config/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export const getRootDir = (config: any) => {
return isDoczProject ? path.resolve(root, '../') : root
}

export const getThemeDir = (config: { themesDir: string }) => {
return path.join(getRootDir(config), config.themesDir)
}

export interface Paths {
root: string
templates: string
Expand All @@ -48,6 +52,7 @@ export interface Paths {

checkIsDoczProject: (config: any) => boolean
getRootDir: (config: any) => string
getThemeDir: (config: any) => string
getDist: (dest: string) => string
distPublic: (dest: string) => string

Expand Down

0 comments on commit 7418e55

Please sign in to comment.