Skip to content

Commit

Permalink
Clean-up for logging / log levels (#48)
Browse files Browse the repository at this point in the history
Also removed some string interpolation where not required.
  • Loading branch information
max-leuthaeuser authored Dec 14, 2021
1 parent 189f607 commit 486ac51
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 72 deletions.
35 changes: 17 additions & 18 deletions src/main/scala/io/shiftleft/js2cpg/core/Js2cpgArgumentsParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Js2cpgArgumentsParser {
import Js2cpgArgumentsParser._

private lazy val banner: String =
s"""
"""
| ██╗███████╗██████╗ ██████╗██████╗ ██████╗
| ██║██╔════╝╚════██╗██╔════╝██╔══██╗██╔════╝
| ██║███████╗ █████╔╝██║ ██████╔╝██║ ███╗
Expand Down Expand Up @@ -89,31 +89,31 @@ class Js2cpgArgumentsParser {
failure("Directory of the output file does not exist")
} else success)
opt[Unit](NO_TS)
.text(s"disables transpiling Typescript files to Javascript")
.text("disables transpiling Typescript files to Javascript")
.action((_, c) => c.copy(tsTranspiling = false))
opt[Unit](NO_BABEL)
.text(s"disables transpiling Javascript files with Babel")
.text("disables transpiling Javascript files with Babel")
.action((_, c) => c.copy(babelTranspiling = false))
opt[Unit](NO_VUE)
.text(s"disables transpiling Vue.js files")
.text("disables transpiling Vue.js files")
.action((_, c) => c.copy(vueTranspiling = false))
opt[Unit](NO_NUXT)
.text(s"disables Nuxt.js transpiling")
.text("disables Nuxt.js transpiling")
.action((_, c) => c.copy(nuxtTranspiling = false))
opt[Unit](NO_TEMPLATES)
.text(s"disables transpiling EJS or Pug template files")
.text("disables transpiling EJS or Pug template files")
.action((_, c) => c.copy(templateTranspiling = false))
// for backwards compatibility - has no effect:
opt[Unit](TRANSPILING)
.text(s"enables transpiling Typescript files to Javascript")
.text("enables transpiling Typescript files to Javascript")
.hidden() // deprecated
// for backwards compatibility - has no effect:
opt[Unit](BABEL)
.text(s"enables transpiling Javascript files with Babel")
.text("enables transpiling Javascript files with Babel")
.hidden()
// for backwards compatibility - has no effect:
opt[Unit](TS)
.text(s"enables transpiling Typescript files to Javascript")
.text("enables transpiling Typescript files to Javascript")
.hidden()
opt[Unit](WITH_TS_TYPES)
.text(
Expand All @@ -130,24 +130,23 @@ class Js2cpgArgumentsParser {
// for backwards compatibility - has no effect:
opt[Unit](IGNORE_MINIFIED)
.text(
s"ignore minified Javascript files (filename ending with '-min.js', '.min.js', or 'bundle.js')")
"ignore minified Javascript files (filename ending with '-min.js', '.min.js', or 'bundle.js')")
.hidden() // deprecated
opt[Unit](WITH_MINIFIED)
.action((_, c) => c.copy(ignoreMinified = false))
.hidden() // deprecated
.text(
s"include minified Javascript files (filename ending with '-min.js', '.min.js', or 'bundle.js')")
.text("include minified Javascript files (filename ending with '-min.js', '.min.js', or 'bundle.js')")
opt[Unit](INCLUDE_MINIFIED)
.action((_, c) => c.copy(ignoreMinified = false))
.text(
s"include minified Javascript files (filename ending with '-min.js', '.min.js', or 'bundle.js')")
"include minified Javascript files (filename ending with '-min.js', '.min.js', or 'bundle.js')")
opt[Unit](WITH_TESTS)
.action((_, c) => c.copy(ignoreTests = false))
.hidden() // deprecated
.text(s"include test files")
.text("include test files")
opt[Unit](INCLUDE_TESTS)
.action((_, c) => c.copy(ignoreTests = false))
.text(s"include test files")
.text("include test files")
opt[Unit](IGNORE_PRIVATE_DEPS)
.text(
s"ignores private modules/dependencies in 'node_modules/' (defaults to `${Config.DEFAULT_IGNORE_PRIVATE_DEPS}`)")
Expand All @@ -163,13 +162,13 @@ class Js2cpgArgumentsParser {
.text(
s"additional private dependencies to be analyzed from '${FileDefaults.NODE_MODULES_DIR_NAME}'")
opt[Unit](INCLUDE_CONFIGS)
.text(s"include configuration files (*.conf.js, *.config.js, *.json)")
.text("include configuration files (*.conf.js, *.config.js, *.json)")
.action((_, c) => c.copy(includeConfigs = true))
opt[Unit](INCLUDE_HTML)
.text(s"include HTML files (*.html)")
.text("include HTML files (*.html)")
.action((_, c) => c.copy(includeHtml = true))
opt[Int](JVM_MONITOR)
.text(s"enable JVM metrics logging (requires JMX port number)")
.text("enable JVM metrics logging (requires JMX port number)")
.action((jmxPortNumber, c) => c.copy(jvmMetrics = Some(jmxPortNumber)))
.hidden()
opt[String](MODULE_MODE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ object Scope {
case methodScopeElement: MethodScopeElement =>
methodScopeElement.scopeNode
}
.getOrElse(throw new RuntimeException(s"Cannot find method scope."))
.getOrElse(throw new RuntimeException("Cannot find method scope."))
// There are no references outside of methods. Meaning we always find a MethodScope here.
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class BuiltinTypesPass(cpg: Cpg, keyPool: KeyPool) extends CpgPass(cpg, keyPool
private val logger = LoggerFactory.getLogger(getClass)

override def run(): Iterator[DiffGraph] = {
logger.debug(s"Generating builtin types.")
logger.debug("Generating builtin types.")

val diffGraph = DiffGraph.newBuilder

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class JsMetaDataPass(cpg: Cpg, keyPool: KeyPool, hash: String)
private val logger = LoggerFactory.getLogger(getClass)

override def run(): Iterator[DiffGraph] = {
logger.debug(s"Generating meta-data.")
logger.debug("Generating meta-data.")

val diffGraph = DiffGraph.newBuilder
val metaNode = NewMetaData().language(Languages.JAVASCRIPT).hash(hash)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1727,7 +1727,7 @@ class AstCreator(diffGraph: DiffGraph.Builder, source: JsSource, usedIdentNodes:

val callId = astNodeBuilder.createCallNode(
s"__Runtime.TO_STRING(${args.mkString(",")})",
s"__Runtime.TO_STRING",
"__Runtime.TO_STRING",
DispatchTypes.STATIC_DISPATCH,
astNodeBuilder.lineAndColumn(templateLiteralNode)
)
Expand Down
19 changes: 4 additions & 15 deletions src/main/scala/io/shiftleft/js2cpg/io/ExternalCommand.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,10 @@ object ExternalCommand {

def run(command: String,
inDir: String = ".",
extraEnv: Map[String, String] = Map.empty,
debugLogging: Boolean = false): Try[String] = {
val result = mutable.ArrayBuffer.empty[String]
val lineHandler: String => Unit = line => {
if (debugLogging) {
logger.debug(s"\t\t$line")
}
result.addOne(line)
}

val systemString = System.getProperty(osNameProperty)
extraEnv: Map[String, String] = Map.empty): Try[String] = {
val result = mutable.ArrayBuffer.empty[String]
val lineHandler: String => Unit = line => result.addOne(line)
val systemString = System.getProperty(osNameProperty)
val shellPrefix =
if (systemString != null && systemString.startsWith(windowsSystemPrefix)) {
"cmd" :: "/c" :: Nil
Expand All @@ -37,10 +30,6 @@ object ExternalCommand {
.!(ProcessLogger(lineHandler, lineHandler)) match {
case 0 =>
Success(result.mkString(System.lineSeparator()))
case code if debugLogging =>
logger.debug(
s"\t- '$command' failed with exit code '$code': ${result.mkString(System.lineSeparator())}")
Failure(new RuntimeException(result.mkString(System.lineSeparator())))
case _ =>
Failure(new RuntimeException(result.mkString(System.lineSeparator())))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ class BabelTranspiler(override val config: Config,
s"--out-dir $outDir $constructIgnoreDirArgs"
logger.debug(s"\t+ Babel transpiling $projectPath to $outDir")
ExternalCommand.run(command, in.toString) match {
case Success(result) =>
logger.debug(s"\t+ Babel transpiling finished. $result")
case Failure(exception) =>
logger.debug(s"\t- Babel transpiling failed: ${exception.getMessage}")
case Success(_) => logger.debug("\t+ Babel transpiling finished")
case Failure(exception) => logger.debug("\t- Babel transpiling failed", exception)
}
true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class EjsTranspiler(override val config: Config, override val projectPath: Path)
sourceMapFile.writeText(sourceMap.generate())
} match {
case Failure(exception) =>
logger.debug(s"\t- could not transpile EJS template '$ejsFileName'. $exception")
logger.debug(s"\t- could not transpile EJS template '$ejsFileName'", exception)
case Success(_) =>
logger.debug(s"\t+ transpiled EJS template '$ejsFileName' to '$transpiledFile'")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ class NuxtTranspiler(override val config: Config, override val projectPath: Path
val command = s"$nuxt --force-exit"
logger.debug(s"\t+ Nuxt.js transpiling $projectPath")
ExternalCommand.run(command, projectPath.toString) match {
case Success(result) =>
logger.debug(s"\t+ Nuxt.js transpiling finished. $result")
case Success(_) =>
logger.debug("\t+ Nuxt.js transpiling finished")
new BabelTranspiler(config, projectPath, inDir = Some(Paths.get(NUXT_FOLDER)))
.run(projectPath.resolve(NUXT_FOLDER))
wasExecuted = true
case Failure(exception) =>
logger.debug(s"\t- Nuxt.js transpiling failed: ${exception.getMessage}")
logger.debug("\t- Nuxt.js transpiling failed", exception)
}
// we never want other transpilers down the chain to be executed
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ class PugTranspiler(override val config: Config, override val projectPath: Path)

private def installPugPlugins(): Boolean = {
val command = if (yarnAvailable()) {
s"yarn add pug-cli --dev && ${TranspilingEnvironment.YARN_INSTALL}"
s"yarn add pug-cli --dev --legacy-peer-deps && ${TranspilingEnvironment.YARN_INSTALL}"
} else {
s"npm install --save-dev pug-cli && ${TranspilingEnvironment.NPM_INSTALL}"
s"npm install --save-dev pug-cli --legacy-peer-deps && ${TranspilingEnvironment.NPM_INSTALL}"
}
logger.debug(s"\t+ Installing Pug plugins ...")
logger.info("Installing Pug dependencies and plugins. That will take a while.")
logger.debug(s"\t+ Installing Pug plugins with command '$command' in path '$projectPath'")
ExternalCommand.run(command, projectPath.toString) match {
case Success(_) =>
logger.debug(s"\t+ Pug plugins installed")
logger.info("\t+ Pug plugins installed")
true
case Failure(exception) =>
logger.debug(s"\t- Failed to install Pug plugins: ${exception.getMessage}")
logger.error("\t- Failed to install Pug plugins", exception)
false
}
}
Expand All @@ -42,10 +43,10 @@ class PugTranspiler(override val config: Config, override val projectPath: Path)
val command = s"$pug --client --no-debug --out $tmpTranspileDir ."
logger.debug(s"\t+ transpiling Pug templates in $projectPath to $tmpTranspileDir")
ExternalCommand.run(command, projectPath.toString) match {
case Success(result) =>
logger.debug(s"\t+ transpiling Pug templates finished. $result")
case Success(_) =>
logger.debug("\t+ transpiling Pug templates finished")
case Failure(exception) =>
logger.debug(s"\t- transpiling Pug templates failed: ${exception.getMessage}")
logger.debug("\t- transpiling Pug templates failed", exception)
}
}
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ case class TranspilerGroup(override val config: Config,
logger.info("\t+ Plugins installed")
true
case Failure(exception) =>
logger.error(s"\t- Failed to install plugins: ${exception.getMessage}")
logger.error("\t- Failed to install plugins", exception)
false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ object TranspilingEnvironment {
private var isNpmAvailable: Option[Boolean] = None

val YARN_INSTALL = "yarn install --prefer-offline --ignore-scripts --legacy-peer-deps"
val NPM_INSTALL = "npm install --prefer-offline --no-audit --progress=false --ignore-scripts --legacy-peer-deps"
val NPM_INSTALL =
"npm install --prefer-offline --no-audit --progress=false --ignore-scripts --legacy-peer-deps"
}

trait TranspilingEnvironment {
Expand Down Expand Up @@ -49,13 +50,13 @@ trait TranspilingEnvironment {
}

private def setNpmPython(): Boolean = {
logger.debug(s"\t+ Setting npm config ...")
logger.debug("\t+ Setting npm config ...")
ExternalCommand.run("npm config set python python2.7", projectPath.toString) match {
case Success(_) =>
logger.debug(s"\t+ Set successfully")
logger.debug("\t+ Set successfully")
true
case Failure(exception) =>
logger.debug(s"\t- Failed setting npm config: ${exception.getMessage}")
logger.debug("\t- Failed setting npm config", exception)
false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class TypescriptTranspiler(override val config: Config,
// and that we sadly cannot override with tsc directly:
createCustomTsConfigFile() match {
case Failure(f) =>
logger.debug(s"\t- Creating a custom TS config failed: ${f.getMessage}")
logger.debug("\t- Creating a custom TS config failed", f)
(None, "")
case Success(customTsConfigFile) =>
(Some(customTsConfigFile), s"--project $customTsConfigFile")
Expand All @@ -130,10 +130,8 @@ class TypescriptTranspiler(override val config: Config,
s"\t+ TypeScript compiling $projectPath $projCommand to $projOutDir (using $module style modules)")

ExternalCommand.run(command, projectPath.toString, extraEnv = NODE_OPTIONS) match {
case Success(result) =>
logger.debug(s"\t+ TypeScript compiling finished. $result")
case Failure(exception) =>
logger.debug(s"\t- TypeScript compiling failed: ${exception.getMessage}")
case Success(_) => logger.debug("\t+ TypeScript compiling finished")
case Failure(exception) => logger.debug("\t- TypeScript compiling failed", exception)
}
customTsConfigFile.foreach(_.delete(swallowIOExceptions = true))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,18 @@ class VueTranspiler(override val config: Config, override val projectPath: Path)

private def installVuePlugins(): Boolean = {
val command = if (yarnAvailable()) {
s"yarn add @vue/cli-service-global --dev && ${TranspilingEnvironment.YARN_INSTALL}"
s"yarn add @vue/cli-service-global --dev --legacy-peer-deps && ${TranspilingEnvironment.YARN_INSTALL}"
} else {
s"npm install --save-dev @vue/cli-service-global && ${TranspilingEnvironment.NPM_INSTALL}"
s"npm install --save-dev @vue/cli-service-global --legacy-peer-deps && ${TranspilingEnvironment.NPM_INSTALL}"
}
logger.debug(s"\t+ Installing Vue.js plugins ...")
logger.info("Installing Vue.js dependencies and plugins. That will take a while.")
logger.debug(s"\t+ Installing Vue.js plugins with command '$command' in path '$projectPath'")
ExternalCommand.run(command, projectPath.toString, extraEnv = NODE_OPTIONS) match {
case Success(_) =>
logger.debug(s"\t+ Vue.js plugins installed")
logger.info("\t+ Vue.js plugins installed")
true
case Failure(exception) =>
logger.debug(s"\t- Failed to install Vue.js plugins: ${exception.getMessage}")
logger.error("\t- Failed to install Vue.js plugins", exception)
false
}
}
Expand All @@ -68,10 +69,8 @@ class VueTranspiler(override val config: Config, override val projectPath: Path)
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, extraEnv = NODE_OPTIONS) match {
case Success(result) =>
logger.debug(s"\t+ Vue.js transpiling finished. $result")
case Failure(exception) =>
logger.debug(s"\t- Vue.js transpiling failed: ${exception.getMessage}")
case Success(_) => logger.debug("\t+ Vue.js transpiling finished")
case Failure(exception) => logger.debug("\t- Vue.js transpiling failed", exception)
}
}
true
Expand Down

0 comments on commit 486ac51

Please sign in to comment.