use of com.blackducksoftware.integration.hub.detect.detector.npm.model.PackageLock in project hub-detect by blackducksoftware.
the class NpmLockfileParserTest method recreatePackageJsonFromLock.
private Optional<String> recreatePackageJsonFromLock(String lockFileText) {
// These tests were written before we needed a package json.
// So we replicate a package json with every package as root.
PackageJson packageJson = new PackageJson();
Gson gson = new Gson();
PackageLock packageLock = gson.fromJson(lockFileText, PackageLock.class);
packageLock.dependencies.forEach((key, value) -> packageJson.dependencies.put(key, key));
String text = gson.toJson(packageJson);
return Optional.of(text);
}
use of com.blackducksoftware.integration.hub.detect.detector.npm.model.PackageLock in project hub-detect by blackducksoftware.
the class NpmLockfileParser method parse.
public NpmParseResult parse(final String sourcePath, final Optional<String> packageJsonText, final String lockFileText, final boolean includeDevDependencies) {
final MutableDependencyGraph dependencyGraph = new MutableMapDependencyGraph();
logger.info("Parsing lock file text: ");
logger.debug(lockFileText);
Optional<PackageJson> packageJson = Optional.empty();
if (packageJsonText.isPresent()) {
logger.debug(packageJsonText.get());
packageJson = Optional.of(gson.fromJson(packageJsonText.get(), PackageJson.class));
}
final PackageLock packageLock = gson.fromJson(lockFileText, PackageLock.class);
logger.debug(lockFileText);
logger.info("Processing project.");
if (packageLock.dependencies != null) {
logger.info(String.format("Found %d dependencies.", packageLock.dependencies.size()));
// Convert to our custom format
NpmDependencyConverter dependencyConverter = new NpmDependencyConverter(externalIdFactory);
NpmDependency rootDependency = dependencyConverter.convertLockFile(packageLock, packageJson);
traverse(rootDependency, dependencyGraph, true, includeDevDependencies);
} else {
logger.info("Lock file did not have a 'dependencies' section.");
}
logger.info("Finished processing.");
final ExternalId projectId = externalIdFactory.createNameVersionExternalId(Forge.NPM, packageLock.name, packageLock.version);
final DetectCodeLocation codeLocation = new DetectCodeLocation.Builder(DetectCodeLocationType.NPM, sourcePath, projectId, dependencyGraph).build();
return new NpmParseResult(packageLock.name, packageLock.version, codeLocation);
}
use of com.blackducksoftware.integration.hub.detect.detector.npm.model.PackageLock in project hub-detect by blackducksoftware.
the class NpmDependencyConverter method convertLockFile.
public NpmDependency convertLockFile(PackageLock packageLock, Optional<PackageJson> packageJsonOptional) {
NpmDependency root = createNpmDependency(packageLock.name, packageLock.version, false);
if (packageLock.dependencies != null) {
List<NpmDependency> children = convertPackageMapToDependencies(root, packageLock.dependencies);
root.addAllDependencies(children);
}
if (packageJsonOptional.isPresent()) {
PackageJson packageJson = packageJsonOptional.get();
if (packageJson.dependencies != null) {
List<NpmRequires> rootRequires = convertNameVersionMapToRequires(packageJson.dependencies);
root.addAllRequires(rootRequires);
}
if (packageJson.devDependencies != null) {
List<NpmRequires> rootDevRequires = convertNameVersionMapToRequires(packageJson.devDependencies);
root.addAllRequires(rootDevRequires);
}
} else {
if (packageLock.dependencies != null) {
List<NpmRequires> requires = packageLock.dependencies.entrySet().stream().map(entry -> new NpmRequires(entry.getKey(), entry.getValue().version)).collect(Collectors.toList());
root.addAllRequires(requires);
}
}
return root;
}
Aggregations