Skip to content

Commit

Permalink
Fix for pnpm with sub-projects (#161)
Browse files Browse the repository at this point in the history
pnpm sub-projects may be not a pnpm project themself. Hence, we have to check for a pnpm-lock.yaml file there again.

This is for: ShiftLeftSecurity/product#10353
  • Loading branch information
max-leuthaeuser authored May 3, 2022
1 parent 59f8600 commit b3b618c
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class BabelTranspiler(
true
}

override def validEnvironment(): Boolean = valid()
override def validEnvironment(): Boolean = valid(inDir.map(dir => projectPath.resolve(dir)).getOrElse(projectPath))

override protected def logExecution(): Unit =
logger.info(s"Babel - transpiling source files in '${File(projectPath).name}'")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class NuxtTranspiler(override val config: Config, override val projectPath: Path
false
}

override def validEnvironment(): Boolean = valid()
override def validEnvironment(): Boolean = valid(projectPath)

override protected def logExecution(): Unit =
logger.info(s"Nuxt.js - transpiling source files in '${File(projectPath).name}'")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PugTranspiler(override val config: Config, override val projectPath: Path)
override def shouldRun(): Boolean = config.templateTranspiling && hasPugFiles

private def installPugPlugins(): Boolean = {
val command = if (pnpmAvailable()) {
val command = if (pnpmAvailable(projectPath)) {
s"${TranspilingEnvironment.PNPM_ADD} pug-cli && ${TranspilingEnvironment.PNPM_INSTALL}"
} else if (yarnAvailable()) {
s"${TranspilingEnvironment.YARN_ADD} pug-cli && ${TranspilingEnvironment.YARN_INSTALL}"
Expand Down Expand Up @@ -54,7 +54,7 @@ class PugTranspiler(override val config: Config, override val projectPath: Path)
true
}

override def validEnvironment(): Boolean = valid()
override def validEnvironment(): Boolean = valid(projectPath)

override protected def logExecution(): Unit =
logger.info(s"PUG - transpiling source files in '${File(projectPath).name}'")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
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.FileUtils
import io.shiftleft.js2cpg.parser.PackageJsonParser

import java.nio.file.Path

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ case class TranspilerGroup(override val config: Config, override val projectPath
"@babel/plugin-transform-property-mutators"

private def installPlugins(): Boolean = {
val command = if (pnpmAvailable()) {
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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import better.files.File
import io.shiftleft.js2cpg.io.ExternalCommand
import org.slf4j.LoggerFactory

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

object TranspilingEnvironment {
Expand Down Expand Up @@ -97,17 +98,17 @@ trait TranspilingEnvironment {
}
}

protected def valid(): Boolean = isValid match {
protected def valid(dir: Path): Boolean = isValid match {
case Some(value) =>
value
case None =>
isValid = Some((pnpmAvailable() || yarnAvailable() || npmAvailable()) && setNpmPython())
isValid = Some((pnpmAvailable(dir) || yarnAvailable() || npmAvailable()) && setNpmPython())
isValid.get
}

protected def pnpmAvailable(): Boolean = isPnpmAvailable match {
protected def pnpmAvailable(dir: Path): Boolean = isPnpmAvailable match {
case Some(value) =>
value
value && (File(dir) / "pnpm-lock.yaml").exists
case None =>
isPnpmAvailable = Some((File(projectPath) / "pnpm-lock.yaml").exists && checkForPnpm())
isPnpmAvailable.get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class TypescriptTranspiler(override val config: Config, override val projectPath
}

private def installTsPlugins(): Boolean = {
val command = if (pnpmAvailable()) {
val command = if (pnpmAvailable(projectPath)) {
s"${TranspilingEnvironment.PNPM_ADD} typescript"
} else if (yarnAvailable()) {
s"${TranspilingEnvironment.YARN_ADD} typescript"
Expand Down Expand Up @@ -166,7 +166,7 @@ class TypescriptTranspiler(override val config: Config, override val projectPath
true
}

override def validEnvironment(): Boolean = valid()
override def validEnvironment(): Boolean = valid(projectPath)

override protected def logExecution(): Unit =
logger.info(s"TypeScript - transpiling source files in '${File(projectPath).name}'")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class VueTranspiler(override val config: Config, override val projectPath: Path)
}

private def installVuePlugins(): Boolean = {
val command = if (pnpmAvailable()) {
val command = if (pnpmAvailable(projectPath)) {
s"${TranspilingEnvironment.PNPM_ADD} @vue/cli-service-global && ${TranspilingEnvironment.PNPM_INSTALL}"
} else if (yarnAvailable()) {
s"${TranspilingEnvironment.YARN_ADD} @vue/cli-service-global && ${TranspilingEnvironment.YARN_INSTALL}"
Expand Down Expand Up @@ -90,7 +90,7 @@ class VueTranspiler(override val config: Config, override val projectPath: Path)
true
}

override def validEnvironment(): Boolean = valid()
override def validEnvironment(): Boolean = valid(projectPath)

override protected def logExecution(): Unit =
logger.info(s"Vue.js - transpiling source files in '${File(projectPath).name}'")
Expand Down

0 comments on commit b3b618c

Please sign in to comment.