UNPKG

@moontra/moonui-pro

Version:

Premium React components for MoonUI - Advanced UI library with 50+ pro components including performance, interactive, and gesture components

308 lines (270 loc) • 9.48 kB
#!/usr/bin/env node /** * MoonUI Pro Build Plugin * Provides build-time license validation and code transformation */ import crypto from 'crypto'; import fs from 'fs'; import path from 'path'; // License validation modes const LICENSE_MODES = { STRICT: 'strict', // Always require valid license (CI/CD) PERPETUAL: 'perpetual', // Once licensed, always works GRACE: 'grace' // Allow grace period after expiry }; // Get license mode from environment const getLicenseMode = () => { const mode = process.env.MOONUI_LICENSE_MODE || LICENSE_MODES.PERPETUAL; return mode; }; // Check for existing license stamp const hasLicenseStamp = () => { try { // Check for license stamp file in project const stampPath = path.join(process.cwd(), '.moonui-license-stamp'); if (fs.existsSync(stampPath)) { const stamp = JSON.parse(fs.readFileSync(stampPath, 'utf8')); // Verify stamp integrity if (stamp.projectId && stamp.licensedAt && stamp.version) { console.log('āœ… MoonUI Pro: Using existing license stamp'); console.log(` Licensed on: ${new Date(stamp.licensedAt).toLocaleDateString()}`); console.log(` Version: ${stamp.version}`); return true; } } } catch (err) { // Stamp invalid or corrupted } return false; }; // Create license stamp for perpetual usage const createLicenseStamp = (licenseKey, licenseInfo = {}) => { try { const stamp = { projectId: crypto.randomBytes(16).toString('hex'), licensedAt: new Date().toISOString(), version: licenseInfo.version || '2.x', licensedTo: licenseInfo.email || 'unknown', plan: licenseInfo.plan || 'unknown', mode: 'perpetual', // Don't store the actual key for security keyHash: crypto.createHash('sha256').update(licenseKey).digest('hex').substring(0, 8) }; const stampPath = path.join(process.cwd(), '.moonui-license-stamp'); fs.writeFileSync(stampPath, JSON.stringify(stamp, null, 2)); console.log('šŸ“‹ MoonUI Pro: License stamp created'); console.log(' Your project can now build without active license'); console.log(' Add .moonui-license-stamp to your repository'); return true; } catch (err) { console.warn('āš ļø Could not create license stamp:', err.message); return false; } }; // License validation with perpetual rights const validateLicense = async (licenseKey, options = {}) => { const mode = getLicenseMode(); // Check for existing license stamp first (perpetual mode) if (mode === LICENSE_MODES.PERPETUAL && hasLicenseStamp()) { // Project was previously licensed, allow usage return { valid: true, mode: 'perpetual', message: 'Using perpetual license from previous activation' }; } // If no key provided if (!licenseKey) { // In development, be lenient if (process.env.NODE_ENV === 'development') { console.warn('āš ļø MoonUI Pro: No license key found (development mode)'); return { valid: true, mode: 'development' }; } // In production with perpetual mode, check for stamp if (mode === LICENSE_MODES.PERPETUAL && hasLicenseStamp()) { return { valid: true, mode: 'perpetual' }; } // Otherwise require license throw new Error('MoonUI Pro: License key is required. Set MOONUI_LICENSE_KEY environment variable.'); } // Validate license format const isValid = licenseKey.startsWith('mk_') || licenseKey.startsWith('team_') || licenseKey.startsWith('ci_'); if (!isValid) { throw new Error('MoonUI Pro: Invalid license key format'); } // In real implementation, this would call the API // For now, simulate API response const licenseInfo = { valid: true, email: 'user@example.com', plan: 'pro', version: '2.x', expiresAt: null // or a date }; // Create stamp for perpetual usage if valid if (licenseInfo.valid && mode === LICENSE_MODES.PERPETUAL) { createLicenseStamp(licenseKey, licenseInfo); } return licenseInfo; }; // Webpack plugin for Next.js export function withMoonUI(nextConfig = {}) { const licenseKey = process.env.MOONUI_LICENSE_KEY || process.env.MOONUI_TEAM_TOKEN || process.env.MOONUI_CI_TOKEN || nextConfig.moonui?.licenseKey; return { ...nextConfig, webpack(config, options) { // Validate license at build time validateLicense(licenseKey).then(result => { if (result.mode === 'perpetual') { console.log('šŸ“¦ MoonUI Pro: Building with perpetual license'); } }).catch(err => { console.error('āŒ MoonUI Pro License Error:', err.message); if (process.env.NODE_ENV === 'production' && !hasLicenseStamp()) { process.exit(1); } }); // Add custom webpack plugin for code transformation config.plugins.push({ apply(compiler) { compiler.hooks.compilation.tap('MoonUIProPlugin', (compilation) => { // Add license validation to the build compilation.hooks.processAssets.tap( { name: 'MoonUIProPlugin', stage: compilation.constructor.PROCESS_ASSETS_STAGE_OPTIMIZE, }, (assets) => { // Process MoonUI Pro assets for (const [pathname, source] of Object.entries(assets)) { if (pathname.includes('moonui-pro')) { // Add license check to component initialization const content = source.source(); const modifiedContent = injectLicenseCheck(content, licenseKey); compilation.updateAsset(pathname, { source: () => modifiedContent, size: () => modifiedContent.length, }); } } } ); }); }, }); // Apply user's webpack config if exists if (typeof nextConfig.webpack === 'function') { return nextConfig.webpack(config, options); } return config; }, }; } // Vite plugin export function moonUIVitePlugin(options = {}) { const licenseKey = process.env.MOONUI_LICENSE_KEY || process.env.MOONUI_TEAM_TOKEN || process.env.MOONUI_CI_TOKEN || options.licenseKey; return { name: 'moonui-pro', enforce: 'pre', async buildStart() { // Validate license at build time try { const result = await validateLicense(licenseKey); if (result.mode === 'perpetual') { console.log('šŸ“¦ MoonUI Pro: Building with perpetual license'); } else { console.log('āœ… MoonUI Pro: License validated successfully'); } } catch (err) { console.error('āŒ MoonUI Pro License Error:', err.message); if (process.env.NODE_ENV === 'production' && !hasLicenseStamp()) { throw err; } } }, transform(code, id) { // Transform MoonUI Pro imports if (id.includes('@moontra/moonui-pro')) { // Inject license validation return { code: injectLicenseCheck(code, licenseKey), map: null, }; } return null; }, }; } // Helper function to inject license check function injectLicenseCheck(code, licenseKey) { // Only in production builds if (process.env.NODE_ENV !== 'production') { return code; } // Create a hash of the license for client-side validation const licenseHash = crypto .createHash('sha256') .update(licenseKey + process.env.NODE_ENV) .digest('hex') .substring(0, 16); // Inject validation code at the beginning const validationCode = ` // MoonUI Pro License Validation (function() { const lh = '${licenseHash}'; const validated = window.__MOONUI_PRO_VALIDATED__; if (!validated) { window.__MOONUI_PRO_VALIDATED__ = true; console.log('%c✨ MoonUI Pro Components Loaded', 'color: #8b5cf6; font-weight: bold;'); } })(); `; return validationCode + code; } // ESBuild plugin export function moonUIESBuildPlugin(options = {}) { const licenseKey = process.env.MOONUI_LICENSE_KEY || process.env.MOONUI_TEAM_TOKEN || process.env.MOONUI_CI_TOKEN || options.licenseKey; return { name: 'moonui-pro', setup(build) { // Validate license at build start build.onStart(async () => { try { await validateLicense(licenseKey); console.log('āœ… MoonUI Pro: License validated successfully'); } catch (err) { console.error('āŒ MoonUI Pro License Error:', err.message); if (process.env.NODE_ENV === 'production') { throw err; } } }); // Transform MoonUI Pro files build.onLoad({ filter: /moonui-pro.*\.(js|jsx|ts|tsx)$/ }, async (args) => { const source = await fs.promises.readFile(args.path, 'utf8'); const contents = injectLicenseCheck(source, licenseKey); return { contents, loader: path.extname(args.path).slice(1), }; }); }, }; } // Export all plugins export default { withMoonUI, moonUIVitePlugin, moonUIESBuildPlugin, };