-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (53 loc) · 1.67 KB
/
index.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
'use strict'
const fs = require('fs')
let agentBasePath
let agentBaseBackupPath
try {
agentBasePath = require.resolve('agent-base')
agentBaseBackupPath = `${agentBasePath}.backup`
} catch (e) {
}
let gracefulFsPath
let gracefulFsBackupPath
try {
gracefulFsPath = require.resolve('graceful-fs')
gracefulFsBackupPath = `${gracefulFsPath}.backup`
} catch (e) {
}
module.exports = {
apply,
restore
}
function apply() {
if (agentBasePath) {
fs.copyFileSync(agentBasePath, agentBaseBackupPath)
const agentBaseScript = fs.readFileSync(agentBasePath, 'utf8')
const modifiedAgentBaseScript = agentBaseScript.replace("require('./patch-core');", '')
fs.writeFileSync(agentBasePath, modifiedAgentBaseScript)
}
if (gracefulFsPath) {
fs.copyFileSync(gracefulFsPath, gracefulFsBackupPath)
const gracefulFsScript = fs.readFileSync(gracefulFsPath, 'utf8')
const modifiedGracefulFsScript = gracefulFsScript
.replace(` = (function (fs$close) { return function (fd, cb) {
return fs$close.call(fs, fd, function (err) {
if (!err)
retry()
if (typeof cb === 'function')
cb.apply(this, arguments)
})
}})(fs.close)`, '')
.replace(` = (function (fs$closeSync) { return function (fd) {
// Note that graceful-fs also retries when fs.closeSync() fails.
// Looks like a bug to me, although it's probably a harmless one.
var rval = fs$closeSync.apply(fs, arguments)
retry()
return rval
}})(fs.closeSync)`, '')
fs.writeFileSync(gracefulFsPath, modifiedGracefulFsScript)
}
}
function restore() {
agentBasePath && fs.copyFileSync(agentBaseBackupPath, agentBasePath)
gracefulFsPath && fs.copyFileSync(gracefulFsBackupPath, gracefulFsPath)
}