-
Notifications
You must be signed in to change notification settings - Fork 82
/
sdk-local.js
62 lines (53 loc) · 2 KB
/
sdk-local.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
53
54
55
56
57
58
59
60
61
62
/* eslint-disable @typescript-eslint/no-var-requires */
const {
readFileSync,
writeFileSync,
existsSync
} = require('fs');
const fix = `fallback:{
crypto: require.resolve("crypto-browserify"),
stream: require.resolve("stream-browserify"),
util: require.resolve("util"),
},`;
const patchTag = '//-patched';
const fileToPatch = 'node_modules/@angular-devkit/build-angular/src/webpack/configs/browser.js';
const codeToPatch = 'mainFields: [\'es2020\', \'es2015\', \'browser\', \'module\', \'main\'],';
const patch =codeToPatch+ fix +patchTag;
function getAllIndexes(arr, val) {
const indexes = [];
let i;
console.log('arr ', arr)
console.log('val ', val)
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) {
console.log('sourceCode', sourceCode);
const lineNumbers = getAllIndexes(contents, sourceCode);
console.log('lineNumbers', lineNumbers, contents);
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);/* eslint-disable @typescript-eslint/no-var-requires */