-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.js
153 lines (115 loc) · 3.21 KB
/
file.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
const fs = require('fs');
const path = require('path');
/**
* Functional programing magic
*/
const resolveFunction = (res, rej) => (err, data) => err ? rej(err) : res(data);
const newPromise = fn => new Promise((resolve, reject) => {
fn(resolveFunction(resolve, reject));
});
/**
* Main File init
*/
function File(filepath) {
if (typeof filepath === 'string')
throw new Error(`A file path is required, got ${filepath}`);
this.exists = fs.existsSync(filepath);
this.filepath = filepath;
this.filename = path.basename(filepath);
this.filedir = path.dirname(filepath);
this.fileex = path.extname(filepath);
this.readstream = null;
this.writestream = null;
}
// Stats
File.prototype.exists = function () {
return fs.existsSync(this.filepath);
};
File.prototype.checkIfExists = function () {
if (!this.exists)
throw new Error(this.filepath + ": File doesn't exist.");
};
File.prototype.getStatSync = function () {
this.checkIfExists();
return fs.statSync(this.filepath);
};
File.prototype.getStats = function () {
this.checkIfExists();
return newPromise(resolver => {
fs.stats(this.filepath, resolver);
});
};
File.prototype.rename = function (newname) {
this.checkIfExists();
const {
filepath,
filedir,
} = this;
const newpath = path.join(filedir, newname);
return newPromise(resolver => {
fs.rename(filepath, newpath, resolver);
});
};
File.prototype.delete = function () {
this.checkIfExists();
return newPromise(resolver => {
fs.unlink(this.filepath, resolver);
});
};
File.prototype.read = function (encoding = 'utf8') {
this.checkIfExists();
return newPromise(resolver => {
fs.readFile(this.filepath, encoding, resolver);
});
};
File.prototype.readSync = function (encoding = 'utf8') {
this.checkIfExists();
return fs.readFileSync(this.filepath, encoding);
};
File.prototype.getLinesSync = function () {
this.checkIfExists();
const data = this.readSync();
return data.split('\n');
};
File.prototype.getLines = function () {
return newPromise(resolver => {
try {
const data = this.getLinesSync();
resolver(null, data);
} catch (err) {
resolver(err, null);
}
});
};
File.prototype.getReadStream = function () {
this.checkIfExists();
if (!this.readstream)
this.readstream = fs.createReadStream();
return this.readstream;
};
File.prototype.write = function (data) {
return newPromise(resolver => {
fs.writeFile(this.filepath, data, (err) => {
this.exists = true;
resolver(err, data);
});
});
};
File.prototype.getWriteStream = function () {
return fs.createWriteStream(this.filepath);
};
File.prototype.append = function (data) {
return newPromise(resolver => {
fs.appendFile(this.filepath, data, resolver);
});
};
File.prototype.appendNewLine = function (data) {
return this.append('\n' + data);
};
File.prototype.duplicate = function (name) {
this.checkIfExists();
return newPromise(resolver => {
fs.copyFile(this.filepath, name, resolver);
});
};
module.exports = File;