forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs-temp.ts
More file actions
49 lines (44 loc) · 1.51 KB
/
fs-temp.ts
File metadata and controls
49 lines (44 loc) · 1.51 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as tmp from 'tmp';
import { ITempFileSystem, TemporaryFile } from './types';
interface IRawTempFS {
// TODO (https://github.com/microsoft/vscode/issues/84517)
// This functionality has been requested for the
// VS Code FS API (vscode.workspace.fs.*).
file(
config: tmp.Options,
callback?: (err: any, path: string, fd: number, cleanupCallback: () => void) => void,
): void;
}
// Operations related to temporary files and directories.
export class TemporaryFileSystem implements ITempFileSystem {
constructor(
// (effectively) the third-party "tmp" module to use
private readonly raw: IRawTempFS,
) {}
public static withDefaults(): TemporaryFileSystem {
return new TemporaryFileSystem(
// Use the actual "tmp" module.
tmp,
);
}
// Create a new temp file with the given filename suffix.
public createFile(suffix: string, mode?: number): Promise<TemporaryFile> {
const opts = {
postfix: suffix,
mode,
};
return new Promise<TemporaryFile>((resolve, reject) => {
this.raw.file(opts, (err, filename, _fd, cleanUp) => {
if (err) {
return reject(err);
}
resolve({
filePath: filename,
dispose: cleanUp,
});
});
});
}
}