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);
}
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);
}
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();
}
}
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");
}
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);
}
Aggregations