|
1 |
| -const { call } = require('./utils'); |
| 1 | +const glob = require('glob'); |
| 2 | +const fs = require('fs-extra'); |
| 3 | +const path = require('path'); |
2 | 4 |
|
3 | 5 | module.exports = {
|
4 | 6 | command: 'copy',
|
5 | 7 | describe: 'Copy assets, styles, and static files to the dist folder',
|
6 | 8 | handler: () => {
|
7 |
| - call('shx', "--verbose cp -r src/assets/. dist/assets/ || echo 'no assets copied'"); |
8 |
| - call('shx', "--verbose cp -R src/template/. dist/template/ || echo 'no template copied'"); |
9 |
| - call('shx', "--verbose cp -R src/templates/. dist/templates/ || echo 'no templates copied'"); |
10 |
| - call('shx', "--verbose cp -R src/scss/. dist/scss/ || echo 'no scss files copied'"); |
11 |
| - call('shx', '--verbose cp "src/*.{txt,html,ejs,json}" dist/ || echo \'no file copied\''); |
| 9 | + const srcDir = 'src'; |
| 10 | + const distDir = 'dist'; |
| 11 | + // Files to copy |
| 12 | + const filePattern = '**/*.@(txt|html|ejs|json|md|scss|css|js|png|jpg|jpeg|gif|svg|ico|webmanifest|xml)'; |
| 13 | + |
| 14 | + // Additional folders to copy |
| 15 | + const additionalFolders = ['assets', 'template', 'templates', 'scss']; |
| 16 | + |
| 17 | + // Copy specified folders from source to destination |
| 18 | + additionalFolders.forEach((folder) => { |
| 19 | + const srcFolderPath = path.join(srcDir, folder); |
| 20 | + |
| 21 | + if (fs.existsSync(srcFolderPath)) { |
| 22 | + const distFolderPath = path.join(distDir, folder); |
| 23 | + |
| 24 | + // Ensure the destination directory exists |
| 25 | + fs.ensureDirSync(distFolderPath); |
| 26 | + |
| 27 | + // Copy the folder from source to destination |
| 28 | + fs.copySync(srcFolderPath, distFolderPath, { overwrite: true }); |
| 29 | + console.log(`Copied ${srcFolderPath} to ${distFolderPath}`); |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + // Get a list of files matching the pattern |
| 34 | + const files = glob.sync(filePattern, { cwd: srcDir }); |
| 35 | + files.forEach((file) => { |
| 36 | + const srcPath = path.join(srcDir, file); |
| 37 | + const distPath = path.join(distDir, file); |
| 38 | + |
| 39 | + // Ensure the destination directory exists |
| 40 | + const distDirPath = path.dirname(distPath); |
| 41 | + fs.ensureDirSync(distDirPath); |
| 42 | + |
| 43 | + // Copy the file from source to destination |
| 44 | + fs.copyFileSync(srcPath, distPath); |
| 45 | + console.log(`Copied ${srcPath} to ${distPath}`); |
| 46 | + }); |
12 | 47 | },
|
13 | 48 | };
|
0 commit comments