tiny-essentials 1.12.1 → 1.12.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/legacy/firebase/index.mjs +5 -5
- package/dist/v1/TinyBasicsEs.js +1 -1257
- package/dist/v1/TinyBasicsEs.min.js +1 -1
- package/dist/v1/TinyEssentials.js +336 -146
- package/dist/v1/TinyEssentials.min.js +1 -1
- package/dist/v1/TinyUploadClicker.js +330 -147
- package/dist/v1/TinyUploadClicker.min.js +1 -1
- package/dist/v1/basics/index.cjs +0 -23
- package/dist/v1/basics/index.d.mts +1 -23
- package/dist/v1/basics/index.mjs +1 -2
- package/dist/v1/fileManager/asyncFuncs.cjs +272 -0
- package/dist/v1/fileManager/asyncFuncs.d.mts +93 -0
- package/dist/v1/fileManager/asyncFuncs.mjs +236 -0
- package/dist/v1/fileManager/index.cjs +35 -0
- package/dist/v1/fileManager/index.d.mts +30 -0
- package/dist/v1/fileManager/index.mjs +3 -0
- package/dist/v1/{basics/fileManager.cjs → fileManager/normalFuncs.cjs} +10 -105
- package/dist/v1/{basics/fileManager.d.mts → fileManager/normalFuncs.d.mts} +3 -56
- package/dist/v1/{basics/fileManager.mjs → fileManager/normalFuncs.mjs} +48 -131
- package/dist/v1/index.cjs +30 -23
- package/dist/v1/index.d.mts +29 -23
- package/dist/v1/index.mjs +3 -2
- package/dist/v1/libs/TinyUploadClicker.cjs +1 -0
- package/docs/v1/README.md +3 -1
- package/docs/v1/{basics/fileManager.md → fileManager/main.md} +12 -0
- package/package.json +5 -1
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { existsSync } from 'fs';
|
|
2
|
+
import { readFile, writeFile, copyFile, unlink, readdir, lstat, rm, stat } from 'fs/promises';
|
|
3
|
+
import { join, dirname } from 'path';
|
|
4
|
+
import { dirExists, ensureDirectory, fileExists, getLatestBackupPath } from './normalFuncs.mjs';
|
|
5
|
+
/*========================*
|
|
6
|
+
* JSON Operations
|
|
7
|
+
*========================*/
|
|
8
|
+
/**
|
|
9
|
+
* Reads and parses a JSON file.
|
|
10
|
+
* Throws an error if the file content is not valid JSON.
|
|
11
|
+
* @param {string} filePath
|
|
12
|
+
* @returns {Promise<any>}
|
|
13
|
+
*/
|
|
14
|
+
export async function readJsonFileAsync(filePath) {
|
|
15
|
+
if (!existsSync(filePath))
|
|
16
|
+
throw new Error(`File not found: ${filePath}`);
|
|
17
|
+
const content = await readFile(filePath, 'utf-8');
|
|
18
|
+
return JSON.parse(content);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Saves an object as JSON to a file.
|
|
22
|
+
* Automatically creates the directory if it does not exist.
|
|
23
|
+
* @param {string} filePath
|
|
24
|
+
* @param {any} data
|
|
25
|
+
* @param {number} [spaces=2]
|
|
26
|
+
* @returns {Promise<void>}
|
|
27
|
+
*/
|
|
28
|
+
export function writeJsonFileAsync(filePath, data, spaces = 2) {
|
|
29
|
+
const json = JSON.stringify(data, null, spaces);
|
|
30
|
+
return writeFile(filePath, json, 'utf-8');
|
|
31
|
+
}
|
|
32
|
+
/*========================*
|
|
33
|
+
* Directory Management
|
|
34
|
+
*========================*/
|
|
35
|
+
/**
|
|
36
|
+
* Clears all contents inside a directory but keeps the directory.
|
|
37
|
+
* @param {string} dirPath
|
|
38
|
+
*/
|
|
39
|
+
export async function clearDirectoryAsync(dirPath) {
|
|
40
|
+
if (!existsSync(dirPath))
|
|
41
|
+
return;
|
|
42
|
+
const files = await readdir(dirPath);
|
|
43
|
+
/** @type {Record<string, import('fs').Stats>} */
|
|
44
|
+
const dataList = {};
|
|
45
|
+
const promises = [];
|
|
46
|
+
for (const file of files) {
|
|
47
|
+
const fullPath = join(dirPath, file);
|
|
48
|
+
const lsResult = lstat(fullPath);
|
|
49
|
+
lsResult.then((statData) => {
|
|
50
|
+
dataList[fullPath] = statData;
|
|
51
|
+
return statData;
|
|
52
|
+
});
|
|
53
|
+
promises.push(lsResult);
|
|
54
|
+
}
|
|
55
|
+
await Promise.all(promises);
|
|
56
|
+
const promises2 = [];
|
|
57
|
+
for (const fullPath in dataList) {
|
|
58
|
+
const statData = dataList[fullPath];
|
|
59
|
+
if (statData.isDirectory()) {
|
|
60
|
+
promises2.push(rm(fullPath, { recursive: true, force: true }));
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
promises2.push(unlink(fullPath));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
await Promise.all(promises2);
|
|
67
|
+
}
|
|
68
|
+
/*========================*
|
|
69
|
+
* File Checks
|
|
70
|
+
*========================*/
|
|
71
|
+
/**
|
|
72
|
+
* Checks whether a directory is empty.
|
|
73
|
+
* @param {string} dirPath
|
|
74
|
+
* @returns {Promise<boolean>}
|
|
75
|
+
*/
|
|
76
|
+
export async function isDirEmptyAsync(dirPath) {
|
|
77
|
+
const data = await readdir(dirPath);
|
|
78
|
+
return data.length === 0;
|
|
79
|
+
}
|
|
80
|
+
/*========================*
|
|
81
|
+
* File Operations
|
|
82
|
+
*========================*/
|
|
83
|
+
/**
|
|
84
|
+
* Copies a file to a destination.
|
|
85
|
+
* @param {string} src
|
|
86
|
+
* @param {string} dest
|
|
87
|
+
* @param {number} [mode]
|
|
88
|
+
* @returns {Promise<void>}
|
|
89
|
+
*/
|
|
90
|
+
export function ensureCopyFileAsync(src, dest, mode) {
|
|
91
|
+
ensureDirectory(dirname(dest));
|
|
92
|
+
return copyFile(src, dest, mode);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Deletes a file (If the file exists).
|
|
96
|
+
* @param {string} filePath
|
|
97
|
+
* @returns {Promise<boolean>}
|
|
98
|
+
*/
|
|
99
|
+
export async function tryDeleteFileAsync(filePath) {
|
|
100
|
+
if (fileExists(filePath)) {
|
|
101
|
+
await unlink(filePath);
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
/*========================*
|
|
107
|
+
* Text Operations
|
|
108
|
+
*========================*/
|
|
109
|
+
/**
|
|
110
|
+
* Writes text to a file (Ensures that the directory exists, creating it recursively if needed).
|
|
111
|
+
* @param {string} filePath
|
|
112
|
+
* @param {string} content
|
|
113
|
+
* @param {import('fs').WriteFileOptions} [ops='utf-8']
|
|
114
|
+
* @returns {Promise<void>}
|
|
115
|
+
*/
|
|
116
|
+
export function writeTextFileAsync(filePath, content, ops = 'utf-8') {
|
|
117
|
+
const dir = dirname(filePath);
|
|
118
|
+
ensureDirectory(dir);
|
|
119
|
+
return writeFile(filePath, content, ops);
|
|
120
|
+
}
|
|
121
|
+
/*========================*
|
|
122
|
+
* Directory Listings
|
|
123
|
+
*========================*/
|
|
124
|
+
/**
|
|
125
|
+
* Lists all files and dirs in a directory (optionally recursive).
|
|
126
|
+
* @param {string} dirPath
|
|
127
|
+
* @param {boolean} [recursive=false]
|
|
128
|
+
* @returns {Promise<{ files: string[]; dirs: string[] }>}
|
|
129
|
+
*/
|
|
130
|
+
export async function listFilesAsync(dirPath, recursive = false) {
|
|
131
|
+
/** @type {{ files: string[]; dirs: string[] }} */
|
|
132
|
+
const results = { files: [], dirs: [] };
|
|
133
|
+
if (!dirExists(dirPath))
|
|
134
|
+
return results;
|
|
135
|
+
const entries = await readdir(dirPath);
|
|
136
|
+
for (const entry of entries) {
|
|
137
|
+
const fullPath = join(dirPath, entry);
|
|
138
|
+
const statData = await lstat(fullPath);
|
|
139
|
+
if (statData.isDirectory()) {
|
|
140
|
+
results.dirs.push(fullPath);
|
|
141
|
+
if (recursive) {
|
|
142
|
+
const results2 = await listFilesAsync(fullPath, true);
|
|
143
|
+
results.files.push(...results2.files);
|
|
144
|
+
results.dirs.push(...results2.dirs);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
results.files.push(fullPath);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return results;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Lists all directories in a directory (optionally recursive).
|
|
155
|
+
* @param {string} dirPath
|
|
156
|
+
* @param {boolean} [recursive=false]
|
|
157
|
+
* @returns {Promise<string[]>}
|
|
158
|
+
*/
|
|
159
|
+
export async function listDirsAsync(dirPath, recursive = false) {
|
|
160
|
+
/** @type {string[]} */
|
|
161
|
+
const results = [];
|
|
162
|
+
if (!dirExists(dirPath))
|
|
163
|
+
return results;
|
|
164
|
+
const entries = await readdir(dirPath);
|
|
165
|
+
for (const entry of entries) {
|
|
166
|
+
const fullPath = join(dirPath, entry);
|
|
167
|
+
const statData = await lstat(fullPath);
|
|
168
|
+
if (statData.isDirectory()) {
|
|
169
|
+
results.push(fullPath);
|
|
170
|
+
if (recursive) {
|
|
171
|
+
results.push(...(await listDirsAsync(fullPath, true)));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return results;
|
|
176
|
+
}
|
|
177
|
+
/*========================*
|
|
178
|
+
* File Info
|
|
179
|
+
*========================*/
|
|
180
|
+
/**
|
|
181
|
+
* Returns the size of a file in bytes.
|
|
182
|
+
* @param {string} filePath
|
|
183
|
+
* @returns {Promise<number>}
|
|
184
|
+
*/
|
|
185
|
+
export async function fileSizeAsync(filePath) {
|
|
186
|
+
if (!fileExists(filePath))
|
|
187
|
+
return 0;
|
|
188
|
+
const stats = await stat(filePath);
|
|
189
|
+
return stats.size;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Returns the total size of a directory in bytes.
|
|
193
|
+
* @param {string} dirPath
|
|
194
|
+
* @returns {Promise<number>}
|
|
195
|
+
*/
|
|
196
|
+
export async function dirSizeAsync(dirPath) {
|
|
197
|
+
let total = 0;
|
|
198
|
+
const { files } = await listFilesAsync(dirPath, true);
|
|
199
|
+
const promises = [];
|
|
200
|
+
for (const file of files) {
|
|
201
|
+
const result = fileSizeAsync(file);
|
|
202
|
+
result.then((item) => {
|
|
203
|
+
total += item;
|
|
204
|
+
return item;
|
|
205
|
+
});
|
|
206
|
+
promises.push(result);
|
|
207
|
+
}
|
|
208
|
+
await Promise.all(promises);
|
|
209
|
+
return total;
|
|
210
|
+
}
|
|
211
|
+
/*========================*
|
|
212
|
+
* Backup Utilities
|
|
213
|
+
*========================*/
|
|
214
|
+
/**
|
|
215
|
+
* Restores the most recent backup of a file.
|
|
216
|
+
* @param {string} filePath
|
|
217
|
+
* @param {string} [ext='bak']
|
|
218
|
+
* @returns {Promise<void>}
|
|
219
|
+
*/
|
|
220
|
+
export function restoreLatestBackupAsync(filePath, ext = 'bak') {
|
|
221
|
+
const latestBackup = getLatestBackupPath(filePath, ext);
|
|
222
|
+
return ensureCopyFileAsync(latestBackup, filePath);
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Creates a backup copy of a file with .bak timestamp suffix.
|
|
226
|
+
* @param {string} filePath
|
|
227
|
+
* @param {string} [ext='bak']
|
|
228
|
+
* @returns {Promise<void>}
|
|
229
|
+
*/
|
|
230
|
+
export async function backupFileAsync(filePath, ext = 'bak') {
|
|
231
|
+
if (!fileExists(filePath))
|
|
232
|
+
return;
|
|
233
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
234
|
+
const backupPath = `${filePath}.${ext}.${timestamp}`;
|
|
235
|
+
return ensureCopyFileAsync(filePath, backupPath);
|
|
236
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var normalFuncs = require('./normalFuncs.cjs');
|
|
4
|
+
var asyncFuncs = require('./asyncFuncs.cjs');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
exports.backupFile = normalFuncs.backupFile;
|
|
9
|
+
exports.clearDirectory = normalFuncs.clearDirectory;
|
|
10
|
+
exports.dirExists = normalFuncs.dirExists;
|
|
11
|
+
exports.dirSize = normalFuncs.dirSize;
|
|
12
|
+
exports.ensureCopyFile = normalFuncs.ensureCopyFile;
|
|
13
|
+
exports.ensureDirectory = normalFuncs.ensureDirectory;
|
|
14
|
+
exports.fileExists = normalFuncs.fileExists;
|
|
15
|
+
exports.fileSize = normalFuncs.fileSize;
|
|
16
|
+
exports.getLatestBackupPath = normalFuncs.getLatestBackupPath;
|
|
17
|
+
exports.isDirEmpty = normalFuncs.isDirEmpty;
|
|
18
|
+
exports.listDirs = normalFuncs.listDirs;
|
|
19
|
+
exports.listFiles = normalFuncs.listFiles;
|
|
20
|
+
exports.readJsonFile = normalFuncs.readJsonFile;
|
|
21
|
+
exports.renameFileAddPrefixSuffix = normalFuncs.renameFileAddPrefixSuffix;
|
|
22
|
+
exports.renameFileBatch = normalFuncs.renameFileBatch;
|
|
23
|
+
exports.renameFileNormalizeCase = normalFuncs.renameFileNormalizeCase;
|
|
24
|
+
exports.renameFilePadNumbers = normalFuncs.renameFilePadNumbers;
|
|
25
|
+
exports.renameFileRegex = normalFuncs.renameFileRegex;
|
|
26
|
+
exports.restoreLatestBackup = normalFuncs.restoreLatestBackup;
|
|
27
|
+
exports.tryDeleteFile = normalFuncs.tryDeleteFile;
|
|
28
|
+
exports.writeJsonFile = normalFuncs.writeJsonFile;
|
|
29
|
+
exports.writeTextFile = normalFuncs.writeTextFile;
|
|
30
|
+
exports.clearDirectoryAsync = asyncFuncs.clearDirectoryAsync;
|
|
31
|
+
exports.dirSizeAsync = asyncFuncs.dirSizeAsync;
|
|
32
|
+
exports.fileSizeAsync = asyncFuncs.fileSizeAsync;
|
|
33
|
+
exports.isDirEmptyAsync = asyncFuncs.isDirEmptyAsync;
|
|
34
|
+
exports.listDirsAsync = asyncFuncs.listDirsAsync;
|
|
35
|
+
exports.listFilesAsync = asyncFuncs.listFilesAsync;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { isDirEmptyAsync } from './asyncFuncs.mjs';
|
|
2
|
+
import { fileSizeAsync } from './asyncFuncs.mjs';
|
|
3
|
+
import { dirSizeAsync } from './asyncFuncs.mjs';
|
|
4
|
+
import { listFilesAsync } from './asyncFuncs.mjs';
|
|
5
|
+
import { listDirsAsync } from './asyncFuncs.mjs';
|
|
6
|
+
import { getLatestBackupPath } from './normalFuncs.mjs';
|
|
7
|
+
import { readJsonFile } from './normalFuncs.mjs';
|
|
8
|
+
import { writeJsonFile } from './normalFuncs.mjs';
|
|
9
|
+
import { ensureDirectory } from './normalFuncs.mjs';
|
|
10
|
+
import { clearDirectory } from './normalFuncs.mjs';
|
|
11
|
+
import { clearDirectoryAsync } from './asyncFuncs.mjs';
|
|
12
|
+
import { fileExists } from './normalFuncs.mjs';
|
|
13
|
+
import { dirExists } from './normalFuncs.mjs';
|
|
14
|
+
import { isDirEmpty } from './normalFuncs.mjs';
|
|
15
|
+
import { ensureCopyFile } from './normalFuncs.mjs';
|
|
16
|
+
import { tryDeleteFile } from './normalFuncs.mjs';
|
|
17
|
+
import { writeTextFile } from './normalFuncs.mjs';
|
|
18
|
+
import { listFiles } from './normalFuncs.mjs';
|
|
19
|
+
import { listDirs } from './normalFuncs.mjs';
|
|
20
|
+
import { fileSize } from './normalFuncs.mjs';
|
|
21
|
+
import { dirSize } from './normalFuncs.mjs';
|
|
22
|
+
import { backupFile } from './normalFuncs.mjs';
|
|
23
|
+
import { restoreLatestBackup } from './normalFuncs.mjs';
|
|
24
|
+
import { renameFileBatch } from './normalFuncs.mjs';
|
|
25
|
+
import { renameFileRegex } from './normalFuncs.mjs';
|
|
26
|
+
import { renameFileAddPrefixSuffix } from './normalFuncs.mjs';
|
|
27
|
+
import { renameFileNormalizeCase } from './normalFuncs.mjs';
|
|
28
|
+
import { renameFilePadNumbers } from './normalFuncs.mjs';
|
|
29
|
+
export { isDirEmptyAsync, fileSizeAsync, dirSizeAsync, listFilesAsync, listDirsAsync, getLatestBackupPath, readJsonFile, writeJsonFile, ensureDirectory, clearDirectory, clearDirectoryAsync, fileExists, dirExists, isDirEmpty, ensureCopyFile, tryDeleteFile, writeTextFile, listFiles, listDirs, fileSize, dirSize, backupFile, restoreLatestBackup, renameFileBatch, renameFileRegex, renameFileAddPrefixSuffix, renameFileNormalizeCase, renameFilePadNumbers };
|
|
30
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { readJsonFile, writeJsonFile, ensureDirectory, clearDirectory, fileExists, dirExists, isDirEmpty, ensureCopyFile, tryDeleteFile, writeTextFile, listFiles, listDirs, fileSize, dirSize, backupFile, restoreLatestBackup, renameFileBatch, renameFileRegex, renameFileAddPrefixSuffix, renameFileNormalizeCase, renameFilePadNumbers, getLatestBackupPath, } from './normalFuncs.mjs';
|
|
2
|
+
import { listFilesAsync, listDirsAsync, clearDirectoryAsync, fileSizeAsync, dirSizeAsync, isDirEmptyAsync, } from './asyncFuncs.mjs';
|
|
3
|
+
export { isDirEmptyAsync, fileSizeAsync, dirSizeAsync, listFilesAsync, listDirsAsync, getLatestBackupPath, readJsonFile, writeJsonFile, ensureDirectory, clearDirectory, clearDirectoryAsync, fileExists, dirExists, isDirEmpty, ensureCopyFile, tryDeleteFile, writeTextFile, listFiles, listDirs, fileSize, dirSize, backupFile, restoreLatestBackup, renameFileBatch, renameFileRegex, renameFileAddPrefixSuffix, renameFileNormalizeCase, renameFilePadNumbers, };
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var fs = require('fs');
|
|
4
4
|
var path = require('path');
|
|
5
|
-
var text = require('
|
|
5
|
+
var text = require('../basics/text.cjs');
|
|
6
6
|
|
|
7
7
|
/*========================*
|
|
8
8
|
* JSON Operations
|
|
@@ -32,31 +32,6 @@ function writeJsonFile(filePath, data, spaces = 2) {
|
|
|
32
32
|
fs.writeFileSync(filePath, json, 'utf-8');
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
/**
|
|
36
|
-
* Reads and parses a JSON file.
|
|
37
|
-
* Throws an error if the file content is not valid JSON.
|
|
38
|
-
* @param {string} filePath
|
|
39
|
-
* @returns {Promise<any>}
|
|
40
|
-
*/
|
|
41
|
-
async function readJsonFileAsync(filePath) {
|
|
42
|
-
if (!fs.existsSync(filePath)) throw new Error(`File not found: ${filePath}`);
|
|
43
|
-
const content = await fs.promises.readFile(filePath, 'utf-8');
|
|
44
|
-
return JSON.parse(content);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Saves an object as JSON to a file.
|
|
49
|
-
* Automatically creates the directory if it does not exist.
|
|
50
|
-
* @param {string} filePath
|
|
51
|
-
* @param {any} data
|
|
52
|
-
* @param {number} [spaces=2]
|
|
53
|
-
* @returns {Promise<void>}
|
|
54
|
-
*/
|
|
55
|
-
function writeJsonFileAsync(filePath, data, spaces = 2) {
|
|
56
|
-
const json = JSON.stringify(data, null, spaces);
|
|
57
|
-
return fs.promises.writeFile(filePath, json, 'utf-8');
|
|
58
|
-
}
|
|
59
|
-
|
|
60
35
|
/*========================*
|
|
61
36
|
* Directory Management
|
|
62
37
|
*========================*/
|
|
@@ -80,8 +55,8 @@ function clearDirectory(dirPath) {
|
|
|
80
55
|
const files = fs.readdirSync(dirPath);
|
|
81
56
|
for (const file of files) {
|
|
82
57
|
const fullPath = path.join(dirPath, file);
|
|
83
|
-
const
|
|
84
|
-
if (
|
|
58
|
+
const statData = fs.lstatSync(fullPath);
|
|
59
|
+
if (statData.isDirectory()) {
|
|
85
60
|
fs.rmSync(fullPath, { recursive: true, force: true });
|
|
86
61
|
} else {
|
|
87
62
|
fs.unlinkSync(fullPath);
|
|
@@ -117,7 +92,7 @@ function dirExists(dirPath) {
|
|
|
117
92
|
* @returns {boolean}
|
|
118
93
|
*/
|
|
119
94
|
function isDirEmpty(dirPath) {
|
|
120
|
-
return fs.
|
|
95
|
+
return fs.readdirSync(dirPath).length === 0;
|
|
121
96
|
}
|
|
122
97
|
|
|
123
98
|
/*========================*
|
|
@@ -148,31 +123,6 @@ function tryDeleteFile(filePath) {
|
|
|
148
123
|
return false;
|
|
149
124
|
}
|
|
150
125
|
|
|
151
|
-
/**
|
|
152
|
-
* Copies a file to a destination.
|
|
153
|
-
* @param {string} src
|
|
154
|
-
* @param {string} dest
|
|
155
|
-
* @param {number} [mode]
|
|
156
|
-
* @returns {Promise<void>}
|
|
157
|
-
*/
|
|
158
|
-
function ensureCopyFileAsync(src, dest, mode) {
|
|
159
|
-
ensureDirectory(path.dirname(dest));
|
|
160
|
-
return fs.promises.copyFile(src, dest, mode);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Deletes a file (If the file exists).
|
|
165
|
-
* @param {string} filePath
|
|
166
|
-
* @returns {Promise<boolean>}
|
|
167
|
-
*/
|
|
168
|
-
async function tryDeleteFileAsync(filePath) {
|
|
169
|
-
if (fileExists(filePath)) {
|
|
170
|
-
await fs.promises.unlink(filePath);
|
|
171
|
-
return true;
|
|
172
|
-
}
|
|
173
|
-
return false;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
126
|
/*========================*
|
|
177
127
|
* Text Operations
|
|
178
128
|
*========================*/
|
|
@@ -181,7 +131,7 @@ async function tryDeleteFileAsync(filePath) {
|
|
|
181
131
|
* Writes text to a file (Ensures that the directory exists, creating it recursively if needed).
|
|
182
132
|
* @param {string} filePath
|
|
183
133
|
* @param {string} content
|
|
184
|
-
* @param {fs.WriteFileOptions} [ops='utf-8']
|
|
134
|
+
* @param {import('fs').WriteFileOptions} [ops='utf-8']
|
|
185
135
|
*/
|
|
186
136
|
function writeTextFile(filePath, content, ops = 'utf-8') {
|
|
187
137
|
const dir = path.dirname(filePath);
|
|
@@ -189,19 +139,6 @@ function writeTextFile(filePath, content, ops = 'utf-8') {
|
|
|
189
139
|
fs.writeFileSync(filePath, content, ops);
|
|
190
140
|
}
|
|
191
141
|
|
|
192
|
-
/**
|
|
193
|
-
* Writes text to a file (Ensures that the directory exists, creating it recursively if needed).
|
|
194
|
-
* @param {string} filePath
|
|
195
|
-
* @param {string} content
|
|
196
|
-
* @param {fs.WriteFileOptions} [ops='utf-8']
|
|
197
|
-
* @returns {Promise<void>}
|
|
198
|
-
*/
|
|
199
|
-
function writeTextFileAsync(filePath, content, ops = 'utf-8') {
|
|
200
|
-
const dir = path.dirname(filePath);
|
|
201
|
-
ensureDirectory(dir);
|
|
202
|
-
return fs.promises.writeFile(filePath, content, ops);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
142
|
/*========================*
|
|
206
143
|
* Directory Listings
|
|
207
144
|
*========================*/
|
|
@@ -220,8 +157,8 @@ function listFiles(dirPath, recursive = false) {
|
|
|
220
157
|
const entries = fs.readdirSync(dirPath);
|
|
221
158
|
for (const entry of entries) {
|
|
222
159
|
const fullPath = path.join(dirPath, entry);
|
|
223
|
-
const
|
|
224
|
-
if (
|
|
160
|
+
const statData = fs.lstatSync(fullPath);
|
|
161
|
+
if (statData.isDirectory()) {
|
|
225
162
|
results.dirs.push(fullPath);
|
|
226
163
|
if (recursive) {
|
|
227
164
|
const results2 = listFiles(fullPath, true);
|
|
@@ -249,8 +186,8 @@ function listDirs(dirPath, recursive = false) {
|
|
|
249
186
|
const entries = fs.readdirSync(dirPath);
|
|
250
187
|
for (const entry of entries) {
|
|
251
188
|
const fullPath = path.join(dirPath, entry);
|
|
252
|
-
const
|
|
253
|
-
if (
|
|
189
|
+
const statData = fs.lstatSync(fullPath);
|
|
190
|
+
if (statData.isDirectory()) {
|
|
254
191
|
results.push(fullPath);
|
|
255
192
|
if (recursive) {
|
|
256
193
|
results.push(...listDirs(fullPath, true));
|
|
@@ -314,8 +251,7 @@ function backupFile(filePath, ext = 'bak') {
|
|
|
314
251
|
function getLatestBackupPath(filePath, ext = 'bak') {
|
|
315
252
|
const dir = path.dirname(filePath);
|
|
316
253
|
const baseName = path.basename(filePath);
|
|
317
|
-
const backups = fs
|
|
318
|
-
.readdirSync(dir)
|
|
254
|
+
const backups = fs.readdirSync(dir)
|
|
319
255
|
.filter((name) => name.startsWith(`${baseName}.${ext}.`))
|
|
320
256
|
.sort()
|
|
321
257
|
.reverse();
|
|
@@ -335,30 +271,6 @@ function restoreLatestBackup(filePath, ext = 'bak') {
|
|
|
335
271
|
ensureCopyFile(latestBackup, filePath);
|
|
336
272
|
}
|
|
337
273
|
|
|
338
|
-
/**
|
|
339
|
-
* Restores the most recent backup of a file.
|
|
340
|
-
* @param {string} filePath
|
|
341
|
-
* @param {string} [ext='bak']
|
|
342
|
-
* @returns {Promise<void>}
|
|
343
|
-
*/
|
|
344
|
-
function restoreLatestBackupAsync(filePath, ext = 'bak') {
|
|
345
|
-
const latestBackup = getLatestBackupPath(filePath, ext);
|
|
346
|
-
return ensureCopyFileAsync(latestBackup, filePath);
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
/**
|
|
350
|
-
* Creates a backup copy of a file with .bak timestamp suffix.
|
|
351
|
-
* @param {string} filePath
|
|
352
|
-
* @param {string} [ext='bak']
|
|
353
|
-
* @returns {Promise<void>}
|
|
354
|
-
*/
|
|
355
|
-
async function backupFileAsync(filePath, ext = 'bak') {
|
|
356
|
-
if (!fileExists(filePath)) return;
|
|
357
|
-
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
358
|
-
const backupPath = `${filePath}.${ext}.${timestamp}`;
|
|
359
|
-
return ensureCopyFileAsync(filePath, backupPath);
|
|
360
|
-
}
|
|
361
|
-
|
|
362
274
|
/*========================*
|
|
363
275
|
* Rename Utilities
|
|
364
276
|
*========================*/
|
|
@@ -494,12 +406,10 @@ function renameFilePadNumbers(dirPath, totalDigits = 3, extensions = []) {
|
|
|
494
406
|
}
|
|
495
407
|
|
|
496
408
|
exports.backupFile = backupFile;
|
|
497
|
-
exports.backupFileAsync = backupFileAsync;
|
|
498
409
|
exports.clearDirectory = clearDirectory;
|
|
499
410
|
exports.dirExists = dirExists;
|
|
500
411
|
exports.dirSize = dirSize;
|
|
501
412
|
exports.ensureCopyFile = ensureCopyFile;
|
|
502
|
-
exports.ensureCopyFileAsync = ensureCopyFileAsync;
|
|
503
413
|
exports.ensureDirectory = ensureDirectory;
|
|
504
414
|
exports.fileExists = fileExists;
|
|
505
415
|
exports.fileSize = fileSize;
|
|
@@ -508,17 +418,12 @@ exports.isDirEmpty = isDirEmpty;
|
|
|
508
418
|
exports.listDirs = listDirs;
|
|
509
419
|
exports.listFiles = listFiles;
|
|
510
420
|
exports.readJsonFile = readJsonFile;
|
|
511
|
-
exports.readJsonFileAsync = readJsonFileAsync;
|
|
512
421
|
exports.renameFileAddPrefixSuffix = renameFileAddPrefixSuffix;
|
|
513
422
|
exports.renameFileBatch = renameFileBatch;
|
|
514
423
|
exports.renameFileNormalizeCase = renameFileNormalizeCase;
|
|
515
424
|
exports.renameFilePadNumbers = renameFilePadNumbers;
|
|
516
425
|
exports.renameFileRegex = renameFileRegex;
|
|
517
426
|
exports.restoreLatestBackup = restoreLatestBackup;
|
|
518
|
-
exports.restoreLatestBackupAsync = restoreLatestBackupAsync;
|
|
519
427
|
exports.tryDeleteFile = tryDeleteFile;
|
|
520
|
-
exports.tryDeleteFileAsync = tryDeleteFileAsync;
|
|
521
428
|
exports.writeJsonFile = writeJsonFile;
|
|
522
|
-
exports.writeJsonFileAsync = writeJsonFileAsync;
|
|
523
429
|
exports.writeTextFile = writeTextFile;
|
|
524
|
-
exports.writeTextFileAsync = writeTextFileAsync;
|
|
@@ -13,22 +13,6 @@ export function readJsonFile(filePath: string): any;
|
|
|
13
13
|
* @param {number} [spaces=2]
|
|
14
14
|
*/
|
|
15
15
|
export function writeJsonFile(filePath: string, data: any, spaces?: number): void;
|
|
16
|
-
/**
|
|
17
|
-
* Reads and parses a JSON file.
|
|
18
|
-
* Throws an error if the file content is not valid JSON.
|
|
19
|
-
* @param {string} filePath
|
|
20
|
-
* @returns {Promise<any>}
|
|
21
|
-
*/
|
|
22
|
-
export function readJsonFileAsync(filePath: string): Promise<any>;
|
|
23
|
-
/**
|
|
24
|
-
* Saves an object as JSON to a file.
|
|
25
|
-
* Automatically creates the directory if it does not exist.
|
|
26
|
-
* @param {string} filePath
|
|
27
|
-
* @param {any} data
|
|
28
|
-
* @param {number} [spaces=2]
|
|
29
|
-
* @returns {Promise<void>}
|
|
30
|
-
*/
|
|
31
|
-
export function writeJsonFileAsync(filePath: string, data: any, spaces?: number): Promise<void>;
|
|
32
16
|
/**
|
|
33
17
|
* Ensures that the directory exists, creating it recursively if needed.
|
|
34
18
|
* @param {string} dirPath
|
|
@@ -70,35 +54,13 @@ export function ensureCopyFile(src: string, dest: string, mode?: number): void;
|
|
|
70
54
|
* @returns {boolean}
|
|
71
55
|
*/
|
|
72
56
|
export function tryDeleteFile(filePath: string): boolean;
|
|
73
|
-
/**
|
|
74
|
-
* Copies a file to a destination.
|
|
75
|
-
* @param {string} src
|
|
76
|
-
* @param {string} dest
|
|
77
|
-
* @param {number} [mode]
|
|
78
|
-
* @returns {Promise<void>}
|
|
79
|
-
*/
|
|
80
|
-
export function ensureCopyFileAsync(src: string, dest: string, mode?: number): Promise<void>;
|
|
81
|
-
/**
|
|
82
|
-
* Deletes a file (If the file exists).
|
|
83
|
-
* @param {string} filePath
|
|
84
|
-
* @returns {Promise<boolean>}
|
|
85
|
-
*/
|
|
86
|
-
export function tryDeleteFileAsync(filePath: string): Promise<boolean>;
|
|
87
57
|
/**
|
|
88
58
|
* Writes text to a file (Ensures that the directory exists, creating it recursively if needed).
|
|
89
59
|
* @param {string} filePath
|
|
90
60
|
* @param {string} content
|
|
91
|
-
* @param {fs.WriteFileOptions} [ops='utf-8']
|
|
61
|
+
* @param {import('fs').WriteFileOptions} [ops='utf-8']
|
|
92
62
|
*/
|
|
93
|
-
export function writeTextFile(filePath: string, content: string, ops?: fs.WriteFileOptions): void;
|
|
94
|
-
/**
|
|
95
|
-
* Writes text to a file (Ensures that the directory exists, creating it recursively if needed).
|
|
96
|
-
* @param {string} filePath
|
|
97
|
-
* @param {string} content
|
|
98
|
-
* @param {fs.WriteFileOptions} [ops='utf-8']
|
|
99
|
-
* @returns {Promise<void>}
|
|
100
|
-
*/
|
|
101
|
-
export function writeTextFileAsync(filePath: string, content: string, ops?: fs.WriteFileOptions): Promise<void>;
|
|
63
|
+
export function writeTextFile(filePath: string, content: string, ops?: import("fs").WriteFileOptions): void;
|
|
102
64
|
/**
|
|
103
65
|
* Lists all files and dirs in a directory (optionally recursive).
|
|
104
66
|
* @param {string} dirPath
|
|
@@ -147,20 +109,6 @@ export function getLatestBackupPath(filePath: string, ext?: string): string;
|
|
|
147
109
|
* @param {string} [ext='bak']
|
|
148
110
|
*/
|
|
149
111
|
export function restoreLatestBackup(filePath: string, ext?: string): void;
|
|
150
|
-
/**
|
|
151
|
-
* Restores the most recent backup of a file.
|
|
152
|
-
* @param {string} filePath
|
|
153
|
-
* @param {string} [ext='bak']
|
|
154
|
-
* @returns {Promise<void>}
|
|
155
|
-
*/
|
|
156
|
-
export function restoreLatestBackupAsync(filePath: string, ext?: string): Promise<void>;
|
|
157
|
-
/**
|
|
158
|
-
* Creates a backup copy of a file with .bak timestamp suffix.
|
|
159
|
-
* @param {string} filePath
|
|
160
|
-
* @param {string} [ext='bak']
|
|
161
|
-
* @returns {Promise<void>}
|
|
162
|
-
*/
|
|
163
|
-
export function backupFileAsync(filePath: string, ext?: string): Promise<void>;
|
|
164
112
|
/**
|
|
165
113
|
* Renames multiple files in a directory using a rename function.
|
|
166
114
|
* @param {string} dirPath - The target directory.
|
|
@@ -205,5 +153,4 @@ export function renameFileNormalizeCase(dirPath: string, mode?: "lower" | "upper
|
|
|
205
153
|
* @param {string[]} [extensions]
|
|
206
154
|
*/
|
|
207
155
|
export function renameFilePadNumbers(dirPath: string, totalDigits?: number, extensions?: string[]): void;
|
|
208
|
-
|
|
209
|
-
//# sourceMappingURL=fileManager.d.mts.map
|
|
156
|
+
//# sourceMappingURL=normalFuncs.d.mts.map
|