-
-
Save hfossli/c0818637d67beda27f9767a811dc76df to your computer and use it in GitHub Desktop.
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() |
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() | |
} | |
} | |
} |
Thanks for asking! I bundled the node executable in the app by copying it and adding it to the app target in xcode. Did the same with the js code.
@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 run yarn dev
in applicationDidFinishLaunching
. 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 just cd
into its root and run yarn dev
to start a local server.
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
Hi, I'm trying to start a node server from a swift mac app and came across your example here. It seems that
Bundle.main.path(forResource: "node", ofType: "")
is null though, is there anythng special I have to do to make node available in my app?