Skip to content

Commit 6804deb

Browse files
committed
Make root path an init option
1 parent a6b3b9b commit 6804deb

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ set options to use in analysis (this step is optional)
1717
- type: Number
1818
- default: `null`
1919
- description: Limit number of files to output analysis of, sorted by DESC size
20+
- **root** - *optional*
21+
- type: String
22+
- default: `process.cwd()`
23+
- description: Application directory, used to display file paths relatively
2024

2125
### formatted(bundle)
2226
returns Promise which resolves with well formatted analysis string (for CLI printing)

index-es6.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
const buf = ' '
55
const tab = ' '
66
const borderX = `${Array(30).join('-')}\n`
7+
let proc = process
8+
let root = proc && proc.cwd ? proc.cwd() : null
79
let limit
810

911
// Helpers
@@ -23,6 +25,7 @@ export {init, analyze, formatted}
2325
function init (opts) {
2426
opts = opts || {}
2527
limit = opts.limit
28+
root = opts.root || root
2629
}
2730

2831
function formatted (bndl) { return analyze(bndl, true) }
@@ -31,9 +34,9 @@ function analyze (bundle, format) {
3134
let deps = {}
3235
return new Promise((resolve, reject) => {
3336
let modules = bundle.modules.map((m, i) => {
34-
let id = m.id.replace(__dirname, '')
37+
let id = m.id.replace(root, '')
3538
m.dependencies.forEach((d) => {
36-
d = d.replace(__dirname, '')
39+
d = d.replace(root, '')
3740
deps[d] = deps[d] || []
3841
deps[d].push(id)
3942
})
@@ -50,7 +53,7 @@ function analyze (bundle, format) {
5053
formatted += `size:${buf}${formatBytes(m.size)}\n`
5154
formatted += `dependents:${buf}${m.dependents.length}\n`
5255
m.dependents.forEach((d) => {
53-
formatted += `${tab}-${buf}${d.replace(__dirname, '')}\n`
56+
formatted += `${tab}-${buf}${d.replace(root, '')}\n`
5457
})
5558
formatted += `${borderX}`
5659
})

0 commit comments

Comments
 (0)