From a5a5f556455ef0fd4ff6a47d5977a73965d15304 Mon Sep 17 00:00:00 2001 From: yu-ogi Date: Wed, 6 Sep 2023 12:21:22 +0900 Subject: [PATCH] refactor: drop PathUtil --- src/Module.ts | 2 +- src/ModuleManager.ts | 2 +- src/PathUtil.ts | 126 ------------------------ src/__tests__/PathUtilSpec.ts | 174 ---------------------------------- src/index.common.ts | 4 +- 5 files changed, 5 insertions(+), 303 deletions(-) delete mode 100644 src/PathUtil.ts delete mode 100644 src/__tests__/PathUtilSpec.ts diff --git a/src/Module.ts b/src/Module.ts index 8457a80ef..6cbf6168b 100644 --- a/src/Module.ts +++ b/src/Module.ts @@ -1,5 +1,5 @@ +import { PathUtil } from "@akashic/game-configuration/lib/utils/PathUtil"; import type { Module as PdiModule, ScriptAssetRuntimeValue, ScriptAssetRuntimeValueBase } from "@akashic/pdi-types"; -import { PathUtil } from "./PathUtil"; import type { Require } from "./Require"; export interface ModuleParameterObject { diff --git a/src/ModuleManager.ts b/src/ModuleManager.ts index 8463bc22d..c94511c62 100644 --- a/src/ModuleManager.ts +++ b/src/ModuleManager.ts @@ -1,8 +1,8 @@ +import { PathUtil } from "@akashic/game-configuration/lib/utils/PathUtil"; import type { Asset, ScriptAssetRuntimeValueBase } from "@akashic/pdi-types"; import type { AssetManager, OneOfAsset } from "./AssetManager"; import { ExceptionFactory } from "./ExceptionFactory"; import { Module } from "./Module"; -import { PathUtil } from "./PathUtil"; import type { RequireCacheable } from "./RequireCacheable"; import { RequireCachedValue } from "./RequireCachedValue"; import { ScriptAssetContext } from "./ScriptAssetContext"; diff --git a/src/PathUtil.ts b/src/PathUtil.ts deleted file mode 100644 index 5839ee50a..000000000 --- a/src/PathUtil.ts +++ /dev/null @@ -1,126 +0,0 @@ -import { ExceptionFactory } from "./ExceptionFactory"; - -/** - * パスユーティリティ。 - * 通常、ゲーム開発者がファイルパスを扱うことはなく、このモジュールのメソッドを呼び出す必要はない。 - */ -export module PathUtil { - export interface PathComponents { - host: string; - path: string; - } - - /** - * 二つのパス文字列をつなぎ、相対パス表現 (".", "..") を解決して返す。 - * @param base 左辺パス文字列 (先頭の "./" を除き、".", ".." を含んではならない) - * @param path 右辺パス文字列 - */ - export function resolvePath(base: string, path: string): string { - function split(str: string): string[] { - const ret = str.split("/"); - if (ret[ret.length - 1] === "") ret.pop(); - return ret; - } - if (path === "") return base; - const baseComponents = PathUtil.splitPath(base); - const parts = split(baseComponents.path).concat(split(path)); - let resolved: string[] = []; - for (let i = 0; i < parts.length; ++i) { - const part = parts[i]; - switch (part) { - case "..": - const popped = resolved.pop(); - if (popped === undefined || popped === "" || popped === ".") - throw ExceptionFactory.createAssertionError("PathUtil.resolvePath: invalid arguments"); - break; - case ".": - if (resolved.length === 0) { - resolved.push("."); - } - break; - case "": // 絶対パス - resolved = [""]; - break; - default: - resolved.push(part); - } - } - return baseComponents.host + resolved.join("/"); - } - - /** - * パス文字列からディレクトリ名部分を切り出して返す。 - * @param path パス文字列 - */ - export function resolveDirname(path: string): string { - const index = path.lastIndexOf("/"); - if (index === -1) return path; - - return path.substr(0, index); - } - - /** - * パス文字列から拡張子部分を切り出して返す。 - * @param path パス文字列 - */ - export function resolveExtname(path: string): string { - for (let i = path.length - 1; i >= 0; --i) { - const c = path.charAt(i); - if (c === ".") { - return path.substr(i); - } else if (c === "/") { - return ""; - } - } - return ""; - } - - /** - * パス文字列から、node.js において require() の探索範囲になるパスの配列を作成して返す。 - * @param path ディレクトリを表すパス文字列 - */ - export function makeNodeModulePaths(path: string): string[] { - const pathComponents = PathUtil.splitPath(path); - const host = pathComponents.host; - path = pathComponents.path; - - if (path[path.length - 1] === "/") { - path = path.slice(0, path.length - 1); - } - - const parts = path.split("/"); - const firstDir = parts.indexOf("node_modules"); - const root = firstDir > 0 ? firstDir - 1 : 0; - const dirs: string[] = []; - for (let i = parts.length - 1; i >= root; --i) { - if (parts[i] === "node_modules") continue; - const dirParts = parts.slice(0, i + 1); - dirParts.push("node_modules"); - const dir = dirParts.join("/"); - dirs.push(host + dir); - } - return dirs; - } - - /** - * 与えられたパス文字列からホストを切り出す。 - * @param path パス文字列 - */ - export function splitPath(path: string): PathComponents { - let host = ""; - const doubleSlashIndex = path.indexOf("//"); - if (doubleSlashIndex >= 0) { - const hostSlashIndex = path.indexOf("/", doubleSlashIndex + 2); // 2 === "//".length - if (hostSlashIndex >= 0) { - host = path.slice(0, hostSlashIndex); - path = path.slice(hostSlashIndex); // 先頭に "/" を残して絶対パス扱いさせる - } else { - host = path; - path = "/"; // path全体がホストだったので、絶対パス扱いさせる - } - } else { - host = ""; - } - return { host: host, path: path }; - } -} diff --git a/src/__tests__/PathUtilSpec.ts b/src/__tests__/PathUtilSpec.ts deleted file mode 100644 index 0694073d5..000000000 --- a/src/__tests__/PathUtilSpec.ts +++ /dev/null @@ -1,174 +0,0 @@ -import { PathUtil } from ".."; - -describe("test PathUtil", () => { - describe("resolvePath", () => { - it("resolves relative path", () => { - expect(PathUtil.resolvePath("hoge/fuga/sugoi/", "../a/./b/../c.js")).toBe("hoge/fuga/a/c.js"); - expect(PathUtil.resolvePath("/absolute/directory/foo", "../././a.js")).toBe("/absolute/directory/a.js"); - expect(PathUtil.resolvePath("/", "a/./b/../c.js")).toBe("/a/c.js"); - expect(PathUtil.resolvePath("/", "a.js")).toBe("/a.js"); - expect(PathUtil.resolvePath("./hoge/fuga/sugoi/", "../a/./b/../c.js")).toBe("./hoge/fuga/a/c.js"); - expect(PathUtil.resolvePath("./relative/directory/foo", "../././a.js")).toBe("./relative/directory/a.js"); - expect(PathUtil.resolvePath("", "a.js")).toBe("a.js"); - expect(PathUtil.resolvePath("", "a/./b/../c.js")).toBe("a/c.js"); - expect(PathUtil.resolvePath("./", "a/./b/../c.js")).toBe("./a/c.js"); - expect(PathUtil.resolvePath("./", "a.js")).toBe("./a.js"); - expect(PathUtil.resolvePath(".", "a/./b/../c.js")).toBe("./a/c.js"); - expect(PathUtil.resolvePath(".", "a.js")).toBe("./a.js"); - expect(PathUtil.resolvePath("hoge/", "../hoge/../a.js")).toBe("a.js"); - expect(PathUtil.resolvePath("hoge/", "../hoge/a.js")).toBe("hoge/a.js"); - expect(PathUtil.resolvePath("hoge/", "./hoge/a.js")).toBe("hoge/hoge/a.js"); - expect(PathUtil.resolvePath("http://hoge/fuga/sugoi/", "../a/./b/../c.js")).toBe("http://hoge/fuga/a/c.js"); - expect(PathUtil.resolvePath("http://foo.test/absolute/directory/foo/", "../././a.js")).toBe( - "http://foo.test/absolute/directory/a.js" - ); - expect(PathUtil.resolvePath("http://foo.test:80/", "a/./b/../c.js")).toBe("http://foo.test:80/a/c.js"); - expect(PathUtil.resolvePath("http://foo.test/", "a.js")).toBe("http://foo.test/a.js"); - expect(PathUtil.resolvePath("http://hoge/fuga/sugoi", "../a/./b/../c.js")).toBe("http://hoge/fuga/a/c.js"); - expect(PathUtil.resolvePath("http://foo.test/absolute/directory/foo", "../././a.js")).toBe( - "http://foo.test/absolute/directory/a.js" - ); - expect(PathUtil.resolvePath("http://foo.test:80", "a/./b/../c.js")).toBe("http://foo.test:80/a/c.js"); - expect(PathUtil.resolvePath("http://foo.test", "a.js")).toBe("http://foo.test/a.js"); - }); - - it("resolves absolute path", () => { - expect(PathUtil.resolvePath("hoge/fuga/sugoi/", "/a/./b/../c.js")).toBe("/a/c.js"); - expect(PathUtil.resolvePath("/absolute/directory/foo", "..//././a.js")).toBe("/a.js"); - expect(PathUtil.resolvePath("/", "/a/./b/../c.js")).toBe("/a/c.js"); - expect(PathUtil.resolvePath("/", "/a.js")).toBe("/a.js"); - expect(PathUtil.resolvePath("./hoge/fuga/sugoi/", "/a/./b/../c.js")).toBe("/a/c.js"); - expect(PathUtil.resolvePath("./relative/directory/foo", "/./a.js")).toBe("/a.js"); - expect(PathUtil.resolvePath("", "/a.js")).toBe("/a.js"); - expect(PathUtil.resolvePath("", "/a/./b/../c.js")).toBe("/a/c.js"); - expect(PathUtil.resolvePath("./", "a/.//b//c.js")).toBe("/c.js"); - expect(PathUtil.resolvePath("./", "/a.js")).toBe("/a.js"); - expect(PathUtil.resolvePath(".", "/a/./b/../c.js")).toBe("/a/c.js"); - expect(PathUtil.resolvePath(".", "/a.js")).toBe("/a.js"); - expect(PathUtil.resolvePath("hoge/", "/hoge/../a.js")).toBe("/a.js"); - expect(PathUtil.resolvePath("hoge/", "/hoge/a.js")).toBe("/hoge/a.js"); - expect(PathUtil.resolvePath("http://hoge/fuga/sugoi/", "/a/./b/../c.js")).toBe("http://hoge/a/c.js"); - expect(PathUtil.resolvePath("http://foo.test/absolute/directory/foo/", "//./a.js")).toBe("http://foo.test/a.js"); - expect(PathUtil.resolvePath("http://foo.test:80/", "/a/./b/../c.js")).toBe("http://foo.test:80/a/c.js"); - expect(PathUtil.resolvePath("http://foo.test/", "/a.js")).toBe("http://foo.test/a.js"); - expect(PathUtil.resolvePath("http://hoge/fuga/sugoi", "..//a/./b/../c.js")).toBe("http://hoge/a/c.js"); - expect(PathUtil.resolvePath("http://foo.test/absolute/directory/foo", "/a.js")).toBe("http://foo.test/a.js"); - expect(PathUtil.resolvePath("http://foo.test:80", "/a/./b/../c.js")).toBe("http://foo.test:80/a/c.js"); - expect(PathUtil.resolvePath("http://foo.test", "/a.js")).toBe("http://foo.test/a.js"); - }); - - it("rejects invalid arguments", () => { - expect(() => { - PathUtil.resolvePath("hoge/", "../../a.js"); - }).toThrowError("PathUtil.resolvePath: invalid arguments"); - expect(() => { - PathUtil.resolvePath(".", "../a.js"); - }).toThrowError("PathUtil.resolvePath: invalid arguments"); - expect(() => { - PathUtil.resolvePath("./", "../a.js"); - }).toThrowError("PathUtil.resolvePath: invalid arguments"); - expect(() => { - PathUtil.resolvePath("./hoge", "./../../a.js"); - }).toThrowError("PathUtil.resolvePath: invalid arguments"); - }); - }); - - it("resolveExtname", () => { - expect(PathUtil.resolveExtname("hoge/fuga/sugoi/foo.js")).toBe(".js"); - expect(PathUtil.resolveExtname("/absolute/directory/bar.js")).toBe(".js"); - expect(PathUtil.resolveExtname("/")).toBe(""); - expect(PathUtil.resolveExtname("")).toBe(""); - }); - - it("resolveDirname", () => { - expect(PathUtil.resolveDirname("hoge/fuga/sugoi/c.js")).toBe("hoge/fuga/sugoi"); - expect(PathUtil.resolveDirname("hoge")).toBe("hoge"); - expect(PathUtil.resolveDirname("")).toBe(""); - }); - - it("makeNodeModulePaths", () => { - let paths = PathUtil.makeNodeModulePaths("/foo/bar/zoo/node_modules/aaa/node_modules/ccc/node_modules/ddd/eee"); - expect(paths).toEqual([ - "/foo/bar/zoo/node_modules/aaa/node_modules/ccc/node_modules/ddd/eee/node_modules", - "/foo/bar/zoo/node_modules/aaa/node_modules/ccc/node_modules/ddd/node_modules", - "/foo/bar/zoo/node_modules/aaa/node_modules/ccc/node_modules", - "/foo/bar/zoo/node_modules/aaa/node_modules", - "/foo/bar/zoo/node_modules" - ]); - - paths = PathUtil.makeNodeModulePaths("/foo/bar/zoo/node_modules/aaa/node_modules/ccc/node_modules/ddd/eee/"); - expect(paths).toEqual([ - "/foo/bar/zoo/node_modules/aaa/node_modules/ccc/node_modules/ddd/eee/node_modules", - "/foo/bar/zoo/node_modules/aaa/node_modules/ccc/node_modules/ddd/node_modules", - "/foo/bar/zoo/node_modules/aaa/node_modules/ccc/node_modules", - "/foo/bar/zoo/node_modules/aaa/node_modules", - "/foo/bar/zoo/node_modules" - ]); - - paths = PathUtil.makeNodeModulePaths("/foo/bar/zoo/aaa"); - expect(paths).toEqual([ - "/foo/bar/zoo/aaa/node_modules", - "/foo/bar/zoo/node_modules", - "/foo/bar/node_modules", - "/foo/node_modules", - "/node_modules" - ]); - - paths = PathUtil.makeNodeModulePaths("/foo/bar/"); - expect(paths).toEqual(["/foo/bar/node_modules", "/foo/node_modules", "/node_modules"]); - - paths = PathUtil.makeNodeModulePaths("/"); - expect(paths).toEqual(["/node_modules"]); - - paths = PathUtil.makeNodeModulePaths("/node_modules/foo/node_modules/bar/node_modules/aaa"); - expect(paths).toEqual([ - "/node_modules/foo/node_modules/bar/node_modules/aaa/node_modules", - "/node_modules/foo/node_modules/bar/node_modules", - "/node_modules/foo/node_modules", - "/node_modules" - ]); - - paths = PathUtil.makeNodeModulePaths("https://hoge.test/foo/bar/zoo/node_modules/aaa/node_modules/ccc/node_modules/ddd/eee"); - expect(paths).toEqual([ - "https://hoge.test/foo/bar/zoo/node_modules/aaa/node_modules/ccc/node_modules/ddd/eee/node_modules", - "https://hoge.test/foo/bar/zoo/node_modules/aaa/node_modules/ccc/node_modules/ddd/node_modules", - "https://hoge.test/foo/bar/zoo/node_modules/aaa/node_modules/ccc/node_modules", - "https://hoge.test/foo/bar/zoo/node_modules/aaa/node_modules", - "https://hoge.test/foo/bar/zoo/node_modules" - ]); - - paths = PathUtil.makeNodeModulePaths("https://hoge.test/foo/bar/zoo/aaa"); - expect(paths).toEqual([ - "https://hoge.test/foo/bar/zoo/aaa/node_modules", - "https://hoge.test/foo/bar/zoo/node_modules", - "https://hoge.test/foo/bar/node_modules", - "https://hoge.test/foo/node_modules", - "https://hoge.test/node_modules" - ]); - - paths = PathUtil.makeNodeModulePaths("https://hoge.test/"); - expect(paths).toEqual(["https://hoge.test/node_modules"]); - paths = PathUtil.makeNodeModulePaths("https://hoge.test"); - expect(paths).toEqual(["https://hoge.test/node_modules"]); - - paths = PathUtil.makeNodeModulePaths("https://node_modules/"); - expect(paths).toEqual(["https://node_modules/node_modules"]); - paths = PathUtil.makeNodeModulePaths("https://node_modules"); - expect(paths).toEqual(["https://node_modules/node_modules"]); - }); - - it("splitPath", () => { - expect(PathUtil.splitPath("http://example.com/file.ext")).toEqual({ - host: "http://example.com", - path: "/file.ext" - }); - expect(PathUtil.splitPath("http://example.com")).toEqual({ - host: "http://example.com", - path: "/" - }); - expect(PathUtil.splitPath("example/file.ext")).toEqual({ - host: "", - path: "example/file.ext" - }); - }); -}); diff --git a/src/index.common.ts b/src/index.common.ts index 2ff43544e..32d4becba 100644 --- a/src/index.common.ts +++ b/src/index.common.ts @@ -12,6 +12,9 @@ export { Module } from "./Module"; export { ShaderProgram } from "./ShaderProgram"; export { VideoSystem } from "./VideoSystem"; +// 後方互換性のため PathUtil のみ reexport する。 +export { PathUtil } from "@akashic/game-configuration/lib/utils/PathUtil"; + export * from "./entities/CacheableE"; export * from "./entities/E"; export * from "./entities/FilledRect"; @@ -59,7 +62,6 @@ export * from "./OperationPlugin"; export * from "./OperationPluginManager"; export * from "./OperationPluginOperation"; export * from "./OperationPluginStatic"; -export * from "./PathUtil"; export * from "./Player"; export * from "./PointEventResolver"; export * from "./RandomGenerator";