Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/chat-lib/script/postinstall.ts
13388 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as fs from 'fs';
7
import * as path from 'path';
8
9
async function copyStaticAssets(srcpaths: string[], dst: string): Promise<void> {
10
await Promise.all(srcpaths.map(async srcpath => {
11
const src = path.join(REPO_ROOT, srcpath);
12
const dest = path.join(REPO_ROOT, dst, path.basename(srcpath));
13
await fs.promises.mkdir(path.dirname(dest), { recursive: true });
14
await fs.promises.copyFile(src, dest);
15
}));
16
}
17
18
async function fileExists(filePath: string): Promise<boolean> {
19
try {
20
await fs.promises.access(filePath, fs.constants.F_OK);
21
return true;
22
} catch {
23
return false;
24
}
25
}
26
27
const treeSitterGrammars: string[] = [
28
'tree-sitter-c-sharp',
29
'tree-sitter-cpp',
30
'tree-sitter-go',
31
'tree-sitter-javascript', // Also includes jsx support
32
'tree-sitter-python',
33
'tree-sitter-ruby',
34
'tree-sitter-typescript',
35
'tree-sitter-tsx',
36
'tree-sitter-java',
37
'tree-sitter-rust',
38
'tree-sitter-php'
39
];
40
41
const REPO_ROOT = path.join(__dirname, '..');
42
43
async function platformDir(): Promise<string> {
44
const distPath = 'dist/src/_internal/platform';
45
const srcPath = 'src/_internal/platform';
46
if (await fileExists(path.join(REPO_ROOT, distPath))) {
47
return distPath;
48
} else if (await fileExists(path.join(REPO_ROOT, srcPath))) {
49
return srcPath;
50
} else {
51
throw new Error('Could not find the source directory for tokenizer files');
52
}
53
}
54
55
function treeSitterWasmDir(): string {
56
const modulePath = path.dirname(require.resolve('@vscode/tree-sitter-wasm'));
57
return path.relative(REPO_ROOT, modulePath);
58
}
59
60
async function main() {
61
const platform = await platformDir();
62
const vendoredTiktokenFiles = [`${platform}/tokenizer/node/cl100k_base.tiktoken`, `${platform}/tokenizer/node/o200k_base.tiktoken`];
63
const wasm = treeSitterWasmDir();
64
65
// copy static assets to dist
66
await copyStaticAssets([
67
...vendoredTiktokenFiles,
68
...treeSitterGrammars.map(grammar => `${wasm}/${grammar}.wasm`),
69
`${wasm}/tree-sitter.wasm`,
70
], 'dist');
71
72
}
73
74
main();
75
76