Skip to content

Commit

Permalink
fix(docz-core): throw error when parse ast
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Apr 17, 2018
1 parent ad9ee58 commit 05138bf
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions packages/docz-core/src/Entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ import { parse } from 'babylon'
import * as paths from './config/paths'
import { traverseAndAssign } from './utils/traverse'

const convertToAst = (entry: string): File =>
parse(fs.readFileSync(entry, 'utf-8'), {
plugins: ['jsx'],
sourceType: 'module',
})
const convertToAst = (entry: string): File | null => {
try {
return parse(fs.readFileSync(entry, 'utf-8'), {
plugins: ['jsx'],
sourceType: 'module',
})
} catch (err) {
console.log(err)
return null
}
}

const getNameFromDoc = traverseAndAssign<any, string>({
assign: p => p.node.arguments[0].value,
Expand Down Expand Up @@ -43,21 +49,22 @@ export interface EntryConstructor {
}

export class Entry {
public static parseName(file: string): string | undefined {
public static parseName(file: string): string | undefined | null {
const ast = convertToAst(file)
return getNameFromDoc(ast)
return ast && getNameFromDoc(ast)
}

public static check(entry: string): boolean | undefined {
return checkImport(convertToAst(entry))
public static check(entry: string): boolean | undefined | null {
const ast = convertToAst(entry)
return ast && checkImport(ast)
}

public name: string
public name: string | undefined
public filepath: string

constructor({ src, file }: EntryConstructor) {
const ast = convertToAst(file)
const name = getNameFromDoc(ast) || ''
const name = ast ? getNameFromDoc(ast) : ''
const filepath = path.relative(paths.root, file)

this.name = name
Expand Down

0 comments on commit 05138bf

Please sign in to comment.