-
Notifications
You must be signed in to change notification settings - Fork 19
/
utils.js
198 lines (134 loc) · 4.58 KB
/
utils.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* @author [Tristan Valcke]{@link https://github.com/Itee}
* @license [BSD-3-Clause]{@link https://opensource.org/licenses/BSD-3-Clause}
*
* @file Todo
*
* @example Todo
*
*/
const fs = require( 'fs' )
const path = require( 'path' )
function fileExistForPath ( filePath ) {
return fs.existsSync( filePath )
}
function getFileForPath ( filePath ) {
// In case files doesn't exist
if ( !fileExistForPath( filePath ) ) {
throw new Error( 'Invalid file path "' + filePath + '" file does not exist !' )
}
return fs.readFileSync( filePath, 'utf8' )
}
function getUncommentedFileForPath ( filePath ) {
return getFileForPath( filePath ).replace( /\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/g, '$1' )
}
/**
* Return all the files paths under filePaths in a recursive way.
*
* @param filePaths - An array of string, representing the base path where looking for get all files paths
* @return {Array.<string>} - An array of files paths
* @private
*/
function getFilesPathsUnder ( filePaths ) {
let files = []
if ( Array.isArray( filePaths ) ) {
let filePath = undefined
for ( let pathIndex = 0, numberOfPaths = filePaths.length ; pathIndex < numberOfPaths ; pathIndex++ ) {
filePath = filePaths[ pathIndex ]
checkStateOf( filePath )
}
} else {
checkStateOf( filePaths )
}
return files
function getFilesPathsUnderFolder ( folder ) {
fs.readdirSync( folder ).forEach( ( name ) => {
const filePath = path.resolve( folder, name )
checkStateOf( filePath )
} )
}
function checkStateOf ( filePath ) {
if ( !fileExistForPath( filePath ) ) {
console.error( 'ES6Converter: Invalid file path "' + filePath + '"' )
return
}
const stats = fs.statSync( filePath )
if ( stats.isFile() ) {
files.push( filePath )
} else if ( stats.isDirectory() ) {
Array.prototype.push.apply( files, getFilesPathsUnderFolder( filePath ) )
} else {
console.error( 'Invalid stat object !' )
}
}
}
/**
* Will create an array without the strings in filePaths that are matched in excludes paths
*
* @param {Array.<string>} filePaths - An array of string to clean
* @param {Array.<string>} excludes - The paths to remove
* @return {Array.<string>} The cleaned filePaths of excludes paths
* @private
*/
function excludesFilesPaths ( filePaths, excludes ) {
let filteredFilesPath = []
let filePath = undefined
for ( let filePathIndex = 0, numberOfFilePaths = filePaths.length ; filePathIndex < numberOfFilePaths ; filePathIndex++ ) {
filePath = filePaths[ filePathIndex ]
if ( isExclude( filePath ) ) {
continue
}
filteredFilesPath.push( filePath )
}
return filteredFilesPath
function isExclude ( path ) {
let isExclude = false
let excludePattern = undefined
for ( let i = 0, pathLength = excludes.length ; i < pathLength ; i++ ) {
excludePattern = excludes[ i ]
// In case this is a file name it must fully match
if ( excludePattern.indexOf( '.' ) > -1 ) {
const fileName = path.replace( /^.*(\\|\/|\:)/, '' )
if ( fileName === excludePattern ) {
isExclude = true
}
} else if ( path.contains( excludePattern ) ) {
isExclude = true
}
}
return isExclude
}
}
/**
* Will filter file paths an keep only js files
*
* @param {Array.<string>} filePaths - An array of path to filter
* @return {Array.<string>} The filtered path with only javascript files
* @private
*/
function filterJavascriptFiles ( filePaths, filter ) {
let filteredFilesPath = []
let filePath = undefined
for ( let filePathIndex = 0, numberOfFilePaths = filePaths.length ; filePathIndex < numberOfFilePaths ; filePathIndex++ ) {
filePath = filePaths[ filePathIndex ]
// Not a js file like fonts or shaders
if ( filter && !filter( filePath ) ) {
continue
} else {
const fileExtension = path.extname( filePath )
if ( filePath.indexOf( 'glsl' ) > -1 || fileExtension !== '.js' ) {
continue
}
}
filteredFilesPath.push( filePath )
}
return filteredFilesPath
}
module.exports = {
fileExistForPath,
getFileForPath,
getFilesPathsUnder,
getUncommentedFileForPath,
excludesFilesPaths,
filterJavascriptFiles
}