feat: 更新 fs 类库 优化 require

This commit is contained in:
coding 2018-01-09 12:16:36 +00:00
parent 318a7cdcfd
commit a6be26206e
2 changed files with 32 additions and 12 deletions

View File

@ -5,6 +5,9 @@ var File = Java.type("java.io.File");
var Files = Java.type("java.nio.file.Files"); var Files = Java.type("java.nio.file.Files");
var separatorChar = File.separatorChar; var separatorChar = File.separatorChar;
var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption"); var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption");
var _toString = function (obj) {
return Object.prototype.toString.call(obj);
}
/** /**
* 用文件分割符合并路径 * 用文件分割符合并路径
@ -24,10 +27,16 @@ function file() {
} }
switch (arguments.length) { switch (arguments.length) {
case 1: case 1:
if (path(arguments[0])) { var f = arguments[0]
return arguments[0]; if (f instanceof File) {
return f;
}
if (typeof f === "string") {
return new File(f);
}
if (f instanceof Path) {
return f.toFile();
} }
return new File(arguments[0]);
case 2: case 2:
return new File(exports.file(arguments[0]), arguments[1]); return new File(exports.file(arguments[0]), arguments[1]);
} }
@ -73,14 +82,14 @@ function copy(inputStream, target, override) {
* 读取文件 * 读取文件
* @param file 文件路径 * @param file 文件路径
*/ */
function read(file) { function read(f) {
f = file(file); var file = exports.file(f);
if (!f.exists()) { if (!file.exists()) {
console.warn('读取文件', f, '错误 文件不存在!'); console.warn('读取文件', file, '错误 文件不存在!');
return; return;
} }
// noinspection JSPrimitiveTypeWrapperUsage // noinspection JSPrimitiveTypeWrapperUsage
return new java.lang.String(Files.readAllBytes(f.toPath()), "UTF-8"); return new java.lang.String(Files.readAllBytes(file.toPath()), "UTF-8");
}; };
/** /**
* 保存内容文件 * 保存内容文件
@ -89,7 +98,9 @@ function read(file) {
* @param override 是否覆盖 * @param override 是否覆盖
*/ */
function save(path, content, override) { function save(path, content, override) {
Files.write(new File(path).toPath(), var file = new File(path);
file.getParentFile().mkdirs();
Files.write(file.toPath(),
content.getBytes("UTF-8"), content.getBytes("UTF-8"),
override ? StandardCopyOption['REPLACE_EXISTING'] : StandardCopyOption['ATOMIC_MOVE']); override ? StandardCopyOption['REPLACE_EXISTING'] : StandardCopyOption['ATOMIC_MOVE']);
}; };
@ -116,6 +127,15 @@ function move(src, des, override) {
override ? StandardCopyOption['REPLACE_EXISTING'] : StandardCopyOption['ATOMIC_MOVE']) override ? StandardCopyOption['REPLACE_EXISTING'] : StandardCopyOption['ATOMIC_MOVE'])
}; };
function del(file) {
file = exports.file(file);
if (!file.exists()) { return; }
if (file.isDirectory()) {
Files.list(file.toPath()).collect(Collectors.toList()).forEach(function (f) { del(f); })
}
Files.delete(file.toPath());
}
exports = module.exports = { exports = module.exports = {
canonical: path, canonical: path,
concat: concat, concat: concat,
@ -127,5 +147,6 @@ exports = module.exports = {
read: read, read: read,
save: save, save: save,
list: list, list: list,
move: move move: move,
del: del
} }

View File

@ -260,8 +260,7 @@
}; };
} }
// 判断是否存在 isFile 不存在说明 parent 是一个字符串 需要转成File if (typeof parent === "string") {
if (parent.isFile) {
parent = new File(parent); parent = new File(parent);
} }
var cacheModules = []; var cacheModules = [];