@appium/support
Version:
Support libs used across Appium packages
94 lines • 3.39 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.staticDir = exports.openDir = void 0;
exports.path = path;
exports.open = open;
/* This library is originated from temp.js at http://github.com/bruce/node-temp */
const fs_1 = require("./fs");
const node_os_1 = __importDefault(require("node:os"));
const node_path_1 = __importDefault(require("node:path"));
const lodash_1 = __importDefault(require("lodash"));
const node_fs_1 = require("node:fs");
const logger_1 = __importDefault(require("./logger"));
const RDWR_EXCL = node_fs_1.constants.O_CREAT | node_fs_1.constants.O_TRUNC | node_fs_1.constants.O_RDWR | node_fs_1.constants.O_EXCL;
/**
* Generate a temporary directory with arbitrary prefix/suffix for the directory name.
*
* @param rawAffixes - Prefix string, or object with prefix/suffix, or omitted.
* @param defaultPrefix - Default prefix when rawAffixes is omitted.
* @returns A path to the temporary directory.
*/
async function path(rawAffixes, defaultPrefix) {
const affixes = parseAffixes(rawAffixes, defaultPrefix);
const name = `${affixes.prefix ?? ''}${affixes.suffix ?? ''}`;
const tempDirectory = await tempDir();
return node_path_1.default.join(tempDirectory, name);
}
/**
* Generate a temp file path with prefix/suffix, open the file, and return path and fd.
*
* @param affixes - Prefix/suffix for the file name.
* @returns The opened file path and descriptor.
*/
async function open(affixes) {
const filePath = await path(affixes, 'f-');
try {
const fd = await fs_1.fs.open(filePath, RDWR_EXCL, 0o600);
return { path: filePath, fd };
}
catch (err) {
throw logger_1.default.errorWithException(err);
}
}
/** Returns a new path to a temporary directory (alias for the tempDir implementation). */
exports.openDir = tempDir;
/**
* Returns a path to a temporary directory which is reused for the life of the process.
*
* @returns The same temp directory path on every call.
*/
exports.staticDir = lodash_1.default.memoize(async function staticDir() {
return tempDir();
});
// #region Private
/**
* Generate a temporary directory in os.tmpdir() or process.env.APPIUM_TMP_DIR.
*/
async function tempDir() {
const now = new Date();
const filePath = node_path_1.default.join(process.env.APPIUM_TMP_DIR || node_os_1.default.tmpdir(), [
now.getFullYear(),
now.getMonth(),
now.getDate(),
'-',
process.pid,
'-',
(Math.random() * 0x100000000 + 1).toString(36),
].join(''));
await fs_1.fs.mkdir(filePath, { recursive: true });
return filePath;
}
function parseAffixes(rawAffixes, defaultPrefix) {
let affixes = {};
if (rawAffixes !== undefined && rawAffixes !== null) {
switch (typeof rawAffixes) {
case 'string':
affixes = { prefix: rawAffixes };
break;
case 'object':
affixes = rawAffixes;
break;
default:
throw new Error(`Unknown affix declaration: ${String(rawAffixes)}`);
}
}
else {
affixes.prefix = defaultPrefix;
}
return affixes;
}
// #endregion
//# sourceMappingURL=tempdir.js.map