Skip to content

Commit

Permalink
Improve NPM package support
Browse files Browse the repository at this point in the history
  • Loading branch information
vgarciabnz committed Oct 16, 2024
1 parent 5fa1af7 commit 557c827
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repositories {
maven { url = uri("https://oss.sonatype.org/content/repositories/snapshots") }
}

version = "3.1.0-SNAPSHOT"
version = "3.1.1-SNAPSHOT"
group = "org.hisp.dhis.rules"

if (project.hasProperty("removeSnapshotSuffix")) {
Expand All @@ -33,8 +33,8 @@ kotlin {
useCommonJs()
} else {
useEsModules()
generateTypeScriptDefinitions()
}
generateTypeScriptDefinitions()
binaries.library()
}
val hostOs = System.getProperty("os.name")
Expand Down
44 changes: 40 additions & 4 deletions buildSrc/src/main/kotlin/npm-publish-conventions.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import dev.petuska.npm.publish.extension.domain.NpmAccess
import java.nio.file.Files

plugins {
id("dev.petuska.npm.publish")
Expand All @@ -13,14 +14,15 @@ project.afterEvaluate {
packageName.set("rule-engine")
readme.set(File("./README.md"))
packageJson {
"module" by "${project.name}.mjs"
"main" by "${project.name}.js"
"module" by "mjs/${project.name}.mjs"
"main" by "cjs/${project.name}.js"
"exports" by {
"." by {
"import" by "./${project.name}.mjs"
"require" by "./${project.name}.js"
"import" by "./mjs/${project.name}.mjs"
"require" by "./cjs/${project.name}.js"
}
}
"types" by "./mjs/${project.name}.d.ts"
"contributors" by Props.DEVELOPERS.map { developer ->
"${developer.name} <${developer.email}>"
}
Expand All @@ -38,4 +40,38 @@ project.afterEvaluate {
}
}
}

tasks.named("packJsPackage") {
doLast {
val buildFolder = "./build/packages/js"

if (project.hasProperty("useCommonJs")) {
moveFilesToSubFolder(buildFolder, subFolder = "cjs", packageType = "commonjs")
} else {
moveFilesToSubFolder(buildFolder, subFolder = "mjs", packageType = "module")
}
}
}
}

fun moveFilesToSubFolder(buildFolder: String, subFolder: String, packageType: String) {
val excludedFiles = listOf("README.md", "package.json")
Files.createDirectory(File("$buildFolder/$subFolder").toPath())

println("\nMoving to final destination folder '$subFolder', type '$packageType'")

File(buildFolder).listFiles()?.forEach { file ->
if (file.isFile && !excludedFiles.contains(file.name)) {
val dstPath = "${file.parent}/$subFolder/${file.name}"
file.renameTo(File(dstPath))
}
}

// Add a package.json file in each subfolder containing the package type
File("$buildFolder/$subFolder/package.json")
.writeText("""
{
"type" : "$packageType"
}
""".trimIndent())
}

0 comments on commit 557c827

Please sign in to comment.