How do I pre-install plugins?



  • Installing plugins through the UI is great but what if I want to pre-install plugins before I first launch NodeBB?

    I can npm install them but that requires me to know what exact version I need for that version of NodeBB. Is there a way to through the command line ask NodeBB to install the version of a plugin that works with it?


  • GNU/Linux

    Simple bash script (needs jq to be installed besides common curl and of course npm):

    #!/bin/bash
    
    type jq >/dev/null 2>&1 || { echo >&2 "I require \`jq\` but it's not installed.  Aborting."; exit 1; }
    
    nbb_version=$(jq -r -e ".version" < package.json)
    
    echo -e "NodeBB version \033[1m$nbb_version\033[0m"
    
    i=0
    packages=()
    
    while [ "$1" ]; do
      package="nodebb-$1"
      version=$(curl -s https://packages.nodebb.org/api/v1/suggest\?package\="$package"\&version\="$nbb_version" | jq -r -e ".version")
      if [ "$version" == "null" ]; then
        echo -e "\033[31;1mNo suggested version of $package for NodeBB $nbb_version. Skipping.\033[0m"
      else
        packages[$i]="$package@$version"
        i=$(($i+1))
      fi
      shift
    done
    
    if [ ${#packages[@]} -ge 1 ]; then
      echo "> npm install ${packages[@]}"
      npm install ${packages[@]}
    else
      echo "No package to be installed."
      exit 2
    fi
    

    syntax: my-script plugin-shortcuts theme-persona [...] (no nodebb- suffix, can be easily changed thought)
    needs to be called from within your NodeBB-root


  • Admin

    Wow, that's a handy script @frissdiegurke :smile:


  • GNU/Linux

    @julian thanks, just wrote it for @ferik 'cause I've had some spare minutes (and the knowledge to do so).

    Always feel free to use it in any way you'd like to ;-) I have no interest in prohibiting any rights on it :)

    Btw: @psychobunny after the syntax-highlighting of code-blocks gets applied, the horizontal scrollbar is nested within the vertical scrolling block. Thus not visible until scrolled to bottom of it. I consider this a design bug.


  • Community Rep

    ooo, bash script.

    sexy



  • Awesomeness, @frissdiegurke !


  • Admin

    If it weren't for the jq dependency, I'd totally integrate it into the ./nodebb executable, so you could install plugins that way...

    Maybe you could substitute with some grep | awk | sed | tar | feather weirdness :laughing:


  • GNU/Linux

    @julian Let's do it with sed:

    #!/bin/bash
    
    sed_json() {
      # Input stream: JSON content
      # Parameters: key
      # Returns: string or null value, full JSON if key not found, last occurrence of key
      # Requires: no two keys (including nested ones) shall be named alike the given key
    
      string_value_matcher='\([[:blank:]]*\(null\)\|[^"]*"\([^"]*\)"\)'
      cat | sed -n "
        # concat all lines within hold buffer
        1h
        1!H
        $ {
          g
          # replace buffer with string value of key within JSON
          s/^.*\"$1\"[[:blank:]]*:$string_value_matcher.*$/\\2\\3/
          # print value
          p
        }
      "
    }
    
    nbb_version=$(sed_json version < package.json)
    
    echo -e "NodeBB version \033[1m$nbb_version\033[0m"
    
    i=0
    packages=()
    url="https://packages.nodebb.org/api/v1/suggest?version=$nbb_version&package="
    
    while [ "$1" ]; do
      package="nodebb-$1"
      version=$(curl -s "$url$package" | sed_json version)
      if [ "$version" == "null" ]; then
        echo -e "\033[31;1mNo suggested version of $package for NodeBB $nbb_version. Skipping.\033[0m"
      else
        packages[$i]="$package@$version"
        i=$(($i+1))
      fi
      shift
    done
    
    if [ ${#packages[@]} -ge 1 ]; then
      echo "> npm install ${packages[@]}"
      npm install ${packages[@]}
    else
      echo "No package to be installed."
      exit 2
    fi
    

  • Community Rep

    Couldn't you just modify the package.json to include the plugins you want as a dependency?



  • @BDHarrington7 no because NodeBB has it own plugin version / system version matching. You could be installing a plugin that's too recent for your version of NodeBB.


  • Community Rep

    Well yeah, but you can install the wrong version using the script too.

    Under the hood, the ACP plugin installer just runs an npm i for the plugin you want, which installs the package in the node_modules folder.

    Npm uses the package.json file to figure out what to install as dependencies for the NodeBB platform, which get installed in the same folder. So when you run npm i in the NodeBB folder, it looks at the package.json file, and installs the dependencies. So, technically you can add whatever plugin you want as a dependency in the package.json folder (adding the appropriate version number that you know will work). At that point, you'd just have to go into the ACP and activate them.

    The only caveat is you'd have to mind what's happening to that root package.json file when you're updating, to avoid conflicts / losing your settings.

    Addressing the original question, you can specify a specific version you want to install by specifying the version of a package you want, i.e. "nodebb-plugin-imgbed": "0.2.1"

    https://docs.npmjs.com/files/package.json @ "dependencies"



  • @BDHarrington7 I understand that but the script installs the specific version that's compatible with my nodebb based on the response from packages.nodebb.org/api/v1/suggest.

    For example: https://packages.nodebb.org/api/v1/suggest?package=nodebb-plugin-composer-default&version=0.9.1


  • GNU/Linux

    @BDHarrington7 said:

    Under the hood, the ACP plugin installer just runs an npm i for the plugin you want, which installs the package in the node_modules folder.

    Under the hood NodeBB runs an npm i package@version with the version resolved via NBBPM (packages.nodebb.org). And this is exactly what @ferik wanted without the need of using the NodeBB web-interface.

    Well yeah, but you can install the wrong version using the script too.

    True if you have listings within the package.json that are not compatible with the plugin authors suggestion.
    But if you have such listings, you wouldn't call the script with this package in the first place since you already know which version you want to have installed.

    Addressing the original question, you can specify a specific version you want to install by specifying the version of a package you want, i.e. "nodebb-plugin-imgbed": "0.2.1"

    As @ferik already said this requires you to know the version of the package you want to use that is compatible with your version of NodeBB. And the original question is how to do this automatically. You might want to read the second sentence of the original question again ;)

    @ferik said:

    I can npm install them but that requires me to know what exact version I need for that version of NodeBB. Is there a way to through the command line ask NodeBB to install the version of a plugin that works with it?



  • @frissdiegurke Yup, you answered all my questions, thank you!


  • Community Rep

    @frissdiegurke yeah I missed that "automatically" part :D

    In my defense, I'd rather reduce the number of external dependencies, and knowing the versions compatible, could still make an installation reproducible without needing to depend on anything. Maybe I just like doing things the hard way, haha.

    Still, kudos for that quick script! Wasn't trying to make it sound like wasted work.


  • Community Rep

    Seriously, a down vote for admitting I missed something and lauding frissdiegurke's script? :|


  • Translator

    @BDHarrington7 said:

    Seriously, a down vote for admitting I missed something and lauding frissdiegurke's script? :|

    missclicked!! haha.

    just wanted to upvote, the opposite.


  • Community Rep

    Haha! True, those voting buttons are really tiny on mobile... @psychobunny :P


  • Admin

    Just keeping you guys on your toes

    Tbh a forum like this should be using the "Likes" plugin instead, flagging is enough for spam etc


  • Translator

    @psychobunny There's just something with pressing the flagging button for some reason on bots, it feels... Rewarding.


Log in to reply
 

Looks like your connection to NodeBB was lost, please wait while we try to reconnect.