# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from __future__ import print_function, absolute_import
import json
import os.path as osp
from jupyterlab_server.process import which
from jupyterlab_server.process_app import ProcessApp

HERE = osp.dirname(osp.realpath(__file__))


def _jupyter_server_extension_points():
    return [
        {
            'module': __name__,
            'app': NodeApp
        }
    ]

class NodeApp(ProcessApp):

    name = __name__
    serverapp_config = dict(
        disable_check_xsrf = True,
        allow_origin = "*",
        token=""
    )

    def get_command(self):
        """Get the command and kwargs to run.
        """
        # Run the node script with command arguments.
        config = dict(
            baseUrl='http://localhost:{}{}'.format(self.serverapp.port, self.settings['base_url']),
            token="")

        with open(osp.join(HERE, 'config.json'), 'w') as fid:
            json.dump(config, fid)

        cmd = [which('node'),
               'index.js', '--jupyter-config-data=./config.json']
        return cmd, dict(cwd=HERE)


if __name__ == '__main__':
    NodeApp.launch_instance()
