From dae9f1bfa45bd8222d8769c821c0858f086dbbc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Del=C3=A9pine?= Date: Wed, 19 Apr 2017 10:47:59 +0200 Subject: [PATCH 1/2] added an item type --- README.md | 8 ++++++++ lib/directory-tree.js | 10 ++++++++-- test/fixture.js | 11 +++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) 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 2d7fbe1..49ba168 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 directoryTree (path, options, onEachFile) { const name = PATH.basename(path); @@ -19,9 +23,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); } @@ -32,6 +37,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; } catch(ex) { if (ex.code == "EACCES") //User does not have permissions, ignore directory @@ -43,4 +49,4 @@ function directoryTree (path, options, onEachFile) { return item; } -module.exports = directoryTree; +module.exports = directoryTree 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": "" } ], From 260af2a2d22a4b3cbcfbefdf8989cf29ed84d073 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Del=C3=A9pine?= Date: Mon, 24 Apr 2017 17:13:51 +0200 Subject: [PATCH 2/2] fix typo --- lib/directory-tree.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/directory-tree.js b/lib/directory-tree.js index be588c5..802a185 100644 --- a/lib/directory-tree.js +++ b/lib/directory-tree.js @@ -58,4 +58,4 @@ function directoryTree (path, options, onEachFile) { return item; } -module.exports = directoryTree +module.exports = directoryTree;