Skip to content

Commit

Permalink
Publish npm lib with typescript (.d.ts) files (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilmveel authored Feb 23, 2024
1 parent 7d63e67 commit 01add50
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ jobs:
registry-url: 'https://registry.npmjs.org'
- name: Build
run: |
./gradlew :src:plugin:cli:build
./gradlew :src:plugin:npm:build
- name: Build
working-directory: ./src/plugin/cli/build/dist/js/productionLibrary
working-directory: ./src/plugin/npm/build/dist/js/productionLibrary
run: |
npm publish --access public
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ include(
"src:plugin:arguments",
"src:plugin:cli",
"src:plugin:maven",
"src:plugin:npm",
"src:plugin:gradle",
"src:converter:openapi",
"src:integration:jackson",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fun Definition.produce(): WsNode =
requests = requests.produce(),
responses = responses.produce(),
)

is Enum -> WsEnum(name, entries.toTypedArray())
is Refined -> WsRefined(name, validator.value)
}
Expand Down Expand Up @@ -81,17 +82,19 @@ private fun Endpoint.Response.produce() = WsResponse(status, content?.produce())
private fun List<Endpoint.Response>.produce() = map { it.produce() }.toTypedArray()

@JsExport
sealed interface WsNode
sealed interface WsNode {
val name: String
}

@JsExport
data class WsType(val name: String, val shape: WsShape) : WsNode
data class WsType(override val name: String, val shape: WsShape) : WsNode

@JsExport
data class WsShape(val value: Array<WsField>)

@JsExport
data class WsEndpoint(
val name: String,
override val name: String,
val method: WsMethod,
val path: Array<WsSegment>,
val query: Array<WsField>,
Expand All @@ -102,10 +105,10 @@ data class WsEndpoint(
) : WsNode

@JsExport
data class WsEnum(val name: String, val entries: Array<String>) : WsNode
data class WsEnum(override val name: String, val entries: Array<String>) : WsNode

@JsExport
data class WsRefined(val name: String, val validator: String) : WsNode
data class WsRefined(override val name: String, val validator: String) : WsNode

@JsExport
enum class WsMethod { GET, POST, PUT, DELETE, OPTIONS, HEAD, PATCH, TRACE }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ abstract class Compiler {
fun tokenize(source: String) = Wirespec.tokenize(source).produce()

fun parse(source: String) = Wirespec.tokenize(source)
.let { Parser(logger).parse(it) }
.let { Parser(logger).parse(it).produce() }

companion object {
protected val logger = object : Logger() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import arrow.core.Either
import arrow.core.Nel
import community.flock.wirespec.compiler.core.emit.common.Emitted
import community.flock.wirespec.compiler.core.exceptions.WirespecException
import community.flock.wirespec.compiler.core.parse.nodes.Definition

fun Either<Nel<WirespecException>, List<Emitted>>.produce(): WsCompilationResult = when (this) {
is Either.Left -> WsCompilationResult(errors = value.map { it.produce() }.toTypedArray())
is Either.Right -> WsCompilationResult(compiled = WsCompiled(value.first().result))
is Either.Right -> WsCompilationResult(result = WsCompiled(value.first().result))
}

@JsExport
class WsCompilationResult(
val compiled: WsCompiled? = null,
val result: WsCompiled? = null,
val errors: Array<WsError> = emptyArray()
)

Expand All @@ -23,3 +24,14 @@ class WsCompiled(val value: String)

@JsExport
class WsCompiledFile(val name: String, val value: String)

fun Either<Nel<WirespecException>, List<Definition>>.produce(): WsParseResult = when (this) {
is Either.Left -> WsParseResult(errors = value.map { it.produce() }.toTypedArray())
is Either.Right -> WsParseResult(result = value.map { it.produce() }.toTypedArray())
}

@JsExport
class WsParseResult(
val result: Array<WsNode>? = null,
val errors: Array<WsError>? = null,
)
7 changes: 1 addition & 6 deletions src/plugin/cli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,5 @@ fun KotlinNativeTargetWithHostTests.build() = binaries {

fun KotlinJsTargetDsl.build() {
nodejs()
// generateTypeScriptDefinitions()
binaries.library()
compilations["main"].packageJson {
customField("name", "@flock/wirespec")
customField("bin", mapOf("wirespec" to "wirespec-bin.js"))
}
binaries.executable()
}
45 changes: 45 additions & 0 deletions src/plugin/npm/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import Versions.KOTLIN_COMPILER

plugins {
kotlin("multiplatform")
kotlin("jvm") apply false
id("com.github.johnrengelman.shadow") apply false
}

group = "${Settings.GROUP_ID}.plugin.npm"
version = Settings.version

repositories {
mavenCentral()
}

kotlin {
js(IR) {
nodejs()
generateTypeScriptDefinitions()
binaries.library()
compilations["main"].packageJson {
customField("name", "@flock/wirespec")
customField("bin", mapOf("wirespec" to "wirespec-bin.js"))
}
}

sourceSets.all {
languageSettings.apply {
languageVersion = KOTLIN_COMPILER
}
}

sourceSets {
val commonMain by getting {
dependencies {
implementation(project(":src:compiler:core"))
implementation(project(":src:compiler:lib"))
implementation(project(":src:plugin:cli"))
}
}
val jsMain by getting {
dependsOn(commonMain)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@file:OptIn(ExperimentalJsExport::class)

package community.flock.wirespec.plugin.npm

import community.flock.wirespec.compiler.core.Wirespec
import community.flock.wirespec.compiler.core.parse.Parser
import community.flock.wirespec.compiler.core.tokenize.tokenize
import community.flock.wirespec.compiler.lib.produce
import community.flock.wirespec.compiler.utils.noLogger
import community.flock.wirespec.plugin.cli.main

@JsExport
fun cli(args: Array<String>) = main(args)

@JsExport
fun parse(source: String) = Wirespec
.tokenize(source)
.let { Parser(noLogger).parse(it).produce() }

0 comments on commit 01add50

Please sign in to comment.