/
index.js
503 lines (503 loc) · 16.6 KB
/
index.js
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
"use strict";
/**
* This file is part of the vscode-helpers distribution.
* Copyright (c) Marcel Joachim Kloubert.
*
* vscode-helpers is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, version 3.
*
* vscode-helpers is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const FastGlob = require("fast-glob");
const FS = require("fs");
const FSExtra = require("fs-extra");
const Glob = require("glob");
const MergeDeep = require('merge-deep');
const Path = require("path");
const TMP = require("tmp");
const vscode_helpers = require("../index");
const vscode_workflows = require("../workflows");
/**
* Creates a directory (if needed).
*
* @param {string} dir The path of the directory to create.
*
* @return {Promise<boolean>} The promise that indicates if directory has been created or not.
*/
async function createDirectoryIfNeeded(dir) {
dir = vscode_helpers.toStringSafe(dir);
if (!(await exists(dir))) {
await FSExtra.mkdirs(dir);
return true;
}
return false;
}
exports.createDirectoryIfNeeded = createDirectoryIfNeeded;
/**
* Promise version of 'FS.exists()' function.
*
* @param {string|Buffer} path The path.
*
* @return {Promise<boolean>} The promise that indicates if path exists or not.
*/
function exists(path) {
return new Promise((resolve, reject) => {
const COMPLETED = vscode_helpers.createCompletedAction(resolve, reject);
try {
FS.exists(path, (doesExist) => {
COMPLETED(null, doesExist);
});
}
catch (e) {
COMPLETED(e);
}
});
}
exports.exists = exists;
/**
* Fast version of 'node-glob'.
*
* @param {string|string[]} patterns One or more patterns to search for.
* @param {FastGlob.Options} [opts] Custom options.
*
* @return {Promise<FastGlobEntryItem[]>} Promise with the found files / directories.
*/
function fastGlob(patterns, opts) {
return FastGlob(patterns, opts);
}
exports.fastGlob = fastGlob;
/**
* Fast version of 'node-glob' (sync).
*
* @param {string|string[]} patterns One or more patterns to search for.
* @param {FastGlob.Options} [opts] Custom options.
*
* @return {FastGlobEntryItem[]} The found files / directories.
*/
function fastGlobSync(patterns, opts) {
return FastGlob.sync(patterns, opts);
}
exports.fastGlobSync = fastGlobSync;
/**
* Promise version of 'Glob()' function.
*
* @param {string|string[]} patterns One or more patterns.
* @param {Glob.IOptions} [opts] Custom options.
*
* @return {Promise<string[]>} The promise with the matches.
*/
async function glob(patterns, opts) {
opts = normalizeGlobOptions(opts, {
sync: false,
});
const WF = vscode_workflows.buildWorkflow();
WF.next(() => {
return [];
});
vscode_helpers.asArray(patterns).forEach(p => {
WF.next((allMatches) => {
return new Promise((res, rej) => {
const COMPLETED = vscode_helpers.createCompletedAction(res, rej);
try {
Glob(p, opts, (err, matches) => {
if (err) {
COMPLETED(err);
}
else {
allMatches.push
.apply(allMatches, matches);
COMPLETED(null, allMatches);
}
});
}
catch (e) {
COMPLETED(e);
}
});
});
});
return vscode_helpers.from(await WF.start())
.select(m => Path.resolve(m))
.distinct()
.toArray();
}
exports.glob = glob;
/**
* Multi pattern version of 'Glob.sync()' function.
*
* @param {string|string[]} patterns One or more patterns.
* @param {Glob.IOptions} [opts] Custom options.
*
* @return {string[]} The matches.
*/
function globSync(patterns, opts) {
opts = normalizeGlobOptions(opts, {
sync: true,
});
const ALL_MATCHES = [];
vscode_helpers.asArray(patterns).forEach(p => {
ALL_MATCHES.push
.apply(ALL_MATCHES, Glob.sync(p, opts));
});
return vscode_helpers.from(ALL_MATCHES)
.select(m => Path.resolve(m))
.distinct()
.toArray();
}
exports.globSync = globSync;
async function invokeForStats(path, useLSTAT, func, defaultValue) {
path = vscode_helpers.toStringSafe(path);
useLSTAT = vscode_helpers.toBooleanSafe(useLSTAT, true);
if (await exists(path)) {
const STATS = useLSTAT ? (await FSExtra.lstat(path))
: (await FSExtra.stat(path));
if (STATS) {
return func(STATS);
}
}
return defaultValue;
}
function invokeForStatsSync(path, useLSTAT, func, defaultValue) {
path = vscode_helpers.toStringSafe(path);
useLSTAT = vscode_helpers.toBooleanSafe(useLSTAT, true);
if (FS.existsSync(path)) {
const STATS = useLSTAT ? FS.lstatSync(path)
: FS.statSync(path);
if (STATS) {
return func(STATS);
}
}
return defaultValue;
}
/**
* Checks if a path exists and is a block device.
*
* @param {string} path The path to check.
* @param {boolean} [useLSTAT] If (true) use 'FS.lstat()' function, otherwise 'FS.stat()'.
*
* @return {Promise<boolean>} The promise with the value that indicates if condition matches or not.
*/
async function isBlockDevice(path, useLSTAT = true) {
return invokeForStats(path, useLSTAT, (stats) => stats.isBlockDevice(), false);
}
exports.isBlockDevice = isBlockDevice;
/**
* Checks if a path exists and is a block device.
*
* @param {string} path The path to check.
* @param {boolean} [useLSTAT] If (true) use 'FS.lstat()' function, otherwise 'FS.stat()'.
*
* @return {boolean} A value that indicates if condition matches or not.
*/
function isBlockDeviceSync(path, useLSTAT = true) {
return invokeForStatsSync(path, useLSTAT, (stats) => stats.isBlockDevice(), false);
}
exports.isBlockDeviceSync = isBlockDeviceSync;
/**
* Checks if a path exists and is a character device.
*
* @param {string} path The path to check.
* @param {boolean} [useLSTAT] If (true) use 'FS.lstat()' function, otherwise 'FS.stat()'.
*
* @return {Promise<boolean>} The promise with the value that indicates if condition matches or not.
*/
async function isCharacterDevice(path, useLSTAT = true) {
return invokeForStats(path, useLSTAT, (stats) => stats.isCharacterDevice(), false);
}
exports.isCharacterDevice = isCharacterDevice;
/**
* Checks if a path exists and is a character device.
*
* @param {string} path The path to check.
* @param {boolean} [useLSTAT] If (true) use 'FS.lstat()' function, otherwise 'FS.stat()'.
*
* @return {boolean} A value that indicates if condition matches or not.
*/
function isCharacterDeviceSync(path, useLSTAT = true) {
return invokeForStatsSync(path, useLSTAT, (stats) => stats.isCharacterDevice(), false);
}
exports.isCharacterDeviceSync = isCharacterDeviceSync;
/**
* Checks if a path exists and is a directory.
*
* @param {string} path The path to check.
* @param {boolean} [useLSTAT] If (true) use 'FS.lstat()' function, otherwise 'FS.stat()'.
*
* @return {Promise<boolean>} The promise with the value that indicates if condition matches or not.
*/
async function isDirectory(path, useLSTAT = true) {
return invokeForStats(path, useLSTAT, (stats) => stats.isDirectory(), false);
}
exports.isDirectory = isDirectory;
/**
* Checks if a path exists and is a directory.
*
* @param {string} path The path to check.
* @param {boolean} [useLSTAT] If (true) use 'FS.lstat()' function, otherwise 'FS.stat()'.
*
* @return {boolean} A value that indicates if condition matches or not.
*/
function isDirectorySync(path, useLSTAT = true) {
return invokeForStatsSync(path, useLSTAT, (stats) => stats.isDirectory(), false);
}
exports.isDirectorySync = isDirectorySync;
/**
* Checks if a path exists and is FIFO.
*
* @param {string} path The path to check.
* @param {boolean} [useLSTAT] If (true) use 'FS.lstat()' function, otherwise 'FS.stat()'.
*
* @return {Promise<boolean>} The promise with the value that indicates if condition matches or not.
*/
async function isFIFO(path, useLSTAT = true) {
return invokeForStats(path, useLSTAT, (stats) => stats.isFIFO(), false);
}
exports.isFIFO = isFIFO;
/**
* Checks if a path exists and is FIFO.
*
* @param {string} path The path to check.
* @param {boolean} [useLSTAT] If (true) use 'FS.lstat()' function, otherwise 'FS.stat()'.
*
* @return {boolean} A value that indicates if condition matches or not.
*/
function isFIFOSync(path, useLSTAT = true) {
return invokeForStatsSync(path, useLSTAT, (stats) => stats.isFIFO(), false);
}
exports.isFIFOSync = isFIFOSync;
/**
* Checks if a path exists and is a file.
*
* @param {string} path The path to check.
* @param {boolean} [useLSTAT] If (true) use 'FS.lstat()' function, otherwise 'FS.stat()'.
*
* @return {Promise<boolean>} The promise with the value that indicates if condition matches or not.
*/
async function isFile(path, useLSTAT = true) {
return invokeForStats(path, useLSTAT, (stats) => stats.isFile(), false);
}
exports.isFile = isFile;
/**
* Checks if a path exists and is a file.
*
* @param {string} path The path to check.
* @param {boolean} [useLSTAT] If (true) use 'FS.lstat()' function, otherwise 'FS.stat()'.
*
* @return {boolean} A value that indicates if condition matches or not.
*/
function isFileSync(path, useLSTAT = true) {
return invokeForStatsSync(path, useLSTAT, (stats) => stats.isFile(), false);
}
exports.isFileSync = isFileSync;
/**
* Checks if a path exists and is a socket.
*
* @param {string} path The path to check.
* @param {boolean} [useLSTAT] If (true) use 'FS.lstat()' function, otherwise 'FS.stat()'.
*
* @return {Promise<boolean>} The promise with the value that indicates if condition matches or not.
*/
async function isSocket(path, useLSTAT = true) {
return invokeForStats(path, useLSTAT, (stats) => stats.isSocket(), false);
}
exports.isSocket = isSocket;
/**
* Checks if a path exists and is a socket.
*
* @param {string} path The path to check.
* @param {boolean} [useLSTAT] If (true) use 'FS.lstat()' function, otherwise 'FS.stat()'.
*
* @return {boolean} A value that indicates if condition matches or not.
*/
function isSocketSync(path, useLSTAT = true) {
return invokeForStatsSync(path, useLSTAT, (stats) => stats.isSocket(), false);
}
exports.isSocketSync = isSocketSync;
/**
* Checks if a path exists and is a symbolic link.
*
* @param {string} path The path to check.
* @param {boolean} [useLSTAT] If (true) use 'FS.lstat()' function, otherwise 'FS.stat()'.
*
* @return {Promise<boolean>} The promise with the value that indicates if condition matches or not.
*/
async function isSymbolicLink(path, useLSTAT = true) {
return invokeForStats(path, useLSTAT, (stats) => stats.isSymbolicLink(), false);
}
exports.isSymbolicLink = isSymbolicLink;
/**
* Checks if a path exists and is a symbolic link.
*
* @param {string} path The path to check.
* @param {boolean} [useLSTAT] If (true) use 'FS.lstat()' function, otherwise 'FS.stat()'.
*
* @return {boolean} A value that indicates if condition matches or not.
*/
function isSymbolicLinkSync(path, useLSTAT = true) {
return invokeForStatsSync(path, useLSTAT, (stats) => stats.isSymbolicLink(), false);
}
exports.isSymbolicLinkSync = isSymbolicLinkSync;
function normalizeGlobOptions(opts, callerDefaultOpts) {
const DEFAULT_OPTS = {
absolute: true,
dot: false,
nocase: true,
nodir: true,
nonull: false,
nosort: false,
};
return MergeDeep({}, DEFAULT_OPTS, callerDefaultOpts, opts);
}
function normalizeTempFileOptions(opts) {
const DEFAULT_OPTS = {};
opts = MergeDeep({}, DEFAULT_OPTS, opts);
opts.dir = vscode_helpers.toStringSafe(opts.dir);
if (vscode_helpers.isEmptyString(opts.dir)) {
opts.dir = undefined;
}
opts.prefix = vscode_helpers.toStringSafe(opts.prefix);
if ('' === opts.prefix) {
opts.prefix = undefined;
}
opts.suffix = vscode_helpers.toStringSafe(opts.suffix);
if ('' === opts.suffix) {
opts.suffix = undefined;
}
return opts;
}
/**
* Returns the size of a file system element.
*
* @param {string|Buffer} path The path to the element.
* @param {boolean} [useLSTAT] Use 'lstat()' (true) or 'stat()' (false) function.
*
* @return {Promise<number>} The promise with the size.
*/
async function size(path, useLSTAT = true) {
useLSTAT = vscode_helpers.toBooleanSafe(useLSTAT, true);
return useLSTAT ? (await FSExtra.lstat(path)).size
: (await FSExtra.stat(path)).size;
}
exports.size = size;
/**
* Returns the size of a file system element (sync).
*
* @param {string|Buffer} path The path to the element.
* @param {boolean} [useLSTAT] Use 'lstatSync()' (true) or 'statSync()' (false) function.
*
* @return {number} The size.
*/
function sizeSync(path, useLSTAT = true) {
useLSTAT = vscode_helpers.toBooleanSafe(useLSTAT, true);
return useLSTAT ? FSExtra.lstatSync(path).size
: FSExtra.statSync(path).size;
}
exports.sizeSync = sizeSync;
/**
* Invokes an action for a temp file.
*
* @param {Function} action The action to invoke.
* @param {TempFileOptions} [opts] The custom options.
*
* @return {Promise<TResult>} The promise with the result of the action.
*/
function tempFile(action, opts) {
opts = normalizeTempFileOptions(opts);
return new Promise((resolve, reject) => {
let completedInvoked = false;
let tempFile = false;
const COMPLETED = (err, result) => {
if (completedInvoked) {
return;
}
completedInvoked = true;
try {
if (err) {
reject(err);
}
else {
resolve(result);
}
}
finally {
tryUnlinkTempFile(tempFile, opts);
}
};
try {
TMP.tmpName(toTmpSimpleOptions(opts), (err, path) => {
if (err) {
COMPLETED(err);
}
else {
tempFile = path;
try {
Promise.resolve(action(tempFile)).then((result) => {
COMPLETED(null, result);
}).catch((e) => {
COMPLETED(e);
});
}
catch (e) {
COMPLETED(e);
}
}
});
}
catch (e) {
COMPLETED(e);
}
});
}
exports.tempFile = tempFile;
function toTmpSimpleOptions(opts) {
return {
dir: opts.dir,
keep: true,
prefix: opts.prefix,
postfix: opts.suffix,
};
}
/**
* Invokes an action for a temp file (sync).
*
* @param {Function} action The action to invoke.
* @param {TempFileOptions} [opts] The custom options.
*
* @return {TResult} The result of the action.
*/
function tempFileSync(action, opts) {
opts = normalizeTempFileOptions(opts);
let tempFile = false;
try {
tempFile = TMP.tmpNameSync(toTmpSimpleOptions(opts));
return action(tempFile);
}
finally {
tryUnlinkTempFile(tempFile, opts);
}
}
exports.tempFileSync = tempFileSync;
function tryUnlinkTempFile(file, opts) {
try {
if (false !== file) {
if (!vscode_helpers.toBooleanSafe(opts.keep)) {
if (isFileSync(file)) {
FSExtra.unlinkSync(file);
}
}
}
return true;
}
catch (_a) {
return false;
}
}
//# sourceMappingURL=index.js.map