mirror of
https://github.com/balena-io/etcher.git
synced 2025-04-23 23:07:17 +00:00
28 lines
560 B
TypeScript
28 lines
560 B
TypeScript
import * as tmp from 'tmp';
|
|
|
|
function tmpFileAsync(
|
|
options: tmp.FileOptions,
|
|
): Promise<{ path: string; cleanup: () => void }> {
|
|
return new Promise((resolve, reject) => {
|
|
tmp.file(options, (error, path, _fd, cleanup) => {
|
|
if (error) {
|
|
reject(error);
|
|
} else {
|
|
resolve({ path, cleanup });
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
export async function withTmpFile<T>(
|
|
options: tmp.FileOptions,
|
|
fn: (path: string) => Promise<T>,
|
|
): Promise<T> {
|
|
const { path, cleanup } = await tmpFileAsync(options);
|
|
try {
|
|
return await fn(path);
|
|
} finally {
|
|
cleanup();
|
|
}
|
|
}
|