| 37 | const files = ['main.js', 'styles.css', 'manifest.json']; |
| 38 | |
| 39 | async function copyToDestination(destPath) { |
| 40 | // Resolve the destination path |
| 41 | const resolvedPath = resolve(destPath); |
| 42 | |
| 43 | // Ensure the directory exists (including nested) |
| 44 | await mkdir(resolvedPath, { recursive: true }); |
| 45 | |
| 46 | // Check each file exists before copying |
| 47 | const copyPromises = files.map(async (file) => { |
| 48 | try { |
| 49 | await access(file, constants.F_OK); |
| 50 | const destFile = join(resolvedPath, file); |
| 51 | await copyFile(file, destFile); |
| 52 | } catch (err) { |
| 53 | if (err && err.code === 'ENOENT') { |
| 54 | // Differentiate between missing source and missing destination path |
| 55 | try { |
| 56 | await access(file, constants.F_OK); |
| 57 | } catch { |
| 58 | console.warn(`⚠️ Warning: source file ${file} not found, skipping`); |
| 59 | return; |
| 60 | } |
| 61 | console.warn(`⚠️ Warning: destination path missing for ${file}. Attempting to create…`); |
| 62 | await mkdir(resolvedPath, { recursive: true }); |
| 63 | const destFileRetry = join(resolvedPath, file); |
| 64 | await copyFile(file, destFileRetry); |
| 65 | } else { |
| 66 | throw new Error(`Failed to copy ${file}: ${err?.message || err}`); |
| 67 | } |
| 68 | } |
| 69 | }); |
| 70 | |
| 71 | await Promise.all(copyPromises); |
| 72 | console.log(`✅ Files copied to: ${resolvedPath}`); |
| 73 | } |
| 74 | |
| 75 | async function copyFiles() { |
| 76 | try { |