pipelinenodejs
v1.0.3
Published
A package for creating pipeline for nodejs
Downloads
2
Readme
pipelineJs
A pipeline system for Node.js
How to create your pipeline ?
There is two ways of creating pipelines. You can:
1. Manually create your pipeline by adding functions to it ...
First, import Pipeline.
const { Pipeline } = require("pipelinenodejs")
Create your function and pipeline and add it !
function yourFunction(args1, args2) {
console.log(args1, args2)
/*if you want to add an other stage after this, you must return a list of arguments to pass to the next function.
But if you want (depending on the arguments and the termination of your function) your pipeline stops, it must return false. */
}
yourPipeline = new Pipeline()
yourPipeline.addStage(yourFunction)
Choose what will trigger your pipeline and then trigger it
if (yourTrigger) {
yourPipeline.trigger(args1, args2)
}
Enjoy !
2. ... or import it from a folder !
This example will use discord.js.
First create a pipeline type.
To do so you will need to call the registerPipelineType
function with two parameter:
- name: This is the name of your pipeline type. It will be needed when creating the pipeline.
- createPipeline: This is a function which will be called every time a pipeline of that type is loaded, and you can use it to trigger the pipeline based on whatever event you want. It accepts a Pipeline object.
const { Client } = require("discord.js")
const pipelineJS = require("pipelinenodejs")
const client = //init your client here
pipelineJS.registerPipelineType("discordMessage", pipeline => {
client.on("messageCreate", (message) => {
pipeline.trigger(message)
})
})
Note: you can retrieve the config by using the getConfig
getter on your pipeline instance
Once your type has been registered, you can create a folder (for this example we are going to call it discord-example
)
mkdir discord-example
Then create a file called config.json
in the folder you just created. There is two required keys, enabled
which define if that pipeline should load and type
which is the type you created before.
This file is what you get when using pipeline.getConfig()
so you can add other keys to use the same type in different contexts.
Best practice: store your data under a settings
key to prevent it from interfering with potentials future system configs.
{
"enabled": true,
"type": "discordMessage"
}
After that, you can create as many files as you want following this naming format <number>-<some comments>.js
. These files are loaded in the ascending order by the number.
// Define if this stage is enabled
module.exports.enabled = true
// The function (whith the same logic as in the first section)
module.exports.accept = (message) => {
if (!message.content.startsWith("&")) return false // stop the pipeline
const args = message.content.split(" ")
// return the args to forward to the next stage
return [args.shift(), args]
}
And finally you can load your pipeline by using the loadPipeline
method with the path of your folder AFTER having registered your type (like in the beginning of this section).
pipelineJS.loadPipeline("discord-example")