forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathfs-temp.ts
More file actions
41 lines (37 loc) · 1.26 KB
/
fs-temp.ts
File metadata and controls
41 lines (37 loc) · 1.26 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as tmp from 'tmp';
import { ITempFileSystem, TemporaryFile } from './types';
interface IRawTempFS {
fileSync(config?: tmp.Options): tmp.SynchrounousResult;
}
// 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) => {
const { name, removeCallback } = this.raw.fileSync(opts);
if (!name) {
return reject(new Error('Failed to create temp file'));
}
resolve({
filePath: name,
dispose: removeCallback,
});
});
}
}