/
Constants.ts
103 lines (94 loc) · 3.12 KB
/
Constants.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { CodedError, NativeModulesProxy } from '@unimodules/core';
import { Platform } from 'react-native';
import {
AndroidManifest,
AppOwnership,
Constants,
ExecutionEnvironment,
IOSManifest,
NativeConstants,
PlatformManifest,
UserInterfaceIdiom,
WebManifest,
} from './Constants.types';
import ExponentConstants from './ExponentConstants';
export {
AndroidManifest,
AppOwnership,
Constants,
ExecutionEnvironment,
IOSManifest,
NativeConstants,
PlatformManifest,
UserInterfaceIdiom,
WebManifest,
};
if (!ExponentConstants) {
console.warn(
"No native ExponentConstants module found, are you sure the expo-constants's module is linked properly?"
);
}
let manifest = null;
// If expo-updates defines a non-empty manifest, prefer that one
if (NativeModulesProxy.ExpoUpdates) {
let updatesManifest;
if (NativeModulesProxy.ExpoUpdates.manifest) {
updatesManifest = NativeModulesProxy.ExpoUpdates.manifest;
} else if (NativeModulesProxy.ExpoUpdates.manifestString) {
updatesManifest = JSON.parse(NativeModulesProxy.ExpoUpdates.manifestString);
}
if (updatesManifest && Object.keys(updatesManifest).length > 0) {
manifest = updatesManifest;
}
}
// Fall back to ExponentConstants.manifest if we don't have one from Updates
if (!manifest && ExponentConstants && ExponentConstants.manifest) {
manifest = ExponentConstants.manifest;
// On Android we pass the manifest in JSON form so this step is necessary
if (typeof manifest === 'string') {
manifest = JSON.parse(manifest);
}
}
const { name, appOwnership, ...nativeConstants } = (ExponentConstants || {}) as any;
const constants = {
...nativeConstants,
// Ensure this is null in bare workflow
appOwnership: appOwnership ?? null,
// Legacy aliases
deviceId: nativeConstants.installationId,
linkingUrl: nativeConstants.linkingUri,
};
Object.defineProperties(constants, {
manifest: {
enumerable: true,
get() {
if (!manifest) {
const invalidManifestType = manifest === null ? 'null' : 'undefined';
if (
nativeConstants.executionEnvironment === ExecutionEnvironment.Bare &&
Platform.OS !== 'web'
) {
console.warn(
`Constants.manifest is ${invalidManifestType} because the embedded app.config could not be read. Ensure that you have installed the expo-constants build scripts if you need to read from Constants.manifest.`
);
} else if (
nativeConstants.executionEnvironment === ExecutionEnvironment.StoreClient ||
nativeConstants.executionEnvironment === ExecutionEnvironment.Standalone
) {
// If we somehow get here, this is a truly exceptional state to be in.
// Constants.manifest should *always* be defined in those contexts.
throw new CodedError(
'ERR_CONSTANTS_MANIFEST_UNAVAILABLE',
`Constants.manifest is ${invalidManifestType}, must be an object.`
);
}
}
return manifest;
},
// This setter is only useful to mock the value for tests
set(value) {
manifest = value;
},
},
});
export default constants as Constants;