1 | ;
|
2 | /**
|
3 | * This file is part of the @egodigital/egoose distribution.
|
4 | * Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/)
|
5 | *
|
6 | * @egodigital/egoose is free software: you can redistribute it and/or modify
|
7 | * it under the terms of the GNU Lesser General Public License as
|
8 | * published by the Free Software Foundation, version 3.
|
9 | *
|
10 | * @egodigital/egoose is distributed in the hope that it will be useful, but
|
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13 | * Lesser General Public License for more details.
|
14 | *
|
15 | * You should have received a copy of the GNU Lesser General Public License
|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17 | */
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | const _ = require("lodash");
|
20 | const fastGlob = require("fast-glob");
|
21 | const fs = require("fs-extra");
|
22 | const MergeDeep = require("merge-deep");
|
23 | const tmp = require("tmp");
|
24 | const index_1 = require("../index");
|
25 | async function checkExistingFSItemByStats(path, useLSTAT, flagProvider) {
|
26 | useLSTAT = index_1.toBooleanSafe(useLSTAT);
|
27 | if (await exists(path)) {
|
28 | return flagProvider(await (useLSTAT ? fs.lstat : fs.stat)(path));
|
29 | }
|
30 | return false;
|
31 | }
|
32 | function checkExistingFSItemByStatsSync(path, useLSTAT, flagProvider) {
|
33 | useLSTAT = index_1.toBooleanSafe(useLSTAT);
|
34 | if (fs.existsSync(path)) {
|
35 | return flagProvider((useLSTAT ? fs.lstatSync : fs.statSync)(path));
|
36 | }
|
37 | return false;
|
38 | }
|
39 | /**
|
40 | * Promise version of 'fs.exists()'.
|
41 | *
|
42 | * @param {string} path The path.
|
43 | *
|
44 | * @return {Promise<boolean>} The promise that indicates if path exists or not.
|
45 | */
|
46 | function exists(path) {
|
47 | return new Promise((resolve, reject) => {
|
48 | try {
|
49 | fs.exists(path, (itemExists) => {
|
50 | resolve(itemExists);
|
51 | });
|
52 | }
|
53 | catch (e) {
|
54 | reject(e);
|
55 | }
|
56 | });
|
57 | }
|
58 | exports.exists = exists;
|
59 | /**
|
60 | * Scans for files.
|
61 | *
|
62 | * @param {string|string[]} patterns One or more glob patterns.
|
63 | * @param {Partial<IOptions<EntryItem>>} [opts] Custom options.
|
64 | *
|
65 | * @return {Promise<EntryItem[]>} The promise with the found items.
|
66 | */
|
67 | async function glob(patterns, opts) {
|
68 | return await fastGlob(normalizeGlobPatterns(patterns), mergeGlobOptions(opts));
|
69 | }
|
70 | exports.glob = glob;
|
71 | /**
|
72 | * Scans for files (synchronious).
|
73 | *
|
74 | * @param {string|string[]} patterns One or more glob patterns.
|
75 | * @param {Partial<IOptions<EntryItem>>} [opts] Custom options.
|
76 | *
|
77 | * @return {EntryItem[]} The found items.
|
78 | */
|
79 | function globSync(patterns, opts) {
|
80 | return fastGlob.sync(normalizeGlobPatterns(patterns), mergeGlobOptions(opts));
|
81 | }
|
82 | exports.globSync = globSync;
|
83 | /**
|
84 | * Checks if a path represents an existing block device.
|
85 | *
|
86 | * @param {FileSystemPath} path The path to check.
|
87 | * @param {boolean} [useLSTAT] Use 'fs.lstat()' function instead of 'fs.stat()'.
|
88 | *
|
89 | * @return {Promise<boolean>} The promise with the boolean that represents if path is a block device or not.
|
90 | */
|
91 | function isBlockDevice(path, useLSTAT) {
|
92 | return checkExistingFSItemByStats(path, useLSTAT, (stats) => stats.isBlockDevice());
|
93 | }
|
94 | exports.isBlockDevice = isBlockDevice;
|
95 | /**
|
96 | * Checks if a path represents an existing block device (synchronous).
|
97 | *
|
98 | * @param {FileSystemPath} path The path to check.
|
99 | * @param {boolean} [useLSTAT] Use 'fs.lstatSync()' function instead of 'fs.statSync()'.
|
100 | *
|
101 | * @return {boolean} The boolean that represents if path is a block device or not.
|
102 | */
|
103 | function isBlockDeviceSync(path, useLSTAT) {
|
104 | return checkExistingFSItemByStatsSync(path, useLSTAT, (stats) => stats.isBlockDevice());
|
105 | }
|
106 | exports.isBlockDeviceSync = isBlockDeviceSync;
|
107 | /**
|
108 | * Checks if a path represents an existing block device.
|
109 | *
|
110 | * @param {FileSystemPath} path The path to check.
|
111 | * @param {boolean} [useLSTAT] Use 'fs.lstat()' function instead of 'fs.stat()'.
|
112 | *
|
113 | * @return {Promise<boolean>} The promise with the boolean that represents if path is a block device or not.
|
114 | */
|
115 | function isCharDevice(path, useLSTAT) {
|
116 | return checkExistingFSItemByStats(path, useLSTAT, (stats) => stats.isCharacterDevice());
|
117 | }
|
118 | exports.isCharDevice = isCharDevice;
|
119 | /**
|
120 | * Checks if a path represents an existing character device (synchronous).
|
121 | *
|
122 | * @param {FileSystemPath} path The path to check.
|
123 | * @param {boolean} [useLSTAT] Use 'fs.lstatSync()' function instead of 'fs.statSync()'.
|
124 | *
|
125 | * @return {boolean} The boolean that represents if path is a character device or not.
|
126 | */
|
127 | function isCharDeviceSync(path, useLSTAT) {
|
128 | return checkExistingFSItemByStatsSync(path, useLSTAT, (stats) => stats.isCharacterDevice());
|
129 | }
|
130 | exports.isCharDeviceSync = isCharDeviceSync;
|
131 | /**
|
132 | * Checks if a path represents an existing FIFO.
|
133 | *
|
134 | * @param {FileSystemPath} path The path to check.
|
135 | * @param {boolean} [useLSTAT] Use 'fs.lstat()' function instead of 'fs.stat()'.
|
136 | *
|
137 | * @return {Promise<boolean>} The promise with the boolean that represents if path is a FIFO or not.
|
138 | */
|
139 | function isFIFO(path, useLSTAT) {
|
140 | return checkExistingFSItemByStats(path, useLSTAT, (stats) => stats.isFIFO());
|
141 | }
|
142 | exports.isFIFO = isFIFO;
|
143 | /**
|
144 | * Checks if a path represents an existing FIFO (synchronous).
|
145 | *
|
146 | * @param {FileSystemPath} path The path to check.
|
147 | * @param {boolean} [useLSTAT] Use 'fs.lstatSync()' function instead of 'fs.statSync()'.
|
148 | *
|
149 | * @return {boolean} The boolean that represents if path is a FIFO or not.
|
150 | */
|
151 | function isFIFOSync(path, useLSTAT) {
|
152 | return checkExistingFSItemByStatsSync(path, useLSTAT, (stats) => stats.isFIFO());
|
153 | }
|
154 | exports.isFIFOSync = isFIFOSync;
|
155 | /**
|
156 | * Checks if a path represents an existing directory.
|
157 | *
|
158 | * @param {FileSystemPath} path The path to check.
|
159 | * @param {boolean} [useLSTAT] Use 'fs.lstat()' function instead of 'fs.stat()'.
|
160 | *
|
161 | * @return {Promise<boolean>} The promise with the boolean that represents if path is a directory or not.
|
162 | */
|
163 | function isDir(path, useLSTAT) {
|
164 | return checkExistingFSItemByStats(path, useLSTAT, (stats) => stats.isDirectory());
|
165 | }
|
166 | exports.isDir = isDir;
|
167 | /**
|
168 | * Checks if a path represents an existing directory (synchronous).
|
169 | *
|
170 | * @param {FileSystemPath} path The path to check.
|
171 | * @param {boolean} [useLSTAT] Use 'fs.lstatSync()' function instead of 'fs.statSync()'.
|
172 | *
|
173 | * @return {boolean} The boolean that represents if path is a directory or not.
|
174 | */
|
175 | function isDirSync(path, useLSTAT) {
|
176 | return checkExistingFSItemByStatsSync(path, useLSTAT, (stats) => stats.isDirectory());
|
177 | }
|
178 | exports.isDirSync = isDirSync;
|
179 | /**
|
180 | * Checks if a path represents an existing file.
|
181 | *
|
182 | * @param {FileSystemPath} path The path to check.
|
183 | * @param {boolean} [useLSTAT] Use 'fs.lstat()' function instead of 'fs.stat()'.
|
184 | *
|
185 | * @return {Promise<boolean>} The promise with the boolean that represents if path is a file or not.
|
186 | */
|
187 | function isFile(path, useLSTAT) {
|
188 | return checkExistingFSItemByStats(path, useLSTAT, (stats) => stats.isFile());
|
189 | }
|
190 | exports.isFile = isFile;
|
191 | /**
|
192 | * Checks if a path represents an existing file (synchronous).
|
193 | *
|
194 | * @param {FileSystemPath} path The path to check.
|
195 | * @param {boolean} [useLSTAT] Use 'fs.lstatSync()' function instead of 'fs.statSync()'.
|
196 | *
|
197 | * @return {boolean} The boolean that represents if path is a file or not.
|
198 | */
|
199 | function isFileSync(path, useLSTAT) {
|
200 | return checkExistingFSItemByStatsSync(path, useLSTAT, (stats) => stats.isFile());
|
201 | }
|
202 | exports.isFileSync = isFileSync;
|
203 | /**
|
204 | * Checks if a path represents an existing socket.
|
205 | *
|
206 | * @param {FileSystemPath} path The path to check.
|
207 | * @param {boolean} [useLSTAT] Use 'fs.lstat()' function instead of 'fs.stat()'.
|
208 | *
|
209 | * @return {Promise<boolean>} The promise with the boolean that represents if path is a socket or not.
|
210 | */
|
211 | function isSocket(path, useLSTAT) {
|
212 | return checkExistingFSItemByStats(path, useLSTAT, (stats) => stats.isSocket());
|
213 | }
|
214 | exports.isSocket = isSocket;
|
215 | /**
|
216 | * Checks if a path represents an existing socket (synchronous).
|
217 | *
|
218 | * @param {FileSystemPath} path The path to check.
|
219 | * @param {boolean} [useLSTAT] Use 'fs.lstatSync()' function instead of 'fs.statSync()'.
|
220 | *
|
221 | * @return {boolean} The boolean that represents if path is a socket or not.
|
222 | */
|
223 | function isSocketSync(path, useLSTAT) {
|
224 | return checkExistingFSItemByStatsSync(path, useLSTAT, (stats) => stats.isSocket());
|
225 | }
|
226 | exports.isSocketSync = isSocketSync;
|
227 | /**
|
228 | * Checks if a path represents an existing symbolic link.
|
229 | *
|
230 | * @param {FileSystemPath} path The path to check.
|
231 | *
|
232 | * @return {Promise<boolean>} The promise with the boolean that represents if path is a symbolic link or not.
|
233 | */
|
234 | function isSymLink(path) {
|
235 | return checkExistingFSItemByStats(path, true, (stats) => stats.isSymbolicLink());
|
236 | }
|
237 | exports.isSymLink = isSymLink;
|
238 | /**
|
239 | * Checks if a path represents an existing symbolic link (synchronous).
|
240 | *
|
241 | * @param {FileSystemPath} path The path to check.
|
242 | *
|
243 | * @return {boolean} The boolean that represents if path is a symbolic link or not.
|
244 | */
|
245 | function isSymLinkSync(path) {
|
246 | return checkExistingFSItemByStatsSync(path, true, (stats) => stats.isSymbolicLink());
|
247 | }
|
248 | exports.isSymLinkSync = isSymLinkSync;
|
249 | function mergeGlobOptions(opts) {
|
250 | const DEFAULT_OPTS = {
|
251 | absolute: true,
|
252 | deep: true,
|
253 | dot: true,
|
254 | followSymlinkedDirectories: true,
|
255 | markDirectories: false,
|
256 | onlyDirectories: false,
|
257 | onlyFiles: true,
|
258 | stats: false,
|
259 | unique: true,
|
260 | };
|
261 | return MergeDeep(DEFAULT_OPTS, opts);
|
262 | }
|
263 | function normalizeGlobPatterns(patterns) {
|
264 | return index_1.asArray(patterns).map(p => index_1.toStringSafe(p))
|
265 | .filter(p => '' !== p.trim());
|
266 | }
|
267 | /**
|
268 | * Executes an action for a temp file.
|
269 | *
|
270 | * @param {Function} action The action to invoke.
|
271 | * @param {TempFileOptions} [opts] Custom options.
|
272 | *
|
273 | * @return {Promise<TResult>} The promise with the result of the action.
|
274 | */
|
275 | function tempFile(action, opts) {
|
276 | if (_.isNil(opts)) {
|
277 | opts = {};
|
278 | }
|
279 | return new Promise((resolve, reject) => {
|
280 | try {
|
281 | tmp.tmpName(toSimpleOptions(opts), (err, tmpFile) => {
|
282 | if (err) {
|
283 | reject(err);
|
284 | }
|
285 | else {
|
286 | const TRY_DELETE_TEMP_FILE = () => {
|
287 | if (!index_1.toBooleanSafe(opts.keep)) {
|
288 | try {
|
289 | fs.unlinkSync(tmpFile);
|
290 | }
|
291 | catch (_a) { }
|
292 | }
|
293 | };
|
294 | try {
|
295 | Promise.resolve(action(tmpFile)).then((res) => {
|
296 | TRY_DELETE_TEMP_FILE();
|
297 | resolve(res);
|
298 | }).catch((err) => {
|
299 | TRY_DELETE_TEMP_FILE();
|
300 | reject(err);
|
301 | });
|
302 | }
|
303 | catch (e) {
|
304 | reject(e);
|
305 | }
|
306 | }
|
307 | });
|
308 | }
|
309 | catch (e) {
|
310 | reject(e);
|
311 | }
|
312 | });
|
313 | }
|
314 | exports.tempFile = tempFile;
|
315 | /**
|
316 | * Executes an action for a temp file (sync).
|
317 | *
|
318 | * @param {Function} action The action to invoke.
|
319 | * @param {TempFileOptions} [opts] Custom options.
|
320 | *
|
321 | * @return {TResult} The result of the action.
|
322 | */
|
323 | function tempFileSync(action, opts) {
|
324 | if (_.isNil(opts)) {
|
325 | opts = {};
|
326 | }
|
327 | const TEMP_FILE = tmp.tmpNameSync(toSimpleOptions(opts));
|
328 | try {
|
329 | return action(TEMP_FILE);
|
330 | }
|
331 | finally {
|
332 | if (!index_1.toBooleanSafe(opts.keep)) {
|
333 | try {
|
334 | fs.unlinkSync(TEMP_FILE);
|
335 | }
|
336 | catch (_a) { }
|
337 | }
|
338 | }
|
339 | }
|
340 | exports.tempFileSync = tempFileSync;
|
341 | function toSimpleOptions(opts) {
|
342 | return {
|
343 | dir: _.isNil(opts.dir) ? undefined : index_1.toStringSafe(opts.dir),
|
344 | keep: true,
|
345 | postfix: index_1.toStringSafe(opts.suffix),
|
346 | prefix: index_1.toStringSafe(opts.prefix),
|
347 | };
|
348 | }
|
349 | //# sourceMappingURL=index.js.map |
\ | No newline at end of file |