From d3427ebd12fdcd1f11f061cd053f299f112e343a Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Wed, 9 Mar 2022 18:23:42 +0800 Subject: [PATCH] feat(ts): chagne ecma import logic --- .../kotlin/chapi/app/path/EcmaImportPath.kt | 33 ++++++++++++------- .../chapi/app/path/EcmaImportPathKtTest.kt | 25 ++++++++++---- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/chapi-application/src/main/kotlin/chapi/app/path/EcmaImportPath.kt b/chapi-application/src/main/kotlin/chapi/app/path/EcmaImportPath.kt index 7f86c762..7bba9e0d 100644 --- a/chapi-application/src/main/kotlin/chapi/app/path/EcmaImportPath.kt +++ b/chapi-application/src/main/kotlin/chapi/app/path/EcmaImportPath.kt @@ -2,34 +2,43 @@ package chapi.app.path import java.io.File +fun ecmaImportConvert(workspace: String, filepath: String, importPath: String): String { + val relativePath = relativeRoot(workspace, filepath) + return importConvert(relativePath, importPath) +} + // filePath: point to current file // sourcePath: like `../../` // // output: to normalize path -fun ecmaPathConvert(basedPath: String, sourcePath: String): String { +fun importConvert(filepath: String, importPath: String): String { // import "@/src/component/Hello.js" - val isResolvePath = sourcePath.startsWith("@/") + val isResolvePath = importPath.startsWith("@/") if(isResolvePath) { - return sourcePath.removeRange(0, 2) + return importPath.removeRange(0, 2) } - var file = File(basedPath) + if(importPath.startsWith("./") || importPath.startsWith("../")) { + var file = File(filepath) + + // use parent to convert + if(file.extension.isNotEmpty()) { + file = file.parentFile + } - // use parent to convert - if(file.isFile) { - file = file.parentFile + val resolve = file.resolve(File(importPath)) + return resolve.normalize().toString() } - val resolve = file.resolve(File(sourcePath)) - return resolve.normalize().toString() + return importPath } -fun relativeRoot(basedPath: String, sourcePath: String): String { - var pathname = sourcePath +fun relativeRoot(filepath: String, importPath: String): String { + var pathname = importPath val isResolvePath = pathname.startsWith("@/") if(isResolvePath) { pathname = pathname.removeRange(0, 2) } - return File(pathname).relativeTo(File(basedPath)).toString() + return File(pathname).relativeTo(File(filepath)).toString() } diff --git a/chapi-application/src/test/kotlin/chapi/app/path/EcmaImportPathKtTest.kt b/chapi-application/src/test/kotlin/chapi/app/path/EcmaImportPathKtTest.kt index 4ce4fa32..ef7f510a 100644 --- a/chapi-application/src/test/kotlin/chapi/app/path/EcmaImportPathKtTest.kt +++ b/chapi-application/src/test/kotlin/chapi/app/path/EcmaImportPathKtTest.kt @@ -7,39 +7,50 @@ internal class EcmaImportPathKtTest { @Test internal fun sameLevelPath() { - val path = ecmaPathConvert("src/components/", "./config.js") + val path = importConvert("src/components/", "./config.js") assertEquals(path, "src/components/config.js") } @Test internal fun parentPath() { - val path = ecmaPathConvert("src/components/", "../config.js") + val path = importConvert("src/components/", "../config.js") assertEquals(path, "src/config.js") } @Test internal fun fileSameLevel() { - val path = ecmaPathConvert("src/components/Hello.js", "../config.js") - assertEquals(path, "src/components/config.js") + val path = importConvert("src/components/Hello.js", "../config.js") + assertEquals(path, "src/config.js") } @Test internal fun resolvePath() { - val path = ecmaPathConvert("src/components/Hello.js", "@/src/components/config.js") + val path = importConvert("src/components/Hello.js", "@/src/components/config.js") assertEquals(path, "src/components/config.js") } @Test internal fun resolveRealWorldPath() { val full = "chapi/chapi-application/ts/apicall/addition/" - val path = ecmaPathConvert(full, "../components/config.js") + val path = importConvert(full, "../components/config.js") assertEquals(path, "chapi/chapi-application/ts/apicall/components/config.js") } - // source: /Volumes/source/modernizing/chapi/chapi-application/build/resources/test/languages/ts/apicall/addition/systemInfo.ts @Test internal fun relativePath() { val path = relativeRoot("root/src/components/Hello.js", "root/src/components/config.js") assertEquals(path, "../config.js") } + + @Test + internal fun withoutChangeNotRelative() { + val path = importConvert("src/components/", "jquery") + assertEquals(path, "jquery") + } + + @Test + internal fun convertFullPath() { + val path = ecmaImportConvert("modernizing/chapi/chapi-application", "modernizing/chapi/chapi-application/src/components/hello.js", "./config.js") + assertEquals(path, "src/components/config.js") + } }