UNPKG

@netlify/content-engine

Version:
57 lines 2.64 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.truncatePath = exports.tooLongSegmentsInPath = exports.getCommonDir = exports.withTrailingSlash = exports.withBasePath = void 0; const path_1 = __importDefault(require("path")); const core_utils_1 = require("../core-utils"); const withBasePath = (basePath) => (...paths) => (0, core_utils_1.joinPath)(basePath, ...paths); exports.withBasePath = withBasePath; const withTrailingSlash = (basePath) => `${basePath}/`; exports.withTrailingSlash = withTrailingSlash; const posixJoinWithLeadingSlash = (paths) => path_1.default.posix.join(...paths.map((segment, index) => segment === `` && index === 0 ? `/` : segment)); const getCommonDir = (path1, path2) => { const path1Segments = path1.split(/[/\\]/); const path2Segments = path2.split(/[/\\]/); for (let i = 0; i < path1Segments.length; i++) { if (i >= path2Segments.length) { return posixJoinWithLeadingSlash(path2Segments); } else if (path1Segments[i].toLowerCase() !== path2Segments[i].toLowerCase()) { const joined = path1Segments.slice(0, i); return posixJoinWithLeadingSlash(joined); } } return posixJoinWithLeadingSlash(path1Segments); }; exports.getCommonDir = getCommonDir; // MacOS (APFS) and Windows (NTFS) filename length limit = 255 chars, Others = 255 bytes const MAX_PATH_SEGMENT_CHARS = 255; const MAX_PATH_SEGMENT_BYTES = 255; const SLICING_INDEX = 50; const pathSegmentRe = /[^/]+/g; const isMacOs = process.platform === `darwin`; const isWindows = process.platform === `win32`; const isNameTooLong = (segment) => isMacOs || isWindows ? segment.length > MAX_PATH_SEGMENT_CHARS // MacOS (APFS) and Windows (NTFS) filename length limit (255 chars) : Buffer.from(segment).length > MAX_PATH_SEGMENT_BYTES; // Other (255 bytes) const tooLongSegmentsInPath = (path) => { const invalidFilenames = []; for (const segment of path.split(`/`)) { if (isNameTooLong(segment)) { invalidFilenames.push(segment); } } return invalidFilenames; }; exports.tooLongSegmentsInPath = tooLongSegmentsInPath; const truncatePath = (path) => path.replace(pathSegmentRe, (match) => { if (isNameTooLong(match)) { return (match.slice(0, SLICING_INDEX) + (0, core_utils_1.createContentDigest)(match.slice(SLICING_INDEX))); } return match; }); exports.truncatePath = truncatePath; //# sourceMappingURL=path.js.map