fs.js
4.04 KB
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
'use strict';
const fs = require('graceful-fs');
const makeDir = require('make-dir');
const pify = require('pify');
const CpFileError = require('./cp-file-error');
const fsP = pify(fs);
exports.closeSync = fs.closeSync.bind(fs);
exports.createWriteStream = fs.createWriteStream.bind(fs);
exports.createReadStream = (path, options) => new Promise((resolve, reject) => {
const read = fs.createReadStream(path, options);
read.once('error', error => {
reject(new CpFileError(`Cannot read from \`${path}\`: ${error.message}`, error));
});
read.once('readable', () => {
resolve(read);
});
read.once('end', () => {
resolve(read);
});
});
exports.stat = path => fsP.stat(path).catch(error => {
throw new CpFileError(`Cannot stat path \`${path}\`: ${error.message}`, error);
});
exports.lstat = path => fsP.lstat(path).catch(error => {
throw new CpFileError(`lstat \`${path}\` failed: ${error.message}`, error);
});
exports.utimes = (path, atime, mtime) => fsP.utimes(path, atime, mtime).catch(error => {
throw new CpFileError(`utimes \`${path}\` failed: ${error.message}`, error);
});
exports.chmod = (path, mode) => fsP.chmod(path, mode).catch(error => {
throw new CpFileError(`chmod \`${path}\` failed: ${error.message}`, error);
});
exports.chown = (path, uid, gid) => fsP.chown(path, uid, gid).catch(error => {
throw new CpFileError(`chown \`${path}\` failed: ${error.message}`, error);
});
exports.openSync = (path, flags, mode) => {
try {
return fs.openSync(path, flags, mode);
} catch (error) {
if (flags.includes('w')) {
throw new CpFileError(`Cannot write to \`${path}\`: ${error.message}`, error);
}
throw new CpFileError(`Cannot open \`${path}\`: ${error.message}`, error);
}
};
// eslint-disable-next-line max-params
exports.readSync = (fileDescriptor, buffer, offset, length, position, path) => {
try {
return fs.readSync(fileDescriptor, buffer, offset, length, position);
} catch (error) {
throw new CpFileError(`Cannot read from \`${path}\`: ${error.message}`, error);
}
};
// eslint-disable-next-line max-params
exports.writeSync = (fileDescriptor, buffer, offset, length, position, path) => {
try {
return fs.writeSync(fileDescriptor, buffer, offset, length, position);
} catch (error) {
throw new CpFileError(`Cannot write to \`${path}\`: ${error.message}`, error);
}
};
exports.statSync = path => {
try {
return fs.statSync(path);
} catch (error) {
throw new CpFileError(`stat \`${path}\` failed: ${error.message}`, error);
}
};
exports.fstatSync = (fileDescriptor, path) => {
try {
return fs.fstatSync(fileDescriptor);
} catch (error) {
throw new CpFileError(`fstat \`${path}\` failed: ${error.message}`, error);
}
};
exports.futimesSync = (fileDescriptor, atime, mtime, path) => {
try {
return fs.futimesSync(fileDescriptor, atime, mtime, path);
} catch (error) {
throw new CpFileError(`futimes \`${path}\` failed: ${error.message}`, error);
}
};
exports.utimesSync = (path, atime, mtime) => {
try {
return fs.utimesSync(path, atime, mtime);
} catch (error) {
throw new CpFileError(`utimes \`${path}\` failed: ${error.message}`, error);
}
};
exports.chmodSync = (path, mode) => {
try {
return fs.chmodSync(path, mode);
} catch (error) {
throw new CpFileError(`chmod \`${path}\` failed: ${error.message}`, error);
}
};
exports.chownSync = (path, uid, gid) => {
try {
return fs.chownSync(path, uid, gid);
} catch (error) {
throw new CpFileError(`chown \`${path}\` failed: ${error.message}`, error);
}
};
exports.makeDir = path => makeDir(path, {fs}).catch(error => {
throw new CpFileError(`Cannot create directory \`${path}\`: ${error.message}`, error);
});
exports.makeDirSync = path => {
try {
makeDir.sync(path, {fs});
} catch (error) {
throw new CpFileError(`Cannot create directory \`${path}\`: ${error.message}`, error);
}
};
if (fs.copyFileSync) {
exports.copyFileSync = (source, destination, flags) => {
try {
fs.copyFileSync(source, destination, flags);
} catch (error) {
throw new CpFileError(`Cannot copy from \`${source}\` to \`${destination}\`: ${error.message}`, error);
}
};
}