diff --git a/README.md b/README.md index 065e64d..42ebe39 100644 --- a/README.md +++ b/README.md @@ -57,21 +57,25 @@ photos "path": "photos", "name": "photos", "size": 600, + "type": "directory", "children": [ { "path": "photos/summer", "name": "summer", "size": 400, + "type": "directory", "children": [ { "path": "photos/summer/june", "name": "june", "size": 400, + "type": "directory", "children": [ { "path": "photos/summer/june/windsurf.jpg", "name": "windsurf.jpg", "size": 400, + "type": "file", "extension": ".jpg" } ] @@ -82,22 +86,26 @@ photos "path": "photos/winter", "name": "winter", "size": 200, + "type": "directory", "children": [ { "path": "photos/winter/january", "name": "january", "size": 200, + "type": "directory", "children": [ { "path": "photos/winter/january/ski.png", "name": "ski.png", "size": 100, + "type": "file", "extension": ".png" }, { "path": "photos/winter/january/snowboard.jpg", "name": "snowboard.jpg", "size": 100, + "type": "file", "extension": ".jpg" } ] diff --git a/lib/directory-tree.js b/lib/directory-tree.js index 4ac5e62..802a185 100644 --- a/lib/directory-tree.js +++ b/lib/directory-tree.js @@ -2,6 +2,10 @@ const FS = require('fs'); const PATH = require('path'); +const constants = { + DIRECTORY: 'directory', + FILE: 'file' +} function safeReadDirSync (path) { let dirData = {}; @@ -32,9 +36,10 @@ function directoryTree (path, options, onEachFile) { options.extensions.length && options.extensions.indexOf(ext) === -1) return null; - + item.size = stats.size; // File size in bytes item.extension = ext; + item.type = constants.FILE; if (onEachFile) { onEachFile(item, PATH); } @@ -46,6 +51,7 @@ function directoryTree (path, options, onEachFile) { .map(child => directoryTree(PATH.join(path, child), options, onEachFile)) .filter(e => !!e); item.size = item.children.reduce((prev, cur) => prev + cur.size, 0); + item.type = constants.DIRECTORY; } else { return null; // Or set item.size = 0 for devices, FIFO and sockets ? } diff --git a/test/fixture.js b/test/fixture.js index 1bfd973..f7e1100 100644 --- a/test/fixture.js +++ b/test/fixture.js @@ -1,37 +1,44 @@ tree = { "path": "./test/test_data", "name": "test_data", + "type": "directory", "children": [ { "path": "test/test_data/file_a.txt", "name": "file_a.txt", "size": 12, + "type": "file", "extension": ".txt" }, { "path": "test/test_data/file_b.txt", "name": "file_b.txt", "size": 3756, + "type": "file", "extension": ".txt" }, { "path": "test/test_data/some_dir", "name": "some_dir", + "type": "directory", "children": [ { "path": "test/test_data/some_dir/another_dir", "name": "another_dir", + "type": "directory", "children": [ { "path": "test/test_data/some_dir/another_dir/file_a.txt", "name": "file_a.txt", "size": 12, + "type": "file", "extension": ".txt" }, { "path": "test/test_data/some_dir/another_dir/file_b.txt", "name": "file_b.txt", "size": 3756, + "type": "file", "extension": ".txt" } ], @@ -41,12 +48,14 @@ tree = { "path": "test/test_data/some_dir/file_a.txt", "name": "file_a.txt", "size": 12, + "type": "file", "extension": ".txt" }, { "path": "test/test_data/some_dir/file_b.txt", "name": "file_b.txt", "size": 3756, + "type": "file", "extension": ".txt" } ], @@ -55,11 +64,13 @@ tree = { { "path": "test/test_data/some_dir_2", "name": "some_dir_2", + "type": "directory", "children": [ { "path": "test/test_data/some_dir_2/.gitkeep", "name": ".gitkeep", "size": 0, + "type": "file", "extension": "" } ],