Setting Environment Variables is great, but it has it's limitations.
When passing complex data to a script, for instance, escaping becomes cumbersome and difficult. For instance, for a string with a semi-colon ";", there's always some finagling that has to happen in order to make sure that the semi-colon is serialized and then deserialized to prevent things from breaking.
But if we had the ability to use stdin
, it would enable us to send more complex data to a target script:
let shellScriptWrapper = {
script: "/absolute/path/to/my_script.js",
launchPath: '/bin/bash',
parameters: '-c',
stdin: JSON.stringify(someObj)
};
let result = await runShellScript(shellScriptWrapper);
With a script like:
// my_script.js
(async () => {
let chunks = [];
for await (let chunk of process.stdin) {
chunks.push(chunk);
}
const raw = chunks.join('');
try {
const obj = JSON.parse(raw);
console.log(obj);
} catch (err) {
console.error('Invalid JSON:', err.message);
process.exit(1);
}
})();