Run Scripts
Using the jko
, you can execute any standard script defined in jko.js
, package.json
or in any other file.
Run a Script from jko.js
Given the following jko.js
:
export default {
scripts: {
yourScript: "someCommand"
}
}
Execute:
$ jko yourScript
someCommand
will be executed.
Commands to execute must be available via a path specified by the environment variable $PATH
, in the current folder or within local directory ./.bin
, or provided by a package installed locally (./node_modules/.bin
) or globally.
If you need to modify
$PATH
, you can set it using an environment file
Run a Script from package.json
Given the following package.json
:
{
"scripts": {
"yourScript" : "someCommand"
}
}
Execute:
$ jko yourScript
someCommand
will be executed.
When jko
is run, it will search for jko.js
. If that file is not found, it will then look for package.json
. If neither file is found, an error message will be displayed.
$ jko
◤
jko error - No configuration file found
Please ensure that either jko.js or package.json exists in the working directory,
or use --config-file to specify a custom one.◣
jko.js
takes precedence over package.json
.
Run a Script with Arguments
Given the following package.json
:
{
"scripts": {
"yourScript" : "someCommand $jko_arg_0 ... $jko_arg_N_1"
}
}
Execute:
$ jko yourScript arg1 ... argN
jko
will execute someCommand
with arg1
... argN
as arguments, where arg1
is the first argument and argN
is the last argument.
jko
will create an environment variable for each argument passed, 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.
Run Scripts from a File
$ jko --config-file=./path/to/config.js yourScript arg1 ... argN
or
$ jko -c=./path/to/config.js yourScript arg1 ... argN
File can be any .js
, .mjs
, .cjs
or .json
file.
The file path must start with /
, ./
, or ../
.
Run Scripts from a Package
$ jko --config-file=somePackageName yourScript arg1 ... argN
or
$ jko -c=somePackageName yourScript arg1 ... argN
The package must be available, i.e. it must be installed first.
Run Scripts in a Specified Environment
$ jko --env-file=./path/to/.env yourScript arg1 ... argN
or
$ jko -e=./path/to/.env yourScript arg1 ... argN
or define the envFile
value at jko.js
(or package.json
)
export default {
scripts: {
yourScript: "someCommand",
},
envFile: './path/to/.env'
}
$ jko yourScript
command line option --env-file
takes precedence over envFile
.
What happens if the script is not found
Given the following jko.js
:
export default {
scripts: {
yourScript: "someCommand",
}
}
If you run yourScript1
, jko
will attempt to retrieve it. If it is not found, the jko
will stop.
And it will display suggestions if available:
$ jko yourScript1
someText1
◤
jko error - yourScript1 not found, similar scripts are:
yourScript: 'someCommand'
◣
or if no suggestions are available, it will simply show an error:
$ jko yourScript1
◤
jko error - yourScript1 not found.◣
List available scripts
To list available scripts, run jko
without any arguments:
$ jko
Even if no script is defined, at least one task is available: install
.
Output
Exit Code Reference
Code | Description |
---|---|
0 | Execution completed successfully. No errors detected. |
10 | Error encountered with an imported file, such as missing or unreadable files. |
11 | Error with the script being executed: not found, incorrect script type, etc. |
12 | Installation error: dependencies not found or issues during installation process. |
13 | Invalid command-line option: incorrect parameters, unrecognized log level, etc. |
## | When a script fails, jko returns its exit code, which can vary based on the script’s execution. |
- Always verify the existence and validity of imported files (Error 10).
- Ensure the target script exists and follows correct formatting (Error 11).
- or use
$disable
declarative.
- or use
- Check dependency declarations to avoid installation failures (Error 12).
- Review command-line arguments carefully to prevent option-related errors (Error 13).
- If a script fails (##), investigate the returned exit code for further troubleshooting.
- Setting an appropriate logging level can help catch potential issues.
CLI output
jko
will display:
- a green
end
label in the output when the script executes successfully—that is, when the error code is zero:
$ jko yourScript
...
◤
jko end - Successful execution of yourScript.◣
- a orange
end
label in the output when the script executes successfully but warnings have been raised:
$ jko yourScript
...
◤
jko end - Successful execution of yourScript.◣
- a red
end
label in the output when the script executes but fails by returning a non-zero error code (##
):
$ jko yourScript
...
◤
jko end - yourScript: " ... " Failed.◣
- a red
error
label in the output when a misconfiguration (10
,11
,12
,13
) prevents a script from executing:
$ jko yourScript
...
◤
jko error - yourScript not found.◣
When chaining commands, if a failure occurs, jko
makes it easy to identify the last task that failed. Check the following example:
$ jko check
...
◤
jko end - Successful execution of lint.common.◣
...
◤
jko end - lint.bin: "someLintCli someFile" Failed.◣
...
◤
jko end - check: "jko install && jko check.js && jko lint.common && jko lint.bin && jko test.cov" Failed.◣
It is clear that lint.bin
is the task that fails. Now you can focus on fixing lint.bin
—by running jko lint.bin
repeatedly until it passes—and then return to jko check
once it's fixed.