Skip to content

A lightweight and typesafe code generation library built for interfacing with your Node projects using Swift

License

Notifications You must be signed in to change notification settings

TypeSwift/TypeSwift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Header

 

TypeSwift

A lightweight and typesafe code generation library built for interfacing with your Node projects using Swift.

Cover

output.swift

enum TypeSwift {
  case toggle
  case setLabel(_ text: String)
  case addNumbers(_ a: Double, _ b: Double)
  case selectDevice(_ device: Device)

  enum Device {
    case Phone, Pad, Mac
  }

  // ...
}

Which can then be used in your Swift project as such (no additional setup required):

webView.ts(.toggle)

webView.ts(.setLabel("Hello, world!"))

webView.ts(.addNumbers(9, 5))

webView.ts(.selectDevice(.Phone))

 

Walkthrough Demo

Creating a native SwiftUI Toolbar item that replaces the functionality of a React NavBar (46 seconds).

Walthrough.mp4

 

Overview

Refer to the TypeSwift Wiki for full, in-depth documentation.

There are two main components to this project: SwiftGen and WKExtensions.

SwiftGen

A node package that incorporates ts-morph to fulfill the following purpose:

  1. Uses ts-morph to extract variables, functions, data structures and types from your code
  2. Maps each TS type to a Swift equivalent for compatible calling
  3. Generates a Swift data structure for easy interfacing
  4. Stores the compiled JavScript calling method for evaluation in a WebKit object

Identifier extraction techniques are detailed here.

WKExtensions

This Swift package allows you to interface with the TypeScript code, from within your Swift code, using a familiar syntax style. The enum data structure allows for certain features, such as auto-completion, in your favorite Swift IDE.

TSWebView

An optional custom WebKit object that will allow you to interface with your Node project directly. No setup required. Just plugin and play!

Currently in development. See ObservableWebView for more details on the base foundation.

 

Getting Started

Note: I'm very new to publishing with NPM! If you have any suggestions for how to improve this flow, please open an issue.

  1. Drag the TypeSwift/ folder into your Xcode project:

    • Make sure "Copy items if needed" is checked
    • Select "Create groups"
  2. Navigate to SwiftGen/

  3. Run npm install

  4. Modify the config.json:

{
  "inputDir": "path/to/ts-files",
  "outputDir": "path/to/xcode-proj/TypeSwift",
  "outputFileName": "TypeSwift",
   "outputSuffix": ".swift",
}
  • inputDir: Path to directory containing the TypeScript files. Will search recursively. Resolves both relative and absolute paths.
  • outputDir: Absolute path to the TypeSwift/ folder that you dragged into your Xcode project.
  1. Build in the CLI:
swiftgen

You're ready to start interacting with TypeScript in your Swift code! If you add new TypeScript variables or functions, simply build again with swiftgen to generate an updated file.

 

Usage

Refer to Generation Schema by Example for more details.

Basic Syntax

let toggle = TypeSwift.toggle
webView.ts(toggle)

// or
webView.ts(.toggle)

Promises / async await coming soon

webView.somePromise() { result, error
  print(result)
}

let result = await webView.anotherPromise()

 

Preview.mp4

 

Still working on the docs. Check back soon!

 

Releases

No releases published

Packages

No packages published

Languages