Skip to content

Commit

Permalink
Fix handling of main entry point (ScalablyTyped#638)
Browse files Browse the repository at this point in the history
  • Loading branch information
steinybot committed Aug 13, 2024
1 parent 4db15e1 commit 48c4a8f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package importer

import com.olvind.logging.{Formatter, Logger}
import org.scalablytyped.converter.Selection
import org.scalablytyped.converter.internal.importer.Phase1ReadTypescript.fromFilesGlobEntry
import org.scalablytyped.converter.internal.importer.Phase1ReadTypescript.{packageJsonFiles, packageJsonMain}
import org.scalablytyped.converter.internal.maps._
import org.scalablytyped.converter.internal.phases.{GetDeps, IsCircular, Phase, PhaseRes}
import org.scalablytyped.converter.internal.ts.TsTreeScope.LoopDetector
Expand Down Expand Up @@ -58,10 +58,10 @@ class Phase1ReadTypescript(
f.shortenedFiles
case f: LibTsSource.FromFolder =>
/* There are often whole trees parallel to what is specified in `typings` (or similar). This ignores some of them. */
val filesTypings = fromFilesGlobEntry(f, f.packageJsonOpt.flatMap(_.files))
val bound = (f.shortenedFiles ++ filesTypings).map(_.folder)
val boundOrParent = if (bound.isEmpty) IArray(f.folder) else bound
boundOrParent.flatMap(PathsFromTsLibSource.filesFrom).distinct
val main = packageJsonMain(f)
val includedFiles = packageJsonFiles(f)
val bound = (f.shortenedFiles ++ main ++ includedFiles).map(_.folder)
bound.flatMap(PathsFromTsLibSource.filesFrom).distinct
}

val includedViaDirective = mutable.Set.empty[InFile]
Expand Down Expand Up @@ -345,8 +345,15 @@ object Phase1ReadTypescript {
).visitTsParsedFile(scope.caching),
)

private def fromFilesGlobEntry(fromFolder: LibTsSource.FromFolder, globs: Option[IArray[String]]): IArray[InFile] = {
private def packageJsonMain(fromFolder: LibTsSource.FromFolder): IArray[InFile] = {
val baseDirectory = fromFolder.folder.path
val main = os.RelPath(fromFolder.packageJsonOpt.flatMap(_.main).getOrElse("index.js"))
IArray(InFile(baseDirectory / main))
}

private def packageJsonFiles(fromFolder: LibTsSource.FromFolder): IArray[InFile] = {
val baseDirectory = fromFolder.folder.path
val globs = fromFolder.packageJsonOpt.flatMap(_.files)
val files = globs.fold(os.walk(baseDirectory))(GlobWalker.walkFiles(baseDirectory, _)).map(InFile(_))
IArray.fromTraversable(files)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ case class PackageJson(
peerDependencies: Option[Map[TsIdentLibrary, String]],
typings: Option[Json],
module: Option[Json],
main: Option[String],
types: Option[Json],
files: Option[IArray[String]],
dist: Option[PackageJson.Dist],
Expand Down Expand Up @@ -127,7 +128,7 @@ object PackageJson {
implicit val decodesDist: Decoder[Dist] = deriveDecoder
}

val Empty: PackageJson = PackageJson(None, None, None, None, None, None, None, None, None, None)
val Empty: PackageJson = PackageJson(None, None, None, None, None, None, None, None, None, None, None)

implicit val encodes: Encoder[PackageJson] = deriveEncoder
implicit val decodes: Decoder[PackageJson] = deriveDecoder
Expand Down

0 comments on commit 48c4a8f

Please sign in to comment.