[Gradle, JS]] Add custom fields for package.json
^KT-35330 fixed
This commit is contained in:
+24
-1
@@ -5,6 +5,8 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.gradle.targets.js.npm
|
package org.jetbrains.kotlin.gradle.targets.js.npm
|
||||||
|
|
||||||
|
import com.google.gson.ExclusionStrategy
|
||||||
|
import com.google.gson.FieldAttributes
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import com.google.gson.GsonBuilder
|
import com.google.gson.GsonBuilder
|
||||||
import org.jetbrains.kotlin.gradle.internal.ensureParentDirsCreated
|
import org.jetbrains.kotlin.gradle.internal.ensureParentDirsCreated
|
||||||
@@ -15,6 +17,8 @@ class PackageJson(
|
|||||||
var name: String,
|
var name: String,
|
||||||
var version: String
|
var version: String
|
||||||
) {
|
) {
|
||||||
|
private val customFields = mutableMapOf<String, Any>()
|
||||||
|
|
||||||
val empty: Boolean
|
val empty: Boolean
|
||||||
get() = main == null &&
|
get() = main == null &&
|
||||||
private == null &&
|
private == null &&
|
||||||
@@ -55,6 +59,10 @@ class PackageJson(
|
|||||||
val bundledDependencies = mutableListOf<String>()
|
val bundledDependencies = mutableListOf<String>()
|
||||||
get() = field ?: mutableListOf()
|
get() = field ?: mutableListOf()
|
||||||
|
|
||||||
|
fun customField(key: String, value: Any) {
|
||||||
|
customFields[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun scopedName(name: String): ScopedName = if (name.contains("/")) ScopedName(
|
fun scopedName(name: String): ScopedName = if (name.contains("/")) ScopedName(
|
||||||
scope = name.substringBeforeLast("/").removePrefix("@"),
|
scope = name.substringBeforeLast("/").removePrefix("@"),
|
||||||
@@ -72,11 +80,26 @@ class PackageJson(
|
|||||||
fun saveTo(packageJsonFile: File) {
|
fun saveTo(packageJsonFile: File) {
|
||||||
val gson = GsonBuilder()
|
val gson = GsonBuilder()
|
||||||
.setPrettyPrinting()
|
.setPrettyPrinting()
|
||||||
|
.addSerializationExclusionStrategy(
|
||||||
|
object : ExclusionStrategy {
|
||||||
|
override fun shouldSkipField(f: FieldAttributes?): Boolean =
|
||||||
|
f?.name == this@PackageJson::customFields.name
|
||||||
|
|
||||||
|
override fun shouldSkipClass(clazz: Class<*>?): Boolean =
|
||||||
|
false
|
||||||
|
}
|
||||||
|
)
|
||||||
.create()
|
.create()
|
||||||
|
|
||||||
packageJsonFile.ensureParentDirsCreated()
|
packageJsonFile.ensureParentDirsCreated()
|
||||||
|
val jsonTree = gson.toJsonTree(this)
|
||||||
|
customFields
|
||||||
|
.forEach { (key, value) ->
|
||||||
|
val valueElement = gson.toJsonTree(value)
|
||||||
|
jsonTree.asJsonObject.add(key, valueElement)
|
||||||
|
}
|
||||||
packageJsonFile.writer().use {
|
packageJsonFile.writer().use {
|
||||||
gson.toJson(this, it)
|
gson.toJson(jsonTree, it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user