Skip to content

Commit

Permalink
feat(ts): chagne ecma import logic
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Mar 9, 2022
1 parent 4ebb06a commit d3427eb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
33 changes: 21 additions & 12 deletions chapi-application/src/main/kotlin/chapi/app/path/EcmaImportPath.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit d3427eb

Please # to comment.