static-fs
Version:
A static filesystem to bundle files and read them using NodeJS
50 lines (39 loc) • 1.42 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.unixifyPath = unixifyPath;
exports.isWindowsPath = isWindowsPath;
exports.sanitizePath = sanitizePath;
var _path = require("path");
var _constants = require("./constants");
// Strips down a path into an absolute-style unix path
function unixifyPath(filePath) {
if (!_constants.isWindows) return filePath;
if (filePath && typeof filePath === 'string') {
return filePath // change \\?\<letter>:\ to <letter>:\
.replace(/^\\\\\?\\(.):\\/, '$1:\\') // change backslashes to forward slashes. (and remove duplicates)
// eslint-disable-next-line no-useless-escape
.replace(/[\\\/]+/g, '/') // remove drive letter from front
.replace(/^([a-zA-Z]+:|\.\/)/, '') // drop any trailing slash
.replace(/(.+?)\/$/, '$1');
}
return filePath;
}
function isWindowsPath(filePath) {
if (!_constants.isWindows) return filePath;
if (filePath && filePath.length >= 3) {
if (filePath.charCodeAt(0) === 92 && filePath.charCodeAt(1) === 92) {
return true;
}
if (filePath.charCodeAt(1) === 58 && filePath.charCodeAt(2) === 92) {
const code = filePath.charCodeAt(0);
return code >= 65 && code <= 90 || code >= 97 && code <= 122;
}
}
return false;
}
function sanitizePath(...args) {
const resolvedPath = (0, _path.resolve)(...args);
return unixifyPath(resolvedPath);
}
;