-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostinstall.js
52 lines (47 loc) · 1.57 KB
/
postinstall.js
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
/* eslint-disable */
const fs = require('node:fs');
const path = require('node:path');
const cwd = process.env.INIT_CWD;
const reactNativeFiles = ['ios', 'android', 'metro.config.js'];
const isReactNativeProject = reactNativeFiles.every(file =>
fs.existsSync(path.join(cwd, file)),
);
/**
* Patch `cipher-base` so that `stream` is replaced with `stream-browserify`
*/
function patchCipherBase() {
if (!fs.existsSync(path.join(cwd, 'node_modules'))) return;
if (!fs.existsSync(path.join(cwd, 'node_modules', 'cipher-base'))) return;
const cipherBasePackageJson = JSON.parse(
fs.readFileSync(
path.join(cwd, 'node_modules', 'cipher-base', 'package.json'),
'utf8',
),
);
cipherBasePackageJson['react-native'] = {
stream: 'stream-browserify',
};
fs.writeFileSync(
path.join(cwd, 'node_modules', 'cipher-base', 'package.json'),
JSON.stringify(cipherBasePackageJson, null, 2),
'utf8',
);
}
function warnIfNoGetRandomValues() {
const packageJson = JSON.parse(
fs.readFileSync(path.join(cwd, 'package.json'), 'utf8'),
);
if ('react-native-get-random-values' in packageJson['dependencies']) return;
throw new Error(
'react-native-get-random-values is missing. It seems like you are ' +
'installing @railgun-community/wallet in a React Native project. ' +
'This requires the peer dependency react-native-get-random-values. ' +
'\n' +
'Please add it to your package.json dependencies and run npm install ' +
'(or yarn) again.',
);
}
if (isReactNativeProject) {
patchCipherBase();
warnIfNoGetRandomValues();
}