Skip to content

Commit

Permalink
Added NODE_OPTIONS for Vue.js Transpiling (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-leuthaeuser authored Nov 24, 2021
1 parent babe02b commit 80ba498
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ trait NpmEnvironment {
}
}

protected def nodeVersion(): Option[String] = {
logger.debug(s"\t+ Checking node ...")
ExternalCommand.run("node -v", projectPath.toString) match {
case Success(result) =>
logger.debug(s"\t+ node is available: $result")
Some(result)
case Failure(_) =>
logger.error("\t- node is not installed.")
None
}
}

protected def valid(): Boolean = isValid match {
case Some(value) =>
value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package io.shiftleft.js2cpg.preprocessing

import better.files.File
import io.shiftleft.js2cpg.core.Config
import io.shiftleft.js2cpg.io.FileDefaults.VUE_SUFFIX
import io.shiftleft.js2cpg.io.{ExternalCommand, FileUtils}
import io.shiftleft.js2cpg.io.ExternalCommand
import org.slf4j.LoggerFactory

import java.nio.file.{Path, Paths}
Expand All @@ -15,16 +14,27 @@ class VueTranspiler(override val config: Config, override val projectPath: Path)

private val logger = LoggerFactory.getLogger(getClass)

private lazy val NODE_OPTIONS: Map[String, String] = nodeOptions()

override def shouldRun(): Boolean = config.vueTranspiling && isVueProject

private def nodeOptions(): Map[String, String] = {
// TODO: keep this until https://github.com/webpack/webpack/issues/14532 is fixed
if (nodeVersion().exists(_.startsWith("v17"))) {
Map("NODE_OPTIONS" -> "--openssl-legacy-provider")
} else {
Map.empty
}
}

private def installVuePlugins(): Boolean = {
val command = if ((File(projectPath) / "yarn.lock").exists) {
s"yarn add @vue/cli-service-global --dev && ${NpmEnvironment.YARN_INSTALL}"
} else {
s"npm install --save-dev @vue/cli-service-global && ${NpmEnvironment.NPM_INSTALL}"
}
logger.debug(s"\t+ Installing Vue.js plugins ...")
ExternalCommand.run(command, projectPath.toString) match {
ExternalCommand.run(command, projectPath.toString, extraEnv = NODE_OPTIONS) match {
case Success(_) =>
logger.debug(s"\t+ Vue.js plugins installed")
true
Expand All @@ -39,7 +49,7 @@ class VueTranspiler(override val config: Config, override val projectPath: Path)
val vue = Paths.get(projectPath.toString, "node_modules", ".bin", "vue-cli-service")
val command = s"$vue build --dest $tmpTranspileDir --mode development --no-clean"
logger.debug(s"\t+ Vue.js transpiling $projectPath to $tmpTranspileDir")
ExternalCommand.run(command, projectPath.toString) match {
ExternalCommand.run(command, projectPath.toString, extraEnv = NODE_OPTIONS) match {
case Success(result) =>
logger.debug(s"\t+ Vue.js transpiling finished. $result")
case Failure(exception) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class TranspilationRunnerTest extends AnyWordSpec with Matchers {
val tmpProjectPath = File(projectPath).copyToDirectory(tmpDir)

val cpgPath = (tmpDir / "cpg.bin.zip").path.toString
Js2CpgMain.main(Array(tmpProjectPath.pathAsString, "--output", cpgPath, "--no-babel"))
Js2CpgMain.main(Array(tmpProjectPath.pathAsString, "--output", cpgPath))

val cpg =
CpgLoader
Expand Down

0 comments on commit 80ba498

Please sign in to comment.