Saturday, 13 August 2016

Post 63: writing nodeBB plugin

Structure and files:
# nodeBB-plugin-thad-test
|  # lib
|  |  - controllers.js
|  # static
|  |  # lib
|  |  |  - admin.js
|  |  |  - main.js
|  |  # templates
|  |  |  # admin
|  |  |  |  # plugins
|  |  |  |  |  - thadTest.tpl
|  - library.js
|  - package.json
|  - plugin.json
|  - README.md
|  - ... (other files that are not important though like README.md; .gitignore; .npmignore)

----------------
The file content:
controller.js

'use strict';

var Controllers = {};

Controllers.renderAdminPage = function (req, res, next) {
 /*
  Make sure the route matches your path to template exactly.

  If your route was:
   myforum.com/some/complex/route/
  your template should be:
   templates/some/complex/route.tpl
  and you would render it like so:
   res.render('some/complex/route');
 */

 res.render('admin/plugins/thadTest', {});
};

module.exports = Controllers;
admin.js

'use strict';
/* globals $, app, socket */

define('admin/plugins/quickstart', ['settings'], function(Settings) {

 var ACP = {};

 ACP.init = function() {
  Settings.load('quickstart', $('.quickstart-settings'));

  $('#save').on('click', function() {
   Settings.save('quickstart', $('.quickstart-settings'), function() {
    app.alert({
     type: 'success',
     alert_id: 'quickstart-saved',
     title: 'Settings Saved',
     message: 'Please reload your NodeBB to apply these settings',
     clickfn: function() {
      socket.emit('admin.reload');
     }
    });
   });
  });
 };

 return ACP;
});


main.js
"use strict";

(function() {
 /*
  This file shows how client-side javascript can be included via a plugin.
  If you check `plugin.json`, you'll see that this file is listed under "scripts".
  That array tells NodeBB which files to bundle into the minified javascript
  that is served to the end user.

  Some events you can elect to listen for:

  $(document).ready();   Fired when the DOM is ready
  $(window).on('action:ajaxify.end', function(data) { ... });   "data" contains "url"
 */

 console.log('nodebb-plugin-thad-test: Hello world');
 // Note how this is shown in the console on the first load of every page
}());


thadTest.tpl

thadTest Settings
Adjust these settings. You can then retrieve these settings in code via: meta.settings.get('quickstart');


library.js
"use strict";

var controllers = require('./lib/controllers'),
 plugin = {};

plugin.init = function(params, callback) {
 var router = params.router,
  hostMiddleware = params.middleware,
  hostControllers = params.controllers;
  
 // We create two routes for every view. One API call, and the actual route itself.
 // Just add the buildHeader middleware to your route and NodeBB will take care of everything for you. 
 router.get('/admin/plugins/quickstart', hostMiddleware.admin.buildHeader, controllers.renderAdminPage);
 router.get('/api/admin/plugins/thadTest', controllers.renderAdminPage);
 
 callback();
};

plugin.addAdminNavigation = function(header, callback) {
 header.plugins.push({
  route: '/plugins/thadTest',
  icon: 'fa-tint',
  name: 'Quickstart'
 });

 callback(null, header);
};

module.exports = plugin;


package.json
{
  "name": "nodebb-plugin-thad-test",
  "version": "0.2.6",
  "description": "nodebb-plugin-thadeuszlay-test",
  "main": "library.js",
  "repository": {
    "type": "git",
    "url": "https://github.com/thadeuszlay/nodebb-plugin-thad-test"
  },
  "keywords": [
    "nodebb",
    "plugin",
    "test",
    "shell"
  ],
  "author": {
    "name": "thadeuszlay",
    "email": "thadeuszlay@gmail.com"
  },
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/thadeuszlay/nodebb-plugin-thade-test/issues"
  },
  "readmeFilename": "README.md",
  "nbbpm": {
    "compatibility": "^1.0.0"
  }
}


plugin.json
{
  "name": "nodebb-plugin-thad-test",
  "version": "0.2.6",
  "description": "nodebb-plugin-thadeuszlay-test",
  "main": "library.js",
  "repository": {
    "type": "git",
    "url": "https://github.com/thadeuszlay/nodebb-plugin-thad-test"
  },
  "keywords": [
    "nodebb",
    "plugin",
    "test",
    "shell"
  ],
  "author": {
    "name": "thadeuszlay",
    "email": "thadeuszlay@gmail.com"
  },
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/thadeuszlay/nodebb-plugin-thade-test/issues"
  },
  "readmeFilename": "README.md",
  "nbbpm": {
    "compatibility": "^1.0.0"
  }
}



README.md
# Quickstart Plugin for NodeBB

