Skip to content

Instantly share code, notes, and snippets.

@jRimbault
Last active December 13, 2018 19:11
Show Gist options
  • Save jRimbault/1c91bfc19874f810db3099a457d3f06e to your computer and use it in GitHub Desktop.
Save jRimbault/1c91bfc19874f810db3099a457d3f06e to your computer and use it in GitHub Desktop.
#!/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())
}

Both print the content of the file.

cat file | cat.js
cat.js file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment