-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
115 lines (94 loc) · 3.44 KB
/
index.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
var fs = require('fs');
var output = {
filenames: [],
dirnames: []
};
/**
* I have used the object style for implementing this NodeJs module
* to have access to its private functions for mocking and test purposes.
*/
module.exports = {
/**
* pathContentRetriever - This fucntion does content retrieval of a path
*
* @param {string} path Valid path
* @return {object} An object contains the name of the files and directory of a path
*/
pathContentRetriever: function(path) {
var pathElements = fs.readdirSync(path);
output.dirnames.push(path);
return this.pathElementsIterator(path, pathElements)
.then((res) => {
return Promise.resolve(output);
})
.catch((error) => {
return Promise.reject(error);
})
},
/**
* pathChecker - Validity checker of a path
*
* @param {string} path description
* @return {boolean} true/false
*/
pathChecker: function(path) {
return fs.existsSync(path);
},
/**
* pathElementsIterator - Check path elements to detect the files and sub directories, then save them
*
* @param {string} path Valid path
* @param {array} pathElements Its element
* @return {promise} Always resolve because of checker validity function beforehand
*/
pathElementsIterator: function(path, pathElements) {
return new Promise((resolve, reject) => {
for (var i = 0; i < pathElements.length; i++) {
var currentElement = `${path}/${pathElements[i]}`;
if (this.typeChecker(currentElement) === 1) {
var subdir = currentElement;
output.dirnames.push(subdir);
var subdirElements = fs.readdirSync(subdir);
this.pathElementsIterator(subdir, subdirElements)
}
if (this.typeChecker(currentElement) === 2) {
var filename = currentElement;
output.filenames.push(filename);
}
if (this.typeChecker(currentElement) === 0)
reject(new Error('Unkown component!!'))
}
resolve();
})
},
/**
* typeChecker - Check the type of a path
*
* @param {string} path
* @return {integer} According to the path type, it will return 1,2,0 if the path is a sub directory, file and unknown respectively
*/
typeChecker: function(path) {
if (fs.lstatSync(path).isDirectory()) return 1;
else if (fs.lstatSync(path).isFile()) return 2;
else return 0;
},
// Main function to call all the components
run: function(path) {
if (!this.pathChecker(path))
return Promise.reject(new Error('The input path does not exist, Please try again with a valid path.'));
if (this.typeChecker(path) === 0)
return Promise.reject('The input path is not valid, Please try again with a valid path.');
if (this.typeChecker(path) === 2) {
output.filenames.push(path);
return output;
} else {
return this.pathContentRetriever(path)
.then((result) => {
return result
})
.catch((error) => {
return Promise.reject(error);
})
}
}
}