Search in sources :

Example 6 with PackageJson

use of com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson in project synopsys-detect by blackducksoftware.

the class PackageJsonExtractorTest method createPackageJson.

private PackageJson createPackageJson() {
    PackageJson packageJson = new PackageJson();
    packageJson.name = "test";
    packageJson.version = "test-version";
    packageJson.dependencies = new HashMap<>();
    packageJson.devDependencies = new HashMap<>();
    packageJson.dependencies.put(testDep1.getName(), testDep1.getVersion());
    packageJson.dependencies.put(testDep2.getName(), testDep2.getVersion());
    packageJson.devDependencies.put(testDevDep1.getName(), testDevDep1.getVersion());
    packageJson.devDependencies.put(testDevDep2.getName(), testDevDep2.getVersion());
    return packageJson;
}
Also used : PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson)

Example 7 with PackageJson

use of com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson in project synopsys-detect by blackducksoftware.

the class PackageJsonExtractorTest method extractWithDevNoPeerDependencies.

@Test
void extractWithDevNoPeerDependencies() {
    PackageJson packageJson = createPackageJson();
    Extraction extraction = createExtractor(NpmDependencyType.PEER).extract(packageJson);
    assertEquals(1, extraction.getCodeLocations().size());
    CodeLocation codeLocation = extraction.getCodeLocations().get(0);
    DependencyGraph dependencyGraph = codeLocation.getDependencyGraph();
    GraphAssert graphAssert = new GraphAssert(Forge.RUBYGEMS, dependencyGraph);
    graphAssert.hasRootDependency(testDep1);
    graphAssert.hasRootDependency(testDep2);
    graphAssert.hasRootDependency(testDevDep1);
    graphAssert.hasRootDependency(testDevDep2);
    graphAssert.hasRootSize(4);
}
Also used : CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) GraphAssert(com.synopsys.integration.detectable.util.graph.GraphAssert) Extraction(com.synopsys.integration.detectable.extraction.Extraction) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) Test(org.junit.jupiter.api.Test) UnitTest(com.synopsys.integration.detectable.annotations.UnitTest)

Example 8 with PackageJson

use of com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson in project synopsys-detect by blackducksoftware.

the class LernaDetectableTest method addPackageJson.

private void addPackageJson(Path directory, String packageName, String packageVersion, List<NameVersion> dependencies, List<NameVersion> devDependencies, List<NameVersion> peerDependencies) throws IOException {
    PackageJson packageJson = new PackageJson();
    packageJson.name = packageName;
    packageJson.version = packageVersion;
    packageJson.dependencies = dependencies.stream().collect(Collectors.toMap(NameVersion::getName, NameVersion::getVersion));
    packageJson.devDependencies = devDependencies.stream().collect(Collectors.toMap(NameVersion::getName, NameVersion::getVersion));
    packageJson.peerDependencies = peerDependencies.stream().collect(Collectors.toMap(NameVersion::getName, NameVersion::getVersion));
    addFile(directory.resolve(LernaDetectable.PACKAGE_JSON), gson.toJson(packageJson));
}
Also used : NameVersion(com.synopsys.integration.util.NameVersion) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson)

Example 9 with PackageJson

use of com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson in project synopsys-detect by blackducksoftware.

the class LernaExternalDetectableTest method addPackageJson.

private void addPackageJson(Path directory, String packageName, String packageVersion, NameVersion... dependencies) throws IOException {
    PackageJson packageJson = new PackageJson();
    packageJson.name = packageName;
    packageJson.version = packageVersion;
    packageJson.dependencies = Arrays.stream(dependencies).collect(Collectors.toMap(NameVersion::getName, NameVersion::getVersion));
    addFile(directory.resolve(LernaDetectable.PACKAGE_JSON), gson.toJson(packageJson));
}
Also used : NameVersion(com.synopsys.integration.util.NameVersion) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson)

Example 10 with PackageJson

use of com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson in project synopsys-detect by blackducksoftware.

the class NpmCliExtractor method extract.

