0

In my npm script i have the following:

#!/usr/bin/env node
import { main } from './main';
import { CONFIG } from '../config';

(async () => {

    const res = await main(CONFIG);
    process.stdout.write(res.join('\n'));

    return res;

})();

Now want to do some stuff depending on what's been returned in bash script. Attempts to do it so won't work properly:

npm run update-imports &
PID=$!
UpdateResult=$(wait $PID)


if [ -z "$UpdateResult" ];
then
    echo "No imports updated, committing changes"
else
    echo "Check the following files:\n ${UpdateResult}"
    exit 1
fi

In short - if nothing or empty string returned - proceed with executing script, otherwise - exit script with warning.

How do i make it work?

1 Answers1

0

In bash, wait returns the exit value of the process. Not the standard output as you expect. You can use process.exit(value) to return a value.

If you want to capture and process the standard output of node program, see the answer to question: How do I set a variable to the output of a command in Bash?

This should do the work:

UpdateResult=$(npm run update-imports)

if [ -z "$UpdateResult" ];
then
    echo "No imports updated, committing changes"
else
    echo "Check the following files:\n ${UpdateResult}"
    exit 1
fi