always be composin'

Aside from providing a CLI more often, I'm doing my best to see if my application could take input from the stdin.

common use of a cli param

// sorta-composin.js; dont use *Sync in real life if you can help it
const program = require('commander');
const fs = require('fs');

    .option('-f, --file <filePath>', 'The file to read and do stuff with')

const someData = JSON.parse(fs.readfileSync(program.filePath));

// ... here you do some work. Upload to mongo or redis or something.

which supports this usage sorta-composin -f "./data/somefile.json". I don't hate it, and in many cases this can be useful depending on how you have to template commands or write your scripts.

use stdin in like the big kids

Possibly more composable version would be something like this

const process = require('process');

function doThatTwerk(fileContents) {
  const myObj = JSON.parse(fileContents);
  // ... upload to mongo yay whatever

// this shit example doesn't deal with all kinds of things correctly though
// including binary data and some other considerations about how to store the
// chunks as they come or what you want to do with them
// better options include
// * concat with a few library choices
// * `pipe` to another writeable stream you have
// * be in a situation like an HTTP endpoint where this matters
let stdinContents = '';
process.stdin.on('data', (chunk) => {
  if (!chunk) {

  console.log(`debug: reading ${chunk.length} bytes`);
  stdinContents += chunk.toString();

process.stdin.on('end', () => {
  doThatTwerk(stdinContents); // "Am I misreading that function" haha

process.stdin.on('error', (err) => {
  console.log('Daddy-Reverend why have you abaonded us?', err);

What is the target usage now? cat myfile.json | pipe-me. There is the added benefit of not dealing with reading those JSON files and doing that sort of boilerplate.

It's not necessarily more simple, it's just more features you can support. I find it very useful.

cat, grep, echo aren't the only ones that get to play this game; they weren't meant to be.

Along with sneaking the bin entries into your package.json, the intermingling can really begin.

example code always-be-composin

always be cobblin

Show Comments