This repository has been archived by the owner on Aug 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
finder.js
149 lines (99 loc) · 2.71 KB
/
finder.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'use strict';
const finder = {}
const fs = require( 'fs' )
const path = require('path')
/**
* Checks whether the file path is a regular file
*
* @param {String} filePath File name
* @param {String} fileExt File extension
* @return {Boolean} True if a valid file
*/
finder.isFile = ( filePath, fileExt ) => {
if ( ! fs.statSync( finder.absolutePath( filePath ) ).isFile() ) {
return false;
}
if ( fileExt ) {
return path.extname( filePath ) === fileExt;
}
return true;
}
/**
* Checks whether the dirpath is a regular directory
*
* @param {String} dirPath Directory name
* @return {Boolean} True if a valid directory
*/
finder.isDir = ( dirPath ) => {
return fs.statSync( finder.absolutePath( dirPath ) ).isDirectory();
}
/**
* Reads and returns the content of a file
*
* @param {String} filePath File name
* @return {Mixed} File content
*/
finder.readFile = ( filePath ) => {
return fs.readFileSync( finder.absolutePath( filePath ) );
}
/**
* Reads the content of a directory
*
* @param {String} dirPath Directory name
* @param {String} callback Callback function
* @return {Object} Content object
*/
finder.readDir = ( dirPath, callback ) => {
fs.readdir( finder.absolutePath( dirPath ), callback );
}
/**
* Reads and returns content lines of a file
*
* @param {String} filePath File name
* @return {Array} Content lines
*/
finder.readFileLines = ( filePath ) => {
return finder.readFile( filePath ).toString().split( "\n" ).filter( Boolean )
}
/**
* Loads a module from a JavaScript file
*
* @param {String} filePath File name
* @return {Object} module.exports from the resolved module
*/
finder.requireFile = ( filePath ) => {
return require( finder.absolutePath( filePath ) );
}
/**
* Checks if a file is blacklisted
*
* @param {String} filePath File name
* @param {Array} blacklist Blacklisted items
* @return {Boolean} True if the file is blacklisted
*/
finder.isBlacklistedFile = ( filePath, blacklist ) => {
return blacklist.includes( path.basename( filePath ) );
}
/**
* Makes a path absolute
*
* @param {String} objPath Object path
* @return {String} Absolute path
*/
finder.absolutePath = ( objPath ) => {
if ( path.isAbsolute( objPath ) ) {
return objPath;
}
return path.join( __dirname, '..', objPath );
}
/**
* Join two paths
*
* @param {String} path1 First path
* @param {String} path2 Second path
* @return {String} Joined paths
*/
finder.joinPaths = ( path1, path2 ) => {
return path.join( path1, path2 );
}
module.exports = finder;