|
1 | 1 | package lib
|
2 | 2 |
|
3 |
| -import java.io.File |
| 3 | +import java.net.URI |
| 4 | +import java.nio.file.{FileSystems, Files, Paths} |
| 5 | +import java.util |
4 | 6 |
|
5 |
| -object WebpackBuildFile { |
6 |
| - private val d = new File("public/bundle") |
7 |
| - val jsBundle: String = if (d.exists && d.isDirectory) { |
8 |
| - d.listFiles.filter(_.isFile).toList.find(f => f.getName.contains("js.bundle.")).get.getName.replace(".gz", "") |
9 |
| - } else "" |
| 7 | +import com.typesafe.scalalogging.StrictLogging |
10 | 8 |
|
11 |
| - val cssBundle: String = if (d.exists && d.isDirectory) { |
12 |
| - d.listFiles.filter(_.isFile).toList.find(f => f.getName.contains("style.bundle.")).get.getName.replace(".gz", "") |
13 |
| - } else "" |
| 9 | +import scala.collection.JavaConverters._ |
| 10 | + |
| 11 | +object WebpackBuildFile extends StrictLogging { |
| 12 | + |
| 13 | + private val bundleDir = if (Files.exists(Paths.get("public", "bundle"))) { |
| 14 | + Paths.get("public", "bundle") |
| 15 | + } else { |
| 16 | + val uri: URI = getClass.getClassLoader.getResource("public/bundle").toURI |
| 17 | + logger.debug(s"Found bundle resources @ $uri") |
| 18 | + |
| 19 | + val uriParts: Array[String] = uri.toString.split("!") |
| 20 | + val fs = FileSystems.newFileSystem(URI.create(uriParts.head), new util.HashMap[String, Object]()) |
| 21 | + fs.getPath(uriParts(1)) |
| 22 | + } |
| 23 | + |
| 24 | + logger.info(s"Locating built bundle at ${bundleDir.toUri}") |
| 25 | + |
| 26 | + val jsBundleName: String = { |
| 27 | + val packedFile = Files |
| 28 | + .list(bundleDir) |
| 29 | + .iterator() |
| 30 | + .asScala |
| 31 | + .filter(Files.isRegularFile(_)) |
| 32 | + .toList |
| 33 | + .collectFirst { |
| 34 | + case f if f.toString.contains("js.bundle.") && !f.toString.contains("gz") => f |
| 35 | + } |
| 36 | + .getOrElse(sys.error("Could not locate JS bundle")) |
| 37 | + |
| 38 | + packedFile.getFileName.toString |
| 39 | + } |
| 40 | + |
| 41 | + val cssBundleName: String = { |
| 42 | + val packedFile = Files |
| 43 | + .list(bundleDir) |
| 44 | + .iterator() |
| 45 | + .asScala |
| 46 | + .filter(Files.isRegularFile(_)) |
| 47 | + .toList |
| 48 | + .collectFirst { |
| 49 | + case f if f.toString.contains("style.bundle.") && !f.toString.contains("gz") => f |
| 50 | + } |
| 51 | + .getOrElse(sys.error("Could not locate styles bundle")) |
| 52 | + |
| 53 | + packedFile.getFileName.toString |
| 54 | + } |
14 | 55 |
|
15 | 56 | }
|
0 commit comments