
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
to-path-tree
Advanced tools
Convert a file path array to a tree.
pnpm i to-path-tree
import { pathToTree } from 'to-path-tree'
const paths = [
'src/index.ts',
'src/a/index.ts',
'src/a/b/index.ts',
'src/a/b/d/index.ts',
'src/foo.ts',
'src/b/index.js',
'src/b/c/index.ts',
'a/index.ts',
'a/b.ts',
'a/b/index.js'
]
const tree = pathToTree(paths)
import { PathTreeBuilder } from 'to-path-tree'
const builder = new PathTreeBuilder()
builder.addPath('src/index.ts')
builder.addPath('src/a/index.ts')
builder.removePath('src/index.ts')
builder.getItems('src/a/') // get all file items under 'src/a/'
builder.getSubDirectories('src/') // get all subdirectories under 'src/'
The data structure of the tree is as follows:
export interface NodeItem<T> {
path: string
/**
* e.g. index
*/
filename: string
/**
* e.g. ts
*/
ext: string
/**
* e.g. index.ts
*/
file: string
data?: T
isEntry: boolean // the entry is index
parent: TreeNode<T>
}
export interface TreeNode<T> {
items: NodeItem<T>[]
name: string
/**
* This path is strict path, must start with sep, e.g. `/`
*/
path: string
/**
* Relative to parent path
*/
relativePath: string
/**
* Relative to root path, exclude sep
*/
relativePathName: string
subDirectory: {
[key: string]: TreeNode<T>
} | null
parent?: TreeNode<T>
}
You can use walkPathTree to traverse the tree.
For example:
import { pathToTree, walkPathTree } from 'to-path-tree'
const tree = pathToTree(input)
walkPathTree(tree, (node) => {
for (const item of node.items) {
// NOTE: item.path is strict path, must start with sep, e.g. `/`
if (item.path === '/src/a/b/d/index.ts') {
// do something...
}
}
})
MIT
FAQs
Convert a file path array to a tree.
We found that to-path-tree demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.