-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniquifier.js
105 lines (94 loc) · 2.94 KB
/
uniquifier.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* This script can rename files uniquely per folder (Recursively)
*/
const path = require('path');
const crypto = require('crypto');
const {
fileExist,
isFile,
isDirectory,
getFiles,
writeFile,
getFolders,
renameFile,
} = require('./utils/FileManager');
const moment = require('moment/moment');
const LOG_PATH = path.resolve(
__dirname,
`storage/log/${moment().format('YYYY-MM-DD_HH-mm-ss')}~${getRandomHex()}.json`
);
const workingDir = process.cwd();
const renamed = {}
let countLabels = {};
console.count = label => {
countLabels[label] = countLabels[label] ?? 0;
countLabels[label]++;
process.stdout.write(`${label}: ${countLabels[label]}\r`);
};
main();
async function main() {
if (!process.argv[2]) {
console.error('You have to provide a location');
return;
}
const workingPath = path.resolve(workingDir, process.argv[2]);
if (!fileExist(workingPath)) {
console.error('You have to provide a valid location');
return;
}
if (isFile(workingPath)) {
prefixFile(workingPath);
} else if (isDirectory(workingPath)) {
console.log('Processing:', workingPath);
const folders = getFolders(workingPath);
prefixFilesByFolder(workingPath);
for (const folder of folders) {
prefixFilesByFolder(folder, { logLabel: path.relative(workingPath, folder) });
}
console.log(
`Total: ${Object.values(countLabels).reduce((acc, cur) => acc + cur, 0)}`
);
console.log(`Log File: ${LOG_PATH}`);
} else console.error(`Provide either a file path or a directory path`);
}
function prefixFilesByFolder(folder, { logLabel } = {}) {
const unique = getUniqueText(path.basename(folder));
logLabel = logLabel ?? path.basename(folder)
const files = getFiles(folder, '*.*', false)
for (const file of files) {
prefixFile(file, unique);
console.count(`Processed From '${logLabel}'`);
}
if (files.length > 0) console.log();
}
function prefixFile(file, prefix = null) {
prefix = prefix ?? getUniqueText(path.basename(path.dirname(file)));
const renamePath = path.resolve(path.dirname(file), prefix+path.basename(file))
if (renameFile(file, renamePath)) {
renamed[file] = renamePath
writeFile(LOG_PATH, JSON.stringify(renamed))
} else console.error(`Error Renaming ${file} to ${renamePath}`)
}
function getUniqueText(cryptoSuffix = null) {
const randomCharacters = getRandomCharacters(5);
let suffix = '';
if (cryptoSuffix)
suffix = `-${crypto
.createHash('md5')
.update(cryptoSuffix)
.digest('hex')
.slice(0, 6)}`;
return `[${randomCharacters}${suffix}~]`;
}
function getRandomHex() {
return Number(
Date.now() * Math.round(Math.random() * 10000 + (Math.random() * 100 + 1))
).toString(16);
}
function getRandomCharacters(length) {
let chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let build = '';
for (let i = 0; i < length; i++)
build += chars[Math.floor(Math.random() * chars.length)];
return build;
}