Search in sources :

Example 1 with NpmPackagerResult

use of com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult in project synopsys-detect by blackducksoftware.

the class NpmLockfilePackager method parseAndTransform.

public NpmPackagerResult parseAndTransform(@Nullable String packageJsonText, String lockFileText, List<NameVersion> externalDependencies) {
    PackageJson packageJson = Optional.ofNullable(packageJsonText).map(content -> gson.fromJson(content, PackageJson.class)).orElse(null);
    PackageLock packageLock = gson.fromJson(lockFileText, PackageLock.class);
    NpmDependencyConverter dependencyConverter = new NpmDependencyConverter(externalIdFactory);
    NpmProject project = dependencyConverter.convertLockFile(packageLock, packageJson);
    DependencyGraph dependencyGraph = graphTransformer.transform(packageLock, project, externalDependencies);
    ExternalId projectId = projectIdTransformer.transform(packageJson, packageLock);
    CodeLocation codeLocation = new CodeLocation(dependencyGraph, projectId);
    return new NpmPackagerResult(projectId.getName(), projectId.getVersion(), codeLocation);
}
Also used : DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) ExternalIdFactory(com.synopsys.integration.bdio.model.externalid.ExternalIdFactory) ArrayList(java.util.ArrayList) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) Nullable(org.jetbrains.annotations.Nullable) NameVersion(com.synopsys.integration.util.NameVersion) List(java.util.List) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) NpmDependencyConverter(com.synopsys.integration.detectable.detectables.npm.lockfile.NpmDependencyConverter) Gson(com.google.gson.Gson) NpmProject(com.synopsys.integration.detectable.detectables.npm.lockfile.model.NpmProject) NpmPackagerResult(com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult) Optional(java.util.Optional) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) PackageLock(com.synopsys.integration.detectable.detectables.npm.lockfile.model.PackageLock) PackageLock(com.synopsys.integration.detectable.detectables.npm.lockfile.model.PackageLock) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) NpmProject(com.synopsys.integration.detectable.detectables.npm.lockfile.model.NpmProject) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) NpmDependencyConverter(com.synopsys.integration.detectable.detectables.npm.lockfile.NpmDependencyConverter) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) NpmPackagerResult(com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult)

Example 2 with NpmPackagerResult

use of com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult in project synopsys-detect by blackducksoftware.

the class NpmCliParser method convertNpmJsonFileToCodeLocation.

public NpmPackagerResult convertNpmJsonFileToCodeLocation(String npmLsOutput, PackageJson packageJson) {
    JsonObject npmJson = JsonParser.parseString(npmLsOutput).getAsJsonObject();
    DependencyGraph graph = new BasicDependencyGraph();
    JsonElement projectNameElement = npmJson.getAsJsonPrimitive(JSON_NAME);
    JsonElement projectVersionElement = npmJson.getAsJsonPrimitive(JSON_VERSION);
    String projectName = null;
    String projectVersion = null;
    if (projectNameElement != null) {
        projectName = projectNameElement.getAsString();
    }
    if (projectVersionElement != null) {
        projectVersion = projectVersionElement.getAsString();
    }
    populateChildren(graph, null, npmJson.getAsJsonObject(JSON_DEPENDENCIES), true, packageJson);
    ExternalId externalId = externalIdFactory.createNameVersionExternalId(Forge.NPMJS, projectName, projectVersion);
    CodeLocation codeLocation = new CodeLocation(graph, externalId);
    return new NpmPackagerResult(projectName, projectVersion, codeLocation);
}
Also used : CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) JsonElement(com.google.gson.JsonElement) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) JsonObject(com.google.gson.JsonObject) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) BasicDependencyGraph(com.synopsys.integration.bdio.graph.BasicDependencyGraph) BasicDependencyGraph(com.synopsys.integration.bdio.graph.BasicDependencyGraph) NpmPackagerResult(com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult)

Example 3 with NpmPackagerResult

use of com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult in project synopsys-detect by blackducksoftware.

the class NpmLockfileExtractor method extract.

/*
    packageJson is optional
     */
public Extraction extract(File lockfile, File packageJson) {
    try {
        String lockText = FileUtils.readFileToString(lockfile, StandardCharsets.UTF_8);
        String packageText = null;
        if (packageJson != null) {
            packageText = FileUtils.readFileToString(packageJson, StandardCharsets.UTF_8);
        }
        NpmPackagerResult result = npmLockfilePackager.parseAndTransform(packageText, lockText);
        return new Extraction.Builder().success(result.getCodeLocation()).projectName(result.getProjectName()).projectVersion(result.getProjectVersion()).build();
    } catch (IOException e) {
        return new Extraction.Builder().exception(e).build();
    }
}
Also used : Extraction(com.synopsys.integration.detectable.extraction.Extraction) IOException(java.io.IOException) NpmPackagerResult(com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult)

Example 4 with NpmPackagerResult

use of com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult in project synopsys-detect by blackducksoftware.

the class NpmOutputParserTest method npmCliDependencyFinder.

