forked from yydfys/CatPawOpen
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcopyDist.js
61 lines (52 loc) · 1.79 KB
/
copyDist.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Import required modules
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
// Check if the destination path is provided
if (process.argv.length < 3) {
console.error('Usage: node script.js <destination_path>');
process.exit(1);
}
// Get the destination path from command-line arguments
const destinationPath = process.argv[2];
// Resolve paths
const distPath = path.resolve(process.cwd(), 'dist');
const targetPath = path.resolve(destinationPath);
// Check if dist directory exists
if (!fs.existsSync(distPath)) {
console.error(`Source directory does not exist: ${distPath}`);
process.exit(1);
}
// Check if target directory exists
if (!fs.existsSync(targetPath)) {
console.error(`Target directory does not exist: ${targetPath}`);
process.exit(1);
}
// Copy files from dist to target
const copyFiles = (srcDir, destDir) => {
const entries = fs.readdirSync(srcDir, { withFileTypes: true });
entries.forEach((entry) => {
const srcPath = path.join(srcDir, entry.name);
const destPath = path.join(destDir, entry.name);
if (entry.isDirectory()) {
// Create directory if it doesn't exist
if (!fs.existsSync(destPath)) {
fs.mkdirSync(destPath);
}
// Recursively copy files
copyFiles(srcPath, destPath);
} else {
// Copy file and overwrite if exists
fs.copyFileSync(srcPath, destPath);
console.log(`Copied: ${srcPath} -> ${destPath}`);
}
});
};
try {
console.log(`Copying files from ${distPath} to ${targetPath}...`);
copyFiles(distPath, targetPath);
console.log('All files copied successfully.');
} catch (error) {
console.error('Error during file copy:', error);
process.exit(1);
}