-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfileSystem.js
69 lines (47 loc) · 1.34 KB
/
fileSystem.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'use strict';
// Dependencies
//
var async = require('async');
var fs = require('fs');
var osenv = require('osenv');
var path = require('path');
// Gets the user's home folder
//
// @return {String} The path of the home folder
//
function getUsersHomeFolder () {
return osenv.home();
}
// Determines the file type, and returns an object
// describing the file
//
function inspectAndDescribeFile (filePath, cb) {
var result = {file: path.basename(filePath), path: filePath, type: ''};
fs.stat(filePath, function (err, stat) {
if (err) { cb(err); }
if (stat.isFile()) { result.type = 'file'; }
if (stat.isDirectory()) {result.type = 'directory'; }
cb(err,result);
});
}
// Retrieves the files in the folder, and
// determines what they are
//
// @param dir {String} The directory we want to list files for
// @param cb {Function} The function that will receive the list of files
//
function getFilesInFolder (folderPath, cb) {
fs.readdir(folderPath, function (err, files) {
if (err) { cb(err); }
async.map(files, function (file, internalCb) {
var resolvedFilePath = path.resolve(folderPath,file);
inspectAndDescribeFile(resolvedFilePath, internalCb);
}, cb);
});
}
// Expose the functions as the public API
//
module.exports = {
getFilesInFolder : getFilesInFolder,
getUsersHomeFolder : getUsersHomeFolder
};