-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileInfo.js
129 lines (115 loc) · 2.56 KB
/
FileInfo.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
var file = require("./file"),
fs = require('fs'),
path = require("path");
var FileInfo = module.exports = function(filePath){
if(!filePath){
throw new Error("filePath not specify.");
return;
}
var that = this,
_stats;
this.fullName = filePath;
this.name = path.basename(filePath);
this.extension = path.extname(filePath);
this.directoryName = path.dirname(filePath);
this.isStatReady = false;
Object.defineProperty(that, "stats", {
get : function(){
if(that.isStatReady){
return _stats;
}else{
try{
_stats = fs.statSync(this.fullName);
that.isStatReady = true;
return _stats;
}catch(err){
if(err.code === "ENOENT"){//swallow file not found exception.
_stats = {};
that.isStatReady = true;
return _stats;
}else{
throw err;
}
}
}
}
});
fs.stat(filePath, function(err, stats){
if(!err && stats){
that.isStatReady = true;
_stats = stats;
}
});
that.createTime = that.stats.ctime;
that.lastAccessTime = that.stats.atime;
that.lastModifyTime = that.stats.mtime;
that.length = that.stats.size;
}
/**
* check if file is exists
* @return {Q.promise}
*/
FileInfo.prototype.isExists = function(){
return file.isExists(this.fullName);
}
/**
* copy file to dest path
* @param {String} destPath destination
* @return {Q.promise}
*/
FileInfo.prototype.copyTo = function(destPath){
return file.copy(this.fullName, destPath);
}
/**
* move file to dest path
* @param {String} destPath destination
* @return {Q.promise}
*/
FileInfo.prototype.moveTo = function(destPath){
return file.move(this.fullName, destPath);
}
/**
* open file as readable stream
* @return {Stream} readable stream
*/
FileInfo.prototype.openRead = function(){
return file.read(this.fullName);
}
/**
* read file text
* @return {Q.promise}
*/
FileInfo.prototype.openText = function(){
return file.readAllText(this.fullName);
}
/**
* open file as writeable stream
* @return {Stream} writeable stream
*/
FileInfo.prototype.openWrite = function(){
return file.write(this.fullName);
}
/**
* write text to the file
* this will override the original file content
* @param {String} text
* @return {Q.Promise}
*/
FileInfo.prototype.writeAllText = function(text){
return file.writeAllText(this.fullName, text);
}
/**
* append text to the end
* @param {String} text
* @return {Q.Promise}
*/
FileInfo.prototype.appendText = function(text){
return file.appendText(this.fullName, text);
}
/**
* delete file
* @return {Q.promise}
*/
FileInfo.prototype.delete = function(){
return file.delete(this.fullName);
}