mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-16 09:42:35 +02:00
* webpack upgrade * refactor docusaurus-utils hash fns * Fix webpack cache eviction problems on config/aliases/swizzle changes * Move/Rename InitPlugin type * fix TS typos * Add tests for webpack aliases * fix windows tests
42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
// Based on https://github.com/gatsbyjs/gatsby/pull/21518/files
|
|
|
|
// 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;
|
|
// Space for appending things to the string like file extensions and so on
|
|
const SPACE_FOR_APPENDING = 10;
|
|
|
|
const isMacOs = process.platform === `darwin`;
|
|
const isWindows = process.platform === `win32`;
|
|
|
|
export const isNameTooLong = (str: string): boolean => {
|
|
return isMacOs || isWindows
|
|
? str.length + SPACE_FOR_APPENDING > MAX_PATH_SEGMENT_CHARS // MacOS (APFS) and Windows (NTFS) filename length limit (255 chars)
|
|
: Buffer.from(str).length + SPACE_FOR_APPENDING > MAX_PATH_SEGMENT_BYTES; // Other (255 bytes)
|
|
};
|
|
|
|
export const shortName = (str: string): string => {
|
|
if (isMacOs || isWindows) {
|
|
const overflowingChars = str.length - MAX_PATH_SEGMENT_CHARS;
|
|
return str.slice(
|
|
0,
|
|
str.length - overflowingChars - SPACE_FOR_APPENDING - 1,
|
|
);
|
|
}
|
|
const strBuffer = Buffer.from(str);
|
|
const overflowingBytes =
|
|
Buffer.byteLength(strBuffer) - MAX_PATH_SEGMENT_BYTES;
|
|
return strBuffer
|
|
.slice(
|
|
0,
|
|
Buffer.byteLength(strBuffer) - overflowingBytes - SPACE_FOR_APPENDING - 1,
|
|
)
|
|
.toString();
|
|
};
|