Here is some node.js code:
#!/usr/bin/env node
const fs = require('fs');
const header = '#!/usr/bin/env bash\n\n';
try {
const bytes = fs.writeSync(3, header, 0);
} catch (err) {
console.error('Please open file descriptor 3 at the command line.');
console.error('For example:');
console.error('bunion --bash-completion 3> completion-location.sh');
process.exit(1);
}
const w = fs.createWriteStream(null, {fd: 3, encoding: 'utf8', start: header.length});
w.write('foo');
w.end('\n');
you can invoke it like so:
node test/fd-3.js 3> temp.sh
and then in temp.sh you will have:
#!/usr/bin/env bash
foo
this way processes can communicate via stdio on fd3, and you can write to stdout/stderr without worring so damn much about altering the important output. Or use JSON to communicate. In other words, stdout becomes for meta-data (instead of all these unimaginative assh*les using stderr for metadata), and fd3 is used for the data.