-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
48 lines (39 loc) · 1.08 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
const {promisify} = require("util");
const tmp = require("tmp");
// file
module.exports.fileSync = tmp.fileSync;
module.exports.file = promisify(
(options, cb) => tmp.file(
options,
(err, path, fd, cleanup) => cb(err, {path, fd, cleanup})
)
);
module.exports.withFile = async function withFile(fn, options) {
const { path, fd, cleanup } = await module.exports.file(options);
try {
return await fn({ path, fd });
} finally {
await promisify(cleanup)();
}
};
// directory
module.exports.dirSync = tmp.dirSync;
module.exports.dir = promisify(
(options, cb) => tmp.dir(
options,
(err, path, cleanup) => cb(err, {path, cleanup})
)
);
module.exports.withDir = async function withDir(fn, options) {
const { path, cleanup } = await module.exports.dir(options);
try {
return await fn({ path });
} finally {
await promisify(cleanup)();
}
};
// name generation
module.exports.tmpNameSync = tmp.tmpNameSync;
module.exports.tmpName = promisify(tmp.tmpName);
module.exports.tmpdir = tmp.tmpdir;
module.exports.setGracefulCleanup = tmp.setGracefulCleanup;