UNPKG

12.9 kBJavaScriptView Raw
1//////////////////////////////////////////////////////////////////////////////////////
2//
3// Copyright (c) 2014-present, Egret Technology.
4// All rights reserved.
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// * Neither the name of the Egret nor the
14// names of its contributors may be used to endorse or promote products
15// derived from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
18// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20// IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
23// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27//
28//////////////////////////////////////////////////////////////////////////////////////
29"use strict";
30const FS = require("fs");
31const Path = require("path");
32var charset = "utf-8";
33/**
34 * 保存数据到指定文件
35 * @param path 文件完整路径名
36 * @param data 要保存的数据
37 */
38function 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}
47exports.save = save;
48/**
49 * 创建文件夹
50 */
51function 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}
86exports.createDirectory = createDirectory;
87var textTemp = {};
88/**
89 * 读取文本文件,返回打开文本的字符串内容,若失败,返回"".
90 * @param path 要打开的文件路径
91 */
92function 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}
113exports.read = read;
114/**
115 * 读取字节流文件,返回字节流,若失败,返回null.
116 * @param path 要打开的文件路径
117 */
118function 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}
128exports.readBinary = readBinary;
129/**
130 * 复制文件或目录
131 * @param source 文件源路径
132 * @param dest 文件要复制到的目标路径
133 */
134function 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}
145exports.copy = copy;
146function 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}
156exports.isDirectory = isDirectory;
157function 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}
167exports.isSymbolicLink = isSymbolicLink;
168function 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}
178exports.isFile = isFile;
179function _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}
184function _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 * @param path 要删除的文件源路径
194 */
195function 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}
206exports.remove = remove;
207function 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}
223function rename(oldPath, newPath) {
224 if (isDirectory(oldPath)) {
225 FS.renameSync(oldPath, newPath);
226 }
227}
228exports.rename = rename;
229/**
230 * 返回指定文件的父级文件夹路径,返回字符串的结尾已包含分隔符。
231 */
232function getDirectory(path) {
233 path = escapePath(path);
234 return Path.dirname(path) + "/";
235}
236exports.getDirectory = getDirectory;
237/**
238 * 获得路径的扩展名,不包含点字符。
239 */
240function 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}
250exports.getExtension = getExtension;
251/**
252 * 获取路径的文件名(不含扩展名)或文件夹名
253 */
254function 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}
271exports.getFileName = getFileName;
272/**
273 * 获取指定文件夹下的文件或文件夹列表,不包含子文件夹内的文件。
274 * @param path 要搜索的文件夹
275 * @param relative 是否返回相对路径,若不传入或传入false,都返回绝对路径。
276 */
277function 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}
305exports.getDirectoryListing = getDirectoryListing;
306/**
307 * 获取指定文件夹下全部的文件列表,包括子文件夹
308 * @param path
309 * @returns {any}
310 */
311function 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}
322exports.getDirectoryAllListing = getDirectoryAllListing;
323/**
324 * 使用指定扩展名搜索文件夹及其子文件夹下所有的文件
325 * @param dir 要搜索的文件夹
326 * @param extension 要搜索的文件扩展名,不包含点字符,例如:"png"。不设置表示获取所有类型文件。
327 */
328function 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}
341exports.search = search;
342/**
343 * 使用过滤函数搜索文件夹及其子文件夹下所有的文件
344 * @param dir 要搜索的文件夹
345 * @param filterFunc 过滤函数:filterFunc(file:File):Boolean,参数为遍历过程中的每一个文件,返回true则加入结果列表
346 */
347function 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}
360exports.searchByFunction = searchByFunction;
361function 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 */
398function exists(path) {
399 path = escapePath(path);
400 return FS.existsSync(path);
401}
402exports.exists = exists;
403/**
404 * 转换本机路径为Unix风格路径。
405 */
406function escapePath(path) {
407 if (!path)
408 return "";
409 return path.split("\\").join("/");
410}
411exports.escapePath = escapePath;
412/**
413 * 连接路径,支持传入多于两个的参数。也支持"../"相对路径解析。返回的分隔符为Unix风格。
414 */
415function join(dir1, dir2, dir3, dir4, dir5) {
416 var path = Path.join.apply(null, arguments);
417 path = escapePath(path);
418 return path;
419}
420exports.join = join;
421function getRelativePath(dir, filename) {
422 var relative = Path.relative(dir, filename);
423 return escapePath(relative);
424 ;
425}
426exports.getRelativePath = getRelativePath;
427function basename(p, ext) {
428 var path = Path.basename.apply(null, arguments);
429 path = escapePath(path);
430 return path;
431}
432exports.basename = basename;
433//获取相对路径 to相对于from的路径
434function relative(from, to) {
435 var path = Path.relative.apply(null, arguments);
436 path = escapePath(path);
437 return path;
438}
439exports.relative = relative;
440function 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 // return join(egret.args.projectDir, path);
449}
450exports.getAbsolutePath = getAbsolutePath;
451//# sourceMappingURL=path.js.map
\No newline at end of file