Skip to content

Commit

Permalink
fix(docz-core): docgen if file has not component definition
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Feb 22, 2019
1 parent 560dd8d commit b0fb846
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 33 deletions.
17 changes: 11 additions & 6 deletions core/docz-core/src/states/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import * as paths from '../config/paths'
import { Config } from '../config/argv'
import { docgen } from '../utils/docgen'

const getPattern = (ts: boolean) => {
const tsPattern = '**/*.{ts,tsx}'
const jsPattern = '**/*.{js,jsx,mjs}'
return [ts ? tsPattern : jsPattern, '!**/node_modules']
const getPattern = (config: Config) => {
const { typescript } = config
return [
typescript ? '**/*.{ts,tsx}' : '**/*.{js,jsx,mjs}',
'!**/node_modules',
'!**/doczrc.js',
]
}

export const mapToArray = (map: any = []) =>
Expand All @@ -21,9 +24,11 @@ export const mapToArray = (map: any = []) =>
.filter(Boolean)

const initial = (config: Config) => async (p: Params) => {
const pattern = getPattern(config.typescript)
const pattern = getPattern(config)
const files = await fastglob<string>(pattern, { cwd: paths.root })
console.time('initial')
const metadata = await docgen(files, config)
console.timeEnd('initial')
p.setState('props', flatten(mapToArray(metadata)))
}

Expand All @@ -44,7 +49,7 @@ const remove = (p: Params) => async (filepath: string) => {
}

export const state = (config: Config): State => {
const pattern = getPattern(config.typescript)
const pattern = getPattern(config)
const watcher = chokidar.watch(pattern, {
cwd: paths.root,
ignored: /(((^|[\/\\])\..+)|(node_modules))/,
Expand Down
65 changes: 38 additions & 27 deletions core/docz-core/src/utils/docgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,50 +51,61 @@ const tsDocgen = (config: Config, tsconfig: string, program: ts.Program) => (
}
})

const tsParser = async (files: string[], config: Config) => {
const tsConfigPath = await findUp('tsconfig.json', { cwd: paths.root })
if (!tsConfigPath) return {}
const program = tsProgram(files)
const parse = tsDocgen(config, tsConfigPath, program)
try {
const operations = await Promise.all(
files.map(async filepath => ({
[fileFullPath(filepath)]: await parse(filepath),
}))
)
const tsParser = async (
filepath: string,
config: Config,
tsconfig?: string,
program?: ts.Program | null
) => {
if (!program || !tsconfig) return

const parse = tsDocgen(config, tsconfig, program)
const fullpath = fileFullPath(filepath)

return operations.reduce((obj, val) => ({ ...obj, ...val }), {})
try {
const data = await parse(filepath)
return { [fullpath]: data }
} catch (err) {
if (config.debug) throwError(err)
return {}
return null
}
}

const jsParser = (files: string[], config: Config) => {
const jsParser = async (filepath: string, config: Config) => {
const resolver =
config.docgenConfig.resolver ||
reactDocgen.resolver.findAllExportedComponentDefinitions

return files.reduce((memo: any, filepath) => {
const handlers = reactDocgen.defaultHandlers.concat([
externalProptypesHandler(filepath),
// importedProptypesHandler(filepath),
actualNameHandler,
])

const code = fs.readFileSync(filepath, 'utf-8')
const handlers = reactDocgen.defaultHandlers.concat([
externalProptypesHandler(filepath),
// importedProptypesHandler(filepath),
actualNameHandler,
])

return new Promise<any>(resolve => {
try {
const code = fs.readFileSync(filepath, 'utf-8')
const data = reactDocgen.parse(code, resolver, handlers)
memo[fileFullPath(filepath)] = data
const fullpath = fileFullPath(filepath)
resolve({ [fullpath]: data })
} catch (err) {
if (config.debug) throwError(err)
resolve(null)
}

return memo
}, {})
})
}

export const docgen = async (files: string[], config: Config) => {
config.typescript ? tsParser(files, config) : jsParser(files, config)
const ts = config.typescript
const program = ts ? tsProgram(files) : null
const tsconfig = await findUp('tsconfig.json', { cwd: paths.root })
const docs = await Promise.all(
files.map(async filepath => {
return ts
? tsParser(filepath, config, tsconfig, program)
: jsParser(filepath, config)
})
)

return docs.reduce((obj, doc) => (doc ? { ...obj, ...doc } : obj), {})
}

0 comments on commit b0fb846

Please sign in to comment.