Skip to content

Commit

Permalink
fix: support nested hierarchies
Browse files Browse the repository at this point in the history
  • Loading branch information
bbmoz committed Apr 23, 2017
1 parent 1c96bd4 commit 8a0c7ca
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 19 deletions.
11 changes: 4 additions & 7 deletions client/src/GraphWs.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,17 @@ class GraphWs {
nodes.length = 0
edges.length = 0

Object.keys(data).forEach(modulePath => {
const splitPath = modulePath.split('/')
const id = splitPath[splitPath.length - 1].split('.js')[0]

Object.keys(data).forEach(id => {
nodes.push({ data: { id } })

const modules = data[modulePath]
const modules = data[id]
const { imports, exports } = modules

Object.keys(imports).forEach(importEntry => {
Object.keys(imports).forEach(importId => {
edges.push({
data: {
source: id,
target: importEntry.slice(importEntry.indexOf('/') + 1),
target: importId,
label: { imports, exports }
}
})
Expand Down
6 changes: 4 additions & 2 deletions example/b.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const b1 = '2'
const b2 = '3'
import d from './nest/d'

const b1 = 2 + d
const b2 = 3 + d

export { b1, b2 }
4 changes: 2 additions & 2 deletions example/entry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import b from './b'
import c from './c'

const d = b + c
const e = b + c

export default d
export default e
5 changes: 5 additions & 0 deletions example/nest/d.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import a from './../a'

const d = 4 + a;

export default d
16 changes: 12 additions & 4 deletions lib/src/AstParser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const resolve = require('path').resolve
// $FlowFixMe
const esprima = require('esprima')
const { stripCwd } = require('./common')

class AstParser {
parser: any
Expand All @@ -11,20 +13,21 @@ class AstParser {
this.sourceType = 'module'
}

parse (text: string) {
parse (filePath: string, text: string) {
const ast: Array<any> = this.parser.parse(text, { sourceType: this.sourceType }).body
return {
imports: extractImports(ast),
imports: extractImports(filePath, ast),
exports: extractExports(ast)
}
}
}

function extractImports (ast: Array<any>) {
function extractImports (filePath: string, ast: Array<any>) {
const imports = {}
ast.filter(isImport)
.forEach(importAst => {
imports[importAst.source.value] = importAst.specifiers[0].local.name
const sourcePath = resolvePath(filePath, importAst.source.value)
imports[sourcePath] = importAst.specifiers[0].local.name
})
return imports
}
Expand All @@ -51,6 +54,11 @@ function extractExports (ast: Array<any>) {
return exports
}

function resolvePath (filePath: string, source: string) {
const stripFilePath = filePath.substr(0, filePath.lastIndexOf('/'))
return stripCwd(`${resolve(stripFilePath, source)}.js`)
}

function isImport (statement: any) {
return statement.type === 'ImportDeclaration'
}
Expand Down
5 changes: 3 additions & 2 deletions lib/src/FileReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs')
// $FlowFixMe
const glob = require('glob-fs')
const AstParser = require('./AstParser')
const { stripCwd } = require('./common')

class FileReader {
globber: any
Expand All @@ -23,8 +24,8 @@ class FileReader {
const projectModules = {}
filePaths.forEach(filePath => {
const text = this.reader.readFileSync(filePath, 'utf-8')
const modules = this.astParser.parse(text)
projectModules[filePath] = modules
const modules = this.astParser.parse(filePath, text)
projectModules[stripCwd(filePath)] = modules
})
resolve(projectModules)
})
Expand Down
7 changes: 7 additions & 0 deletions lib/src/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function stripCwd (path: string) {
return path.slice(process.cwd().length + 1)
}

export {
stripCwd
}
3 changes: 1 addition & 2 deletions lib/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ const logger = require('./../shared/logger')

const argv = yargs
.usage('Usage: gochu [options]')
.example(`gochu -t 'app/*.js'`, 'generate graph from js files in the app dir')
.example(`gochu -t 'example/**/*.js'`, 'generate graph from js files in the example dir')
.demandOption(['t'])
.describe('t', 'Specify glob pattern in quotes')
.alias('t', 'target')
.nargs('t', 1)
.default('t', 'example/*.js')
.help('h')
.argv

Expand Down

0 comments on commit 8a0c7ca

Please sign in to comment.