diff --git a/package.json b/package.json index 00ba3a7599f15b1fa3684717d5a43e33c7eb9071..3ddcad3a0714dcfc6e37c866980fe1668a7e882a 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ ], "scripts": { "build": "webpack --mode production && npm run build:types", + "postbuild": "node scripts/publish-local.js", "build:dev": "webpack --mode development && npm run build:types", "build:types": "tsc --emitDeclarationOnly --declaration --declarationMap", "prepare": "npm run build", diff --git a/scripts/publish-local.js b/scripts/publish-local.js new file mode 100644 index 0000000000000000000000000000000000000000..956574a182468dc670d6d6479eb7e8c507fc296d --- /dev/null +++ b/scripts/publish-local.js @@ -0,0 +1,67 @@ +#!/usr/bin/env node +/** + * Auto-publish to local Verdaccio registry if available. + * This script is run automatically after `npm run build`. + * + * If Verdaccio is running at localhost:4873, the package will be published there. + * If Verdaccio is not running, the script silently exits (no error). + * + * The script unpublishes the current version first (if it exists) to allow + * republishing the same version with updated code during local development. + */ + +const { execSync } = require('child_process'); +const http = require('http'); +const fs = require('fs'); +const path = require('path'); + +const VERDACCIO_URL = process.env.VERDACCIO_URL || 'http://localhost:4873'; + +// Read package.json to get name and version +const packageJsonPath = path.join(process.cwd(), 'package.json'); +const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); +const packageSpec = `${packageJson.name}@${packageJson.version}`; + +// Check if Verdaccio is running +const req = http.get(VERDACCIO_URL, { timeout: 2000 }, (res) => { + if (res.statusCode === 200) { + console.log('[publish-local] Verdaccio detected at', VERDACCIO_URL); + + // First, try to unpublish the current version (ignore errors if it doesn't exist) + try { + console.log(`[publish-local] Unpublishing ${packageSpec} (if it exists)...`); + execSync(`npm unpublish ${packageSpec} --registry ${VERDACCIO_URL} --force`, { + stdio: 'pipe', + cwd: process.cwd() + }); + console.log(`[publish-local] Unpublished ${packageSpec}`); + } catch (err) { + // Package doesn't exist yet - that's fine, continue with publish + console.log(`[publish-local] ${packageSpec} not in registry (this is OK)`); + } + + // Now publish + try { + // Use --ignore-scripts to prevent infinite loop (prepare -> build -> postbuild -> publish -> prepare...) + execSync(`npm publish --registry ${VERDACCIO_URL} --ignore-scripts`, { + stdio: 'inherit', + cwd: process.cwd() + }); + console.log('[publish-local] Successfully published to local Verdaccio'); + } catch (err) { + console.warn('[publish-local] Publish failed:', err.message); + } + } else { + console.log('[publish-local] Verdaccio returned status', res.statusCode, '- skipping'); + } +}); + +req.on('error', () => { + // Verdaccio not running - silently skip + console.log('[publish-local] Verdaccio not running at', VERDACCIO_URL, '- skipping local publish'); +}); + +req.on('timeout', () => { + req.destroy(); + console.log('[publish-local] Verdaccio not responding - skipping local publish'); +});