51 lines
1.3 KiB
JavaScript
51 lines
1.3 KiB
JavaScript
'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;
|