|
1 | | -const fs = require('fs-extra') |
| 1 | +/* eslint-disable */ |
| 2 | +const fs = require('fs').promises |
2 | 3 | const path = require('path') |
3 | 4 |
|
4 | | -function copyDirectory() { |
| 5 | +async function copyDirectory() { |
5 | 6 | const sourceDir = path.resolve(__dirname, './extensions') |
6 | 7 | const destinationDir = path.resolve(__dirname, './dist/extensions') |
7 | | - // Check if the source directory exists |
8 | | - if (!fs.existsSync(sourceDir)) { |
9 | | - console.error(`Source directory '${sourceDir}' not found.`) |
10 | | - return |
| 8 | + |
| 9 | + try { |
| 10 | + try { |
| 11 | + await fs.access(sourceDir) |
| 12 | + } catch (error) { |
| 13 | + console.error(`Source directory '${sourceDir}' not found.`) |
| 14 | + return |
| 15 | + } |
| 16 | + |
| 17 | + // Create the destination directory if it doesn't exist |
| 18 | + try { |
| 19 | + await fs.mkdir(destinationDir, {recursive: true}) |
| 20 | + } catch (err) { |
| 21 | + // Ignore the error if the directory already exists |
| 22 | + if (err.code !== 'EEXIST') throw err |
| 23 | + } |
| 24 | + |
| 25 | + // Copy the contents of the source directory to the destination directory |
| 26 | + await copyRecursive(sourceDir, destinationDir) |
| 27 | + |
| 28 | + console.log(`Copied '${sourceDir}' to '${destinationDir}'.`) |
| 29 | + } catch (err) { |
| 30 | + console.error('An error occurred:', err) |
11 | 31 | } |
| 32 | +} |
12 | 33 |
|
13 | | - // Create the destination directory if it doesn't exist |
14 | | - fs.ensureDirSync(destinationDir) |
| 34 | +async function copyRecursive(src, dest) { |
| 35 | + const entries = await fs.readdir(src, {withFileTypes: true}) |
| 36 | + await fs.mkdir(dest, {recursive: true}).catch((err) => { |
| 37 | + // Handle errors other than "directory already exists" |
| 38 | + if (err.code !== 'EEXIST') throw err |
| 39 | + }) |
15 | 40 |
|
16 | | - // Copy the contents of the source directory to the destination directory |
17 | | - fs.copySync(sourceDir, destinationDir, {overwrite: true}) |
| 41 | + for (const entry of entries) { |
| 42 | + const srcPath = path.join(src, entry.name) |
| 43 | + const destPath = path.join(dest, entry.name) |
18 | 44 |
|
19 | | - console.log(`Copied '${sourceDir}' to '${destinationDir}'.`) |
| 45 | + if (entry.isDirectory()) { |
| 46 | + await copyRecursive(srcPath, destPath) |
| 47 | + } else { |
| 48 | + await fs.copyFile(srcPath, destPath) |
| 49 | + } |
| 50 | + } |
20 | 51 | } |
21 | 52 |
|
22 | | -copyDirectory() |
| 53 | +copyDirectory().catch(console.error) |
0 commit comments