-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.js
125 lines (112 loc) · 3.38 KB
/
find.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
var fs = require('fs')
var path = require('path')
var async = require('async')
var _ = require('underscore')
// ===================================================================
// Global variables
// ===================================================================
var rawFiles, organizedFiles = {}
// ===================================================================
// Functionality
// ===================================================================
var diveMusic = function(URL, filter) {
var location = path.resolve(__dirname, URL)
var regex_filter = RegExp(filter.toLowerCase(), 'gi')
async.series([
// Check if the folder exists
function checkIfExists (cb) {
fs.exists(location, function (exists) {
return (exists ? cb() : cb('No existe el directorio ' + location))
})
},
// Analyze folder content
function printFolderContent (cb) {
fs.readdir(location, function (error, results) {
if (error) {
return cb(error)
}
if (!results || !results.length) {
return cb('No se han encontrado archivos en ' + location)
}
rawFiles = results
cb()
})
},
// Filter results by name
function filterByName (cb) {
rawFiles = rawFiles.filter(function (rawFile) {
var lower_filter = rawFile.toLowerCase()
// console.log(regex_filter + '.test(\'' + lower_filter + '\') : ' + regex_filter.test(lower_filter))
return regex_filter.test(lower_filter)
})
cb()
},
// Organize files
function organizeFilesByExtension (cb) {
for(var i in rawFiles) {
var extension = path.extname(rawFiles[i]).toLowerCase()
var filename = path.basename(rawFiles[i], extension)
if(!extension) {
extension = 'folders'
}
if(!organizedFiles[extension]) {
organizedFiles[extension] = []
}
organizedFiles[extension].push(filename)
}
cb()
},
// Order results by name
function orderResultsByName (cb) {
for(var i in organizedFiles) {
organizedFiles[i].sort()
}
cb()
},
// Print results in console
function printResults (cb) {
console.log('')
if(_.isEmpty(organizedFiles)) {
return cb('No se han encontrado resultados en ' + location)
}
for(i in organizedFiles) {
console.log('\t%s:', i.toUpperCase())
console.log('')
for(j in organizedFiles[i]) {
console.log('\t► %s', organizedFiles[i][j])
}
console.log('')
}
cb()
}
], function (error, results) {
if (error) {
return console.log(error)
}
console.log('')
})
}
// ===================================================================
// Usage
// ===================================================================
var usage = function () {
console.log('Script para buscar, analizar y ordenar archivos de mi carpeta personal.')
console.log('')
console.log('PARÁMETROS (entre comillas dobles):')
console.log('\t1\t\tPath relativo al directorio a analizar.')
console.log('\t2 (opcional)\tClave de búsqueda')
console.log('')
console.log('EJEMPLOS:')
console.log('node find "../../Music/Electrónica/Electro House" "leon bolier"')
console.log('node find ./ find')
}
// ===================================================================
// Main program
// ===================================================================
var main = function (args) {
if(args.length < 3 || args[2].toLowerCase() == 'usage') {
return usage()
}
return diveMusic(args[2], (args[3] || ''))
}
main(process.argv)