|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const { execSync } = require('child_process'); |
| 4 | + |
| 5 | +// Helper function to safely copy files |
| 6 | +function copyFile(src, dest) { |
| 7 | + try { |
| 8 | + fs.copyFileSync(src, dest); |
| 9 | + console.log(`Copied ${src} to ${dest}`); |
| 10 | + } catch (err) { |
| 11 | + console.warn(`::warning:: ${src} not found, skipping.`); |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +// Create 'dist' directory if it doesn't exist |
| 16 | +const distDir = path.join(__dirname, 'dist'); |
| 17 | +if (!fs.existsSync(distDir)) { |
| 18 | + fs.mkdirSync(distDir, { recursive: true }); |
| 19 | +} |
| 20 | + |
| 21 | +// Copy the necessary files |
| 22 | +copyFile('plugin.json', path.join(distDir, 'plugin.json')); |
| 23 | +copyFile('requirements.txt', path.join(distDir, 'requirements.txt')); |
| 24 | +copyFile('README.md', path.join(distDir, 'README.md')); |
| 25 | +copyFile('README', path.join(distDir, 'README')); |
| 26 | + |
| 27 | +// Read the plugin.json |
| 28 | +const pluginJson = JSON.parse(fs.readFileSync('plugin.json', 'utf-8')); |
| 29 | + |
| 30 | +// Handle the backend field |
| 31 | +const backend = pluginJson.backend; |
| 32 | +if (backend !== null && backend) { |
| 33 | + const backendPath = path.join(__dirname, backend); |
| 34 | + if (fs.existsSync(backendPath)) { |
| 35 | + fs.cpSync(backendPath, path.join(distDir, backend), { recursive: true }); |
| 36 | + console.log(`Copied backend: ${backend}`); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// Handle the include field |
| 41 | +const include = pluginJson.include || []; |
| 42 | +include.forEach(file => { |
| 43 | + const filePath = path.join(__dirname, file); |
| 44 | + if (fs.existsSync(filePath)) { |
| 45 | + fs.cpSync(filePath, path.join(distDir, file), { recursive: true }); |
| 46 | + console.log(`Copied included file: ${file}`); |
| 47 | + } |
| 48 | +}); |
| 49 | + |
| 50 | +// Generate metadata |
| 51 | +const commit = execSync('git rev-parse HEAD').toString().trim(); |
| 52 | +const id = execSync('git rev-list --max-parents=0 HEAD').toString().trim(); |
| 53 | +const metadata = { |
| 54 | + commit, |
| 55 | + id, |
| 56 | +}; |
| 57 | + |
| 58 | +fs.writeFileSync(path.join(distDir, 'metadata.json'), JSON.stringify(metadata, null, 2)); |
| 59 | +console.log('::notice::Computing plugin metadata...'); |
| 60 | + |
| 61 | +// Set the ID as an output variable (in GitHub Actions context, for example) |
| 62 | +console.log(`::set-output name=id::${id}`); |
| 63 | + |
| 64 | +// Create the zip file using the 'zip' command |
| 65 | +const zipCommand = `zip -r ${path.join(distDir, `${id}.zip`)} .`; |
| 66 | +execSync(zipCommand, { cwd: distDir }); |
| 67 | + |
| 68 | +console.log(`::notice::Successfully built plugin: ${id}.zip`); |
0 commit comments