public Extraction extract(File directory, ExecutableTarget npmExe, @Nullable String npmArguments, File packageJsonFile) {
    toolVersionLogger.log(directory, npmExe);
    PackageJson packageJson;
    try {
        packageJson = parsePackageJson(packageJsonFile);
    } catch (IOException e) {
        return new Extraction.Builder().exception(e).build();
    }
    List<String> exeArgs = new ArrayList<>();
    exeArgs.add("ls");
    exeArgs.add("-json");
    Optional.ofNullable(npmArguments).map(arg -> arg.split(" ")).ifPresent(additionalArguments -> exeArgs.addAll(Arrays.asList(additionalArguments)));
    ExecutableOutput npmLsOutput;
    try {
        npmLsOutput = executableRunner.execute(ExecutableUtils.createFromTarget(directory, npmExe, exeArgs));
    } catch (Exception e) {
        return new Extraction.Builder().exception(e).build();
    }
    String standardOutput = npmLsOutput.getStandardOutput();
    String errorOutput = npmLsOutput.getErrorOutput();
    if (StringUtils.isNotBlank(errorOutput)) {
        logger.error("Error when running npm ls -json command");
        logger.error(errorOutput);
        return new Extraction.Builder().failure("Npm wrote to stderr while running npm ls.").build();
    } else if (StringUtils.isNotBlank(standardOutput)) {
        logger.debug("Parsing npm ls file.");
        logger.debug(standardOutput);
        NpmPackagerResult result = npmCliParser.generateCodeLocation(standardOutput, packageJson);
        String projectName = result.getProjectName() != null ? result.getProjectName() : packageJson.name;
        String projectVersion = result.getProjectVersion() != null ? result.getProjectVersion() : packageJson.version;
        return new Extraction.Builder().success(result.getCodeLocation()).projectName(projectName).projectVersion(projectVersion).build();
    } else {
        logger.error("Nothing returned from npm ls -json command");
        return new Extraction.Builder().failure("Npm returned error after running npm ls.").build();
    }
}
Also used : Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Extraction(com.synopsys.integration.detectable.extraction.Extraction) LoggerFactory(org.slf4j.LoggerFactory) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) StringUtils(org.apache.commons.lang3.StringUtils) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) ArrayList(java.util.ArrayList) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) NpmCliParser(com.synopsys.integration.detectable.detectables.npm.cli.parse.NpmCliParser) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ToolVersionLogger(com.synopsys.integration.detectable.util.ToolVersionLogger) ExecutableTarget(com.synopsys.integration.detectable.ExecutableTarget) Gson(com.google.gson.Gson) DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) NpmPackagerResult(com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult) Optional(java.util.Optional) ExecutableUtils(com.synopsys.integration.detectable.ExecutableUtils) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) ArrayList(java.util.ArrayList) Extraction(com.synopsys.integration.detectable.extraction.Extraction) IOException(java.io.IOException) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) NpmPackagerResult(com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult) IOException(java.io.IOException)

Aggregations

PackageJson (com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson)12 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)4 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)4 NameVersion (com.synopsys.integration.util.NameVersion)4 Test (org.junit.jupiter.api.Test)4 UnitTest (com.synopsys.integration.detectable.annotations.UnitTest)3 NpmPackagerResult (com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult)3 NullSafePackageJson (com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson)3 WorkspacePackageJson (com.synopsys.integration.detectable.detectables.yarn.packagejson.WorkspacePackageJson)3 Extraction (com.synopsys.integration.detectable.extraction.Extraction)3 Gson (com.google.gson.Gson)2 ExternalId (com.synopsys.integration.bdio.model.externalid.ExternalId)2 ExternalIdFactory (com.synopsys.integration.bdio.model.externalid.ExternalIdFactory)2 NpmCliParser (com.synopsys.integration.detectable.detectables.npm.cli.parse.NpmCliParser)2 YarnLock (com.synopsys.integration.detectable.detectables.yarn.parse.YarnLock)2 YarnLockDependency (com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockDependency)2 YarnLockResult (com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockResult)2 YarnLockEntry (com.synopsys.integration.detectable.detectables.yarn.parse.entry.YarnLockEntry)2 YarnLockEntryId (com.synopsys.integration.detectable.detectables.yarn.parse.entry.YarnLockEntryId)2 YarnWorkspace (com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspace)2