How to pass parameters to a script processed by ts-node - ts-node

I just started ts-node utilizing. It is the a very convenient tool. Run time looks clear. But it does not work for CLI solutions. I can not pass arguments into a script compiled.
ts-node --preserve-symlinks src/cli.ts -- printer:A
It does not work. I am asking for a help.

You did not provide your script, so I can only guess at how you are extracting the arguments. This is how I have made it work with my own test script args.ts:
const a = process.argv[2];
const b = process.argv[3];
const c = process.argv[4];
console.log(`a: '${a}', b: '${b}', c: '${c}'`);
Called from package.json like this:
"scripts": {
"args": "ts-node ./args.ts -- 4 2 printer:A"
}
This will give me output like this:
a: '4', b: '2', c: 'printer:A'

command
ts-node ./test.ts hello stackoverflow
ts file
console.log("testing: >>", process.argv[2], process.argv[3]);
output
$ testing: >> hello stackoverflow
Happy coding

Try this:
node --preserve-symlinks -r ts-node/register src/cli.ts printer:A

NODE_OPTIONS
For the case of node options, in addition to -r ts-node/register mentioned at https://stackoverflow.com/a/60162828/895245 they now also mention in the docs the NODE_OPTIONS environment variable: https://typestrong.org/ts-node/docs/configuration/#node-flags
NODE_OPTIONS='--trace-deprecation --abort-on-uncaught-exception' ts-node ./index.ts
A quick test with:
main.ts
(async () => { throw 'asdf' })()
and run:
NODE_OPTIONS='--unhandled-rejections=strict' ts-node main.ts
echo $?
which gives 1 as expected.
Tested on Node v14.16.0, ts-node v10.0.0.

Related

Ampersand to run process in background causes invalid parameters in Bash 5

I have a bash script that runs perfectly well on Bash 3.2. The script contains an ampersand to run a process in the background. However, when I run it in Bash 5.x, it doesn't pass the variables correctly (I get a "SyntaxError: Unexpected end of JSON input"). When I take off the ampersand at the end (of the mgeneratejs line), it executes normally in Bash 5.
#!/bin/bash
#Works on Bash 3.2 on MacOS
#Doesn't work in bash-5.0/5.1
##!/usr/bin/env bash
NUM_ROWS_PER_RUN=5
NUM_RUNS=2
TEMPLATE_STRING='{
name: "$name"
}'
for i in $(seq 1 "$NUM_RUNS")
do
echo "Starting run ${i}"
#If you dont have it, then run "npm install -g mgeneratejs"
mgeneratejs -n "$NUM_ROWS_PER_RUN" "${TEMPLATE_STRING//[$'\r\n ']}" &
done
echo "Waiting"
wait
echo "Finished"
How can I get the process (mgeneratejs) to run in the background when using Bash 5.x?
Bash may or may not be in fault here, but be sure that the problema is in mgeneratejs.
Taking a look mgeneratejs's source code I found this:
if (process.stdin.isTTY) {
var str = argv._[0];
template = _.startsWith(str, '{') ? parseTemplate(str) : parseTemplate(read(str, 'utf8'));
generate();
} else {
template = '';
process.stdin.setEncoding('utf-8');
process.stdin.on('readable', function() {
var chunk = process.stdin.read();
if (chunk !== null) {
template += chunk;
}
});
process.stdin.on('end', function() {
template = JSON.parse(template);
generate();
});
}
If stdin is not a TTY then mgeneratejs assumes that stdin is a pipe, and tries to read from it. This is wrong, they should at least check if the template has been given in the command line args.
I would't recommend that you fix mgeneratejs, but I can recommend you to do this:
function do_run() {
echo "${TEMPLATE_STRING//[$'\r\n ']}" | mgeneratejs -n "$NUM_ROWS_PER_RUN"
}
for i in $(seq 1 "$NUM_RUNS")
do
echo "Starting run ${i}"
#If you dont have it, then run "npm install -g mgeneratejs"
do_run &
done

Bash array in Declarative Jenkinsfile

