|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Downloads the given NPM theme package with the version specified in the given Java theme class, |
| 5 | + * and copies its distribution files to the target directory to expose them as static resources in |
| 6 | + * a web application. The script is automatically run in the Maven build for the Aura and Lumo |
| 7 | + * theme modules. |
| 8 | + */ |
| 9 | + |
| 10 | +const fs = require('fs'); |
| 11 | +const path = require('path'); |
| 12 | +const { execSync } = require('child_process'); |
| 13 | + |
| 14 | +// Check command line arguments |
| 15 | +if (process.argv.length !== 4) { |
| 16 | + console.error('Usage: node copy-theme-distribution.js <path-to-theme-class> <npm-theme-package>'); |
| 17 | + process.exit(1); |
| 18 | +} |
| 19 | + |
| 20 | +const javaThemeClass = process.argv[2]; |
| 21 | +const npmThemePackage = process.argv[3]; |
| 22 | + |
| 23 | +// Read Java file and extract NPM package info |
| 24 | +if (!fs.existsSync(javaThemeClass)) { |
| 25 | + console.error(`Theme class not found: ${javaThemeClass}`); |
| 26 | + process.exit(1); |
| 27 | +} |
| 28 | + |
| 29 | +const javaContent = fs.readFileSync(javaThemeClass, 'utf8'); |
| 30 | + |
| 31 | +// Find version from NpmPackage annotation for the specific package |
| 32 | +const packageRegex = new RegExp(`@NpmPackage\\s*\\(\\s*value\\s*=\\s*"${npmThemePackage.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}"\\s*,\\s*version\\s*=\\s*"([^"]+)"`); |
| 33 | +const match = javaContent.match(packageRegex); |
| 34 | + |
| 35 | +if (!match) { |
| 36 | + console.error(`Could not extract NPM package version for ${npmThemePackage} from ${javaThemeClass}`); |
| 37 | + process.exit(1); |
| 38 | +} |
| 39 | + |
| 40 | +const npmThemePackageVersion = match[1]; |
| 41 | + |
| 42 | +console.log(`Extracting theme distribution for ${npmThemePackage}@${npmThemePackageVersion}`); |
| 43 | + |
| 44 | +// Create temporary directory for npm install |
| 45 | +const tempDir = path.join('target', 'npm-workspace'); |
| 46 | +fs.mkdirSync(tempDir, { recursive: true }); |
| 47 | + |
| 48 | +// Install the theme package using npm |
| 49 | +const packageJson = { |
| 50 | + name: 'theme-extractor', |
| 51 | + version: '1.0.0', |
| 52 | + dependencies: { |
| 53 | + [npmThemePackage]: npmThemePackageVersion |
| 54 | + } |
| 55 | +}; |
| 56 | + |
| 57 | +fs.writeFileSync( |
| 58 | + path.join(tempDir, 'package.json'), |
| 59 | + JSON.stringify(packageJson, null, 2) |
| 60 | +); |
| 61 | + |
| 62 | +execSync('npm install', { |
| 63 | + cwd: tempDir, |
| 64 | + stdio: 'inherit' |
| 65 | +}); |
| 66 | + |
| 67 | +// Create target directory |
| 68 | +const targetDir = path.join('target/classes/META-INF/resources', npmThemePackage); |
| 69 | +fs.mkdirSync(targetDir, { recursive: true }); |
| 70 | + |
| 71 | +// Copy the distribution files |
| 72 | +const sourceDir = path.join(tempDir, 'node_modules', npmThemePackage, 'dist'); |
| 73 | +if (!fs.existsSync(sourceDir)) { |
| 74 | + console.error(`Distribution directory not found: ${sourceDir}`); |
| 75 | + process.exit(1); |
| 76 | +} |
| 77 | + |
| 78 | +fs.cpSync(sourceDir, targetDir, { recursive: true }); |
| 79 | + |
| 80 | +console.log(`Copied theme distribution to ${targetDir}`); |
0 commit comments