-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- handle glob expansion
- Loading branch information
1 parent
d9a36fa
commit 488fbf8
Showing
8 changed files
with
256 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...r-portable/src/main/scala/org/scalablytyped/converter/internal/importer/ProxyModule.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package org.scalablytyped.converter.internal | ||
package importer | ||
|
||
import com.olvind.logging.Logger | ||
import org.scalablytyped.converter.internal.ts._ | ||
|
||
case class ProxyModule( | ||
comments: Comments, | ||
libName: TsIdentLibrary, | ||
fromModule: TsIdentModule, | ||
toModule: TsIdentModule, | ||
) { | ||
val asModule = TsDeclModule( | ||
comments = comments, | ||
declared = false, | ||
name = toModule, | ||
members = IArray( | ||
TsExport( | ||
comments = NoComments, | ||
typeOnly = false, | ||
tpe = ExportType.Named, | ||
exported = TsExportee.Star(None, fromModule), | ||
), | ||
), | ||
codePath = CodePath.HasPath(libName, TsQIdent(IArray(toModule))), | ||
jsLocation = JsLocation.Zero, | ||
) | ||
} | ||
|
||
object ProxyModule { | ||
val FromExports = Comments("/* from `exports` in `package.json` */\n") | ||
|
||
def fromExports( | ||
source: LibTsSource, | ||
logger: Logger[_], | ||
resolve: LibraryResolver, | ||
existing: TsIdent => Boolean, | ||
exports: Map[String, String], | ||
): Iterable[ProxyModule] = { | ||
val expandedGlobs = exports.flatMap { | ||
case tuple @ (exportedName, exportedTypesRelPath) => | ||
exportedTypesRelPath.split('*') match { | ||
case Array(_) => Some(tuple) | ||
case Array(pre, post) => | ||
val splitPrePath = pre.split('/').filterNot(_ == ".") | ||
|
||
// last part of `pre` may not be a full path fragment, so drop it and consider it below | ||
val (folderPrePart, preFileNameStart) = | ||
if (pre.endsWith("/")) (splitPrePath, "") | ||
else (splitPrePath.dropRight(1), splitPrePath.lastOption.getOrElse("")) | ||
|
||
val lookIn = folderPrePart.foldLeft(source.folder.path)(_ / _) | ||
|
||
// need to take whatever the glob expanded to and expand it into both `name` and `types` | ||
val expandedFragments = os.walk(lookIn).flatMap { path => | ||
val relPathString = path.relativeTo(lookIn).toString() | ||
|
||
if (relPathString.startsWith(preFileNameStart) && relPathString.endsWith(post)) | ||
Some(relPathString.drop(preFileNameStart.length).dropRight(post.length)) | ||
else None | ||
} | ||
|
||
val expanded = | ||
expandedFragments.map(m => (exportedName.replace("*", m), exportedTypesRelPath.replace("*", m))) | ||
expanded | ||
|
||
case _ => logger.fatal(s"need to add support for more than one '*' in glob pattern $exportedTypesRelPath") | ||
} | ||
} | ||
|
||
val libModule = TsIdentModule.fromLibrary(source.libName) | ||
|
||
expandedGlobs.flatMap { | ||
case tuple @ (name, types) => | ||
val fromModule = resolve.module(source, source.folder, types) match { | ||
case Some(resolvedModule) => resolvedModule.moduleName | ||
case None => logger.fatal(s"couldn't resolve $tuple") | ||
} | ||
|
||
val toModule = | ||
libModule.copy(fragments = libModule.fragments ++ name.split("/").toList.filterNot(_ == ".")) | ||
|
||
if (existing(toModule)) None | ||
else { | ||
logger.info(s"exposing module ${toModule.value} from ${fromModule.value}") | ||
Some(ProxyModule(FromExports, source.libName, fromModule, toModule)) | ||
} | ||
} | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
importer/src/test/scala/org/scalablytyped/converter/internal/importer/ExportsJsonTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package org.scalablytyped.converter.internal.importer | ||
|
||
import org.scalablytyped.converter.internal.Json | ||
import org.scalablytyped.converter.internal.ts.PackageJson | ||
import org.scalatest.funsuite.AnyFunSuite | ||
|
||
class ExportsJsonTest extends AnyFunSuite { | ||
test("no - 1") { | ||
val content = | ||
"""{"exports": [ | ||
| { | ||
| "default": "./index.js" | ||
| }, | ||
| "./index.js" | ||
| ] | ||
|}""".stripMargin | ||
val actual = Json.force[PackageJson](content).parsedExported | ||
|
||
assert(actual === None) | ||
} | ||
|
||
test("no - 2") { | ||
val content = | ||
"""{"exports": { | ||
| "import": "./build/lib/index.js", | ||
| "require": "./build/index.cjs" | ||
| } | ||
|}""".stripMargin | ||
val actual = Json.force[PackageJson](content).parsedExported | ||
|
||
assert(actual === None) | ||
} | ||
test("no - 3") { | ||
val content = | ||
"""{"exports": { | ||
| ".": [ | ||
| { | ||
| "import": "./build/lib/index.js", | ||
| "require": "./build/index.cjs" | ||
| }, | ||
| "./build/index.cjs" | ||
| ] | ||
| } | ||
|}""".stripMargin | ||
val actual = Json.force[PackageJson](content).parsedExported | ||
|
||
assert(actual === None) | ||
} | ||
test("no - 4") { | ||
val content = | ||
"""{"exports": { | ||
| ".": [ | ||
| { | ||
| "import": "./build/lib/index.js", | ||
| "require": "./build/index.cjs" | ||
| }, | ||
| "./build/index.cjs" | ||
| ], | ||
| "./browser": [ | ||
| "./browser.js" | ||
| ] | ||
| }}""".stripMargin | ||
val actual = Json.force[PackageJson](content).parsedExported | ||
|
||
assert(actual === None) | ||
} | ||
|
||
test("no - 5") { | ||
val content = | ||
"""{"exports": "picocolors.js"}""".stripMargin | ||
val actual = Json.force[PackageJson](content).parsedExported | ||
|
||
assert(actual === None) | ||
} | ||
|
||
test("yes - 5") { | ||
val content = | ||
"""{"exports": { | ||
| "./analytics": { | ||
| "types": "./analytics/dist/analytics/index.d.ts", | ||
| "node": { | ||
| "require": "./analytics/dist/index.cjs.js", | ||
| "import": "./analytics/dist/index.mjs" | ||
| }, | ||
| "default": "./analytics/dist/index.esm.js" | ||
| }, | ||
| "./app": { | ||
| "types": "./app/dist/app/index.d.ts", | ||
| "node": { | ||
| "require": "./app/dist/index.cjs.js", | ||
| "import": "./app/dist/index.mjs" | ||
| }, | ||
| "default": "./app/dist/index.esm.js" | ||
| }, | ||
| "./package.json": "./package.json" | ||
| }}""".stripMargin | ||
val actual = Json.force[PackageJson](content).parsedExported | ||
val expected = Some( | ||
Map( | ||
"./analytics" -> "./analytics/dist/analytics/index.d.ts", | ||
"./app" -> "./app/dist/app/index.d.ts", | ||
), | ||
) | ||
assert(actual === expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters