UNPKG

@itwin/presentation-testing

Version:

Testing utilities for iTwin.js Presentation library

93 lines 4.15 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FILE_PATH_RESERVED_CHARACTERS = void 0; exports.getTestOutputDir = getTestOutputDir; exports.setTestOutputDir = setTestOutputDir; exports.setupOutputFileLocation = setupOutputFileLocation; exports.createFileNameFromString = createFileNameFromString; exports.limitFilePathLength = limitFilePathLength; const os_1 = require("os"); const path_1 = __importDefault(require("path")); const sanitize_filename_1 = __importDefault(require("sanitize-filename")); const core_backend_1 = require("@itwin/core-backend"); const defaultTestOutputDir = (0, os_1.tmpdir)(); let testOutputDir; /** * Get the output directory used by `setupOutputFileLocation` utility. * @public */ function getTestOutputDir() { return testOutputDir ?? defaultTestOutputDir; } /** * Set the output directory used by `setupOutputFileLocation` utility. * @public */ function setTestOutputDir(directoryPath) { testOutputDir = directoryPath; } /** * Prepare for an output file path by: * - Resolving the output file name under the known test output directory (see `getTestOutputDir` & `setTestOutputDir`). * - Making sure the output directories exist. * - Removing a previous copy of the output file. * * @param fileName Name of output file. The actual file name may get modified to fit within the file system limits. * * @public */ function setupOutputFileLocation(fileName) { const outputDirectoryPath = getTestOutputDir(); !core_backend_1.IModelJsFs.existsSync(outputDirectoryPath) && core_backend_1.IModelJsFs.mkdirSync(outputDirectoryPath); const outputFilePath = limitFilePathLength(path_1.default.join(outputDirectoryPath, fileName)); core_backend_1.IModelJsFs.existsSync(outputFilePath) && core_backend_1.IModelJsFs.unlinkSync(outputFilePath); return outputFilePath; } /** * An utility to create a valid file name from any string. Sanitizes all invalid characters, * replaces spaces with `-`, makes everything lower case. * * @public */ function createFileNameFromString(str) { return (0, sanitize_filename_1.default)(str.replace(/[ ]+/g, "-").replaceAll("`", "").replaceAll("'", "")).toLocaleLowerCase(); } /** * `itwinjs-core` creates some accompanying files for each iModels and their names are formed by appending * a suffix to iModel file name. This constant should account for the maximum suffix length. * @internal */ exports.FILE_PATH_RESERVED_CHARACTERS = 13; /** * An utility to make sure file path is shorter than 260 characters. * * 1. Takes a full file path, splits into directory and file name parts. * 2. If the path is already short enough, returns it. * 3. Else, calculates tha max allowed file name length, and shortens the file name by replacing the middle part with `...`. * 4. Joins back the directory with the shortened file name and returns it. * * @public */ function limitFilePathLength(filePath) { const { dir, name, ext } = path_1.default.parse(filePath); const THREE_DOTS_LENGTH = 3; let allowedFileNameLength = 260 - exports.FILE_PATH_RESERVED_CHARACTERS - (dir.length + 1) - ext.length; if (name.length <= allowedFileNameLength) { return filePath; } allowedFileNameLength -= THREE_DOTS_LENGTH; if (allowedFileNameLength <= 0) { throw new Error(`File path "${filePath}" is too long.`); } const pieceLength = allowedFileNameLength / 2; const shortenedName = `${name.slice(0, Math.ceil(pieceLength))}...${name.slice(Math.ceil(name.length - pieceLength))}`; return path_1.default.join(dir, `${shortenedName}${ext}`); } //# sourceMappingURL=FilenameUtils.js.map