-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathindex.ts
More file actions
83 lines (71 loc) · 2.37 KB
/
index.ts
File metadata and controls
83 lines (71 loc) · 2.37 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { execSync } from 'child_process'
import type {
ItemsParams,
ItemsResult,
MentionsParams,
MentionsResult,
MetaResult,
Provider,
} from '@openctx/provider'
import { readFile, readdir, writeFile } from 'fs/promises'
import JSZip from 'jszip'
/** Settings for the Google Docs OpenCtx provider. */
export type Settings = {
path: string
}
/**
* An [OpenCtx](https://openctx.org) provider that brings Google Docs context to code AI and
* editors.
*/
const excelVBA: Provider<Settings> = {
meta(): MetaResult {
return { name: 'Excel VBA', mentions: {} }
},
async mentions(params: MentionsParams, settings: Settings): Promise<MentionsResult> {
const files = (await readdir(settings.path, { recursive: true })).filter(
name => !name.startsWith('~$') && name.endsWith('.xlsm')
)
return (files ?? []).map((file: any) => ({
title: file,
uri: `file://${settings.path}/${file}`,
}))
},
async items(params: ItemsParams, settings: Settings): Promise<ItemsResult> {
if (!params.mention) {
return []
}
const modules = await extractVBAModules(params.mention.uri.replace(/^file:\/\//, ''))
return [
{
title: params.mention.title,
url: params.mention.uri,
ai: {
content: modules.join('\n\n'),
},
},
]
},
}
async function extractVBAModules(filePath: string): Promise<string[]> {
// Read the .xlsm file as a zip archive
const data = await readFile(filePath)
const zip = await JSZip.loadAsync(data)
// Get the entries in the zip file
const vbaModules: string[] = []
zip.forEach(async (relPath, zipEntry) => {
if (relPath.endsWith('/vbaProject.bin')) {
// Read the vbaProject.bin file
const vbaProjectBin = await zipEntry.async('nodebuffer')
const tmpFile = '/tmp/TMP1.ole'
await writeFile(tmpFile, vbaProjectBin)
// exec extract_vba_code.py
const stdout = execSync(
'python /Users/sqs/src/github.com/sourcegraph/openctx/provider/excel-vba/extract_vba_code.py ' +
tmpFile
)
vbaModules.push(stdout.toString())
}
})
return vbaModules
}
export default excelVBA