Both print the content of the file.
cat file | cat.js
cat.js file| #!/usr/bin/env node | |
| const fs = require('fs') | |
| const fileinput = { | |
| /** | |
| * Basic behavior of python's method of the same name | |
| * @returns string[] lines of the file passed as an argument or piped | |
| */ | |
| input: () => { | |
| const encoding = 'utf-8'; | |
| const file = process.stdin.isTTY ? process.argv[2] : '/dev/stdin'; | |
| const newline = line => `${line}\n`; // to keep the newlines | |
| const content = fs.readFileSync(file, encoding).split('\n'); | |
| content.pop(); // somehow it reads a last empty line | |
| return content.map(newline); | |
| } | |
| } | |
| function main(file) { | |
| console.info(file.join('')) | |
| } | |
| if (require.main === module) { | |
| main(fileinput.input()) | |
| } |