This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.js
221 lines (200 loc) · 5.81 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* Module for working with file. Shall implement
* IEmmetFile interface.
*
* Since implementation of this module depends
* greatly on current runtime, this module must be
* initialized with actual implementation first
* before use. E.g.
* require('./plugin/file')({
* read: function() {...}
* })
*
* By default, this module provides Node.JS implementation
*/
if (typeof module === 'object' && typeof define !== 'function') {
var define = function (factory) {
module.exports = factory(require, exports, module);
};
}
define(function(require, exports, module) {
var utils = require('../utils/common');
var _transport = {};
// hide it from Require.JS parser
(function(r) {
if (typeof define === 'undefined' || !define.amd) {
try {
fs = r('fs');
path = r('path');
_transport.http = r('http');
_transport.https = r('https');
} catch(e) { }
}
})(require);
// module is a function that can extend itself
module.exports = function(obj) {
if (obj) {
utils.extend(module.exports, obj);
}
};
function bts(bytes) {
var out = [];
for (var i = 0, il = bytes.length; i < il; i++) {
out.push(String.fromCharCode(bytes[i]));
}
return out.join('');
}
function isURL(path) {
var re = /^https?:\/\//;
return re.test(path);
}
return utils.extend(module.exports, {
_parseParams: function(args) {
var params = {
path: args[0],
size: 0
};
args = utils.toArray(args, 1);
params.callback = args[args.length - 1];
args = args.slice(0, args.length - 1);
if (args.length) {
params.size = args[0];
}
return params;
},
_read: function(params, callback) {
if (isURL(params.path)) {
var req = _transport[/^https:/.test(params.path) ? 'https' : 'http'].get(params.path, function(res) {
var bufs = [];
var totalLength = 0;
var finished = false;
res
.on('data', function(chunk) {
totalLength += chunk.length;
bufs.push(chunk);
if (params.size && totalLength >= params.size) {
finished = true;
callback(null, Buffer.concat(bufs));
req.abort();
}
})
.on('end', function() {
if (!finished) {
finished = true;
callback(null, Buffer.concat(bufs));
}
});
}).on('error', callback);
} else {
if (params.size) {
var fd = fs.openSync(params.path, 'r');
var buf = new Buffer(params.size);
fs.read(fd, buf, 0, params.size, null, function(err, bytesRead) {
callback(err, buf)
});
} else {
callback(null, fs.readFileSync(params.path));
}
}
},
/**
* Reads binary file content and return it
* @param {String} path File's relative or absolute path
* @return {String}
*/
read: function(path, size, callback) {
var params = this._parseParams(arguments);
this._read(params, function(err, buf) {
params.callback(err, err ? '' : bts(buf));
});
},
/**
* Read file content and return it
* @param {String} path File's relative or absolute path
* @return {String}
*/
readText: function(path, size, callback) {
var params = this._parseParams(arguments);
this._read(params, function(err, buf) {
params.callback(err, err ? '' : buf.toString());
});
},
/**
* Locate <code>file_name</code> file that relates to <code>editor_file</code>.
* File name may be absolute or relative path
*
* <b>Dealing with absolute path.</b>
* Many modern editors have a "project" support as information unit, but you
* should not rely on project path to find file with absolute path. First,
* it requires user to create a project before using this method (and this
* is not very convenient). Second, project path doesn't always points to
* to website's document root folder: it may point, for example, to an
* upper folder which contains server-side scripts.
*
* For better result, you should use the following algorithm in locating
* absolute resources:
* 1) Get parent folder for <code>editorFile</code> as a start point
* 2) Append required <code>fileName</code> to start point and test if
* file exists
* 3) If it doesn't exists, move start point one level up (to parent folder)
* and repeat step 2.
*
* @param {String} editorFile
* @param {String} fileName
* @return {String} Returns null if <code>fileName</code> cannot be located
*/
locateFile: function(editorFile, fileName, callback) {
if (isURL(fileName)) {
return callback(fileName);
}
var dirname = editorFile
var filepath;
fileName = fileName.replace(/^\/+/, '');
while (dirname && dirname !== path.dirname(dirname)) {
dirname = path.dirname(dirname);
filepath = path.join(dirname, fileName);
if (fs.existsSync(filepath))
return callback(filepath);
}
callback(null);
},
/**
* Creates absolute path by concatenating <code>parent</code> and <code>fileName</code>.
* If <code>parent</code> points to file, its parent directory is used
* @param {String} parent
* @param {String} fileName
* @return {String}
*/
createPath: function(parent, fileName, callback) {
fs.stat(parent, function(err, stat) {
if (err) {
return callback(err);
}
if (stat.isFile()) {
parent = path.dirname(parent);
}
var filepath = path.resolve(parent, fileName);
callback(null, filepath);
});
},
/**
* Saves <code>content</code> as <code>file</code>
* @param {String} file File's absolute path
* @param {String} content File content
*/
save: function(file, content, callback) {
fs.writeFile(file, content, 'ascii', function(err) {
callback(err ? err : null);
});
},
/**
* Returns file extension in lower case
* @param {String} file
* @return {String}
*/
getExt: function(file) {
var m = (file || '').match(/\.([\w\-]+)$/);
return m ? m[1].toLowerCase() : '';
}
});
});