-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
146 lines (133 loc) · 3.95 KB
/
index.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
/*
* @Author: lushijie
* @Date: 2017-03-16 09:50:44
* @Last Modified by: lushijie
* @Last Modified time: 2018-04-19 18:42:37
*/
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const helper = require('think-helper');
const Debounce = require('think-debounce');
const readFile = helper.promisify(fs.readFile, fs);
const writeFile = helper.promisify(fs.writeFile, fs);
const unlink = helper.promisify(fs.unlink, fs);
const appendFile = helper.promisify(fs.appendFile, fs);
const access = helper.promisify(fs.access, fs);
const stat = helper.promisify(fs.stat, fs);
const debounceInstance = new Debounce();
const getFilePath = Symbol('think-get-file-path');
/**
* file store
*/
class FileStore {
/**
* constructor
* @param {String} storePath store file root path
*/
constructor(storePath) {
assert(storePath && path.isAbsolute(storePath), 'storePath need be an absolute path');
this.storePath = storePath;
}
/**
* get file path
* @param {String} relativePath [description]
* @return {String} [description]
*/
[getFilePath](relativePath) {
const filePath = path.join(this.storePath, relativePath);
assert(filePath.indexOf(this.storePath) === 0, 'the file should be in storePath');
return filePath;
}
/**
* get file data
* @param {String} relativePath [relativePath]
* @param {Number} times [try times when can not get file content]
* @return {Promise} []
*/
get(relativePath) {
const filePath = this[getFilePath](relativePath);
if (!helper.isFile(filePath)) {
return Promise.resolve();
}
function getFileContent(times = 1) {
return readFile(filePath, { encoding: 'utf8' }).then(content => {
if (!content && times <= 3) {
return Promise.reject(new Error(`content empty, file path is ${filePath}`));
}
return content;
}).catch(err => {
if (times <= 3) {
return helper.timeout(10).then(() => {
return getFileContent(times + 1);
});
}
return Promise.reject(err);
});
}
return debounceInstance.debounce(filePath, () => getFileContent());
}
/**
* set file content
* @param {String} relativePath [relativePath]
* @param {String} content []
*/
set(relativePath, content) {
const filePath = this[getFilePath](relativePath);
helper.mkdir(path.dirname(filePath));
return writeFile(filePath, content).then(() => {
return helper.chmod(filePath);
});
}
/**
* delete file
* @param {String} relativePath [relativePath]
* @return {Promise} []
*/
delete(relativePath) {
const filePath = this[getFilePath](relativePath);
if (!helper.isFile(filePath)) {
return Promise.resolve();
}
// mutiple process will cause error if just unlink(filePath);
return unlink(filePath).catch(err => {
if (err.code === 'ENOENT') return;
return Promise.reject(err);
});
}
/**
* append file content
* @param {String} relativePath [relativePath]
* @param {String | Buffer} data [String or Buffer data]
* @return {Promise} []
*/
append(relativePath, data) {
const filePath = this[getFilePath](relativePath);
if (!helper.isFile(filePath)) {
return Promise.resolve();
}
return appendFile(filePath, data);
}
/**
* Does the file exist
* @param {String} relativePath [relativePath]
* @return {Promise} []
*/
has(relativePath) {
const filePath = this[getFilePath](relativePath);
return access(filePath, fs.constants.F_OK);
}
/**
* get file info
* @param {String} relativePath [relativePath]
* @return {Promise} []
*/
info(relativePath) {
const filePath = this[getFilePath](relativePath);
if (!helper.isFile(filePath)) {
return Promise.resolve();
}
return stat(filePath);
}
}
module.exports = FileStore;