-
Notifications
You must be signed in to change notification settings - Fork 0
/
postinstall.js
52 lines (48 loc) · 1.84 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
// IIFE
(function() {
'use strict';
// node
var fs = require('fs');
var path = require('path');
// Patch encoding module due to iconv issues -> make it use iconv-lite
(function() {
var PATCH_VERSION = '0.1.12';
var PATCH_MODULE = 'encoding';
var PATCH_REASON = 'Use iconv-lite instead of iconv, helpful for webpack bundling';
console.log('patching `%s`(%s) module', PATCH_MODULE, PATCH_VERSION);
var pathToModule = path.join(__dirname, 'node_modules', PATCH_MODULE);
var pathToModulePackage = path.join(pathToModule, 'package.json');
var pathToModulePatchedFile1 = path.join(pathToModule, 'lib/iconv-loader.js');
var pathToModulePatchedFile2 = path.join(pathToModule, 'lib/encoding.js');
var moduleInfo = require(pathToModulePackage);
if (moduleInfo.version !== PATCH_VERSION) {
console.error(
'patching `encoding` failed - expected `%s` but detected `%s`',
PATCH_VERSION,
moduleInfo.version
);
process.exit(1);
}
var contents;
if (fs.existsSync(pathToModulePatchedFile1)) {
contents = [
'\'use strict\';',
'module.exports = require(\'iconv-lite\');',
'',
].join('\n');
fs.writeFileSync(pathToModulePatchedFile1, contents);
} else {
console.error('patching `%s` failed because the file does not exist in', PATCH_MODULE, pathToModule);
process.exit(1);
}
if (fs.existsSync(pathToModulePatchedFile2)) {
contents = fs.readFileSync(pathToModulePatchedFile2).toString();
contents = contents.replace('console.error(E);','');
fs.writeFileSync(pathToModulePatchedFile2, contents);
} else {
console.error('patching `%s` failed because the file does not exist in', PATCH_MODULE, pathToModule);
process.exit(1);
}
console.log('patching `%s`, reason: `%s` - completed', PATCH_MODULE, PATCH_REASON);
})();
})(this);