-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(node)!: Make
Npm
separate from Yarn
Analog to 0eb1eea, remove the inheritance between the two managers and re-write large parts of `Npm` to extract all needed information solely based on the output of the `npm` CLI command, instead of relying on the file hierarchy under the `node_modules` directory. This reduces complexity and makes the implementation(s) easy to understand, maintain and change in isolation. Note: The handling of the `installIssues` in `Yarn` has been used only for `Npm`, which is why that code is moved from `Yarn` to `Npm`. Signed-off-by: Frank Viernau <frank_viernau@epam.com>
- Loading branch information
Showing
4 changed files
with
241 additions
and
32 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
plugins/package-managers/node/src/main/kotlin/npm/ModuleInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) 2024 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
package org.ossreviewtoolkit.plugins.packagemanagers.node.npm | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
|
||
private val JSON = Json { ignoreUnknownKeys = true } | ||
|
||
internal fun parseNpmList(json: String): ModuleInfo = JSON.decodeFromString(json) | ||
|
||
@Serializable | ||
internal data class ModuleInfo( | ||
val name: String = "", | ||
val version: String = "", | ||
val path: String? = null, | ||
@SerialName("_id") | ||
val id: String? = null, | ||
@SerialName("_dependencies") | ||
val dependencyConstraints: Map<String, String> = emptyMap(), | ||
val dependencies: Map<String, ModuleInfo> = emptyMap(), | ||
val optional: Boolean = false, | ||
val dev: Boolean = false, | ||
val resolved: String? = null | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
plugins/package-managers/node/src/main/kotlin/npm/NpmDependencyHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright (C) 2024 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
package org.ossreviewtoolkit.plugins.packagemanagers.node.npm | ||
|
||
import java.io.File | ||
|
||
import org.ossreviewtoolkit.model.Identifier | ||
import org.ossreviewtoolkit.model.Issue | ||
import org.ossreviewtoolkit.model.Package | ||
import org.ossreviewtoolkit.model.PackageLinkage | ||
import org.ossreviewtoolkit.model.utils.DependencyHandler | ||
import org.ossreviewtoolkit.plugins.packagemanagers.node.PackageJson | ||
import org.ossreviewtoolkit.plugins.packagemanagers.node.parsePackage | ||
import org.ossreviewtoolkit.plugins.packagemanagers.node.parsePackageJson | ||
import org.ossreviewtoolkit.utils.common.realFile | ||
|
||
internal class NpmDependencyHandler(private val npm: Npm) : DependencyHandler<ModuleInfo> { | ||
private val packageJsonCache = mutableMapOf<File, PackageJson>() | ||
|
||
override fun identifierFor(dependency: ModuleInfo): Identifier { | ||
val type = npm.managerName.takeIf { dependency.isProject } ?: "NPM" | ||
val namespace = dependency.name.substringBeforeLast("/", "") | ||
val name = dependency.name.substringAfterLast("/") | ||
val version = if (dependency.isProject) { | ||
readPackageJson(dependency.packageJsonFile).version.orEmpty() | ||
} else { | ||
dependency.version.takeUnless { it.startsWith("link:") || it.startsWith("file:") }.orEmpty() | ||
} | ||
|
||
return Identifier(type, namespace, name, version) | ||
} | ||
|
||
override fun dependenciesFor(dependency: ModuleInfo): List<ModuleInfo> = | ||
dependency.dependencies.values.filter { it.isInstalled } | ||
|
||
override fun linkageFor(dependency: ModuleInfo): PackageLinkage = | ||
PackageLinkage.DYNAMIC.takeUnless { dependency.isProject } ?: PackageLinkage.PROJECT_DYNAMIC | ||
|
||
override fun createPackage(dependency: ModuleInfo, issues: MutableCollection<Issue>): Package? = | ||
dependency.takeUnless { it.isProject || !it.isInstalled }?.let { | ||
parsePackage( | ||
workingDir = it.workingDir, | ||
packageJsonFile = it.packageJsonFile, | ||
getRemotePackageDetails = npm::getRemotePackageDetails | ||
) | ||
} | ||
|
||
private fun readPackageJson(packageJsonFile: File): PackageJson = | ||
packageJsonCache.getOrPut(packageJsonFile.realFile()) { parsePackageJson(packageJsonFile) } | ||
} | ||
|
||
private val ModuleInfo.workingDir: File get() = File(path) | ||
|
||
private val ModuleInfo.isInstalled: Boolean get() = path != null | ||
|
||
private val ModuleInfo.isProject: Boolean get() = resolved == null | ||
|
||
private val ModuleInfo.packageJsonFile: File get() = File(path, "package.json") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters