def build_or_install_bokehjs():
    ''' Build a new BokehJS (and install it) or install a previously build
    BokehJS.

    If no options ``--build-js`` or ``--install-js`` are detected, the
    user is prompted for what to do.

    If ``--existing-js`` is detected, then this setup.py is being run from a
    packaged sdist, no action is taken.

    Note that ``-build-js`` is only compatible with the following ``setup.py``
    commands: install, develop, sdist, egg_info, build

    Returns:
        str : one of 'built', 'installed', 'packaged'
            How (or if) BokehJS was installed into the python source tree

    '''

    # This happens when building from inside a published, pre-packaged sdist
    # The --existing-js option is not otherwise documented
    if '--existing-js' in sys.argv:
        sys.argv.remove('--existing-js')
        return "packaged"

    if '--build-js' not in sys.argv and '--install-js' not in sys.argv:
        jsbuild = jsbuild_prompt()

    elif '--build-js' in sys.argv:
        jsbuild = True
        sys.argv.remove('--build-js')

    # must be "--install-js"
    else:
        jsbuild = False
        sys.argv.remove('--install-js')

    jsbuild_ok = ('install', 'develop', 'sdist', 'egg_info', 'build')
    if jsbuild and not any(arg in sys.argv for arg in jsbuild_ok):
        print("Error: Option '--build-js' only valid with 'install', 'develop', 'sdist', or 'build', exiting.")
        sys.exit(1)

    if jsbuild:
        build_js()
        install_js()
        return "built"
    else:
        install_js()
        return "installed"