-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
127 lines (107 loc) · 3.6 KB
/
index.mjs
File metadata and controls
127 lines (107 loc) · 3.6 KB
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
import { readdir, stat } from 'fs/promises';
import { readdirSync, statSync } from 'fs';
/**
* Check if a directory is empty (async)
* @param {string} dirPath - Path to directory to check
* @param {Object} [options] - Options object
* @param {Array<string|RegExp|Function>} [options.ignore] - Patterns to ignore
* @param {boolean} [options.followSymlinks] - Whether to follow symbolic links
* @returns {Promise<boolean>} - True if directory is empty or only contains ignored files
*/
async function isEmptyDir(dirPath, options = {}) {
const { ignore = [], followSymlinks = false } = options;
if (typeof dirPath !== "string") {
throw new TypeError("Expected dirPath to be a string");
}
try {
const stats = await stat(dirPath);
if (!stats.isDirectory()) {
throw new Error(`Path is not a directory: ${dirPath}`);
}
const files = await readdir(dirPath, { withFileTypes: true });
// Early exit optimization - check each file immediately
for (const file of files) {
if (!followSymlinks && file.isSymbolicLink()) {
continue;
}
if (!shouldIgnore(file.name, ignore)) {
return false; // Found a file that shouldn't be ignored
}
}
return true;
} catch (error) {
if (error.code === "ENOENT") {
throw new Error(`Directory does not exist: ${dirPath}`);
}
if (error.code === "EACCES") {
throw new Error(`Permission denied: ${dirPath}`);
}
throw error;
}
}
/**
* Check if a directory is empty (sync)
* @param {string} dirPath - Path to directory to check
* @param {Object} [options] - Options object
* @param {Array<string|RegExp|Function>} [options.ignore] - Patterns to ignore
* @param {boolean} [options.followSymlinks] - Whether to follow symbolic links
* @returns {boolean} - True if directory is empty or only contains ignored files
*/
function isEmptyDirSync(dirPath, options = {}) {
const { ignore = [], followSymlinks = false } = options;
if (typeof dirPath !== "string") {
throw new TypeError("Expected dirPath to be a string");
}
try {
const stats = statSync(dirPath);
if (!stats.isDirectory()) {
throw new Error(`Path is not a directory: ${dirPath}`);
}
const files = readdirSync(dirPath, { withFileTypes: true });
// Early exit optimization - check each file immediately
for (const file of files) {
if (!followSymlinks && file.isSymbolicLink()) {
continue;
}
if (!shouldIgnore(file.name, ignore)) {
return false; // Found a file that shouldn't be ignored
}
}
return true;
} catch (error) {
if (error.code === "ENOENT") {
throw new Error(`Directory does not exist: ${dirPath}`);
}
if (error.code === "EACCES") {
throw new Error(`Permission denied: ${dirPath}`);
}
throw error;
}
}
/**
* Helper function to determine if a file should be ignored
* @param {string} filename - Name of the file
* @param {Array<string|RegExp|Function>} ignorePatterns - Patterns to check against
* @returns {boolean} - True if file should be ignored
*/
function shouldIgnore(filename, ignorePatterns) {
if (!Array.isArray(ignorePatterns)) {
return false;
}
return ignorePatterns.some((pattern) => {
if (typeof pattern === "string") {
return filename === pattern;
}
if (pattern instanceof RegExp) {
return pattern.test(filename);
}
if (typeof pattern === "function") {
return pattern(filename);
}
return false;
});
}
// ES modules exports (will be handled by build script)
// ES modules exports
export default isEmptyDir;
export { isEmptyDir, isEmptyDirSync };