UNPKG

@nxworker/workspace

Version:

Nx plugin providing generators for managing workspace files, including the move-file generator for safely moving files between projects while updating all imports

55 lines (54 loc) 2.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "isValidPathInput", { enumerable: true, get: function() { return isValidPathInput; } }); const _interop_require_default = require("@swc/helpers/_/_interop_require_default"); const _escape = /*#__PURE__*/ _interop_require_default._(require("core-js-pure/stable/regexp/escape")); /** * Characters that are valid in Unix filenames but not on Windows. * These should only be allowed when not running on Windows. * - < (less than) * - > (greater than) * - : (colon) */ const UNIX_ONLY_CHARS = '<>:'; /** * Backslash is the path separator on Windows but not valid in filenames on Unix. * It should only be allowed as input on Windows platforms. */ const WINDOWS_ONLY_CHARS = '\\'; function isValidPathInput(str, options) { const { allowUnicode = false, maxLength, additionalAllowedChars = '', allowGlobPatterns = false } = options || {}; if (typeof str !== 'string') { return false; } if (typeof maxLength === 'number' && str.length > maxLength) { return false; } // Only allow Unix-specific characters on non-Windows platforms const unixChars = process.platform === 'win32' ? '' : UNIX_ONLY_CHARS; // Only allow backslash on Windows platforms const windowsChars = process.platform === 'win32' ? WINDOWS_ONLY_CHARS : ''; // When glob patterns are allowed, include glob-specific characters: * ? [ ] { } , // Note: comma is needed for brace expansion like {ts,js} const globChars = allowGlobPatterns ? '*?[]{},!+@' : ''; let pathRegex; if (allowUnicode) { // In character class, we need to escape certain characters: ] - \ // Build the pattern with proper escaping const escapedGlobChars = globChars.replace(/[\]\\-]/g, '\\$&'); const unicodePathPattern = `^[\\p{L}\\p{N}\\p{M}\\p{Pc}@./${(0, _escape.default)(windowsChars)}${(0, _escape.default)(unixChars)} ${(0, _escape.default)(additionalAllowedChars)}${escapedGlobChars}-]*$`; pathRegex = new RegExp(unicodePathPattern, 'u'); } else { // In character class, we need to escape certain characters: ] - \ const escapedGlobChars = globChars.replace(/[\]\\-]/g, '\\$&'); const asciiPathPattern = `^[A-Za-z0-9_@./${(0, _escape.default)(windowsChars)}${(0, _escape.default)(unixChars)} ${(0, _escape.default)(additionalAllowedChars)}${escapedGlobChars}-]*$`; pathRegex = new RegExp(asciiPathPattern); } return pathRegex.test(str); } //# sourceMappingURL=is-valid-path-input.js.map