|
| 1 | +/* |
| 2 | + * Copyright 2025 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | +import chokidar from 'chokidar'; |
| 13 | +import fs from 'fs'; |
| 14 | +import path from 'path'; |
| 15 | +import { fileURLToPath } from 'url'; |
| 16 | + |
| 17 | +const __filename = fileURLToPath(import.meta.url); |
| 18 | +const __dirname = path.dirname(__filename); |
| 19 | + |
| 20 | +const srcDir = path.join(__dirname, '..', 'src'); |
| 21 | +const destDir = path.join(__dirname, '..', '.aws-sam/build/SpacecatAuditWorkerFunction/src'); |
| 22 | + |
| 23 | +function copyFile(filePath) { |
| 24 | + try { |
| 25 | + const relativePath = path.relative(srcDir, filePath); |
| 26 | + const destPath = path.join(destDir, relativePath); |
| 27 | + |
| 28 | + // Skip files that start with a dot |
| 29 | + if (path.basename(filePath).startsWith('.')) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + fs.mkdirSync(path.dirname(destPath), { recursive: true }); |
| 34 | + |
| 35 | + // Read and compare file contents |
| 36 | + const srcContent = fs.readFileSync(filePath); |
| 37 | + let destContent; |
| 38 | + try { |
| 39 | + destContent = fs.readFileSync(destPath); |
| 40 | + } catch { |
| 41 | + // If the destination file doesn't exist, proceed with copying |
| 42 | + destContent = null; |
| 43 | + } |
| 44 | + |
| 45 | + if (!destContent || !srcContent.equals(destContent)) { |
| 46 | + fs.copyFileSync(filePath, destPath); |
| 47 | + console.log(`Copied ${filePath} to ${destPath}`); |
| 48 | + } |
| 49 | + } catch (error) { |
| 50 | + console.error(`Error copying file ${filePath}:`, error); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// Initialize watcher. |
| 55 | +const watcher = chokidar.watch(srcDir, { |
| 56 | + ignored: /(^|[\\])\../, // ignore dotfiles |
| 57 | + persistent: true, |
| 58 | +}); |
| 59 | + |
| 60 | +// Add event listeners. |
| 61 | +watcher |
| 62 | + .on('add', copyFile) |
| 63 | + .on('change', copyFile) |
| 64 | + .on('error', (error) => console.error(`Watcher error: ${error}`)); |
| 65 | + |
| 66 | +console.log(`Watching for changes in ${srcDir}...`); |
| 67 | + |
| 68 | +// Handle graceful shutdown |
| 69 | +process.on('SIGINT', () => { |
| 70 | + console.log('Stopping file watcher...'); |
| 71 | + watcher.close().then(() => { |
| 72 | + console.log('File watcher closed.'); |
| 73 | + process.exit(0); |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +// Output some initial stats |
| 78 | +setTimeout(() => { |
| 79 | + const watchedPaths = watcher.getWatched(); |
| 80 | + const fileCount = Object.values(watchedPaths).reduce((acc, files) => acc + files.length, 0); |
| 81 | + console.log(`Watching ${fileCount} files in ${Object.keys(watchedPaths).length} directories`); |
| 82 | +}, 2000); |
0 commit comments