|
| 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 | +import * as https from 'https'; |
| 9 | + |
| 10 | +const CODICONS_DIR = path.join(__dirname, '..', 'resources', 'icons', 'codicons'); |
| 11 | +const BASE_URL = 'https://raw.githubusercontent.com/microsoft/vscode-codicons/refs/heads/mrleemurray/new-icons/src/icons'; |
| 12 | + |
| 13 | +interface UpdateResult { |
| 14 | + filename: string; |
| 15 | + status: 'updated' | 'unchanged' | 'error'; |
| 16 | + error?: string; |
| 17 | +} |
| 18 | + |
| 19 | +function readLocalIconFilenames(): string[] { |
| 20 | + return fs.readdirSync(CODICONS_DIR).filter(f => f.endsWith('.svg')); |
| 21 | +} |
| 22 | + |
| 23 | +function fetchRemoteIcon(filename: string): Promise<string> { |
| 24 | + const url = `${BASE_URL}/${encodeURIComponent(filename)}`; |
| 25 | + return new Promise((resolve, reject) => { |
| 26 | + https.get(url, res => { |
| 27 | + const { statusCode } = res; |
| 28 | + if (statusCode !== 200) { |
| 29 | + res.resume(); // drain |
| 30 | + return reject(new Error(`Failed to fetch ${filename}: HTTP ${statusCode}`)); |
| 31 | + } |
| 32 | + let data = ''; |
| 33 | + res.setEncoding('utf8'); |
| 34 | + res.on('data', chunk => { data += chunk; }); |
| 35 | + res.on('end', () => resolve(data)); |
| 36 | + }).on('error', reject); |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +async function updateIcon(filename: string): Promise<UpdateResult> { |
| 41 | + const localPath = path.join(CODICONS_DIR, filename); |
| 42 | + const oldContent = fs.readFileSync(localPath, 'utf8'); |
| 43 | + try { |
| 44 | + const newContent = await fetchRemoteIcon(filename); |
| 45 | + if (normalize(oldContent) === normalize(newContent)) { |
| 46 | + return { filename, status: 'unchanged' }; |
| 47 | + } |
| 48 | + fs.writeFileSync(localPath, newContent, 'utf8'); |
| 49 | + return { filename, status: 'updated' }; |
| 50 | + } catch (err: any) { |
| 51 | + return { filename, status: 'error', error: err?.message ?? String(err) }; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +function normalize(svg: string): string { |
| 56 | + return svg.replace(/\r\n?/g, '\n').trim(); |
| 57 | +} |
| 58 | + |
| 59 | +async function main(): Promise<void> { |
| 60 | + const icons = readLocalIconFilenames(); |
| 61 | + if (!icons.length) { |
| 62 | + console.log('No codicon SVGs found to update.'); |
| 63 | + return; |
| 64 | + } |
| 65 | + console.log(`Updating ${icons.length} codicon(s) from upstream...`); |
| 66 | + |
| 67 | + const concurrency = 8; |
| 68 | + const queue = icons.slice(); |
| 69 | + const results: UpdateResult[] = []; |
| 70 | + |
| 71 | + async function worker(): Promise<void> { |
| 72 | + while (queue.length) { |
| 73 | + const file = queue.shift(); |
| 74 | + if (!file) { |
| 75 | + break; |
| 76 | + } |
| 77 | + const result = await updateIcon(file); |
| 78 | + results.push(result); |
| 79 | + if (result.status === 'updated') { |
| 80 | + console.log(` ✔ ${file} updated`); |
| 81 | + } else if (result.status === 'unchanged') { |
| 82 | + console.log(` • ${file} unchanged`); |
| 83 | + } else { |
| 84 | + // allow-any-unicode-next-line |
| 85 | + console.warn(` ✖ ${file} ${result.error}`); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + const workers = Array.from({ length: Math.min(concurrency, icons.length) }, () => worker()); |
| 91 | + await Promise.all(workers); |
| 92 | + |
| 93 | + const updated = results.filter(r => r.status === 'updated').length; |
| 94 | + const unchanged = results.filter(r => r.status === 'unchanged').length; |
| 95 | + const errored = results.filter(r => r.status === 'error').length; |
| 96 | + console.log(`Done. Updated: ${updated}, Unchanged: ${unchanged}, Errors: ${errored}.`); |
| 97 | + if (errored) { |
| 98 | + process.exitCode = 1; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +main().catch(err => { |
| 103 | + console.error(err?.stack || err?.message || String(err)); |
| 104 | + process.exit(1); |
| 105 | +}); |
| 106 | + |
| 107 | +export { }; // ensure this file is treated as a module |
0 commit comments