@bencapp3/react-native-static-server
Version:
Embedded HTTP server for React Native
57 lines (53 loc) • 1.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.resolveAssetsPath = resolveAssetsPath;
var _reactNative = require("react-native");
var _reactNativeFs = require("@bencapp3/react-native-fs");
var _constants = require("./constants.js");
function getPlatform() {
switch (_reactNative.Platform.OS) {
case 'android':
return 'ANDROID';
case 'ios':
return _constants.IS_MAC_CATALYST ? 'MACOS' : 'IOS';
case 'windows':
return 'WINDOWS';
default:
throw Error(`Unsupported platform ${_reactNative.Platform.OS}`);
}
}
const PLATFORM = getPlatform();
const BASE_ASSET_DIRS = {
ANDROID: _reactNativeFs.DocumentDirectoryPath,
IOS: _reactNativeFs.MainBundlePath || '',
MACOS: `${_reactNativeFs.MainBundlePath}/Contents/Resources`,
WINDOWS: _reactNativeFs.MainBundlePath || ''
};
const BASE_ASSET_DIR = BASE_ASSET_DIRS[PLATFORM];
const SEP = PLATFORM === 'WINDOWS' ? '\\' : '/';
/**
* Returns `true` if given path is absolute, `false` otherwise.
* @param {string} path
* @return {boolean}
*/
function isAbsolutePath(path) {
if (!path) return false;
if (_reactNative.Platform.OS === 'windows') {
return !!path.match(/^[a-zA-Z]:\\/);
}
// This should do for Android and iOS.
return path.startsWith('/') || path.startsWith('file:///');
}
/**
* If given `path` is relative, it returns the corresponding absolute path,
* resolved relative to the platform-specific base location for bundled assets;
* otherwise, it just returns given absolute path as is.
* @param path Absolute or relative path.
* @return Absolute path.
*/
function resolveAssetsPath(path) {
return isAbsolutePath(path) ? path : `${BASE_ASSET_DIR}${SEP}${path}`;
}
//# sourceMappingURL=utils.js.map