-
-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy pathfile-helper.cjs
163 lines (141 loc) · 5.64 KB
/
file-helper.cjs
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
const path = require('path');
const fs = require('fs');
const matcher = require('./matcher.cjs');
function isFile(path, followSymlinks = true){
const stat = fs.lstatSync(path);
if(stat.isFile()) return true;
if(followSymlinks && stat.isSymbolicLink()){
return fs.lstatSync(fs.realpathSync(path)).isFile();
}
return false;
}
function isDirectory(path, followSymlinks = true){
const stat = fs.lstatSync(path);
if(stat.isDirectory()) return true;
if(followSymlinks && stat.isSymbolicLink()){
try {
const resolvedPath = fs.realpathSync(path);
return fs.lstatSync(resolvedPath).isDirectory();
} catch (error) {
console.error(error);
return false;
}
}
return false;
}
/**
* Returns all subdirectories (non-recursive) of a given path
* which don't match an exclude pattern
* @param {String} startPath
* @param {String[]} excludePatterns
* @param {boolean} followSymlinks Defines if symlinks should be considered
* @returns a string array containing the full paths of the subdirectories
*/
function getSubdirectories(startPath, excludePatterns = [], followSymlinks = true){
if (!fs.existsSync(startPath)) {
console.log("💣 Directory doesn't exist:", startPath);
return;
}
var files = fs.readdirSync(startPath);
let directories = [];
files.forEach(file => {
var fullPath = path.join(startPath, file);
if (matcher.matchAny(fullPath, excludePatterns)) {
return;
}
if (isDirectory(fullPath, followSymlinks)) {
directories.push(fullPath);
}
})
return directories;
}
/**
* Returns all file names in a path (non-recursive) with a given file extension.
* This function only returns the file names without path info.
* @param {*} path
* @param {*} extension
* @returns a string array containing the file names
*/
function getFilesWithExtension(path, extension){
return fs.readdirSync(path).filter(aFile => aFile.endsWith('.' + extension));
}
/**
* Returns a list of file paths based on the start path
* @param {*} startPath
* @param {*} searchPattern
* @param {*} excludePatterns
* @param {boolean} followSymlinks Defines if symlinks should be considered
* @param {*} matchingFiles
* @returns
*/
function findAllFilesAndFolders(startPath, searchPattern = null, excludePatterns = [], followSymlinks = true, matchingFiles = []) {
if(matcher.matchAny(startPath, excludePatterns)){
// console.log("Excluding directory " + startPath);
return matchingFiles;
}
// console.log('Starting from dir ' + startPath + '/');
if (!fs.existsSync(startPath)) {
console.log("💣 Directory doesn't exist:", startPath, " search pattern: ", searchPattern);
return null;
}
var files = fs.readdirSync(startPath);
for (let file of files) {
var filePath = path.join(startPath, file);
if (!matcher.matchAny(filePath, excludePatterns)) {
if(!searchPattern) {
matchingFiles.push(filePath);
continue;
}
let patterns = Array.isArray(searchPattern) ? searchPattern : [searchPattern];
patterns.forEach(pattern => {
if(filePath.indexOf(pattern) >= 0){
// console.log('-- found: ', filename);
matchingFiles.push(filePath);
}
});
if (isDirectory(filePath, followSymlinks)) {
findAllFiles(filePath, searchPattern, excludePatterns, followSymlinks, matchingFiles);
}
};
};
return matchingFiles;
}
function findAllFolders(startPath, searchPattern, excludePatterns = [], followSymlinks = true, matchingFiles = []) {
return findAllFilesAndFolders(startPath, searchPattern, excludePatterns, followSymlinks, matchingFiles)?.filter(file => {
if(!isDirectory(file, followSymlinks)) return false;
const lastPathComponent = file.substring(file.lastIndexOf('/') + 1);
return matcher.matchAny(lastPathComponent, [searchPattern]);
});
}
/**
*
* @param {String} startPath The directory from which to start a recursive search
* @param {String} searchPattern The file name that should be looked for
* @param {String[]} excludePatterns An array of paths that should be excluded from the search
* @param {boolean} followSymlinks Defines if symlinks should be considered
* @param {String[]} matchingFiles The matching files as recursion parameter
*/
function findAllFiles(startPath, searchPattern, excludePatterns = [], followSymlinks = true, matchingFiles = []) {
return findAllFilesAndFolders(startPath, searchPattern, excludePatterns, followSymlinks, matchingFiles)?.filter(file => {
return isFile(file, followSymlinks);
});
};
function createDirectoryIfNecessary(path){
if(!fs.existsSync(path)){
fs.mkdirSync(path, { recursive: true });
}
}
function getLineNumberFromIndex(index, haystack){
const tempString = haystack.substring(0, index);
const lineNumber = tempString.split('\n').length;
return lineNumber;
}
function getColumnFromIndex(index, haystack){
const tempString = haystack.substring(0, index);
const lines = tempString.split('\n');
lines.pop();
const indexOfLastLine = lines.length > 0 ? lines.join('\n').length + 1 : 0;
const column = (index - indexOfLastLine) + 1;
return column;
}
module.exports = { isFile, isDirectory, findAllFiles, findAllFolders, getFilesWithExtension, getSubdirectories, createDirectoryIfNecessary, getLineNumberFromIndex, getColumnFromIndex};