forked from bitpay/wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.js
More file actions
61 lines (52 loc) · 1.8 KB
/
patch.js
File metadata and controls
61 lines (52 loc) · 1.8 KB
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
/* eslint-disable @typescript-eslint/no-var-requires */
const {
readFileSync,
writeFileSync,
existsSync
} = require('fs');
const fix = `fallback:{
http: require.resolve("stream-http"),
https: require.resolve("https-browserify"),
crypto: require.resolve("crypto-browserify"),
stream:require.resolve("stream-browserify"),
os:require.resolve("os-browserify/browser"),
assert:require.resolve("assert/")
},`;
const patchTag = '//-patched';
const fileToPatch = 'node_modules/@angular-devkit/build-angular/src/webpack/configs/browser.js';
const codeToPatch = 'mainFields: [\'es2015\', \'browser\', \'module\', \'main\'],';
const patch =codeToPatch+ fix +patchTag;
function getAllIndexes(arr, val) {
const indexes = [];
let i;
for (i = 0; i < arr.length; i++)
{if (arr[i].indexOf(val) !== -1)
{indexes.push(i);}}
return indexes;
}
function doPatch(fileName, sourceCode, patchCode, patchIdentifier) {
if (!existsSync(fileName)) {
console.log('file not found ' + fileName);
return;
}
const contents = readFileSync(fileName).toString().split('\n');
// Check if code has been patched already
const hasBeenPatched = contents.find(line => line.indexOf(patchIdentifier) !== -1);
if (!hasBeenPatched) {
const lineNumbers = getAllIndexes(contents, sourceCode);
if (lineNumbers.length < 1) {
console.error('Could not find source code. Please check ' + fileName + ' and update the patch accordingly');
return;
}
// replace the line
lineNumbers.forEach((lineNumber) => {
contents.splice(lineNumber, 1, patchCode);
});
const updatedContents = contents.join('\n');
writeFileSync(fileName, updatedContents);
console.log('Monkey patched');
} else {
console.log('already been patched');
}
}
doPatch(fileToPatch, codeToPatch, patch, patchTag);