forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs.ts
More file actions
66 lines (60 loc) · 2.09 KB
/
fs.ts
File metadata and controls
66 lines (60 loc) · 2.09 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as fs from 'fs';
import * as path from 'path';
import * as tmp from 'tmp';
export function fsExistsAsync(filePath: string): Promise<boolean> {
return new Promise<boolean>(resolve => {
fs.exists(filePath, exists => {
return resolve(exists);
});
});
}
export function fsReaddirAsync(root: string): Promise<string[]> {
return new Promise<string[]>(resolve => {
// Now look for Interpreters in this directory
fs.readdir(root, (err, subDirs) => {
if (err) {
return resolve([]);
}
resolve(subDirs.map(subDir => path.join(root, subDir)));
});
});
}
export function getSubDirectories(rootDir: string): Promise<string[]> {
return new Promise<string[]>(resolve => {
fs.readdir(rootDir, (error, files) => {
if (error) {
return resolve([]);
}
const subDirs: string[] = [];
files.forEach(name => {
const fullPath = path.join(rootDir, name);
try {
if (fs.statSync(fullPath).isDirectory()) {
subDirs.push(fullPath);
}
}
// tslint:disable-next-line:no-empty one-line
catch (ex) { }
});
resolve(subDirs);
});
});
}
export function createTemporaryFile(extension: string, temporaryDirectory?: string): Promise<{ filePath: string; cleanupCallback: Function }> {
// tslint:disable-next-line:no-any
const options: any = { postfix: extension };
if (temporaryDirectory) {
options.dir = temporaryDirectory;
}
return new Promise<{ filePath: string; cleanupCallback: Function }>((resolve, reject) => {
tmp.file(options, (err, tmpFile, _fd, cleanupCallback) => {
if (err) {
return reject(err);
}
resolve({ filePath: tmpFile, cleanupCallback: cleanupCallback });
});
});
}