-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
50 lines (40 loc) · 1.28 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
'use strict';
const { promisify } = require("util");
const tmp = require("tmp");
// file
module.exports.fileSync = tmp.fileSync;
const fileWithOptions = promisify((options, cb) =>
tmp.file(options, (err, path, fd, cleanup) =>
err ? cb(err) : cb(undefined, { path, fd, cleanup: promisify(cleanup) })
)
);
module.exports.file = async (options) => fileWithOptions(options);
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 cleanup();
}
};
// directory
module.exports.dirSync = tmp.dirSync;
const dirWithOptions = promisify((options, cb) =>
tmp.dir(options, (err, path, cleanup) =>
err ? cb(err) : cb(undefined, { path, cleanup: promisify(cleanup) })
)
);
module.exports.dir = async (options) => dirWithOptions(options);
module.exports.withDir = async function withDir(fn, options) {
const { path, cleanup } = await module.exports.dir(options);
try {
return await fn({ path });
} finally {
await cleanup();
}
};
// name generation
module.exports.tmpNameSync = tmp.tmpNameSync;
module.exports.tmpName = promisify(tmp.tmpName);
module.exports.tmpdir = tmp.tmpdir;
module.exports.setGracefulCleanup = tmp.setGracefulCleanup;