|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright 2025 The Kubernetes Authors |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * This script fetches official plugins from https://github.com/headlamp-k8s/plugins/ |
| 21 | + * and bundles them into official-plugins/ directory for inclusion with headlamp-plugin package. |
| 22 | + * These plugins are referenced in AGENTS.md and help agents write good plugins. |
| 23 | + */ |
| 24 | + |
| 25 | +const fs = require('fs-extra'); |
| 26 | +const path = require('path'); |
| 27 | +const { execSync } = require('child_process'); |
| 28 | +const { getRemoteGitHash, storeHash, shouldSkipBasedOnHash } = require('./git-hash-utils'); |
| 29 | + |
| 30 | +const scriptDir = __dirname; |
| 31 | +const pluginDir = path.resolve(scriptDir, '..'); |
| 32 | +const officialPluginsDir = path.resolve(pluginDir, 'official-plugins'); |
| 33 | +const hashFile = path.resolve(officialPluginsDir, '.git-hash'); |
| 34 | +const officialPluginsRepo = 'https://github.com/headlamp-k8s/plugins.git'; |
| 35 | + |
| 36 | +console.log('Fetching official plugins...'); |
| 37 | + |
| 38 | +// Get the current remote hash |
| 39 | +const remoteHash = getRemoteGitHash(officialPluginsRepo); |
| 40 | + |
| 41 | +// Check if we can skip fetching |
| 42 | +if (shouldSkipBasedOnHash(officialPluginsDir, hashFile, remoteHash)) { |
| 43 | + console.log('Official plugins are already up to date (git hash matches)'); |
| 44 | + console.log('Skipping fetch...'); |
| 45 | + process.exit(0); |
| 46 | +} |
| 47 | + |
| 48 | +console.log('Fetching latest official plugins from GitHub...'); |
| 49 | + |
| 50 | +// Remove existing directory if it exists |
| 51 | +if (fs.existsSync(officialPluginsDir)) { |
| 52 | + console.log('Removing existing official-plugins directory...'); |
| 53 | + fs.rmSync(officialPluginsDir, { recursive: true }); |
| 54 | +} |
| 55 | + |
| 56 | +// Create official-plugins directory |
| 57 | +fs.mkdirSync(officialPluginsDir, { recursive: true }); |
| 58 | + |
| 59 | +// Clone the repository into a temporary directory |
| 60 | +const tempDir = path.resolve(pluginDir, '.temp-official-plugins'); |
| 61 | +if (fs.existsSync(tempDir)) { |
| 62 | + fs.rmSync(tempDir, { recursive: true }); |
| 63 | +} |
| 64 | + |
| 65 | +try { |
| 66 | + console.log('Cloning official plugins repository...'); |
| 67 | + execSync(`git clone --depth 1 ${officialPluginsRepo} ${tempDir}`, { |
| 68 | + stdio: 'inherit', |
| 69 | + }); |
| 70 | + |
| 71 | + // Get the current commit hash |
| 72 | + const currentHash = execSync('git rev-parse HEAD', { |
| 73 | + cwd: tempDir, |
| 74 | + encoding: 'utf8', |
| 75 | + }).trim(); |
| 76 | + |
| 77 | + // Get list of plugin directories (skip .git and README.md) |
| 78 | + const pluginDirs = fs |
| 79 | + .readdirSync(tempDir, { withFileTypes: true }) |
| 80 | + .filter( |
| 81 | + dirent => |
| 82 | + dirent.isDirectory() && dirent.name !== '.git' && !dirent.name.startsWith('.') |
| 83 | + ) |
| 84 | + .map(dirent => dirent.name); |
| 85 | + |
| 86 | + console.log(`Found ${pluginDirs.length} official plugins`); |
| 87 | + |
| 88 | + // Copy each plugin directory |
| 89 | + pluginDirs.forEach(pluginName => { |
| 90 | + const sourcePath = path.join(tempDir, pluginName); |
| 91 | + const destPath = path.join(officialPluginsDir, pluginName); |
| 92 | + |
| 93 | + console.log(`Copying ${pluginName}...`); |
| 94 | + |
| 95 | + // Copy the plugin directory |
| 96 | + fs.copySync(sourcePath, destPath, { |
| 97 | + filter: src => { |
| 98 | + // Skip node_modules, dist, and other build artifacts |
| 99 | + const relativePath = path.relative(sourcePath, src); |
| 100 | + if (relativePath.includes('node_modules')) return false; |
| 101 | + if (relativePath.includes('dist')) return false; |
| 102 | + if (relativePath.includes('.git')) return false; |
| 103 | + if (relativePath.includes('.eslintcache')) return false; |
| 104 | + if (relativePath.includes('storybook-static')) return false; |
| 105 | + if (relativePath.includes('package-lock.json')) return false; |
| 106 | + return true; |
| 107 | + }, |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + // Store the git hash |
| 112 | + storeHash(hashFile, currentHash); |
| 113 | + |
| 114 | + console.log( |
| 115 | + `Successfully fetched ${pluginDirs.length} official plugins to official-plugins/` |
| 116 | + ); |
| 117 | + console.log(`Git hash: ${currentHash}`); |
| 118 | +} catch (error) { |
| 119 | + console.error('Failed to fetch official plugins:', error.message); |
| 120 | + process.exit(1); |
| 121 | +} finally { |
| 122 | + // Clean up temporary directory |
| 123 | + if (fs.existsSync(tempDir)) { |
| 124 | + console.log('Cleaning up temporary directory...'); |
| 125 | + fs.rmSync(tempDir, { recursive: true }); |
| 126 | + } |
| 127 | +} |
0 commit comments