|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import fs from "fs"; |
| 4 | +import path from "path"; |
| 5 | +import { fileURLToPath } from "url"; |
| 6 | + |
| 7 | +const __filename = fileURLToPath(import.meta.url); |
| 8 | +const __dirname = path.dirname(__filename); |
| 9 | + |
| 10 | +// Read package.json to determine which brand we're building |
| 11 | +const packagePath = path.join(__dirname, "../vscode/package.json"); |
| 12 | +const packageJson = JSON.parse(fs.readFileSync(packagePath, "utf8")); |
| 13 | + |
| 14 | +// Use the package name to determine branding |
| 15 | +const extensionName = packageJson.name; |
| 16 | +const displayName = extensionName.toUpperCase(); |
| 17 | + |
| 18 | +console.log(`🔄 Running prebuild for ${extensionName}...`); |
| 19 | +console.log(`📦 Transforming package.json...`); |
| 20 | + |
| 21 | +// Define the list of known brands |
| 22 | +const knownBrands = ["konveyor", "mta"]; |
| 23 | + |
| 24 | +// Build regex patterns from the brand list |
| 25 | +const brandPattern = knownBrands.join("|"); |
| 26 | +const brandRegex = new RegExp(brandPattern, "gi"); |
| 27 | +const brandPrefixRegex = new RegExp(`\\b(${brandPattern})\\.`, "gi"); |
| 28 | +const brandWordRegex = new RegExp(`\\b(${brandPattern})(?=\\s|$)`, "gi"); |
| 29 | + |
| 30 | +// Apply branding transformations |
| 31 | +Object.assign(packageJson, { |
| 32 | + displayName: `${displayName} Extension for VSCode`, |
| 33 | + description: |
| 34 | + extensionName === "mta" |
| 35 | + ? "Migration Toolkit for Applications - Enterprise migration and modernization tool" |
| 36 | + : "Open-source migration and modernization tool", |
| 37 | + publisher: extensionName, |
| 38 | + author: extensionName === "mta" ? "Red Hat" : "Konveyor", |
| 39 | + icon: packageJson.icon, // Keep existing icon path - assets will be copied later |
| 40 | +}); |
| 41 | + |
| 42 | +// Transform configuration properties |
| 43 | +if (packageJson.contributes?.configuration?.properties) { |
| 44 | + const props = packageJson.contributes.configuration.properties; |
| 45 | + const newProps = {}; |
| 46 | + |
| 47 | + Object.keys(props).forEach((key) => { |
| 48 | + const newKey = key.replace(/^[^.]+\./, `${extensionName}.`); |
| 49 | + newProps[newKey] = props[key]; |
| 50 | + }); |
| 51 | + |
| 52 | + packageJson.contributes.configuration.properties = newProps; |
| 53 | + packageJson.contributes.configuration.title = displayName; |
| 54 | +} |
| 55 | + |
| 56 | +// Transform commands |
| 57 | +if (packageJson.contributes?.commands) { |
| 58 | + // Categories that should not be transformed by branding |
| 59 | + const preservedCategories = ["diffEditor"]; |
| 60 | + |
| 61 | + packageJson.contributes.commands = packageJson.contributes.commands.map((cmd) => ({ |
| 62 | + ...cmd, |
| 63 | + command: cmd.command.replace(/^[^.]+\./, `${extensionName}.`), |
| 64 | + // Only transform category if it's not in the preserved list |
| 65 | + category: preservedCategories.includes(cmd.category) ? cmd.category : displayName, |
| 66 | + title: cmd.title?.replace(brandRegex, displayName) || cmd.title, |
| 67 | + })); |
| 68 | +} |
| 69 | + |
| 70 | +// Transform views and containers |
| 71 | +if (packageJson.contributes?.viewsContainers?.activitybar) { |
| 72 | + packageJson.contributes.viewsContainers.activitybar = |
| 73 | + packageJson.contributes.viewsContainers.activitybar.map((container) => ({ |
| 74 | + ...container, |
| 75 | + id: extensionName, |
| 76 | + title: displayName, |
| 77 | + icon: container.icon, // Keep existing icon path - assets will be copied later |
| 78 | + })); |
| 79 | +} |
| 80 | + |
| 81 | +if (packageJson.contributes?.views) { |
| 82 | + const newViews = {}; |
| 83 | + Object.keys(packageJson.contributes.views).forEach((viewKey) => { |
| 84 | + newViews[extensionName] = packageJson.contributes.views[viewKey].map((view) => ({ |
| 85 | + ...view, |
| 86 | + id: view.id.replace(/^[^.]+\./, `${extensionName}.`), |
| 87 | + name: view.name.replace(brandRegex, displayName), |
| 88 | + })); |
| 89 | + }); |
| 90 | + packageJson.contributes.views = newViews; |
| 91 | +} |
| 92 | + |
| 93 | +// Transform menus |
| 94 | +if (packageJson.contributes?.menus) { |
| 95 | + const transformMenuCommands = (menuItems) => { |
| 96 | + return menuItems.map((item) => ({ |
| 97 | + ...item, |
| 98 | + command: item.command?.replace(/^[^.]+\./, `${extensionName}.`), |
| 99 | + when: item.when |
| 100 | + ?.replace(brandPrefixRegex, `${extensionName}.`) |
| 101 | + .replace(brandWordRegex, extensionName), |
| 102 | + submenu: item.submenu?.replace(/^[^.]+\./, `${extensionName}.`), |
| 103 | + })); |
| 104 | + }; |
| 105 | + |
| 106 | + const newMenus = {}; |
| 107 | + Object.keys(packageJson.contributes.menus).forEach((menuKey) => { |
| 108 | + const newMenuKey = new RegExp(`^(${brandPattern})`, "i").test(menuKey) |
| 109 | + ? menuKey.replace(/^[^.]+/, extensionName) |
| 110 | + : menuKey; |
| 111 | + newMenus[newMenuKey] = transformMenuCommands(packageJson.contributes.menus[menuKey]); |
| 112 | + }); |
| 113 | + packageJson.contributes.menus = newMenus; |
| 114 | +} |
| 115 | + |
| 116 | +// Transform submenus |
| 117 | +if (packageJson.contributes?.submenus) { |
| 118 | + packageJson.contributes.submenus = packageJson.contributes.submenus.map((submenu) => ({ |
| 119 | + ...submenu, |
| 120 | + id: submenu.id.replace(/^[^.]+/, extensionName), |
| 121 | + label: `${displayName} Actions`, |
| 122 | + })); |
| 123 | +} |
| 124 | + |
| 125 | +// Write the transformed package.json |
| 126 | +fs.writeFileSync(packagePath, JSON.stringify(packageJson, null, 2)); |
| 127 | +console.log(`✅ ${displayName} branding transformations complete`); |
| 128 | + |
| 129 | +// Copy assets - whatever exists in the directories gets used |
| 130 | +console.log(`🖼️ Copying assets...`); |
| 131 | + |
| 132 | +// 1. Copy VSCode sidebar icon (whatever icon exists in sidebar-icons/) |
| 133 | +const iconSource = path.join(__dirname, "..", "assets/branding/sidebar-icons/icon.png"); |
| 134 | +const iconTarget = path.join(__dirname, "..", "vscode/resources/icon.png"); |
| 135 | + |
| 136 | +if (fs.existsSync(iconSource)) { |
| 137 | + fs.copyFileSync(iconSource, iconTarget); |
| 138 | + console.log(` ✅ VSCode sidebar icon copied`); |
| 139 | +} else { |
| 140 | + console.warn(` ⚠️ No sidebar icon found at: assets/branding/sidebar-icons/icon.png`); |
| 141 | +} |
| 142 | + |
| 143 | +// 2. Copy webview avatar (whatever avatar exists in avatar-icons/) |
| 144 | +const avatarSource = path.join(__dirname, "..", "assets/branding/avatar-icons/avatar.svg"); |
| 145 | +const avatarTarget = path.join(__dirname, "..", "webview-ui/public/avatarIcons/avatar.svg"); |
| 146 | + |
| 147 | +if (fs.existsSync(avatarSource)) { |
| 148 | + // Ensure target directory exists |
| 149 | + const avatarDir = path.dirname(avatarTarget); |
| 150 | + if (!fs.existsSync(avatarDir)) { |
| 151 | + fs.mkdirSync(avatarDir, { recursive: true }); |
| 152 | + } |
| 153 | + fs.copyFileSync(avatarSource, avatarTarget); |
| 154 | + console.log(` ✅ Webview avatar copied`); |
| 155 | +} else { |
| 156 | + console.warn(` ⚠️ No avatar found at: assets/branding/avatar-icons/avatar.svg`); |
| 157 | +} |
| 158 | + |
| 159 | +console.log(`✅ Prebuild complete for ${extensionName}`); |
0 commit comments