MasseGuillaume / ScalaKata2

Interactive Playground
https://scastie.scala-lang.org/
98 stars 13 forks source link

source map missing #11

Closed MasseGuillaume closed 8 years ago

MasseGuillaume commented 8 years ago
import java.io.File
def renameSourceMap(inputFile: File, outputPath: File): Seq[(File, File)] = {
  import spray.json._
  import java.nio.file._
  import java.nio.charset.StandardCharsets
  import scala.collection.JavaConverters._

  val inFile = inputFile.getAbsolutePath
  val inFileMap = inFile + ".map"

  val retFile = outputPath.getAbsolutePath + "/" + clientJs
  val retFileMap = retFile + ".map"

  val input = Files.readAllLines(Paths.get(inFileMap)).toArray.mkString("")
  val output = input.stripMargin.parseJson match {
     case JsObject(ks) => JsObject(ks.updated("name", JsString(clientJs))).prettyPrint
     case _ => throw new Exception("invalid source map")
  }
  Files.write(Paths.get(retFileMap), output.getBytes(StandardCharsets.UTF_8))

  val jsFile = Files.readAllLines(Paths.get(inFile), StandardCharsets.UTF_8).asScala
  val nl = System.lineSeparator
  val index = jsFile.indexWhere(_.startsWith("//# sourceMappingURL="))
  val newJsFile = 
    jsFile.slice(0, index).mkString(nl) + nl +
    "//# sourceMappingURL=" + clientJs + nl +
    jsFile.slice(index + 1, jsFile.length).mkString(nl)

  Files.write(Paths.get(retFile), newJsFile.getBytes(StandardCharsets.UTF_8))

  Seq(
    file(inFile) -> file(inFileMap),
    file(retFile) -> file(retFileMap)
  )
}