Last active
January 2, 2024 00:27
-
-
Save hfossli/c0818637d67beda27f9767a811dc76df to your computer and use it in GitHub Desktop.
Start a node.js server using NSTask in Swift.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let mainBundle = NSBundle.mainBundle() | |
let pathToNode = mainBundle.pathForResource("node", ofType: "")! as String | |
let pathToNodeApp = mainBundle.pathForResource("server", ofType: "js", inDirectory: "Server")! as String | |
let pathToAppFolder = (pathToNodeApp as NSString).stringByDeletingLastPathComponent | |
task = NodeTask(nodeJSPath: pathToNode, appPath: pathToNodeApp, currentDirectoryPath: pathToAppFolder) | |
task.launch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Cocoa | |
class NodeTask: NSObject { | |
private let processIdentifier = NSProcessInfo.processInfo().processIdentifier | |
private let nodeTask = NSTask() | |
private let readPipe = NSPipe() | |
private let errorPipe = NSPipe() | |
private let queue = dispatch_queue_create("NodeTask.output.queue", DISPATCH_QUEUE_SERIAL) | |
private var running = false | |
init(nodeJSPath: String, appPath: String, currentDirectoryPath: String) { | |
super.init() | |
readPipe.fileHandleForReading.readabilityHandler = { [unowned self] (handler: NSFileHandle!) in | |
dispatch_async(self.queue) { | |
if self.running { | |
let data = handler.readDataToEndOfFile() | |
self.onRead(data) | |
} | |
} | |
} | |
errorPipe.fileHandleForReading.readabilityHandler = { [unowned self] (handler: NSFileHandle!) in | |
dispatch_async(self.queue) { | |
if self.running { | |
let data = handler.readDataToEndOfFile() | |
self.onError(data) | |
} | |
} | |
} | |
nodeTask.currentDirectoryPath = currentDirectoryPath | |
nodeTask.launchPath = nodeJSPath | |
nodeTask.arguments = [appPath, "\(processIdentifier)"] | |
nodeTask.qualityOfService = .UserInitiated | |
nodeTask.standardOutput = readPipe | |
nodeTask.standardError = errorPipe | |
} | |
deinit { | |
self.quit() | |
} | |
func launch() { | |
if !running { | |
print("------------------------------ Node launch ------------------------------") | |
running = true | |
nodeTask.launch() | |
} | |
} | |
func quit() { | |
dispatch_sync(self.queue) { | |
self.terminate() | |
} | |
} | |
private func terminate() { | |
if running { | |
print("------------------------------ Node quit ------------------------------") | |
running = false | |
readPipe.fileHandleForReading.closeFile() | |
errorPipe.fileHandleForReading.closeFile() | |
nodeTask.terminate() | |
} | |
} | |
private func stringFromData(data: NSData) -> String { | |
return NSString(data: data, encoding: NSUTF8StringEncoding)!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) as String | |
} | |
private func onRead(data: NSData) { | |
let text = stringFromData(data) | |
if text != "" { | |
print("Node: \(text)") | |
} | |
} | |
private func onError(data: NSData) { | |
let text = stringFromData(data) | |
if text != "" { | |
print("------------------------------ Node fatal error ------------------------------") | |
print(text) | |
self.terminate() | |
} | |
} | |
} |
I would suggest you bundle it all to one file (main.jsbundle) and try to run that.
What's your goal? Embedding next js in app and render it in a webview? In that case you don't need this. Then a simple main.jsbundle and index.html is all you need for the webview.
Is there a way to do this in objective-c instead of swift
I'm pretty sure you can do convert it line by line to objc
I'm pretty sure you can do convert it line by line to objc
Would you be willing to convert this.
I have no experience with swift or objective-c. I'm currently learning
No
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@hfossli, thank you so much for this! I'm trying to adapt it to run a Next.js project that's inside of my Xcode project at
./web
. I'm trying to runyarn dev
inapplicationDidFinishLaunching
. I'd love any tips you might have on how to do that given this code, if you have a moment. It's a brand new, Next.js project without any additional configuration. I usually justcd
into its root and runyarn dev
to start a local server.