Search code examples
javascriptnode.jsnode-webkit

Shared settings location on desktop application


Background

I am trying to write an application with NodeWebKit. This application has TWO parts.

First is the service, which will be installed on the user machine, this service will run on boot and watch a directory for changes.

Second is the GUI client, this will be the client that will specify which directories to watch.

So I was thinking that the GUI client will save a file of the list of the directories specified to be watched and the service will look at that file to know which directories he is going to watch.

The idea is that the when the users installs the Client GUI app, it will also install the service and run that service.

Question

I also need to support cross-platform.

  • How do I know where to save my settings file in OSX and Windows?
  • Should I refactor the architecture?

Solution

  • Regarding your architecture: Sure, that should work though it might not be as efficient on Windows as you might like. (Windows doesn't do file change events very well, but then again, what does it do well? ;)

    And as far as application configuration files are concerned:

    Why don't you save your app config to the user's data directory?

    From the node-webkit API docs describing the nw.gui App object:

    nw.gui.App.dataPath

    Get the application's data path in user's directory.

    • Windows: %LOCALAPPDATA%/<name>
    • Linux: ~/.config/<name>
    • OSX: ~/Library/Application Support/<name>

    where <name> is the field in the manifest.

    (Note: This means the name field in the app's package.json file. So if your app is named "Foo" and you're running under Windows 7 and username "User", nw.gui.App.dataPath will return "C:\Users\User\AppData\Local\Foo")

    I'd recommend you save any app config data as JSON, for ease of loading and access.

    From the node.js docs description of the require function:

    File Modules#

    If the exact filename is not found, then node will attempt to load the required filename with the added extension of .js, .json, and then .node.

    .js files are interpreted as JavaScript text files, and .json files are parsed as JSON text files. .node files are interpreted as compiled addon modules loaded with dlopen.

    (Note: emphasis added for clarity.)

    Voila! C'est simple!