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 | "use strict";
|
30 | const FS = require("fs");
|
31 | const Path = require("path");
|
32 | var charset = "utf-8";
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | function save(path, data) {
|
39 | if (exists(path)) {
|
40 | remove(path);
|
41 | }
|
42 | path = escapePath(path);
|
43 | textTemp[path] = data;
|
44 | createDirectory(Path.dirname(path));
|
45 | FS.writeFileSync(path, data, charset);
|
46 | }
|
47 | exports.save = save;
|
48 |
|
49 |
|
50 |
|
51 | function createDirectory(path, mode, made) {
|
52 | path = escapePath(path);
|
53 | if (mode === undefined) {
|
54 | mode = 511 & (~process.umask());
|
55 | }
|
56 | if (!made)
|
57 | made = null;
|
58 | if (typeof mode === 'string')
|
59 | mode = parseInt(mode, 8);
|
60 | path = Path.resolve(path);
|
61 | try {
|
62 | FS.mkdirSync(path, mode);
|
63 | made = made || path;
|
64 | }
|
65 | catch (err0) {
|
66 | switch (err0.code) {
|
67 | case 'ENOENT':
|
68 | made = createDirectory(Path.dirname(path), mode, made);
|
69 | createDirectory(path, mode, made);
|
70 | break;
|
71 | default:
|
72 | var stat;
|
73 | try {
|
74 | stat = FS.statSync(path);
|
75 | }
|
76 | catch (err1) {
|
77 | throw err0;
|
78 | }
|
79 | if (!stat.isDirectory())
|
80 | throw err0;
|
81 | break;
|
82 | }
|
83 | }
|
84 | return made;
|
85 | }
|
86 | exports.createDirectory = createDirectory;
|
87 | var textTemp = {};
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | function read(path, ignoreCache) {
|
93 | path = escapePath(path);
|
94 | var text = textTemp[path];
|
95 | if (text && !ignoreCache) {
|
96 | return text;
|
97 | }
|
98 | try {
|
99 | text = FS.readFileSync(path, charset);
|
100 | text = text.replace(/^\uFEFF/, '');
|
101 | }
|
102 | catch (err0) {
|
103 | return "";
|
104 | }
|
105 | if (text) {
|
106 | var ext = getExtension(path).toLowerCase();
|
107 | if (ext == "ts" || ext == "exml") {
|
108 | textTemp[path] = text;
|
109 | }
|
110 | }
|
111 | return text;
|
112 | }
|
113 | exports.read = read;
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | function readBinary(path) {
|
119 | path = escapePath(path);
|
120 | try {
|
121 | var binary = FS.readFileSync(path);
|
122 | }
|
123 | catch (e) {
|
124 | return null;
|
125 | }
|
126 | return binary;
|
127 | }
|
128 | exports.readBinary = readBinary;
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | function copy(source, dest) {
|
135 | source = escapePath(source);
|
136 | dest = escapePath(dest);
|
137 | var stat = FS.lstatSync(source);
|
138 | if (stat.isDirectory()) {
|
139 | _copy_dir(source, dest);
|
140 | }
|
141 | else {
|
142 | _copy_file(source, dest);
|
143 | }
|
144 | }
|
145 | exports.copy = copy;
|
146 | function isDirectory(path) {
|
147 | path = escapePath(path);
|
148 | try {
|
149 | var stat = FS.statSync(path);
|
150 | }
|
151 | catch (e) {
|
152 | return false;
|
153 | }
|
154 | return stat.isDirectory();
|
155 | }
|
156 | exports.isDirectory = isDirectory;
|
157 | function isSymbolicLink(path) {
|
158 | path = escapePath(path);
|
159 | try {
|
160 | var stat = FS.statSync(path);
|
161 | }
|
162 | catch (e) {
|
163 | return false;
|
164 | }
|
165 | return stat.isSymbolicLink();
|
166 | }
|
167 | exports.isSymbolicLink = isSymbolicLink;
|
168 | function isFile(path) {
|
169 | path = escapePath(path);
|
170 | try {
|
171 | var stat = FS.statSync(path);
|
172 | }
|
173 | catch (e) {
|
174 | return false;
|
175 | }
|
176 | return stat.isFile();
|
177 | }
|
178 | exports.isFile = isFile;
|
179 | function _copy_file(source_file, output_file) {
|
180 | createDirectory(Path.dirname(output_file));
|
181 | var byteArray = FS.readFileSync(source_file);
|
182 | FS.writeFileSync(output_file, byteArray);
|
183 | }
|
184 | function _copy_dir(sourceDir, outputDir) {
|
185 | createDirectory(outputDir);
|
186 | var list = FS.readdirSync(sourceDir);
|
187 | list.forEach(function (fileName) {
|
188 | copy(Path.join(sourceDir, fileName), Path.join(outputDir, fileName));
|
189 | });
|
190 | }
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | function remove(path) {
|
196 | path = escapePath(path);
|
197 | try {
|
198 | FS.lstatSync(path).isDirectory()
|
199 | ? rmdir(path)
|
200 | : FS.unlinkSync(path);
|
201 | getDirectoryListing(path);
|
202 | }
|
203 | catch (e) {
|
204 | }
|
205 | }
|
206 | exports.remove = remove;
|
207 | function rmdir(path) {
|
208 | var files = [];
|
209 | if (FS.existsSync(path)) {
|
210 | files = FS.readdirSync(path);
|
211 | files.forEach(function (file) {
|
212 | var curPath = path + "/" + file;
|
213 | if (FS.statSync(curPath).isDirectory()) {
|
214 | rmdir(curPath);
|
215 | }
|
216 | else {
|
217 | FS.unlinkSync(curPath);
|
218 | }
|
219 | });
|
220 | FS.rmdirSync(path);
|
221 | }
|
222 | }
|
223 | function rename(oldPath, newPath) {
|
224 | if (isDirectory(oldPath)) {
|
225 | FS.renameSync(oldPath, newPath);
|
226 | }
|
227 | }
|
228 | exports.rename = rename;
|
229 |
|
230 |
|
231 |
|
232 | function getDirectory(path) {
|
233 | path = escapePath(path);
|
234 | return Path.dirname(path) + "/";
|
235 | }
|
236 | exports.getDirectory = getDirectory;
|
237 |
|
238 |
|
239 |
|
240 | function getExtension(path) {
|
241 | path = escapePath(path);
|
242 | var index = path.lastIndexOf(".");
|
243 | if (index == -1)
|
244 | return "";
|
245 | var i = path.lastIndexOf("/");
|
246 | if (i > index)
|
247 | return "";
|
248 | return path.substring(index + 1);
|
249 | }
|
250 | exports.getExtension = getExtension;
|
251 |
|
252 |
|
253 |
|
254 | function getFileName(path) {
|
255 | if (!path)
|
256 | return "";
|
257 | path = escapePath(path);
|
258 | var startIndex = path.lastIndexOf("/");
|
259 | var endIndex;
|
260 | if (startIndex > 0 && startIndex == path.length - 1) {
|
261 | path = path.substring(0, path.length - 1);
|
262 | startIndex = path.lastIndexOf("/");
|
263 | endIndex = path.length;
|
264 | return path.substring(startIndex + 1, endIndex);
|
265 | }
|
266 | endIndex = path.lastIndexOf(".");
|
267 | if (endIndex == -1 || isDirectory(path))
|
268 | endIndex = path.length;
|
269 | return path.substring(startIndex + 1, endIndex);
|
270 | }
|
271 | exports.getFileName = getFileName;
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 | function getDirectoryListing(path, relative) {
|
278 | path = escapePath(path);
|
279 | try {
|
280 | var list = FS.readdirSync(path);
|
281 | }
|
282 | catch (e) {
|
283 | return [];
|
284 | }
|
285 | var length = list.length;
|
286 | if (!relative) {
|
287 | for (var i = length - 1; i >= 0; i--) {
|
288 | if (list[i].charAt(0) == ".") {
|
289 | list.splice(i, 1);
|
290 | }
|
291 | else {
|
292 | list[i] = join(path, list[i]);
|
293 | }
|
294 | }
|
295 | }
|
296 | else {
|
297 | for (i = length - 1; i >= 0; i--) {
|
298 | if (list[i].charAt(0) == ".") {
|
299 | list.splice(i, 1);
|
300 | }
|
301 | }
|
302 | }
|
303 | return list;
|
304 | }
|
305 | exports.getDirectoryListing = getDirectoryListing;
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 | function getDirectoryAllListing(path) {
|
312 | var list = [];
|
313 | if (isDirectory(path)) {
|
314 | var fileList = getDirectoryListing(path);
|
315 | for (var key in fileList) {
|
316 | list = list.concat(getDirectoryAllListing(fileList[key]));
|
317 | }
|
318 | return list;
|
319 | }
|
320 | return [path];
|
321 | }
|
322 | exports.getDirectoryAllListing = getDirectoryAllListing;
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 | function search(dir, extension) {
|
329 | var list = [];
|
330 | try {
|
331 | var stat = FS.statSync(dir);
|
332 | }
|
333 | catch (e) {
|
334 | return list;
|
335 | }
|
336 | if (stat.isDirectory()) {
|
337 | findFiles(dir, list, extension, null);
|
338 | }
|
339 | return list;
|
340 | }
|
341 | exports.search = search;
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 | function searchByFunction(dir, filterFunc, checkDir) {
|
348 | var list = [];
|
349 | try {
|
350 | var stat = FS.statSync(dir);
|
351 | }
|
352 | catch (e) {
|
353 | return list;
|
354 | }
|
355 | if (stat.isDirectory()) {
|
356 | findFiles(dir, list, "", filterFunc, checkDir);
|
357 | }
|
358 | return list;
|
359 | }
|
360 | exports.searchByFunction = searchByFunction;
|
361 | function findFiles(filePath, list, extension, filterFunc, checkDir) {
|
362 | var files = FS.readdirSync(filePath);
|
363 | var length = files.length;
|
364 | for (var i = 0; i < length; i++) {
|
365 | if (files[i].charAt(0) == ".") {
|
366 | continue;
|
367 | }
|
368 | var path = join(filePath, files[i]);
|
369 | var stat = FS.statSync(path);
|
370 | if (stat.isDirectory()) {
|
371 | if (checkDir) {
|
372 | if (!filterFunc(path)) {
|
373 | continue;
|
374 | }
|
375 | }
|
376 | findFiles(path, list, extension, filterFunc);
|
377 | }
|
378 | else if (filterFunc != null) {
|
379 | if (filterFunc(path)) {
|
380 | list.push(path);
|
381 | }
|
382 | }
|
383 | else if (extension) {
|
384 | var len = extension.length;
|
385 | if (path.charAt(path.length - len - 1) == "." &&
|
386 | path.substr(path.length - len, len).toLowerCase() == extension) {
|
387 | list.push(path);
|
388 | }
|
389 | }
|
390 | else {
|
391 | list.push(path);
|
392 | }
|
393 | }
|
394 | }
|
395 |
|
396 |
|
397 |
|
398 | function exists(path) {
|
399 | path = escapePath(path);
|
400 | return FS.existsSync(path);
|
401 | }
|
402 | exports.exists = exists;
|
403 |
|
404 |
|
405 |
|
406 | function escapePath(path) {
|
407 | if (!path)
|
408 | return "";
|
409 | return path.split("\\").join("/");
|
410 | }
|
411 | exports.escapePath = escapePath;
|
412 |
|
413 |
|
414 |
|
415 | function join(dir1, dir2, dir3, dir4, dir5) {
|
416 | var path = Path.join.apply(null, arguments);
|
417 | path = escapePath(path);
|
418 | return path;
|
419 | }
|
420 | exports.join = join;
|
421 | function getRelativePath(dir, filename) {
|
422 | var relative = Path.relative(dir, filename);
|
423 | return escapePath(relative);
|
424 | ;
|
425 | }
|
426 | exports.getRelativePath = getRelativePath;
|
427 | function basename(p, ext) {
|
428 | var path = Path.basename.apply(null, arguments);
|
429 | path = escapePath(path);
|
430 | return path;
|
431 | }
|
432 | exports.basename = basename;
|
433 |
|
434 | function relative(from, to) {
|
435 | var path = Path.relative.apply(null, arguments);
|
436 | path = escapePath(path);
|
437 | return path;
|
438 | }
|
439 | exports.relative = relative;
|
440 | function getAbsolutePath(path) {
|
441 | var tempPath = Path.resolve(path);
|
442 | tempPath = escapePath(tempPath);
|
443 | path = escapePath(path);
|
444 | if (tempPath == path) {
|
445 | return path;
|
446 | }
|
447 | throw 'error';
|
448 |
|
449 | }
|
450 | exports.getAbsolutePath = getAbsolutePath;
|
451 |
|
\ | No newline at end of file |