From a6be26206e2011e33e8208f0158292b6e715f793 Mon Sep 17 00:00:00 2001 From: coding Date: Tue, 9 Jan 2018 12:16:36 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=20fs=20=E7=B1=BB?= =?UTF-8?q?=E5=BA=93=20=E4=BC=98=E5=8C=96=20require?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/core/fs.js | 41 ++++++++++++++++++++++-------- src/main/resources/core/require.js | 3 +-- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/main/resources/core/fs.js b/src/main/resources/core/fs.js index eea05e4..1c4bab2 100644 --- a/src/main/resources/core/fs.js +++ b/src/main/resources/core/fs.js @@ -5,6 +5,9 @@ var File = Java.type("java.io.File"); var Files = Java.type("java.nio.file.Files"); var separatorChar = File.separatorChar; 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) { case 1: - if (path(arguments[0])) { - return arguments[0]; + var f = 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: return new File(exports.file(arguments[0]), arguments[1]); } @@ -73,14 +82,14 @@ function copy(inputStream, target, override) { * 读取文件 * @param file 文件路径 */ -function read(file) { - f = file(file); - if (!f.exists()) { - console.warn('读取文件', f, '错误 文件不存在!'); +function read(f) { + var file = exports.file(f); + if (!file.exists()) { + console.warn('读取文件', file, '错误 文件不存在!'); return; } // 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 是否覆盖 */ 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"), override ? StandardCopyOption['REPLACE_EXISTING'] : StandardCopyOption['ATOMIC_MOVE']); }; @@ -116,6 +127,15 @@ function move(src, des, override) { 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 = { canonical: path, concat: concat, @@ -127,5 +147,6 @@ exports = module.exports = { read: read, save: save, list: list, - move: move + move: move, + del: del } \ No newline at end of file diff --git a/src/main/resources/core/require.js b/src/main/resources/core/require.js index dc6ed15..efd3fa3 100644 --- a/src/main/resources/core/require.js +++ b/src/main/resources/core/require.js @@ -260,8 +260,7 @@ }; } - // 判断是否存在 isFile 不存在说明 parent 是一个字符串 需要转成File - if (parent.isFile) { + if (typeof parent === "string") { parent = new File(parent); } var cacheModules = [];