cindy-ai-chatbot
Version:
An AI-powered chatbot component for React applications
124 lines (108 loc) ⢠3.39 kB
JavaScript
import { execSync, spawn } from 'child_process';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import chokidar from 'chokidar';
// Get the directory name in ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Ensure the lib directory exists
const libDir = path.resolve(__dirname, '../src/lib');
if (!fs.existsSync(libDir)) {
fs.mkdirSync(libDir, { recursive: true });
}
let rollupProcess = null;
let isBuilding = false;
let buildTimeout = null;
// Function to trigger rebuild
function triggerRebuild() {
// Clear any pending timeout
if (buildTimeout) {
clearTimeout(buildTimeout);
}
// Debounce rebuilds
buildTimeout = setTimeout(() => {
if (isBuilding) {
return;
}
isBuilding = true;
console.log('\nš Detected changes, copying components...');
try {
// Copy components first
execSync('node scripts/copy-components.js', { stdio: 'inherit' });
console.log('ā
Components copied. Rollup will rebuild automatically.\n');
} catch (error) {
console.error('ā Error copying components:', error.message);
} finally {
isBuilding = false;
}
}, 300); // 300ms debounce
}
console.log('š Starting library watch mode...\n');
// Initial copy of components
console.log('š¦ Copying components...');
execSync('node scripts/copy-components.js', { stdio: 'inherit' });
// Run rollup in watch mode
console.log('š Starting Rollup watch mode...\n');
rollupProcess = spawn('rollup', ['-c', '--bundleConfigAsCjs', '--watch'], {
stdio: 'inherit',
shell: true
});
rollupProcess.on('close', (code) => {
if (code !== 0 && code !== null) {
console.error(`\nā Build process exited with code ${code}`);
}
process.exit(code || 0);
});
// Watch for changes in source files that might not be picked up by Rollup
// This ensures components are copied when they change
const srcDir = path.resolve(__dirname, '../src');
const watcher = chokidar.watch([
path.join(srcDir, 'components/**/*.{ts,tsx,js,jsx}'),
path.join(srcDir, 'providers/**/*.{ts,tsx,js,jsx}'),
path.join(srcDir, 'styles/**/*.{scss,css}'),
'!**/node_modules/**',
'!**/lib/**'
], {
ignored: /node_modules/,
persistent: true,
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: 200,
pollInterval: 100
}
});
watcher
.on('change', (filePath) => {
const relativePath = path.relative(process.cwd(), filePath);
console.log(`\nš File changed: ${relativePath}`);
triggerRebuild();
})
.on('add', (filePath) => {
const relativePath = path.relative(process.cwd(), filePath);
console.log(`\nā File added: ${relativePath}`);
triggerRebuild();
})
.on('unlink', (filePath) => {
const relativePath = path.relative(process.cwd(), filePath);
console.log(`\nšļø File removed: ${relativePath}`);
triggerRebuild();
})
.on('ready', () => {
console.log('ā
File watcher ready. Monitoring for changes...\n');
})
.on('error', (error) => {
console.error('ā Watcher error:', error);
});
// Handle process termination
process.on('SIGINT', () => {
console.log('\n\nš Stopping watch mode...');
if (buildTimeout) {
clearTimeout(buildTimeout);
}
if (rollupProcess) {
rollupProcess.kill();
}
watcher.close();
process.exit(0);
});