Skip to content

Commit

Permalink
Merge pull request #28 from GabrielDelepine/master
Browse files Browse the repository at this point in the history
added an item type
  • Loading branch information
mihneadb authored Apr 28, 2017
2 parents 74b3245 + 260af2a commit 29447e4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
Expand All @@ -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"
}
]
Expand Down
8 changes: 7 additions & 1 deletion lib/directory-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

const FS = require('fs');
const PATH = require('path');
const constants = {
DIRECTORY: 'directory',
FILE: 'file'
}

function safeReadDirSync (path) {
let dirData = {};
Expand Down Expand Up @@ -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);
}
Expand All @@ -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 ?
}
Expand Down
11 changes: 11 additions & 0 deletions test/fixture.js
Original file line number Diff line number Diff line change
@@ -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"
}
],
Expand All @@ -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"
}
],
Expand All @@ -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": ""
}
],
Expand Down

0 comments on commit 29447e4

Please sign in to comment.