-
Notifications
You must be signed in to change notification settings - Fork 5
/
hack-extension.js
40 lines (28 loc) · 1.15 KB
/
hack-extension.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
const fs = require('fs');
const path = process.argv[2];
const js = fs.readFileSync(path, 'utf8');
const matches = js.match(/\(([a-zA-Z]+),"CopilotExtensionApi"\)/);
if (!matches) {
throw new Error('Could not find extension api constructor [calculateInlineCompletions]');
}
const apiName = matches[1];
const apiReg = new RegExp(`var ${apiName}=class ${apiName}\\{constructor\\([a-zA-Z]\\)\\{this.ctx=`);
if (!fs.existsSync(path + '.bak')) {
fs.copyFileSync(path, path + '.bak');
}
if (!apiReg.test(js)) {
throw new Error('Could not find extension api constructor [apiReg]');
}
const replaced = js.replace(apiReg, (match) => {
if (!match.includes('this.ctx=')) {
throw new Error('Could not find extension api constructor [this.ctx=]');
}
const matches = js.match(/\(([a-zA-Z0-9]+),"calculateInlineCompletions"\)/);
if (!matches) {
throw new Error('Could not find extension api constructor [calculateInlineCompletions]');
}
const name = matches[1];
return match.replace('this.ctx=', `this.calculateInlineCompletions=${name};this.ctx=`);
});
fs.writeFileSync(path, replaced, 'utf8');
console.log(`replace ${path}`, js.length, replaced.length);