A starter kit for quickly creating NodeBB plugins. Comes with a pre-setup LESS file, server side JS script with an `action:app.load` hook, and a client-side script. Most plugins need at least one of the above, so this ought to save you some time. For a full list of hooks have a look at our [wiki page](https://github.com/NodeBB/NodeBB/wiki/Hooks), and for more information about creating plugins please visit our [documentation portal](https://docs.nodebb.org/).

Fork this or copy it, and using your favourite text editor find and replace all instances of `nodebb-plugin-quickstart` with `nodebb-plugin-your-plugins-name`. Change the author's name in the LICENSE and package.json files.

Once you're done don't forget to publish it on NPM, and make a thread about it [here](https://docs.nodebb.org/en/latest/plugins/hooks.html).


## Hello World

It will do some random stuff. As of time of writing, I don't even know myself what exactly. Really simple, just edit `static/lib/main.js` and paste in `console.log('hello world');`, and that's it!

## Installation

    npm install nodebb-plugin-thad-test

## Screenshots

Don't forget to add screenshots!

Routing

In order to display your plugin correctly in the nodeBB admin page you have to set the route correctly:

in controller.js:
Controllers.renderAdminPage = function (req, res, next) {
 res.render('admin/plugins/thadTest', {});
};



in library.js:
"use strict";

plugin.init = function(params, callback) {
...
 router.get('/api/admin/plugins/thadTest', controllers.renderAdminPage);
...
};

plugin.addAdminNavigation = function(header, callback) {
 header.plugins.push({
  route: '/plugins/thadTest',
...
  name: 'Name in the dropdown list'
 });
};

module.exports = plugin;

Post 62: Install nodeBB with redis

fork nodeBB project or clone it directly from nodeBB

git clone -b v1.x.x https://github.com/NodeBB/NodeBB.git nodebb

cd to nodeBB directory

install redis (this is installing a newer version than on the one that is already installed on c9):

sudo apt-add-repository ppa:chris-lea/redis-server
sudo apt-get update
sudo apt-get install redis-server

//redis-server --port 16379 --bind $IP &

echo $IP


npm install


node app --setup


--> set url:
jubilee-c9-thadeuszlay.c9users.io
enter for nodeBB secret
for database: redis
set administrator name


node app

If you want to drop your redis database:
log into redis:
redis-cli
Once you are in redis you can type in either of those:
  • FLUSHDB - Removes data from your connection's CURRENT database.
  • FLUSHALL - Removes data from ALL databases.
To exit redis type in exit
-------------------------
It's always good to stay up-to-date of your nodeBB. If you want to upgrade 

1. Shut down your forum

$ cd /path/to/nodebb
$ ./nodebb stop

2. Back up your data

Redis

As with all upgrades, the first step is to back up your data! Nobody likes database corruption/misplacement.
All of the textual data stored in NodeBB is found in a .rdb file. On typical installs of Redis, the main database is found at /var/lib/redis/dump.rdb.

Avatars

Uploaded images (avatars) are stored in /public/uploads. Feel free to back up this folder too:
cd /path/to/nodebb/public
tar -czf ~/nodebb_assets.tar.gz ./uploads

3. Grab the latest and greatest code

$ git fetch    # Grab the latest code from the NodeBB Repository
$ git checkout v0.4.x    # Type this as-is! Not v0.4.2 or v0.4.3, but "v0.4.x"!
$ git merge origin/v0.4.x

If not upgrading between branches (e.g. v0.3.3 to v0.3.4, just run the following commands:
$ git fetch
$ git reset --hard origin/v0.3.x    # Replace v0.3.x with the branch name!
Don’t know what branch you are on? Execute git rev-parse --abbrev-ref HEAD to find out.

4. Run the NodeBB upgrade script

This script will install any missing dependencies, upgrade any plugins or themes (if an upgrade is available), and migrate the database if necessary.
$ ./nodebb upgrade
Note./nodebb upgrade is only available after v0.3.0. If you are running an earlier version, run these instead:
  • npm install
  • ls -d node_modules/nodebb* | xargs -n1 basename | xargs npm update
  • node app --upgrade

Friday, 12 August 2016

Post 61: How to publish npm packaage


create a project for that package if you haven't done so

push it to git hub
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/yourUsername/nodebb-plugin-your-project.git
git push -u origin master

create an a user account on npm if this is your first time:
npm adduser

Type in your username, password, and e-mail (e-mail will be public). Store your crendentials on your local machine:
npm login

To ensure that your credentials or stored on the client type in:
npm config ls

To ensure that your acccount was created successfully go to the following URL and you should see your account:
https://npmjs.com/~/[your-username]

cd to the folder of the module or plugin you want to publish on npm . and type in:
npm publish

Note that everything in the directory will be included unless it is ignored by a local.gitignore or .npmignore file as described in npm-developers.

To ensure that your package is publish go to 
https://npmjs.com/package/[your-package-name]


Tweet