|
| 1 | +import * as fs from "fs" |
| 2 | +import * as path from "path" |
| 3 | + |
| 4 | +/** |
| 5 | + * Copies all files or directories from source to destination |
| 6 | + * @param paths Array of file or directory paths to copy |
| 7 | + * @param srcDir Source directory path |
| 8 | + * @param dstDir Destination directory path |
| 9 | + */ |
| 10 | +export function copyPaths(paths: string[], srcDir: string, dstDir: string) { |
| 11 | + paths.forEach((fileOrDirectory) => { |
| 12 | + const stats = fs.lstatSync(path.join(srcDir, fileOrDirectory)) |
| 13 | + |
| 14 | + if (stats.isDirectory()) { |
| 15 | + const directory = fileOrDirectory |
| 16 | + const count = copyDir(path.join(srcDir, directory), path.join(dstDir, directory), 0) |
| 17 | + console.log(`[copy-src] Copied ${count} files from ${directory} to ${dstDir}`) |
| 18 | + } else { |
| 19 | + const file = fileOrDirectory |
| 20 | + fs.copyFileSync(path.join(srcDir, file), path.join(dstDir, file)) |
| 21 | + console.log(`[copy-src] Copied ${file} to ${dstDir}`) |
| 22 | + } |
| 23 | + }) |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Recursively copies files from source directory to destination directory |
| 28 | + * @param srcDir Source directory path |
| 29 | + * @param dstDir Destination directory path |
| 30 | + * @param count Counter for number of files copied |
| 31 | + * @returns Updated count of files copied |
| 32 | + */ |
| 33 | +export function copyDir(srcDir: string, dstDir: string, count: number): number { |
| 34 | + const entries = fs.readdirSync(srcDir, { withFileTypes: true }) |
| 35 | + |
| 36 | + for (const entry of entries) { |
| 37 | + const srcPath = path.join(srcDir, entry.name) |
| 38 | + const dstPath = path.join(dstDir, entry.name) |
| 39 | + |
| 40 | + if (entry.isDirectory()) { |
| 41 | + fs.mkdirSync(dstPath, { recursive: true }) |
| 42 | + count = copyDir(srcPath, dstPath, count) |
| 43 | + } else { |
| 44 | + count = count + 1 |
| 45 | + fs.copyFileSync(srcPath, dstPath) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return count |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Copies WASM files from node_modules to the distribution directory |
| 54 | + * @param srcDir Source directory path |
| 55 | + * @param distDir Distribution directory path |
| 56 | + */ |
| 57 | +export function copyWasms(srcDir: string, distDir: string): void { |
| 58 | + const nodeModulesDir = path.join(srcDir, "node_modules") |
| 59 | + |
| 60 | + fs.mkdirSync(distDir, { recursive: true }) |
| 61 | + |
| 62 | + // Tiktoken WASM file. |
| 63 | + fs.copyFileSync( |
| 64 | + path.join(nodeModulesDir, "tiktoken", "lite", "tiktoken_bg.wasm"), |
| 65 | + path.join(distDir, "tiktoken_bg.wasm"), |
| 66 | + ) |
| 67 | + |
| 68 | + console.log(`[copy-wasm-files] Copied tiktoken WASMs to ${distDir}`) |
| 69 | + |
| 70 | + // Also copy Tiktoken WASMs to the workers directory. |
| 71 | + const workersDir = path.join(distDir, "workers") |
| 72 | + fs.mkdirSync(workersDir, { recursive: true }) |
| 73 | + |
| 74 | + fs.copyFileSync( |
| 75 | + path.join(nodeModulesDir, "tiktoken", "lite", "tiktoken_bg.wasm"), |
| 76 | + path.join(workersDir, "tiktoken_bg.wasm"), |
| 77 | + ) |
| 78 | + |
| 79 | + console.log(`[copy-wasm-files] Copied tiktoken WASMs to ${workersDir}`) |
| 80 | + |
| 81 | + // Main tree-sitter WASM file. |
| 82 | + fs.copyFileSync( |
| 83 | + path.join(nodeModulesDir, "web-tree-sitter", "tree-sitter.wasm"), |
| 84 | + path.join(distDir, "tree-sitter.wasm"), |
| 85 | + ) |
| 86 | + |
| 87 | + console.log(`[copy-wasm-files] Copied tree-sitter.wasm to ${distDir}`) |
| 88 | + |
| 89 | + // Copy language-specific WASM files. |
| 90 | + const languageWasmDir = path.join(nodeModulesDir, "tree-sitter-wasms", "out") |
| 91 | + |
| 92 | + if (!fs.existsSync(languageWasmDir)) { |
| 93 | + throw new Error(`Directory does not exist: ${languageWasmDir}`) |
| 94 | + } |
| 95 | + |
| 96 | + // Dynamically read all WASM files from the directory instead of using a hardcoded list. |
| 97 | + const wasmFiles = fs.readdirSync(languageWasmDir).filter((file) => file.endsWith(".wasm")) |
| 98 | + |
| 99 | + wasmFiles.forEach((filename) => { |
| 100 | + fs.copyFileSync(path.join(languageWasmDir, filename), path.join(distDir, filename)) |
| 101 | + }) |
| 102 | + |
| 103 | + console.log(`[copy-wasm-files] Copied ${wasmFiles.length} tree-sitter language wasms to ${distDir}`) |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Copies locale files to the distribution directory |
| 108 | + * @param srcDir Source directory path |
| 109 | + * @param distDir Distribution directory path |
| 110 | + */ |
| 111 | +export function copyLocales(srcDir: string, distDir: string): void { |
| 112 | + const destDir = path.join(distDir, "i18n", "locales") |
| 113 | + fs.mkdirSync(destDir, { recursive: true }) |
| 114 | + const count = copyDir(path.join(srcDir, "i18n", "locales"), destDir, 0) |
| 115 | + console.log(`[copy-locales-files] Copied ${count} locale files to ${destDir}`) |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * Copies asset files to the distribution directory |
| 120 | + * @param srcDir Source directory path |
| 121 | + * @param distDir Distribution directory path |
| 122 | + */ |
| 123 | +export function copyAssets(srcDir: string, distDir: string) { |
| 124 | + const copyPaths = [["node_modules/vscode-material-icons/generated", "assets/vscode-material-icons"]] |
| 125 | + |
| 126 | + for (const [srcRelPath, dstRelPath] of copyPaths) { |
| 127 | + if (!srcRelPath || !dstRelPath) { |
| 128 | + throw new Error("Invalid copy path") |
| 129 | + } |
| 130 | + |
| 131 | + const from = path.join(srcDir, srcRelPath) |
| 132 | + const to = path.join(distDir, dstRelPath) |
| 133 | + |
| 134 | + if (!fs.existsSync(from)) { |
| 135 | + throw new Error(`Directory does not exist: ${from}`) |
| 136 | + } |
| 137 | + |
| 138 | + if (fs.existsSync(to)) { |
| 139 | + fs.rmSync(to, { recursive: true }) |
| 140 | + } |
| 141 | + |
| 142 | + fs.mkdirSync(to, { recursive: true }) |
| 143 | + const count = copyDir(from, to, 0) |
| 144 | + console.log(`[copy-assets] Copied ${count} assets: ${from} -> ${to}`) |
| 145 | + } |
| 146 | +} |
0 commit comments