• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3    return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.withTempFile = void 0;
7const promises_1 = __importDefault(require("fs/promises"));
8const os_1 = __importDefault(require("os"));
9const path_1 = __importDefault(require("path"));
10// Invokes the given handler with the path to a temporary file. The file
11// is deleted after the handler returns.
12const withTempFile = async (handler) => withTempDir(async (dir) => handler(path_1.default.join(dir, 'tempfile')));
13exports.withTempFile = withTempFile;
14// Invokes the given handler with a temporary directory. The directory is
15// deleted after the handler returns.
16const withTempDir = async (handler) => {
17    const tmpDir = await promises_1.default.realpath(os_1.default.tmpdir());
18    const dir = await promises_1.default.mkdtemp(tmpDir + path_1.default.sep);
19    try {
20        return await handler(dir);
21    }
22    finally {
23        await promises_1.default.rm(dir, { force: true, recursive: true, maxRetries: 3 });
24    }
25};
26