dev-watch.jsā¢2.42 kB
#!/usr/bin/env node
const { spawn } = require('child_process');
const { watch } = require('fs');
const path = require('path');
console.log('š Starting Superjolt development mode...\n');
// Run initial build
console.log('š¦ Building project...');
const initialBuild = spawn('npm', ['run', 'build'], { stdio: 'inherit' });
initialBuild.on('close', (code) => {
if (code !== 0) {
console.error('ā Initial build failed');
process.exit(code);
}
console.log('š Installing globally...');
const install = spawn('npm', ['install', '-g', '.'], { stdio: 'inherit' });
install.on('close', () => {
console.log('ā
Initial setup complete!\n');
startWatching();
});
});
function startWatching() {
console.log('š Starting watch mode with auto-install...\n');
// Start nest build --watch
const nestWatch = spawn('npx', ['nest', 'build', '--watch'], {
stdio: 'inherit'
});
// Watch the dist directory for changes
const distDir = path.join(__dirname, '..', 'dist');
let installTimeout;
let isInstalling = false;
const watcher = watch(distDir, { recursive: true }, (eventType, filename) => {
// Skip if we're already installing or if it's not a .js file
if (isInstalling || !filename || !filename.endsWith('.js')) return;
// Skip map files and test files
if (filename.endsWith('.map') || filename.includes('.spec.')) return;
// Clear any pending install
clearTimeout(installTimeout);
// Wait 500ms after last change before installing (debounce)
installTimeout = setTimeout(() => {
isInstalling = true;
console.log('\nš Changes detected, updating global command...');
const install = spawn('npm', ['install', '-g', '.'], {
stdio: ['inherit', 'pipe', 'pipe']
});
install.on('close', (code) => {
if (code === 0) {
console.log('ā
Global command updated successfully!\n');
} else {
console.error('ā Failed to update global command\n');
}
isInstalling = false;
});
}, 500);
});
// Handle Ctrl+C
process.on('SIGINT', () => {
console.log('\n\nš Stopping watch mode...');
nestWatch.kill();
watcher.close();
process.exit(0);
});
nestWatch.on('close', (code) => {
console.log('Watch process exited with code', code);
watcher.close();
process.exit(code);
});
}