/
files.js
117 lines (108 loc) · 3.28 KB
/
files.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
const { debug } = require('console');
var fs = require('fs');
var path = require('path');
var rimraf = require('rimraf');
const moveFile = require('move-file');
var list_folder = function(folder_path, recursive) {
folder_path = folder_path.replace("/./", "/");
var folder_content = [];
try {
files = fs.readdirSync(folder_path);
} catch (error) {
if(error.code != "ENOENT") console.log(error);
else console.log("> No content found in " + folder_path);
return false;
}
files.forEach(function(file) {
if(file != ".DS_Store" && file[0] != "."){
var location = folder_path.replace(process.cwd(), "");
var current_path = folder_path + file;
var stat = fs.lstatSync(current_path);
var data = {
name : file,
path : location.replace(".", "") + file + (stat.isDirectory() ? "/" : ""),
folder : location,
ext : path.extname(file),
isFile : !stat.isDirectory(),
size : stat.size,
mtime : stat.mtime,
ctime : stat.ctime,
folder_contents : stat.isDirectory() && recursive ? list_folder(folder_path + file + "/", recursive) : undefined
}
folder_content.push(data);
}
});
return folder_content;
}
var path_exists = function(path){
try {
if (fs.existsSync(path)) return true;
else return false;
} catch (error) {
//if(error.code != "ENOENT") console.log(error);
//else console.log("> No content found in " + folder_path);
return false;
}
}
var getStat = function(path){
try {
return fs.lstatSync(path);
} catch (error) {
if(error.code != "ENOENT") console.log(error);
else console.log("> No content found in " + path);
return undefined;
}
}
var isFile = function(path){
var stat = getStat(path);
if(stat != undefined){
if (!stat.isDirectory()) return true;
else return false;
}
else return false;
}
var delete_path = function(path){
try {
if(fs.lstatSync(path).isDirectory()){
rimraf.sync(path);
} else {
fs.unlinkSync(path)
}
return true;
} catch (error) {
console.log(error)
return false;
}
}
var create_path = function(path){
try {
if (!fs.existsSync(path)){
fs.mkdirSync(path, { recursive: true });
return true;
} else return false
} catch (error) {
console.log(error);
return false;
}
}
var move_path = function(path, new_path){
return moveFile(path, new_path);
}
var array_move = function(arr, old_index, new_index) {
if (new_index >= arr.length) {
var k = new_index - arr.length + 1;
while (k--) {
arr.push(undefined);
}
}
arr.splice(new_index, 0, arr.splice(old_index, 1)[0]);
return arr; // for testing
};
module.exports.path_exists = path_exists;
module.exports.getStat = getStat;
module.exports.isFile = isFile;
module.exports.list_folder = list_folder;
module.exports.delete_path = delete_path;
module.exports.create_path = create_path;
module.exports.move_path = move_path;
module.exports.array_move = array_move;