Skip to content

Commit 2ca4c91

Browse files
authored
Improved copy command to replicate folder structures (#39)
### Developer Checklist (Definition of Done) **Issue** - [ ] All acceptance criteria from the issue are met - [ ] Tested in latest Chrome/Firefox **UI/UX/Vis** - [ ] Requires UI/UX/Vis review - [ ] Reviewer(s) are notified (_tag assignees_) - [ ] Review has occurred (_link to notes_) - [ ] Feedback is included in this PR - [ ] Reviewer(s) approve of concept and design **Code** - [ ] Branch is up-to-date with the branch to be merged with, i.e., develop - [ ] Code is cleaned up and formatted - [ ] Unit tests are written (frontend/backend if applicable) - [ ] Integration tests are written (if applicable) **PR** - [ ] Descriptive title for this pull request is provided (will be used for release notes later) - [ ] Reviewer and assignees are defined - [ ] Add type label (e.g., *bug*, *feature*) to this pull request - [ ] Add release label (e.g., `release: minor`) to this PR following [semver](https://semver.org/) - [ ] The PR is connected to the corresponding issue (via `Closes #...`) - [ ] [Summary of changes](#summary-of-changes) is written ### Summary of changes - Currently, the `cp` commands only copied either folders, or files matching the pattern in the root folder (`src`). This PR updates this with a node script to copy any files matching the pattern in any of the subdirectories, replicates the folder structure exactly as is in the `dist` folder. ### Screenshots ### Additional notes for the reviewer(s) How to test: run `yarn run copy` and check if files are copied, if an error occurs, report it here. - Thanks for creating this pull request 🤗
2 parents dd992b5 + 1cbcf3b commit 2ca4c91

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

bin/commands/copy.js

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,48 @@
1-
const { call } = require('./utils');
1+
const glob = require('glob');
2+
const fs = require('fs-extra');
3+
const path = require('path');
24

35
module.exports = {
46
command: 'copy',
57
describe: 'Copy assets, styles, and static files to the dist folder',
68
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+
});
1247
},
1348
};

0 commit comments

Comments
 (0)