How do I use shell arrays in a Jenkinsfile?
My Jenkins job has a String parameter PROJECTS that is a comma-separated list of projects to build. I have a Build step in which I run some shell script to split that parameter into an array, and then pass that array to a build script:
...
stage("Build") {
steps {
sh"""
projects_list=(${env.PROJECTS//,/ })
./build_script ${projects_list[#]}
"""
}
}
...
however, the Jenkins build keeps failing due to this:
WorkflowScript: 132: unexpected token: # # line 132, column 104.
build_script ${projects_list[#]}
^
1 error
Please see the below code which gives desired result:
Please note : I am using bat command and calling shell scripts inside via cygwin as am using Windows machine.
...
def PROJECTS = "ABC,XYZ"
stage("Build") {
steps {
bat'cygwin.bat -c \"projects_list=(${PROJECTS//,/ }); ./buildscript.sh ${projects_list[#]} \"'
}
}
...
cygwin.bat
IF [%1] == [-c] (
C:\Cygwin\bin\bash.exe -l -i %*
) ELSE (
startC:\Cygwin\bin\mintty.exe --exec C:\Cygwin\bin\bash.exe -l -i
)
With sh: The syntax would be same, just use sh rather than bat and call the command without cywgin.bat -c

How to print out only the number of eslint errors in the terminal?

The project I'm working with has a huge code-base, which means that if I do eslint *.js in the terminal, I get thousands of lines in the output. I want to tweak this command only to print out the number of errors, not to actually list all the errors one by one.
What to do to make my results similar to this:
96 problems
Thinking a bit more, if you really just want a single number, then create your own formatter that would look something like this.
const errorsInFile => (el, currentEl) => el + currentEl.errorCount
module.exports = function (results) {
return `${results.reduce(errorsInFile, 0)} problems`
}
Or just for fun, we could do it functionally with Ramda
import { map, pipe, prop, reduce, sum } from 'ramda'
const sumArgs = (...args) => sum(args)
const nProblems = n => `${n} problems`
module.exports = pipe(
map(prop(‘errorCount’),
reduce(sumArgs),
nProblems,
)
You might find this userful, it will give you the number of each type of error
https://www.npmjs.com/package/eslint-formatter-summary-chart
% eslint --format summary-chart src
==== Files ====
bar.js : ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 33.33%
foo.js : ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 66.67%
==== Rules ====
constructor-super : ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 16.67%
no-cond-assign : ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 16.67%
no-constant-condition : ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 16.67%
no-debugger : ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 16.67%
no-unused-vars : ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 33.33%
In a shell script (bash or zsh, for example) you can count the lines of errors output by eslint using wc, and echo it to the console.
ERRCOUNT=$(eval "eslint | wc -l")
echo $OUTPUT errors
Example result:
185 errors
You can try:
eslint --format compact 2>&1 >/dev/null | egrep '^[0-9]+ problems'
Result:
169 problems
According to the documentation (https://eslint.org/docs/user-guide/command-line-interface), you can run with flag --quiet
eslint --quiet 'src/**'

How to pass jenkins parameters as array to bash script?

I want to pass in jenkins parameters as array args to my bash script.
I have tried below but no luck.
stage('Foo Step') {
steps {
script {
sh "chmod 755 runFooBar.sh"
sh """
./runFooBar.sh --baz="${params.BAZ[#]}" --bar="${params.BAR}" --foo="${params.FOO[#]}"
"""
}
}
}
I am getting this error when I run above script.
WorkflowScript: 48: unexpected token: # # line 48, column 75.
./runFooBar.sh --baz="${params.BAZ[#]}" --bar
^
Thank you in advance.
Cheers!
Had similar issue to resolve.
Try to remove the '[#]' as groovy seems not to appreciate this.
In my case it looks as follows and works thus far:
steps {
script {
RECIPIENTS = '"user1#domain" "user2#domain" "user3#domain"'
sh "./send_email.sh ${RECIPIENTS}"
}
}

How to run a single test in nightwatch

How do I run only Test 3 from the following tests?
module.exports = {
'Test 1':function(){},
'Test 2':function(){}
'Test 3':function(){}
}
A new parameter --testcase has been added to run a specified testcase.
nightwatch.js --test tests\demo.js --testcase "Test 1"
It's a new feature since the v0.6.0
https://github.com/beatfactor/nightwatch/releases/tag/v0.6.0
You must use specific tags before function and separate all functions in diferent files under tests directory, and then call command with --tag argument. See wiki nightwatch tags page and watch this example:
// --- file1.js ---
module.exports = {
tags: ['login'],
'Test 1':function(){
//TODO test 1
}
};
// --- file2.js ---
module.exports = {
tags: ['special', 'createUser'],
'Test 2':function(){
//TODO test 2
},
};
// --- file3.js ---
module.exports = {
tags: ['logoff', 'special'],
'Test 3':function(){
//TODO test 3
},
}
If you run:
nightwatch.js --tag login
only runs Test 1, however if you run:
nightwatch.js --tag special
Test 2 and Test 3 will be executed.
You can specific more than one tag
nightwatch.js --tag tag1 --tag tag2
Separate each test function is mandatory because Nightwatch handled with filematcher each file. See Github code.
PD: If file has syntax errors, is possible that test don't run or test hasn't been found
The --testcase flag can since version 0.6 be used to run a single test from the commandline, e.g.
nightwatch.js --test tests\demo.js --testcase "Test 1"
This could be done using either test groups or test tags. You can also execute a single test with the --test flag, e.g.
nightwatch.js --test tests\demo.js
For me, it only works with:
npm run test -- tests/01_login.js --testcase "Should login into Dashboard"
npm run <script> -- <test suite path> --testcase "<test case>"
my script in package.json:
"test": "env-cmd -f ./.env nightwatch --retries 2 --env selenium.chrome",
at nightwatch version 1.3.4
You can also use tags:
npm run <script> -- <enviroment> <tag>
npm run test -- --env chrome --tag login
just add it to your test case:
module.exports = {
'#tags': ['login', 'sanity', 'zero1'],
...
}
you can do somthing like:
node nightwatch.js -e chrome --test tests/login_test --testcase tc_001
Another possible way of doing so, would be to use the following on each test case that you want to omit:
'#disabled': true,
This can simply be set to false or removed if you wish to test it.

Resources