Skip to content
This repository has been archived by the owner on May 25, 2022. It is now read-only.

Commit

Permalink
feat(sca): add normal npm decl support
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Apr 23, 2022
1 parent e3eceb6 commit e3b40db
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
4 changes: 4 additions & 0 deletions analyser_sca/build.gradle.kts
Expand Up @@ -11,6 +11,10 @@ dependencies {

implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.10")
implementation("org.jetbrains.kotlin:kotlin-reflect:1.6.10")

implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2")

implementation("com.jayway.jsonpath:json-path:2.7.0")
}

application {
Expand Down
@@ -0,0 +1,32 @@
package org.archguard.analyser.sca.parser

import com.jayway.jsonpath.JsonPath
import org.archguard.analyser.sca.model.DeclFileTree
import org.archguard.analyser.sca.model.DepDeclaration
import org.archguard.analyser.sca.model.DepDependency

class NpmParser : Parser() {
override fun lookupSource(file: DeclFileTree): List<DepDeclaration> {
val name: String = JsonPath.read(file.content, "$.name")
val version: String = JsonPath.read(file.content, "$.version")
val depMap: Map<String, String> = JsonPath.read(file.content, "$.dependencies")

return listOf(DepDeclaration(
name,
version,
"npm",
this.createDependencies(depMap)
))
}

private fun createDependencies(depMap: Map<String, String>): List<DepDependency> {
return depMap.map {
DepDependency(
name = it.key,
group = "",
artifact = it.key,
version = it.value
)
}.toList()
}
}

This file was deleted.

@@ -0,0 +1,10 @@
package org.archguard.analyser.sca

import org.junit.jupiter.api.Test

internal class JavaScriptFinderTest {
@Test
internal fun should_handle_json() {

}
}
@@ -0,0 +1,28 @@
package org.archguard.analyser.sca.parser

import org.archguard.analyser.sca.model.DeclFileTree
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals

internal class NpmParserTest {
private val samplePackageJson = """{
"name": "my_package",
"version": "1.0.0",
"dependencies": {
"my_dep": "^1.0.0",
"another_dep": "~2.2.0"
}
}
""".trimIndent()

@Test
fun first_dep() {
val declFileTree = DeclFileTree("package.json", "package.json", samplePackageJson)
val declTree = NpmParser().lookupSource(declFileTree)

assertEquals(1, declTree.size)
assertEquals("my_package", declTree[0].name)
assertEquals("1.0.0", declTree[0].version)
assertEquals(2, declTree[0].dependencies.size)
}
}

0 comments on commit e3b40db

Please sign in to comment.