Function Scripts
Apart from standard scripts, jko
allows the execution of functions defined in JavaScript files.
Run a Function Script
Given the following jko.js
:
export default {
scripts: {
functionScript: function () {
// do something
}
}
}
$ jko functionScript
functionScript
will be executed.
When scripts are listed, no useful description is displayed beyond the default "Function" label.
$ jko
scripts:
functionScript: Function
Use the $comment
directive to add a descriptive comment.
e.g.
export default {
scripts: {
functionScript: function (param1, paramN) {
// do something
}
'$comment (functionScript)': 'This function script that ...'
}
}
$ jko
scripts:
functionScript: Function
This function script that ...
Run a Function Script with Arguments
Given the following jko.js
:
export default {
scripts: {
functionScript: function (param1, paramN) {
// do something
}
}
}
$ jko functionScript arg1 argN
jko
will run functionScript
with arg1
and argN
as arguments to the function; that is, param1
will receive the value of arg1
and paramN
will receive the value of argN
.
For example:
export default {
scripts: {
functionScript: function (param1, paramN) {
const a = parseFloat(param1)
const b = parseFloat(paramN)
console.log(`${a} + ${b} = ${a + b}`)
}
}
}
$ jko functionScript 1 2
$ 1 + 2 = 3
(Actually, the output will be longer, but it has been simplified to emphasize functionality over presentation. To get the exact same output as in the example, use the following command: jko -l=e functionScript 1 2
)
Additionally, jko
will set an environment variable for every argument passed in the command line, each named jko_arg_#
and accessible in the function code through process.env.jko_arg_#
.
For example:
export default {
scripts: {
functionScript: function () {
const a = parseFloat(process.env.jko_arg_0)
const b = parseFloat(process.env.jko_arg_1)
console.log(`${a} + ${b} = ${a + b}`)
}
}
}
$ jko functionScript 1 2
$ 1 + 2 = 3
(Actually, the output will be longer, but it has been simplified to emphasize functionality over presentation. To get the exact same output as in the example, use the following command: jko -l=e functionScript 1 2
)
jko
will create an environment variable for each argument passed in the command line, named jko_arg_#
, where #
starts at 0
.
For example, jko_arg_0
will contain the value of the first argument, and jko_arg_N_1
will contain the value of the last argument.
arg1
saved in jko_arg_0
arg2
saved in jko_arg_1
arg3
saved in jko_arg_2
...and so on.