-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrganize.js
More file actions
55 lines (43 loc) · 1.87 KB
/
Copy pathOrganize.js
File metadata and controls
55 lines (43 loc) · 1.87 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
48
49
50
51
52
// Import required modules
import fs from 'fs';
import fsPro from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
/*
Why do we need this setup?
- In CommonJS (require), __dirname is available by default.
- In ES Modules (import), __dirname is NOT available, so we must construct it manually.
- We use import.meta.url to get the file URL of the current module, then convert it to a file path.
*/
// Get the directory name of the current module (equivalent to __dirname in CommonJS)
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Helper function to check if a file should be ignored
function isIgnoredFile(file) {
return file === 'Organize.js' || file === 'package-lock.json' || file === 'package.json' || file === 'README.md' || file === 'node_modules' || file === '.git' || file.startsWith('.');
}
// Async function to organize files by their extension
async function getFiles() {
let files = await fsPro.readdir(__dirname);
for (let file of files) {
// Skip ignored files
if (isIgnoredFile(file)) continue;
// Get the file extension (without the dot)
const ext = path.extname(file).slice(1);
if (ext === '') continue; // Skip files without an extension
// Build the absolute path for the extension directory
const dirPath = path.join(__dirname, ext);
// Create the directory if it doesn't exist
if (!fs.existsSync(dirPath)) {
console.log(`Directory '${ext}' does not exist. Creating it...`);
fs.mkdirSync(dirPath);
console.log(`Directory '${ext}' created.`);
}
// Move the file into its extension directory
const oldPath = path.join(__dirname, file);
const newPath = path.join(dirPath, file);
fs.renameSync(oldPath, newPath);
console.log(`Moved '${file}' to directory '${ext}'`);
}
}
// Run the function and handle any errors
getFiles().catch(console.error);