92 lines
2.4 KiB
JavaScript
92 lines
2.4 KiB
JavaScript
// =============================================================================
|
|
// DEV SCRIPT
|
|
// =============================================================================
|
|
// Watches TypeScript files and on each change:
|
|
// 1. Recompiles with esbuild
|
|
// 2. Copies the output to all required locations
|
|
//
|
|
// Usage: npm run watch
|
|
|
|
const { spawn, execSync } = require('child_process');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
// Paths
|
|
const SOURCE = path.resolve(__dirname, '../dist/light-preset-manager.js');
|
|
const TARGETS = [
|
|
path.resolve(__dirname, '../../custom_components/light_preset_manager/frontend/light-preset-manager.js'),
|
|
path.resolve(__dirname, '../../ha-dev-config/www/light-preset-manager.js'),
|
|
];
|
|
|
|
// Helper: copy built file to all targets
|
|
function copyToTargets() {
|
|
for (const target of TARGETS) {
|
|
const dir = path.dirname(target);
|
|
if (!fs.existsSync(dir)) {
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
}
|
|
try {
|
|
fs.copyFileSync(SOURCE, target);
|
|
} catch (err) {
|
|
// Ignore errors on first run (file may not exist yet)
|
|
}
|
|
}
|
|
const time = new Date().toLocaleTimeString();
|
|
console.log(`📦 [${time}] Copied to all targets`);
|
|
}
|
|
|
|
// Helper: format current time
|
|
function time() {
|
|
return new Date().toLocaleTimeString();
|
|
}
|
|
|
|
// Start message
|
|
console.log('🚀 Starting development server...');
|
|
console.log('');
|
|
|
|
// Start esbuild in watch mode
|
|
const esbuild = spawn('npx', [
|
|
'esbuild',
|
|
'src/light-preset-manager.ts',
|
|
'--bundle',
|
|
'--format=esm',
|
|
'--outfile=dist/light-preset-manager.js',
|
|
'--sourcemap', // Enables debugging in browser
|
|
'--watch'
|
|
], {
|
|
cwd: path.resolve(__dirname, '..'),
|
|
shell: true,
|
|
stdio: 'pipe'
|
|
});
|
|
|
|
// Handle esbuild output
|
|
esbuild.stdout.on('data', (data) => {
|
|
const output = data.toString().trim();
|
|
if (!output) return;
|
|
|
|
console.log(output);
|
|
|
|
// When esbuild says "watching" or "done", copy the files
|
|
if (output.includes('watching') || output.includes('done')) {
|
|
copyToTargets();
|
|
console.log(`✅ [${time()}] Ready - Refresh your browser (Ctrl+Shift+R)`);
|
|
console.log('');
|
|
}
|
|
});
|
|
|
|
// Handle esbuild errors
|
|
esbuild.stderr.on('data', (data) => {
|
|
console.error(`❌ ${data.toString().trim()}`);
|
|
});
|
|
|
|
// Handle process exit
|
|
esbuild.on('close', (code) => {
|
|
console.log(`\n⏹️ esbuild stopped (code ${code})`);
|
|
});
|
|
|
|
// Handle Ctrl+C gracefully
|
|
process.on('SIGINT', () => {
|
|
console.log('\n👋 Stopping...');
|
|
esbuild.kill();
|
|
process.exit();
|
|
}); |