{"name":"directory-tree","version":"3.6.0","description":"Convert a directory tree to a JS object.","repository":{"type":"git","url":"git+https://github.com/mihneadb/node-directory-tree.git"},"keywords":["directory","tree"],"main":"lib/directory-tree.js","author":{"name":"Mihnea Dobrescu-Balaur"},"license":"MIT","bugs":{"url":"https://github.com/mihneadb/node-directory-tree/issues"},"homepage":"https://github.com/mihneadb/node-directory-tree","dependencies":{"command-line-args":"^5.2.0","command-line-usage":"^6.1.1"},"devDependencies":{"@types/node":"^18.11.17","chai":"^2.3.0","mocha":"^10.2.0","typescript":"^4.9.4"},"bin":{"directory-tree":"bin/index.js"},"engines":{"node":">=10.0"},"scripts":{"test":"mocha","test-ts":"tsc index.d.ts"},"gitHead":"971579287931168a2c5a2ba85b85c5ef405fd951","_id":"directory-tree@3.6.0","_nodeVersion":"24.12.0","_npmVersion":"11.6.2","dist":{"integrity":"sha512-rTMWs+zxr0QEbzQKRfwV6SeEy+zIHFkorskI4bhG2o7ayr82c+FC7yWg3yLpurgp6Hs2NGy1NWrKIaDodr2r8A==","shasum":"f8530bff482c8c420e01874aea3bb95744f5a5e4","tarball":"http://registry-npm.ywwl.com/directory-tree/download/directory-tree-3.6.0.tgz","fileCount":8,"unpackedSize":28979,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQCPs8kfgZzAs283pLrjo4+JbP83sjyZ8uDm34vMHTJndgIgYsmJhoR6QmX0u/S+s5e08r2BJrMt/tvwnRLiTQ0/oww="}],"size":9998,"noattachment":false,"key":"/directory-tree/-/directory-tree-3.6.0.tgz"},"_npmUser":{"name":"mihneadb","email":"mihnea@linux.com"},"directories":{},"maintainers":[{"name":"mihneadb","email":"mihnea@linux.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/directory-tree_3.6.0_1769254882418_0.46731928497666053"},"_hasShrinkwrap":false,"readme":"# directory-tree\n\nCreates a JavaScript object representing a directory tree.\n\nThis library gets [~100k downloads per week](http://npm-stats.org/#/directory-tree). If you find it useful, feel free to <a href=\"https://www.buymeacoffee.com/pxhxnsxsbn\"><img height=\"40\" src=\"https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png\" /></a> .\n\n## Install\n\n```bash\n$ npm install directory-tree\n```\n\n## Usage\n\n```js\nconst dirTree = require(\"directory-tree\");\nconst tree = dirTree(\"/some/path\");\n```\n\nAnd you can also filter by an extensions regex:\nThis is useful for including only certain types of files.\n\n```js\nconst dirTree = require(\"directory-tree\");\nconst filteredTree = dirTree(\"/some/path\", { extensions: /\\.txt/ });\n```\n\nExample for filtering multiple extensions with Regex.\n\n```js\nconst dirTree = require(\"directory-tree\");\nconst filteredTree = dirTree(\"/some/path\", {\n  extensions: /\\.(md|js|html|java|py|rb)$/\n});\n```\n\nYou can also exclude paths from the tree using a regex:\n\n```js\nconst dirTree = require(\"directory-tree\");\nconst filteredTree = dirTree(\"/some/path\", { exclude: /some_path_to_exclude/ });\n```\n\nYou can also specify which additional attributes you would like to be included about each file/directory:\n\n```js\nconst dirTree = require('directory-tree');\nconst filteredTree = dirTree('/some/path', {attributes:['mode', 'mtime']});\n```\n\nThe default attributes are `[name, path]` for Files and `[name, path, children]` for Directories\n\nA callback function can be executed with each file that matches the extensions provided:\n\n```js\nconst PATH = require('path');\nconst dirTree = require('directory-tree');\n\nconst tree = dirTree('./test/test_data', {extensions:/\\.txt$/}, (item, PATH, stats) => {\n  console.log(item);\n});\n```\n\nThe callback function takes the directory item (has path, name, size, and extension) and an instance of [node path](https://nodejs.org/api/path.html) and an instance of [node FS.stats](https://nodejs.org/api/fs.html#fs_class_fs_stats).\n\nYou can also pass a callback function for directories:\n```js\nconst PATH = require('path');\nconst dirTree = require('directory-tree');\n\nconst tree = dirTree('./test/test_data', {extensions:/\\.txt$/}, null, (item, PATH, stats) => {\n  console.log(item);\n});\n```\n\n## Options\n\n`exclude` : `RegExp|RegExp[]` - A RegExp or an array of RegExp to test for exclusion of directories.\n\n`extensions` : `RegExp` - A RegExp to test for exclusion of files with the matching extension.\n\n`attributes` : `string[]` - Array of [FS.stats](https://nodejs.org/api/fs.html#fs_class_fs_stats) attributes.\n\n`normalizePath` : `Boolean` - If true, Windows style paths will be normalized to UNIX style paths (/ instead of \\\\).\n\n`depth` : `number` - Limits directory traversal to the specified depth level. When combined with `size`, directories at the depth limit have `size: undefined` (see \"Using size with depth\" below).\n\n## Result\n\nGiven a directory structured like this:\n\n```\nphotos\n├── summer\n│   └── june\n│       └── windsurf.jpg\n└── winter\n    └── january\n        ├── ski.png\n        └── snowboard.jpg\n```\n\n`directory-tree` with `attributes: [\"size\", \"type\", \"extension\"]`  will return this JS object:\n\n```json\n{\n  \"path\": \"photos\",\n  \"name\": \"photos\",\n  \"size\": 600,\n  \"type\": \"directory\",\n  \"children\": [\n    {\n      \"path\": \"photos/summer\",\n      \"name\": \"summer\",\n      \"size\": 400,\n      \"type\": \"directory\",\n      \"children\": [\n        {\n          \"path\": \"photos/summer/june\",\n          \"name\": \"june\",\n          \"size\": 400,\n          \"type\": \"directory\",\n          \"children\": [\n            {\n              \"path\": \"photos/summer/june/windsurf.jpg\",\n              \"name\": \"windsurf.jpg\",\n              \"size\": 400,\n              \"type\": \"file\",\n              \"extension\": \".jpg\"\n            }\n          ]\n        }\n      ]\n    },\n    {\n      \"path\": \"photos/winter\",\n      \"name\": \"winter\",\n      \"size\": 200,\n      \"type\": \"directory\",\n      \"children\": [\n        {\n          \"path\": \"photos/winter/january\",\n          \"name\": \"january\",\n          \"size\": 200,\n          \"type\": \"directory\",\n          \"children\": [\n            {\n              \"path\": \"photos/winter/january/ski.png\",\n              \"name\": \"ski.png\",\n              \"size\": 100,\n              \"type\": \"file\",\n              \"extension\": \".png\"\n            },\n            {\n              \"path\": \"photos/winter/january/snowboard.jpg\",\n              \"name\": \"snowboard.jpg\",\n              \"size\": 100,\n              \"type\": \"file\",\n              \"extension\": \".jpg\"\n            }\n          ]\n        }\n      ]\n    }\n  ]\n}\n```\n\n## Using size with depth\n\nThe `size` attribute can be used together with the `depth` option, but with an important caveat:\n\n**When depth limiting is active:**\n- **Files** always have accurate sizes\n- **Directories at the depth limit** (whose children are not traversed) will have `size: undefined`\n- **Parent directories** containing depth-limited directories will also have `size: undefined`\n\nThis is because directory sizes are calculated by recursively summing all child sizes. When depth limits prevent full traversal, the size would be incomplete and potentially misleading, so `undefined` is returned instead.\n\n**Example:**\n\n```js\nconst dirTree = require('directory-tree');\nconst tree = dirTree('/some/path', {\n  depth: 1,\n  attributes: ['size', 'type']\n});\n```\n\nGiven this structure:\n```\nfolder/\n├── file1.txt (100 bytes)\n├── file2.txt (200 bytes)\n└── subfolder/\n    └── file3.txt (300 bytes)\n```\n\nThe result will be:\n```js\n{\n  \"path\": \"folder\",\n  \"name\": \"folder\",\n  \"size\": undefined,\n  \"type\": \"directory\",\n  \"children\": [\n    {\n      \"path\": \"folder/file1.txt\",\n      \"name\": \"file1.txt\",\n      \"size\": 100,\n      \"type\": \"file\"\n    },\n    {\n      \"path\": \"folder/file2.txt\",\n      \"name\": \"file2.txt\",\n      \"size\": 200,\n      \"type\": \"file\"\n    },\n    {\n      \"path\": \"folder/subfolder\",\n      \"name\": \"subfolder\",\n      \"size\": undefined,\n      \"type\": \"directory\"\n    }\n  ]\n}\n```\n\n**Note:** When serializing to JSON (e.g., `JSON.stringify(tree)`), `undefined` values are omitted. Properties with `undefined` will not appear in the JSON string.\n\n## Adding custom fields\nYou can easily extend a `DirectoryTree` object with custom fields by adding them to the custom field.\nFor example add an `id` based on the path of a `DirectoryTree` object for each directory and file like so:\n```\nimport { createHash } from 'crypto';\nimport * as directoryTree from 'directory-tree';\nimport { DirectoryTree, DirectoryTreeOptions, DirectoryTreeCallback } from 'directory-tree';\n\nconst callback: DirectoryTreeCallback = (\n            item: DirectoryTree,\n            path: string\n        ) => {\n            item.custom = {id: createHash('sha1').update(path).digest('base64')};\n        };\n\nconst dirTree: DirectoryTree & { id?: string } = directoryTree(\n    \"<your-directory-path>\",\n    {},\n    callback,\n    callback\n);\n\n// to explore the object with the new custom fields\nconsole.log(JSON.stringify(dirTree, null, 2));\n\n```\n\n## Note\n\nDevice, FIFO and socket files are ignored.\n\nFiles to which the user does not have permissions are included in the directory\ntree, however, directories to which the user does not have permissions, along\nwith all of its contained files, are completely ignored.\n\n## Dev\n\nTo run tests go the package root in your CLI and run,\n\n```bash\n$ npm test\n```\n\nMake sure you have the dev dependencies installed (e.g. `npm install .`)\n\n## CLI usage\n\nYou can use script directly from command line for generating json data.\n\n```bash\n$ npx directory-tree --help\n```\n```bash\n$ npx directory-tree --path /Users/user/target --attributes type,extension --pretty -o ./xz.json --depth 1 \n```\n\n### Available options\n-p, --path string      🗂 The input folder to process. Required.                                     \n-e, --exclude string   🐒 Exclude some folders from processing by regexp string. Ex -e \"test_data/some_dir$|js|.DS_Store\"                                            \n-o, --output string    📝 Put result into file provided by this options. Overwrites if exists.       \n-d, --depth number     ☞ Limits directory traversal to specified depth. Combined with size, directories at depth limit have size: undefined.\n--attributes string    ℹ️ Grab file attributes. Example: --attributes size,type,extension. With depth, directories at limit have size: undefined.                                \n--pretty               💎 Json pretty print\n","publish_time":1769254882569,"_cnpm_publish_time":1769254882569,"dist-tags":{"latest":"3.6.0"}}