@bemoje/fs 1.1.0 → 1.1.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/lib/index.cjs +183 -84
- package/lib/index.d.cts +113 -13
- package/lib/index.d.ts +113 -13
- package/lib/index.js +168 -12
- package/package.json +71 -119
- package/lib/deleteOlderThan.cjs +0 -23
- package/lib/deleteOlderThan.d.cts +0 -8
- package/lib/deleteOlderThan.d.ts +0 -8
- package/lib/deleteOlderThan.js +0 -16
- package/lib/getFileAge.cjs +0 -18
- package/lib/getFileAge.d.cts +0 -7
- package/lib/getFileAge.d.ts +0 -7
- package/lib/getFileAge.js +0 -11
- package/lib/getFirstFileInDir.cjs +0 -26
- package/lib/getFirstFileInDir.d.cts +0 -6
- package/lib/getFirstFileInDir.d.ts +0 -6
- package/lib/getFirstFileInDir.js +0 -19
- package/lib/readFileFirstLine.cjs +0 -26
- package/lib/readFileFirstLine.d.cts +0 -6
- package/lib/readFileFirstLine.d.ts +0 -6
- package/lib/readFileFirstLine.js +0 -18
- package/lib/removeDataUrlSchemePrefix.cjs +0 -10
- package/lib/removeDataUrlSchemePrefix.d.cts +0 -8
- package/lib/removeDataUrlSchemePrefix.d.ts +0 -8
- package/lib/removeDataUrlSchemePrefix.js +0 -8
- package/lib/updateFile.cjs +0 -19
- package/lib/updateFile.d.cts +0 -7
- package/lib/updateFile.d.ts +0 -7
- package/lib/updateFile.js +0 -12
- package/lib/updateFileLines.cjs +0 -21
- package/lib/updateFileLines.d.cts +0 -8
- package/lib/updateFileLines.d.ts +0 -8
- package/lib/updateFileLines.js +0 -14
- package/lib/updateFileLinesSync.cjs +0 -21
- package/lib/updateFileLinesSync.d.cts +0 -6
- package/lib/updateFileLinesSync.d.ts +0 -6
- package/lib/updateFileLinesSync.js +0 -14
- package/lib/updateFileSync.cjs +0 -19
- package/lib/updateFileSync.d.cts +0 -6
- package/lib/updateFileSync.d.ts +0 -6
- package/lib/updateFileSync.js +0 -12
- package/lib/updateJsonFile.cjs +0 -23
- package/lib/updateJsonFile.d.cts +0 -8
- package/lib/updateJsonFile.d.ts +0 -8
- package/lib/updateJsonFile.js +0 -16
- package/lib/updateJsonFileSync.cjs +0 -23
- package/lib/updateJsonFileSync.d.cts +0 -6
- package/lib/updateJsonFileSync.d.ts +0 -6
- package/lib/updateJsonFileSync.js +0 -16
- package/lib/walkDirectory.cjs +0 -71
- package/lib/walkDirectory.d.cts +0 -48
- package/lib/walkDirectory.d.ts +0 -48
- package/lib/walkDirectory.js +0 -62
package/lib/index.cjs
CHANGED
|
@@ -1,89 +1,188 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
var removeDataUrlSchemePrefix = require('./removeDataUrlSchemePrefix');
|
|
8
|
-
var updateFile = require('./updateFile');
|
|
9
|
-
var updateFileLines = require('./updateFileLines');
|
|
10
|
-
var updateFileLinesSync = require('./updateFileLinesSync');
|
|
11
|
-
var updateFileSync = require('./updateFileSync');
|
|
12
|
-
var updateJsonFile = require('./updateJsonFile');
|
|
13
|
-
var updateJsonFileSync = require('./updateJsonFileSync');
|
|
14
|
-
var walkDirectory = require('./walkDirectory');
|
|
3
|
+
var fs5 = require('fs-extra');
|
|
4
|
+
var readline = require('readline');
|
|
5
|
+
var upath = require('upath');
|
|
6
|
+
var walkdir = require('walkdir');
|
|
15
7
|
|
|
8
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
16
9
|
|
|
10
|
+
var fs5__default = /*#__PURE__*/_interopDefault(fs5);
|
|
11
|
+
var readline__default = /*#__PURE__*/_interopDefault(readline);
|
|
12
|
+
var upath__default = /*#__PURE__*/_interopDefault(upath);
|
|
13
|
+
var walkdir__default = /*#__PURE__*/_interopDefault(walkdir);
|
|
17
14
|
|
|
18
|
-
Object.
|
|
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
|
-
|
|
15
|
+
var __defProp = Object.defineProperty;
|
|
16
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
17
|
+
async function getFileAge(filepath) {
|
|
18
|
+
const stats = await fs5__default.default.stat(filepath);
|
|
19
|
+
const time = stats.ctimeMs || stats.birthtimeMs || stats.mtimeMs;
|
|
20
|
+
return Date.now() - time;
|
|
21
|
+
}
|
|
22
|
+
__name(getFileAge, "getFileAge");
|
|
23
|
+
|
|
24
|
+
// src/deleteOlderThan.ts
|
|
25
|
+
async function deleteOlderThan(dp, ageThreshold) {
|
|
26
|
+
const files = await fs5__default.default.readdir(dp);
|
|
27
|
+
for (const file of files) {
|
|
28
|
+
const actualAge = await getFileAge(`${dp}/${file}`);
|
|
29
|
+
if (actualAge > ageThreshold) {
|
|
30
|
+
await fs5__default.default.remove(`${dp}/${file}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
__name(deleteOlderThan, "deleteOlderThan");
|
|
35
|
+
async function getFirstFileInDir(dirpath) {
|
|
36
|
+
const dirents = await fs5__default.default.readdir(dirpath, {
|
|
37
|
+
withFileTypes: true
|
|
38
|
+
});
|
|
39
|
+
const fileDirents = dirents.filter((file) => {
|
|
40
|
+
return file.isFile();
|
|
41
|
+
});
|
|
42
|
+
const filenames = fileDirents.map((file) => {
|
|
43
|
+
return file.name;
|
|
44
|
+
});
|
|
45
|
+
const firstFileName = filenames[0];
|
|
46
|
+
return firstFileName;
|
|
47
|
+
}
|
|
48
|
+
__name(getFirstFileInDir, "getFirstFileInDir");
|
|
49
|
+
async function readFileFirstLine(filepath) {
|
|
50
|
+
const fileStream = fs5__default.default.createReadStream(filepath, "utf8");
|
|
51
|
+
const rl = readline__default.default.createInterface({
|
|
52
|
+
input: fileStream,
|
|
53
|
+
crlfDelay: Infinity
|
|
54
|
+
});
|
|
55
|
+
for await (const line of rl) {
|
|
56
|
+
return line.trim();
|
|
57
|
+
}
|
|
58
|
+
return "";
|
|
59
|
+
}
|
|
60
|
+
__name(readFileFirstLine, "readFileFirstLine");
|
|
61
|
+
|
|
62
|
+
// src/removeDataUrlSchemePrefix.ts
|
|
63
|
+
function removeDataUrlSchemePrefix(dataUrl) {
|
|
64
|
+
return dataUrl.replace(/^data:.+;base64,/, "");
|
|
65
|
+
}
|
|
66
|
+
__name(removeDataUrlSchemePrefix, "removeDataUrlSchemePrefix");
|
|
67
|
+
async function updateFile(filepath, update) {
|
|
68
|
+
await fs5__default.default.ensureFile(filepath);
|
|
69
|
+
const content = await fs5__default.default.readFile(filepath, "utf8");
|
|
70
|
+
const updated = await update(content);
|
|
71
|
+
await fs5__default.default.outputFile(filepath, updated);
|
|
72
|
+
}
|
|
73
|
+
__name(updateFile, "updateFile");
|
|
74
|
+
async function updateFileLines(filepath, update) {
|
|
75
|
+
await fs5__default.default.ensureFile(filepath);
|
|
76
|
+
const content = await fs5__default.default.readFile(filepath, "utf8");
|
|
77
|
+
const lines = content.split(/\r?\n/);
|
|
78
|
+
const retval = await update(lines);
|
|
79
|
+
const updated = Array.isArray(retval) ? retval.join("\n") : retval;
|
|
80
|
+
await fs5__default.default.outputFile(filepath, updated);
|
|
81
|
+
}
|
|
82
|
+
__name(updateFileLines, "updateFileLines");
|
|
83
|
+
function updateFileLinesSync(filepath, update) {
|
|
84
|
+
fs5__default.default.ensureFileSync(filepath);
|
|
85
|
+
const content = fs5__default.default.readFileSync(filepath, "utf8");
|
|
86
|
+
const lines = content.split(/\r?\n/);
|
|
87
|
+
const retval = update(lines);
|
|
88
|
+
const updated = Array.isArray(retval) ? retval.join("\n") : retval;
|
|
89
|
+
fs5__default.default.outputFileSync(filepath, updated);
|
|
90
|
+
}
|
|
91
|
+
__name(updateFileLinesSync, "updateFileLinesSync");
|
|
92
|
+
function updateFileSync(filepath, update) {
|
|
93
|
+
fs5__default.default.ensureFileSync(filepath);
|
|
94
|
+
const content = fs5__default.default.readFileSync(filepath, "utf8");
|
|
95
|
+
const updated = update(content);
|
|
96
|
+
fs5__default.default.outputFileSync(filepath, updated);
|
|
97
|
+
}
|
|
98
|
+
__name(updateFileSync, "updateFileSync");
|
|
99
|
+
async function updateJsonFile(filepath, update, defaultValue = {}) {
|
|
100
|
+
let content = defaultValue;
|
|
101
|
+
try {
|
|
102
|
+
content = await fs5__default.default.readJson(filepath);
|
|
103
|
+
} catch (_) {
|
|
104
|
+
}
|
|
105
|
+
const updated = await update(content);
|
|
106
|
+
await fs5__default.default.outputJson(filepath, updated);
|
|
107
|
+
return updated;
|
|
108
|
+
}
|
|
109
|
+
__name(updateJsonFile, "updateJsonFile");
|
|
110
|
+
function updateJsonFileSync(filepath, update, defaultValue = {}) {
|
|
111
|
+
let content = defaultValue;
|
|
112
|
+
try {
|
|
113
|
+
content = fs5__default.default.readJsonSync(filepath);
|
|
114
|
+
} catch (_) {
|
|
115
|
+
}
|
|
116
|
+
const updated = update(content);
|
|
117
|
+
fs5__default.default.outputJsonSync(filepath, updated);
|
|
118
|
+
return updated;
|
|
119
|
+
}
|
|
120
|
+
__name(updateJsonFileSync, "updateJsonFileSync");
|
|
121
|
+
function walkDirectory(dirpath, options) {
|
|
122
|
+
dirpath = upath__default.default.normalizeSafe(dirpath);
|
|
123
|
+
if (!options.stats && !options.only) {
|
|
124
|
+
return walkdir__default.default.sync(dirpath, {
|
|
125
|
+
...convertToWalkdirOptions(options),
|
|
126
|
+
return_object: false
|
|
127
|
+
}).map((fspath) => {
|
|
128
|
+
return upath__default.default.normalizeSafe(fspath);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
let entries = Object.entries(walkdir__default.default.sync(dirpath, convertToWalkdirOptions(options)));
|
|
132
|
+
if (options.only === "files") {
|
|
133
|
+
entries = entries.filter(([_, stats]) => {
|
|
134
|
+
return stats.isFile();
|
|
135
|
+
});
|
|
136
|
+
} else if (options.only === "directories") {
|
|
137
|
+
entries = entries.filter(([_, stats]) => {
|
|
138
|
+
return stats.isDirectory();
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
entries = entries.map(([fspath, stats]) => {
|
|
142
|
+
return [
|
|
143
|
+
upath__default.default.normalizeSafe(fspath),
|
|
144
|
+
stats
|
|
145
|
+
];
|
|
146
|
+
});
|
|
147
|
+
return options.stats ? entries : entries.map(([fspath]) => {
|
|
148
|
+
return fspath;
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
__name(walkDirectory, "walkDirectory");
|
|
152
|
+
function convertToWalkdirOptions(options) {
|
|
153
|
+
const result = {
|
|
154
|
+
fs: fs5__default.default,
|
|
155
|
+
sync: true,
|
|
156
|
+
return_object: true
|
|
157
|
+
};
|
|
158
|
+
if (options.maxDepth !== void 0) {
|
|
159
|
+
result.max_depth = options.maxDepth;
|
|
160
|
+
}
|
|
161
|
+
if (options.followSymlinks !== void 0) {
|
|
162
|
+
result.follow_symlinks = options.followSymlinks;
|
|
163
|
+
}
|
|
164
|
+
if (options.ignoreInodes !== void 0) {
|
|
165
|
+
result.track_inodes = !options.ignoreInodes;
|
|
166
|
+
}
|
|
167
|
+
if (options.filter) {
|
|
168
|
+
const filter = options.filter;
|
|
169
|
+
result.filter = (dirpath, files) => {
|
|
170
|
+
return filter(upath__default.default.normalizeSafe(dirpath), upath__default.default.basename(dirpath)) ? files : [];
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
return result;
|
|
174
|
+
}
|
|
175
|
+
__name(convertToWalkdirOptions, "convertToWalkdirOptions");
|
|
176
|
+
|
|
177
|
+
exports.deleteOlderThan = deleteOlderThan;
|
|
178
|
+
exports.getFileAge = getFileAge;
|
|
179
|
+
exports.getFirstFileInDir = getFirstFileInDir;
|
|
180
|
+
exports.readFileFirstLine = readFileFirstLine;
|
|
181
|
+
exports.removeDataUrlSchemePrefix = removeDataUrlSchemePrefix;
|
|
182
|
+
exports.updateFile = updateFile;
|
|
183
|
+
exports.updateFileLines = updateFileLines;
|
|
184
|
+
exports.updateFileLinesSync = updateFileLinesSync;
|
|
185
|
+
exports.updateFileSync = updateFileSync;
|
|
186
|
+
exports.updateJsonFile = updateJsonFile;
|
|
187
|
+
exports.updateJsonFileSync = updateJsonFileSync;
|
|
188
|
+
exports.walkDirectory = walkDirectory;
|
package/lib/index.d.cts
CHANGED
|
@@ -1,13 +1,113 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
import { Stats } from 'fs-extra';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Delete files older than a given timestamp
|
|
5
|
+
* @param dp The dirpath to a folder in which to delete files from
|
|
6
|
+
* @param ageThreshold The age in milliseconds to compare against
|
|
7
|
+
*/
|
|
8
|
+
declare function deleteOlderThan(dp: string, ageThreshold: number): Promise<void>;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Retrieves the age of a file in milliseconds.
|
|
12
|
+
* @param filepath - The path to the file.
|
|
13
|
+
*/
|
|
14
|
+
declare function getFileAge(filepath: string): Promise<number>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Get the name of the first file (not directory) found in a directory.
|
|
18
|
+
*/
|
|
19
|
+
declare function getFirstFileInDir(dirpath: string): Promise<string>;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Reads the first line of a file asynchronously.
|
|
23
|
+
*/
|
|
24
|
+
declare function readFileFirstLine(filepath: string): Promise<string>;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Removes the data URL scheme prefix from a given string.
|
|
28
|
+
* @param dataUrl - The string to remove the prefix from.
|
|
29
|
+
* @returns The string with the prefix removed.
|
|
30
|
+
*/
|
|
31
|
+
declare function removeDataUrlSchemePrefix(dataUrl: string): string;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Updates a file by reading its content, applying a transformation function, and writing back the result.
|
|
35
|
+
* Creates the file and directories if they don't exist.
|
|
36
|
+
*/
|
|
37
|
+
declare function updateFile(filepath: string, update: (fileContent: string) => string | Promise<string>): Promise<void>;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Updates a file by applying a transformation function to an array of lines.
|
|
41
|
+
* The transformation can return either a string (the entire new content) or an array of lines.
|
|
42
|
+
* Creates the file and directories if they don't exist.
|
|
43
|
+
*/
|
|
44
|
+
declare function updateFileLines(filepath: string, update: (fileContentLines: string[]) => string | string[] | Promise<string | string[]>): Promise<void>;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Synchronous version of `updateFileLines`.
|
|
48
|
+
*/
|
|
49
|
+
declare function updateFileLinesSync(filepath: string, update: (fileContentLines: string[]) => string | string[]): void;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Synchronous version of `updateFile`.
|
|
53
|
+
*/
|
|
54
|
+
declare function updateFileSync(filepath: string, update: (fileContent: string) => string): void;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Updates a JSON file by applying a transformation function to the parsed content.
|
|
58
|
+
* If the file doesn't exist or can't be parsed, uses the default value.
|
|
59
|
+
* Creates the file and directories if they don't exist.
|
|
60
|
+
*/
|
|
61
|
+
declare function updateJsonFile<T extends object, R extends object>(filepath: string, update: (parsedFileContent: T) => R | Promise<R>, defaultValue?: T): Promise<R>;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Synchronous version of `updateJsonFile`.
|
|
65
|
+
*/
|
|
66
|
+
declare function updateJsonFileSync<T extends object, R extends object>(filepath: string, update: (parsedFileContent: T) => R, defaultValue?: T): R;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Walk a directory recursively and return an array of paths.
|
|
70
|
+
*/
|
|
71
|
+
declare function walkDirectory(dirpath: string, options: WalkDirectoryOptionsWithoutStats): string[];
|
|
72
|
+
/**
|
|
73
|
+
* Walk a directory recursively and return an array of [path: string, stats: Stats] tuples.
|
|
74
|
+
*/
|
|
75
|
+
declare function walkDirectory(dirpath: string, options: WalkDirectoryOptionsWithStats): [path: string, stats: Stats][];
|
|
76
|
+
/**
|
|
77
|
+
* Options for @see walkDirectory
|
|
78
|
+
*/
|
|
79
|
+
interface WalkDirectoryOptions {
|
|
80
|
+
/**
|
|
81
|
+
* Return stats for each path.
|
|
82
|
+
*/
|
|
83
|
+
stats?: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Only return either files or directories.
|
|
86
|
+
*/
|
|
87
|
+
only?: 'files' | 'directories';
|
|
88
|
+
/**
|
|
89
|
+
* Recursion depth limit.
|
|
90
|
+
*/
|
|
91
|
+
maxDepth?: number;
|
|
92
|
+
/**
|
|
93
|
+
* Determine whether to walk a given directory.
|
|
94
|
+
*/
|
|
95
|
+
filter?: (dirpath: string, basename: string) => boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Follow symbolic links.
|
|
98
|
+
*/
|
|
99
|
+
followSymlinks?: boolean;
|
|
100
|
+
/**
|
|
101
|
+
* On filesystems where inodes are not unique (eg. windows), some files may not be walked due to inode collision.
|
|
102
|
+
* Turning off this behavior may be required but at the same time may lead to hitting max_depth via link loop.
|
|
103
|
+
*/
|
|
104
|
+
ignoreInodes?: boolean;
|
|
105
|
+
}
|
|
106
|
+
interface WalkDirectoryOptionsWithoutStats extends WalkDirectoryOptions {
|
|
107
|
+
stats?: false;
|
|
108
|
+
}
|
|
109
|
+
interface WalkDirectoryOptionsWithStats extends WalkDirectoryOptions {
|
|
110
|
+
stats: true;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export { type WalkDirectoryOptions, type WalkDirectoryOptionsWithStats, type WalkDirectoryOptionsWithoutStats, deleteOlderThan, getFileAge, getFirstFileInDir, readFileFirstLine, removeDataUrlSchemePrefix, updateFile, updateFileLines, updateFileLinesSync, updateFileSync, updateJsonFile, updateJsonFileSync, walkDirectory };
|
package/lib/index.d.ts
CHANGED
|
@@ -1,13 +1,113 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
import { Stats } from 'fs-extra';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Delete files older than a given timestamp
|
|
5
|
+
* @param dp The dirpath to a folder in which to delete files from
|
|
6
|
+
* @param ageThreshold The age in milliseconds to compare against
|
|
7
|
+
*/
|
|
8
|
+
declare function deleteOlderThan(dp: string, ageThreshold: number): Promise<void>;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Retrieves the age of a file in milliseconds.
|
|
12
|
+
* @param filepath - The path to the file.
|
|
13
|
+
*/
|
|
14
|
+
declare function getFileAge(filepath: string): Promise<number>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Get the name of the first file (not directory) found in a directory.
|
|
18
|
+
*/
|
|
19
|
+
declare function getFirstFileInDir(dirpath: string): Promise<string>;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Reads the first line of a file asynchronously.
|
|
23
|
+
*/
|
|
24
|
+
declare function readFileFirstLine(filepath: string): Promise<string>;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Removes the data URL scheme prefix from a given string.
|
|
28
|
+
* @param dataUrl - The string to remove the prefix from.
|
|
29
|
+
* @returns The string with the prefix removed.
|
|
30
|
+
*/
|
|
31
|
+
declare function removeDataUrlSchemePrefix(dataUrl: string): string;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Updates a file by reading its content, applying a transformation function, and writing back the result.
|
|
35
|
+
* Creates the file and directories if they don't exist.
|
|
36
|
+
*/
|
|
37
|
+
declare function updateFile(filepath: string, update: (fileContent: string) => string | Promise<string>): Promise<void>;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Updates a file by applying a transformation function to an array of lines.
|
|
41
|
+
* The transformation can return either a string (the entire new content) or an array of lines.
|
|
42
|
+
* Creates the file and directories if they don't exist.
|
|
43
|
+
*/
|
|
44
|
+
declare function updateFileLines(filepath: string, update: (fileContentLines: string[]) => string | string[] | Promise<string | string[]>): Promise<void>;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Synchronous version of `updateFileLines`.
|
|
48
|
+
*/
|
|
49
|
+
declare function updateFileLinesSync(filepath: string, update: (fileContentLines: string[]) => string | string[]): void;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Synchronous version of `updateFile`.
|
|
53
|
+
*/
|
|
54
|
+
declare function updateFileSync(filepath: string, update: (fileContent: string) => string): void;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Updates a JSON file by applying a transformation function to the parsed content.
|
|
58
|
+
* If the file doesn't exist or can't be parsed, uses the default value.
|
|
59
|
+
* Creates the file and directories if they don't exist.
|
|
60
|
+
*/
|
|
61
|
+
declare function updateJsonFile<T extends object, R extends object>(filepath: string, update: (parsedFileContent: T) => R | Promise<R>, defaultValue?: T): Promise<R>;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Synchronous version of `updateJsonFile`.
|
|
65
|
+
*/
|
|
66
|
+
declare function updateJsonFileSync<T extends object, R extends object>(filepath: string, update: (parsedFileContent: T) => R, defaultValue?: T): R;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Walk a directory recursively and return an array of paths.
|
|
70
|
+
*/
|
|
71
|
+
declare function walkDirectory(dirpath: string, options: WalkDirectoryOptionsWithoutStats): string[];
|
|
72
|
+
/**
|
|
73
|
+
* Walk a directory recursively and return an array of [path: string, stats: Stats] tuples.
|
|
74
|
+
*/
|
|
75
|
+
declare function walkDirectory(dirpath: string, options: WalkDirectoryOptionsWithStats): [path: string, stats: Stats][];
|
|
76
|
+
/**
|
|
77
|
+
* Options for @see walkDirectory
|
|
78
|
+
*/
|
|
79
|
+
interface WalkDirectoryOptions {
|
|
80
|
+
/**
|
|
81
|
+
* Return stats for each path.
|
|
82
|
+
*/
|
|
83
|
+
stats?: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Only return either files or directories.
|
|
86
|
+
*/
|
|
87
|
+
only?: 'files' | 'directories';
|
|
88
|
+
/**
|
|
89
|
+
* Recursion depth limit.
|
|
90
|
+
*/
|
|
91
|
+
maxDepth?: number;
|
|
92
|
+
/**
|
|
93
|
+
* Determine whether to walk a given directory.
|
|
94
|
+
*/
|
|
95
|
+
filter?: (dirpath: string, basename: string) => boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Follow symbolic links.
|
|
98
|
+
*/
|
|
99
|
+
followSymlinks?: boolean;
|
|
100
|
+
/**
|
|
101
|
+
* On filesystems where inodes are not unique (eg. windows), some files may not be walked due to inode collision.
|
|
102
|
+
* Turning off this behavior may be required but at the same time may lead to hitting max_depth via link loop.
|
|
103
|
+
*/
|
|
104
|
+
ignoreInodes?: boolean;
|
|
105
|
+
}
|
|
106
|
+
interface WalkDirectoryOptionsWithoutStats extends WalkDirectoryOptions {
|
|
107
|
+
stats?: false;
|
|
108
|
+
}
|
|
109
|
+
interface WalkDirectoryOptionsWithStats extends WalkDirectoryOptions {
|
|
110
|
+
stats: true;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export { type WalkDirectoryOptions, type WalkDirectoryOptionsWithStats, type WalkDirectoryOptionsWithoutStats, deleteOlderThan, getFileAge, getFirstFileInDir, readFileFirstLine, removeDataUrlSchemePrefix, updateFile, updateFileLines, updateFileLinesSync, updateFileSync, updateJsonFile, updateJsonFileSync, walkDirectory };
|