Skip to content

Instantly share code, notes, and snippets.

@jRimbault jRimbault/cat.js
Last active Dec 13, 2018

Embed
What would you like to do?
#!/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
You can’t perform that action at this time.