@Test
public void npmCliDependencyFinder() {
    NpmCliParser parser = new NpmCliParser(new ExternalIdFactory(), EnumListFilter.excludeNone());
    String testIn = String.join(System.lineSeparator(), Arrays.asList("{", "   \"name\": \"node-js\",", "   \"version\": \"0.2.0\",", "   \"dependencies\": {", "       \"upper-case\": {", "       \"version\": \"1.1.3\",", "       \"from\": \"upper-case@latest\",", "       \"resolved\": \"https://registry.npmjs.org/upper-case/-/upper-case-1.1.3.tgz\"", "       },", "       \"xml2js\": {", "           \"version\": \"0.4.17\",", "           \"from\": \"xml2js@latest\",", "           \"resolved\": \"https://registry.npmjs.org/xml2js/-/xml2js-0.4.17.tgz\",", "            \"dependencies\": {", "               \"sax\": {", "                   \"version\": \"1.2.2\",", "                   \"from\": \"sax@>=0.6.0\",", "                   \"resolved\": \"https://registry.npmjs.org/sax/-/sax-1.2.2.tgz\"", "               },", "               \"xmlbuilder\": {", "                   \"version\": \"4.2.1\",", "                   \"from\": \"xmlbuilder@>=4.1.0 <5.0.0\",", "                   \"resolved\": \"https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-4.2.1.tgz\",", "                   \"dependencies\": {", "                       \"lodash\": {", "                           \"version\": \"4.17.4\",", "                           \"from\": \"lodash@>=4.0.0 <5.0.0\",", "                           \"resolved\": \"https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz\"", "                       }", "                   }", "               }", "           }", "       }", "   }", "}"));
    NpmDependencyTypeFilter npmDependencyTypeFilter = new NpmDependencyTypeFilter(Collections.emptySet(), Collections.emptySet(), true, true);
    PackageJson packageJson = new PackageJson();
    NpmPackagerResult result = parser.convertNpmJsonFileToCodeLocation(testIn, packageJson);
    Assertions.assertEquals("node-js", result.getProjectName());
    Assertions.assertEquals("0.2.0", result.getProjectVersion());
    NameVersionGraphAssert graphAssert = new NameVersionGraphAssert(Forge.NPMJS, result.getCodeLocation().getDependencyGraph());
    graphAssert.hasRootSize(2);
    graphAssert.hasRootDependency("xml2js", "0.4.17");
    graphAssert.hasRootDependency("upper-case", "1.1.3");
    graphAssert.hasParentChildRelationship("xml2js", "0.4.17", "xmlbuilder", "4.2.1");
    graphAssert.hasParentChildRelationship("xml2js", "0.4.17", "sax", "1.2.2");
    graphAssert.hasParentChildRelationship("xmlbuilder", "4.2.1", "lodash", "4.17.4");
}
Also used : ExternalIdFactory(com.synopsys.integration.bdio.model.externalid.ExternalIdFactory) NpmDependencyTypeFilter(com.synopsys.integration.detectable.detectables.npm.cli.parse.NpmDependencyTypeFilter) NameVersionGraphAssert(com.synopsys.integration.detectable.util.graph.NameVersionGraphAssert) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) NpmPackagerResult(com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult) NpmCliParser(com.synopsys.integration.detectable.detectables.npm.cli.parse.NpmCliParser) Test(org.junit.jupiter.api.Test)

Example 5 with NpmPackagerResult

use of com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult in project synopsys-detect by blackducksoftware.

the class NpmPeerExclusionTest method testPeerDependencyNotExists.

@Test
public void testPeerDependencyNotExists() {
    NpmPackagerResult result = createPackager(NpmDependencyType.DEV, NpmDependencyType.PEER).parseAndTransform(packageJsonText, packageLockText);
    GraphAssert graphAssert = new GraphAssert(Forge.NPMJS, result.getCodeLocation().getDependencyGraph());
    graphAssert.hasNoDependency(childPeer);
    graphAssert.hasNoDependency(parentPeer);
    graphAssert.hasRootSize(0);
}
Also used : GraphAssert(com.synopsys.integration.detectable.util.graph.GraphAssert) NpmPackagerResult(com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult) Test(org.junit.jupiter.api.Test) FunctionalTest(com.synopsys.integration.detectable.annotations.FunctionalTest)

Aggregations

NpmPackagerResult (com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult)9 Test (org.junit.jupiter.api.Test)5 FunctionalTest (com.synopsys.integration.detectable.annotations.FunctionalTest)4 GraphAssert (com.synopsys.integration.detectable.util.graph.GraphAssert)4 PackageJson (com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson)3 Gson (com.google.gson.Gson)2 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)2 ExternalId (com.synopsys.integration.bdio.model.externalid.ExternalId)2 ExternalIdFactory (com.synopsys.integration.bdio.model.externalid.ExternalIdFactory)2 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)2 NpmCliParser (com.synopsys.integration.detectable.detectables.npm.cli.parse.NpmCliParser)2 Extraction (com.synopsys.integration.detectable.extraction.Extraction)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Optional (java.util.Optional)2 Nullable (org.jetbrains.annotations.Nullable)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 BasicDependencyGraph (com.synopsys.integration.bdio.graph.BasicDependencyGraph)1