All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.shiftleft.js2cpg.preprocessing.TranspilerGroup.scala Maven / Gradle / Ivy

There is a newer version: 0.3.3
Show newest version
package io.shiftleft.js2cpg.preprocessing

import better.files.File
import io.shiftleft.js2cpg.core.Config
import io.shiftleft.js2cpg.io.ExternalCommand
import org.slf4j.LoggerFactory

import java.nio.file.Path
import scala.util.{Failure, Success}

case class TranspilerGroup(override val config: Config, override val projectPath: Path, transpilers: Seq[Transpiler])
    extends Transpiler {

  private val logger = LoggerFactory.getLogger(getClass)

  private val BABEL_PLUGINS: String = Versions.babelVersions.keySet.map(Versions.nameAndVersion).mkString(" ")

  private def installPlugins(): Boolean = {
    val command = if (pnpmAvailable(projectPath)) {
      s"${TranspilingEnvironment.PNPM_ADD} $BABEL_PLUGINS && ${TranspilingEnvironment.PNPM_INSTALL}"
    } else if (yarnAvailable()) {
      s"${TranspilingEnvironment.YARN_ADD} $BABEL_PLUGINS && ${TranspilingEnvironment.YARN_INSTALL}"
    } else {
      s"${TranspilingEnvironment.NPM_INSTALL} $BABEL_PLUGINS"
    }
    logger.info("Installing project dependencies and plugins. That will take a while.")
    logger.debug(s"\t+ Installing plugins with command '$command' in path '$projectPath'")
    ExternalCommand.run(command, projectPath.toString, extraEnv = NODE_OPTIONS) match {
      case Success(_) =>
        logger.info("\t+ Plugins installed")
        true
      case Failure(exception) =>
        logger.error("\t- Failed to install plugins", exception)
        false
    }
  }

  override def shouldRun(): Boolean = transpilers.exists(_.shouldRun())

  override protected def transpile(tmpTranspileDir: Path): Boolean = {
    if (installPlugins()) {
      transpilers.takeWhile(_.run(tmpTranspileDir)).length == transpilers.length
    } else {
      true
    }
  }

  override def validEnvironment(): Boolean = transpilers.forall(_.validEnvironment())

  override protected def logExecution(): Unit = {
    logger.info(s"Downloading / installing plugins in '${File(projectPath).name}'")
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy