-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcopy-assets.js
More file actions
47 lines (41 loc) · 1.86 KB
/
copy-assets.js
File metadata and controls
47 lines (41 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const fs = require('fs').promises;
const path = require('path');
const staticSrcPath = path.join(__dirname, '.next/static');
const staticDestPath = path.join(__dirname, '.next/standalone/.next/static');
const publicSrcPath = path.join(__dirname, 'public');
const publicDestPath = path.join(__dirname, '.next/standalone/public');
function copyAssets(src, dest, destRoot = dest) {
return fs.mkdir(dest, { recursive: true })
.then(() => fs.readdir(src, { withFileTypes: true }))
.then(items => {
const promises = items.map(item => {
const srcPath = path.join(src, item.name);
const destPath = path.join(dest, item.name);
// Resolve the real absolute paths to prevent traversal
const resolvedDest = path.resolve(destPath);
const resolvedRoot = path.resolve(destRoot);
if (!resolvedDest.startsWith(resolvedRoot + path.sep) &&
resolvedDest !== resolvedRoot) {
throw new Error(
`Path traversal detected: "${item.name}" resolves outside destination root`
);
}
if (item.isDirectory()) {
return copyAssets(srcPath, destPath, destRoot);
} else {
return fs.copyFile(srcPath, destPath);
}
});
return Promise.all(promises);
})
.catch(err => {
console.error(`Error: ${err}`);
throw err;
});
}
const greenTick = `\x1b[32m\u2713\x1b[0m`;
const redCross = `\x1b[31m\u274C\x1b[0m`;
copyAssets(staticSrcPath, staticDestPath)
.then(() => copyAssets(publicSrcPath, publicDestPath))
.then(() => console.log(`${greenTick} Assets copied successfully`))
.catch(err => console.error(`${redCross} Failed to copy assets: ${err}`));