### Install Dukat CLI (npm) Source: https://github.com/kotlin/dukat/blob/master/README.md Installs the latest stable version of the Dukat command-line interface using npm. This is the recommended way to get started with Dukat. ```shell npm install -g dukat ``` -------------------------------- ### Install Dukat CLI Snapshot (npm) Source: https://github.com/kotlin/dukat/blob/master/README.md Installs a development build (snapshot version) of Dukat from npm. This version reflects the latest changes in the master branch and is useful for testing upcoming features. ```shell npm install -g dukat@next ``` -------------------------------- ### Get Dukat Version Information CLI Source: https://context7.com/kotlin/dukat/llms.txt Displays the installed version of the Dukat CLI tool. ```bash dukat -v ``` -------------------------------- ### Install Dukat CLI via npm Source: https://context7.com/kotlin/dukat/llms.txt Installs the Dukat command-line interface tool globally using npm. Supports installing the stable release or a development snapshot. ```bash # Install stable release npm install -g dukat # Install development snapshot (latest master) npm install -g dukat@next ``` -------------------------------- ### TypeScript Interface to Kotlin Conversion Source: https://context7.com/kotlin/dukat/llms.txt Demonstrates the conversion of a TypeScript interface to its Kotlin equivalent. This example shows how Dukat handles basic types, optional properties, and methods, generating `external interface` in Kotlin. ```typescript interface User { id: number; name: string; email?: string; greet(message: string): void; } declare function createUser(name: string): User; ``` ```kotlin @file:Suppress("INTERFACE_WITH_SUPERCLASS", "OVERRIDING_FINAL_MEMBER", "RETURN_TYPE_MISMATCH_ON_OVERRIDE", "CONFLICTING_OVERLOADS") import kotlin.js.* external interface User { var id: Number var name: String var email: String? fun greet(message: String) } external fun createUser(name: String): User ``` -------------------------------- ### TypeScript Generics to Kotlin Conversion Source: https://context7.com/kotlin/dukat/llms.txt Illustrates Dukat's capability to convert TypeScript generics, such as those found in `Promise`, into their Kotlin counterparts. This example demonstrates how generic type parameters and overloaded methods are translated. ```typescript interface Promise { then( onfulfilled?: (value: T) => TResult | PromiseLike, onrejected?: (reason: any) => TResult | PromiseLike ): Promise; catch( onrejected?: (reason: any) => TResult | PromiseLike ): Promise; } declare function fetch(url: string): Promise; ``` ```kotlin external interface Promise { fun then( onfulfilled: ((value: T) -> dynamic)? = definedExternally, onrejected: ((reason: Any) -> dynamic)? = definedExternally ): Promise fun catch( onrejected: ((reason: Any) -> dynamic)? = definedExternally ): Promise } external fun fetch(url: String): Promise ``` -------------------------------- ### Custom IDL Translation Pipeline Source: https://context7.com/kotlin/dukat/llms.txt This code illustrates setting up a custom IDL translator using Dukat's compiler. It allows for configuration of options like `dynamicAsType` and `useStaticGetters`. The example shows how to translate an IDL file and then iterate through the resulting Kotlin AST model. ```kotlin import org.jetbrains.dukat.compiler.translator.IdlInputTranslator import org.jetbrains.dukat.idlReferenceResolver.IdlReferencesResolver import org.jetbrains.dukat.translatorString.translateSourceSet // Create custom IDL translator with options val nameResolver = IdlReferencesResolver() val translator = IdlInputTranslator( nameResolver = nameResolver, dynamicAsType = true, // Use explicit types instead of dynamic useStaticGetters = false // Don't generate static getter functions ) // Translate a single IDL file val sourceSet = translator.translate("path/to/webapi.idl") // Process the resulting Kotlin model sourceSet.sources.forEach { sourceFile -> println("Source: ${sourceFile.fileName}") println("Module: ${sourceFile.root.name}") sourceFile.root.declarations.forEach { decl -> println(" Declaration: $decl") } } ``` -------------------------------- ### TypeScript Class to Kotlin Conversion Source: https://context7.com/kotlin/dukat/llms.txt Shows the transformation of a TypeScript class declaration, including its constructor and methods, into a Kotlin `external open class`. This example highlights how Dukat handles default parameter values and method signatures. ```typescript declare class EventEmitter { constructor(options?: EventEmitterOptions); on(event: string, listener: (...args: any[]) => void): this; emit(event: string, ...args: any[]): boolean; removeAllListeners(event?: string): this; } interface EventEmitterOptions { captureRejections?: boolean; } ``` ```kotlin external open class EventEmitter(options: EventEmitterOptions = definedExternally) { open fun on(event: String, listener: (args: Array) -> Unit): EventEmitter /* this */ open fun emit(event: String, vararg args: Any): Boolean open fun removeAllListeners(event: String = definedExternally): EventEmitter /* this */ } external interface EventEmitterOptions { var captureRejections: Boolean? } ``` -------------------------------- ### Create Binary Protocol Buffers with Dukat Programmatic API (Node.js) Source: https://context7.com/kotlin/dukat/llms.txt Uses the Dukat Node.js API to create binary representations of TypeScript files. This involves getting the standard library path, creating binary data, and piping it to a readable stream for further processing or saving to a file. ```javascript const { createBinary, createReadableStream, getStdLib } = require('dukat/bin/dukat-cli'); // Get the standard library path const stdLib = getStdLib(); // Create binary representation of TypeScript files const files = ['/path/to/types/index.d.ts']; const tsConfig = null; // Optional tsconfig.json path const emitDiagnostics = false; const failOnWarnings = false; const binary = createBinary(tsConfig, stdLib, emitDiagnostics, failOnWarnings, files); // Create a readable stream from the binary const stream = createReadableStream(binary, (data) => console.log('Received data chunk'), () => console.log('Stream ended') ); // Pipe to file or process const fs = require('fs'); stream.pipe(fs.createWriteStream('output.dukat')); ``` -------------------------------- ### Dukat CLI Usage Source: https://github.com/kotlin/dukat/blob/master/README.md Demonstrates the basic command-line usage of Dukat. It shows how to invoke the tool with options and specifies the input TypeScript definition files. ```shell dukat [] ``` -------------------------------- ### Dukat CLI Options Source: https://github.com/kotlin/dukat/blob/master/README.md Lists and explains the available command-line options for the Dukat tool. These options control aspects like package naming, module annotations, and output destination. ```shell -p package name for the generated file (by default filename.d.ts renamed to filename.d.kt) -m String use this value as @file:JsModule annotation value whenever such annotation occurs -d destination directory for files with converted declarations (by default declarations are generated in current directory) -v, -version print version ``` -------------------------------- ### Clone and Build Dukat Project Source: https://context7.com/kotlin/dukat/llms.txt Provides instructions for cloning the Dukat repository from GitHub and building the project using Gradle. It also includes commands for running the project's tests. ```bash # Clone the repository git clone https://github.com/Kotlin/dukat.git cd dukat # Build the project ./gradlew build # Run tests ./gradlew test -Pdukat.test.failure.always ``` -------------------------------- ### Build Dukat Project (Gradle) Source: https://github.com/kotlin/dukat/blob/master/README.md Builds the Dukat project using the Gradle wrapper. This command compiles the project and prepares it for execution or further testing. ```shell ./gradlew build ``` -------------------------------- ### Web IDL Conversion with Dukat CLI Source: https://context7.com/kotlin/dukat/llms.txt Converts Web IDL files to Kotlin declarations using the Dukat CLI. Supports basic conversion and advanced options like --dynamic-as-type and --use-static-getters. ```bash # Convert Web IDL files dukat dom.idl # Convert WebIDL files with additional options dukat --dynamic-as-type --use-static-getters webgl.webidl ``` -------------------------------- ### Run Dukat Unit Tests (Gradle) Source: https://github.com/kotlin/dukat/blob/master/README.md Executes the unit tests for the Dukat project using Gradle. The `-Pdukat.test.failure.always` flag ensures that the build fails if any tests fail. ```shell ./gradlew test -Pdukat.test.failure.always ``` -------------------------------- ### Clone Dukat Project Source: https://github.com/kotlin/dukat/blob/master/README.md Clones the Dukat project repository from its URL. This step is necessary for building the project from source. It includes a note for Windows users regarding line endings. ```shell # on Windows-based platforms set following: `git config core.autocrlf true` git clone ``` -------------------------------- ### Basic TypeScript Declaration Conversion with Dukat CLI Source: https://context7.com/kotlin/dukat/llms.txt Converts TypeScript declaration files (.d.ts) to Kotlin declarations using the Dukat CLI. Supports single or multiple files, specifying output directory, custom package names, and @JsModule annotation values. ```bash # Convert a single .d.ts file dukat mylib.d.ts # Convert multiple files dukat types/index.d.ts types/core.d.ts # Specify output directory dukat -d ./generated types/index.d.ts # Set custom package name dukat -p com.example.mylib types/index.d.ts # Set @JsModule annotation value dukat -m "my-npm-module" types/index.d.ts # Combined options dukat -p com.example.lib -m "lodash" -d ./kotlin-bindings lodash/index.d.ts ``` -------------------------------- ### Generate Kotlin Metadata Descriptors Source: https://context7.com/kotlin/dukat/llms.txt This snippet demonstrates how to generate Kotlin metadata descriptors from a SourceSetModel. It shows two methods: writing descriptors to individual files or packaging them into a JAR archive. Requires the `org.jetbrains.dukat.descriptors` library. ```kotlin import org.jetbrains.dukat.descriptors.writeDescriptorsToFile import org.jetbrains.dukat.descriptors.writeDescriptorsToJar import org.jetbrains.dukat.astModel.SourceSetModel // Assuming sourceSet is already translated from TypeScript val sourceSet: SourceSetModel = /* ... from translation ... */ val stdLibJarPath = "/path/to/kotlin-stdlib-js.jar" val outputDir = "./descriptors" // Write descriptor files (.meta.js + .kjsm files) val outputFiles = writeDescriptorsToFile(sourceSet, stdLibJarPath, outputDir) outputFiles.forEach { println("Generated descriptor: $it") } // Or write as a single JAR archive val jarFiles = writeDescriptorsToJar(sourceSet, stdLibJarPath, outputDir) jarFiles.forEach { println("Generated JAR: $it") } ``` -------------------------------- ### Translate Web IDL Files with Dukat Kotlin API Source: https://context7.com/kotlin/dukat/llms.txt Translates Web IDL files to Kotlin using the Dukat Kotlin API. Takes a list of IDL file paths and optional flags for dynamic type replacement and static getters, then compiles the resulting Kotlin model into source files. ```kotlin import org.jetbrains.dukat.compiler.translator.translateIdlSources import org.jetbrains.dukat.translatorString.translateSourceSet import org.jetbrains.dukat.translatorString.compileUnits // Translate IDL sources val sources = listOf("path/to/dom.idl", "path/to/webgl.idl") val dynamicAsType = false // Replace dynamic with explicit type val useStaticGetters = true // Use static getters for indexed access val sourceSet = translateIdlSources(sources, dynamicAsType, useStaticGetters) // Convert to Kotlin and write files val translationUnits = translateSourceSet(sourceSet) val outputFiles = compileUnits(translationUnits, "./generated-idl") ``` -------------------------------- ### Direct Translation API with Dukat Programmatic API (Node.js) Source: https://context7.com/kotlin/dukat/llms.txt Utilizes the Dukat Node.js API to directly translate TypeScript files to Kotlin. This method spawns a Java process and manages input/output operations for the conversion. ```javascript const { translate } = require('dukat/bin/dukat-cli'); // Translate TypeScript files to Kotlin // This spawns the Java process and handles I/O translate([ '-d', './output', '-p', 'com.example.bindings', '/path/to/types/index.d.ts' ]); ``` -------------------------------- ### Translate TypeScript Declarations with Dukat Kotlin API Source: https://context7.com/kotlin/dukat/llms.txt Translates TypeScript declarations to Kotlin using the Dukat Kotlin API. Reads TypeScript source as bytes, creates a module name resolver, translates to a Kotlin model, and then compiles the model into Kotlin source code files. ```kotlin import org.jetbrains.dukat.ts.translator.translateTypescriptDeclarations import org.jetbrains.dukat.moduleNameResolver.CommonJsNameResolver import org.jetbrains.dukat.translatorString.translateSourceSet import org.jetbrains.dukat.translatorString.compileUnits // Read TypeScript source as bytes (from stdin or file) val inputBytes: ByteArray = File("types/index.d.ts").readBytes() // Create a module name resolver val moduleResolver = CommonJsNameResolver() // Translate TypeScript declarations to Kotlin model val sourceSet = translateTypescriptDeclarations( input = inputBytes, nameResolver = moduleResolver, packageName = null, // Optional base package name reportRecoveredDiagnostics = true, stdLibPath = "/path/to/stdlib.dukat" ) // Convert model to Kotlin source code val translationUnits = translateSourceSet(sourceSet) // Write output files to directory val outputFiles = compileUnits(translationUnits, "./generated") outputFiles.forEach { println("Generated: $it") } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.