-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
166 lines (112 loc) · 4.1 KB
/
index.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const arg = require("yargs").argv
const fs = require('fs')
const path = require('path')
const mv = require('mv')
const cliProgress = require("cli-progress")
const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic)
const prompt = require('prompt')
cli()
function sleep(sec) {
return new Promise( res => {
progressBar.start(sec, 0)
let progressValue = 0
let incrementer = setInterval(() => {
progressBar.update( ++progressValue )
}, 1000)
setTimeout( () => {
clearInterval(incrementer)
progressBar.stop()
res()
}, sec * 1000 );
})
}
function mkdir( dir ) {
if ( fs.existsSync( dir ) === false ) {
fs.mkdirSync( dir )
console.log('Folder created:', dir)
}
}
async function moveEachFileInOwnFolder(dir, obj){
let folderAcronym = nameToAcronym(obj.name)
await renameFiles(dir, folderAcronym)
}
function nameToAcronym(name) {
let acronym = ''
let splittedName = name.split(' ')
if (splittedName.length > 1) {
for ( let w in splittedName ) {
acronym += splittedName[w][0]
}
} else {
acronym = name.substring(0,3)
}
return acronym
}
async function renameFiles(dir, acronym) {
console.log('Renaming files with Folder acronym so it is easier to find . . . \n', dir)
try {
let files = fs.readdirSync(dir)
console.log('Files are:', JSON.stringify(files, null, 4))
for (let f in files) {
if (files[f].endsWith('.mp3') && files[f].startsWith(`${acronym}`) === false) {
let filePath = path.join(dir, files[f])
let acronimedName = acronym + ' '
acronimedName += files[f].replace(/[^\w\s\][^,]/gi, "")
acronimedName = acronimedName.replace('mp3', '')
acronimedName += '.mp3'
let folder = files[f].replace(/[^\w\s\][^,]/gi, "")
folder = folder.replace('mp3', '')
let newFilePath = path.join(dir, acronimedName)
try {
fs.renameSync(filePath, newFilePath)
console.log('Rename:', newFilePath)
mkdir(path.join(dir, folder))
mv(
newFilePath,
path.join(dir, folder, acronimedName),
e => {
if (e) {
console.error('Moved: Error happened while moving after renaming\n', e)
} else console.log("Moved:", files[f]);
}
)
} catch (e) {
console.error('Rename: DID NOT WORK =>', newFilePath, '\nError is:\n', e)
}
} else console.log(`Rename: Folder or Already renamed => ${files[f]}`)
}
} catch (e) {
console.log('Error happened while trying to read files for renaming', e)
}
}
function cli() {
prompt.start()
let questions = [
`Enter directory path (Everything inside will be foldrized)`
]
prompt.get([questions[0]], function(err, result) {
if (err) {
return onErr(err)
}
let dir = result[ questions[0] ]
if ( dir !== '' ){
console.log( 'Dir is:\n', dir)
let files = fs.readdirSync(dir)
console.log( 'Files listed are:\n\n', JSON.stringify(files, null, 4) )
run({ dir })
} else console.log('Wrong input. Exiting . . . ')
})
}
function onErr(err) {
console.log(err)
return 1
}
function run(params) {
let q = `Is this is the correct directory otherwise [Y/N]`
prompt.get([q], (e, r) => {
if ( r[q] === 'Y' || r[q] === 'y' ) {
let folder = params.dir.split('\\')[ params.dir.split('\\').length - 1 ]
moveEachFileInOwnFolder( params.dir, { name: folder } )
} else console.log('Exiting .... Wrong directory')
})
}