Path: blob/main/extensions/copilot/chat-lib/script/postinstall.ts
13388 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as fs from 'fs';6import * as path from 'path';78async function copyStaticAssets(srcpaths: string[], dst: string): Promise<void> {9await Promise.all(srcpaths.map(async srcpath => {10const src = path.join(REPO_ROOT, srcpath);11const dest = path.join(REPO_ROOT, dst, path.basename(srcpath));12await fs.promises.mkdir(path.dirname(dest), { recursive: true });13await fs.promises.copyFile(src, dest);14}));15}1617async function fileExists(filePath: string): Promise<boolean> {18try {19await fs.promises.access(filePath, fs.constants.F_OK);20return true;21} catch {22return false;23}24}2526const treeSitterGrammars: string[] = [27'tree-sitter-c-sharp',28'tree-sitter-cpp',29'tree-sitter-go',30'tree-sitter-javascript', // Also includes jsx support31'tree-sitter-python',32'tree-sitter-ruby',33'tree-sitter-typescript',34'tree-sitter-tsx',35'tree-sitter-java',36'tree-sitter-rust',37'tree-sitter-php'38];3940const REPO_ROOT = path.join(__dirname, '..');4142async function platformDir(): Promise<string> {43const distPath = 'dist/src/_internal/platform';44const srcPath = 'src/_internal/platform';45if (await fileExists(path.join(REPO_ROOT, distPath))) {46return distPath;47} else if (await fileExists(path.join(REPO_ROOT, srcPath))) {48return srcPath;49} else {50throw new Error('Could not find the source directory for tokenizer files');51}52}5354function treeSitterWasmDir(): string {55const modulePath = path.dirname(require.resolve('@vscode/tree-sitter-wasm'));56return path.relative(REPO_ROOT, modulePath);57}5859async function main() {60const platform = await platformDir();61const vendoredTiktokenFiles = [`${platform}/tokenizer/node/cl100k_base.tiktoken`, `${platform}/tokenizer/node/o200k_base.tiktoken`];62const wasm = treeSitterWasmDir();6364// copy static assets to dist65await copyStaticAssets([66...vendoredTiktokenFiles,67...treeSitterGrammars.map(grammar => `${wasm}/${grammar}.wasm`),68`${wasm}/tree-sitter.wasm`,69], 'dist');7071}7273main();747576