forked from evanx/redexutil
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFiles.js
139 lines (134 loc) · 3.88 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) 2015, Evan Summers (twitter.com/evanxsummers)
// ISC license, see http://github.com/evanx/redexutil/LICENSE
import fs from 'fs';
import _mkdirp from 'mkdirp';
import Loggers from './Loggers';
import Promises from './Promises';
const logger = Loggers.create(__filename, 'info');
const Files = {
mkdirp(dir) {
return Promises.create(cb => _mkdirp(dir, cb));
},
remove(file) {
return Promises.create(cb => fs.unlink(file, cb));
},
deleteFile(dir, file) {
return new Promise(function (resolve, reject) {
var filePath = path.join(dir, file);
fs.lstat(filePath, function (err, stats) {
if (err) {
return reject(err);
}
if (stats.isDirectory()) {
resolve(Files.deleteDirectory(filePath));
} else {
fs.unlink(filePath, function (err) {
if (err) {
return reject(err);
}
resolve();
});
}
});
});
},
deleteDirectory(dir) {
return new Promise(function (resolve, reject) {
fs.access(dir, function (err) {
if (err) {
return reject(err);
}
fs.readdir(dir, function (err, files) {
if (err) {
return reject(err);
}
Promise.all(files.map(function (file) {
return Files.deleteFile(dir, file);
})).then(function () {
fs.rmdir(dir, function (err) {
if (err) {
return reject(err);
}
resolve();
});
}).catch(reject);
});
});
});
},
basename(file) {
var matcher = file.match(/([^\/]+)\.[a-z]+$/);
if (matcher) {
return matcher[1];
} else {
return file;
}
},
stat(file) {
logger.debug('stat', file);
return Promises.create(cb => fs.stat(file, cb));
},
existsFile(file) {
return Files.stat(file).then(stats => stats.isFile()).catch(err => {
logger.debug('existsFile', err.message);
return false;
});
},
existsFileSync(file) {
try {
return fs.statSync(file).isFile();
} catch (err) {
logger.debug('existsFileSync', err.message);
return false;
}
},
watchChanged(dir, timeout) {
return new Promise((resolve, reject) => {
let watcher = fs.watch(dir, (eventType, file) => {
watcher.close();
logger.debug('watch', dir, eventType, file);
if (eventType === 'change') {
resolve(file);
} else {
reject(eventType);
}
});
setTimeout(() => {
watcher.close();
logger.debug('watch timeout', dir, timeout);
reject('timeout: ' + timeout);
}, timeout);
});
},
readFile(file) {
logger.debug('readFile', file);
return new Promise((resolve, reject) => {
fs.readFile(file, (err, content) => {
logger.debug('readFile', file, {err});
if (err) {
reject(err);
} else {
logger.debug('readFile resolve:', file, content.length);
resolve(content);
}
});
});
},
readFileString(file) {
return Files.readFile(file).then(content => content.toString());
},
writeFile(file, content) {
logger.debug('writeFile', file);
return new Promise((resolve, reject) => {
fs.writeFile(file, content, err => {
logger.debug('writeFile', file, {err});
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
};
module.exports = Files;