Creating command line tools using Node

Hi everybody, there are a few things we need to work daily and some tiny tools help us do that job. For example, most of us have formatted our JSON files with those online formatters. There is a lot of our daily task we can automate, by creating tiny tools to help us, with&nbsp;<a href="https://nodejs.org/" target="_blank">Node</a>&nbsp;.&nbsp;Let’s see it by an example.

Introduction

Hi everybody, there are a few things we need to work daily and some tiny tools help us do that job. For example, most of us have formatted our JSON files with those online formatters. There is a lot of our daily task we can automate, by creating tiny tools to help us, with Node . Let’s see it by an example.

What Tech we will be using ?
  •  Node, you should have it installed in your system.
  • JavaScript, You should be comfortable with it.
What we are building ?

For this example, we will be building a simple command line tool with Node that formats JSON files passed to it. Even more, it ignores non-existing and non-JSON files.

What will we learn ?

While we are working on a tiny JSON formatter, It’s not our goal to do this single thing. Instead, we will figure about how we can use Node to create simple command line tools or utilities to automate our daily tasks. Sometimes, It takes less time than googling for a solution and digging into ton of websites, trying different things.

So, Lets jump to our steps.

Step 1. Add a shebang

Probably, our non-UNIX friends will ask, what are you talking about ? If you don’t already know, a shebang ensures that our JavaScript file is treated as an executable and node is used to execute it. Consider it a signal for our shell to invoke Node to run our script file.

Let’s create a file named jf.js and add following content in it.

#!/usr/bin/env node

That’s our shebang. Now let’s give this file executable permission.

Step 2. Require necessary modules

Node have wide range of modules and we will need two of them for our example, fs to handle file read/write and path to manipulate file paths. We can use these modules by requiring them using built-in require function of Node .

const fs = require('fs')
const path = require('path')

Step 3. Function to filter JSON files

Now, let’s write our first function for filtering JSON files.

function isJSON (file) {
  return path.extname(file) === '.json'
}

The modern JavaScript can help us in simplifying it with arrow functions.

const isJSON = file => path.extname(file) === '.json'

As we can see, we pass file name as argument and return true if and only if our file have a json extension. We used extname function from path module to check for file extension.

Step 4. Function to filter existing files

Similarly, we can write our second function for filtering files that exist. We don’t want random non-existing files passed to us.

function isJSON (file) {
  return fs.existsSync(path.join(process.cwd(), file))
}

Or, like earlier, we can use arrow functions here.

const exists = file => fs.existsSync(path.join(process.cwd(), file))

Again, we are passing file name as argument, and returning true only if that file exists. For this, we are using existsSync method from fs module to check if file exists. Further process.cwd() returns the absolute path to current directory and we are using join method from path module to join it with our file name to get absolute path to our file .

Step 5. Getting arguments and filtering files

Arguments in a node script are stored in process.argv which is an Array. furthermore, the first two arguments are —

  • Absolute path of node executable
  • and, absolute path of script file we are executing
if (process.argv.length > 2) {
  const files = process.argv.slice(2, process.argv.length)
} else {
  console.error('No argument passed')
  process.exit(1)
}

Here, we are checking whether an argument is passed or not. If anything is passed, the length of process.argvarray should be more than 2. We are removing first two arguments by slicing the arrayfrom second position to end, hence getting list of arguments or JSON filenames in this case.As files contains file names now, we need to filter files ending with JSON extension and check whether they exist or not. Here will use filter method of Array to do this filtering.

Now, It’s time for us to use isJSON and exists functions we created earlier. We can pass a function to filter method and that function will be applied on every element to check whether that element satisfies our function, and only then that element will be passed down in the chain.

if (process.argv.length > 2) {
  const files = process.argv
    .slice(2, process.argv.length)
    .filter(isJSON)
    .filter(exists)
} else {
  console.error('No argument passed')
  process.exit(1)
}

Our files array now contains only existing JSON files.

Step 6. Reading ugly files and writing formatted files

As we are done with parsing arguments and filtering relevant files, let’s do some real work now, and format each file we received followed by writing it back to a different file.

We can read and parse JSON file using readFileSync method from fs module and passing its output to JSON.parse method.

Assuming file is our file and is in current directory, we can write

let content = JSON.parse(fs.readFileSync(file, 'utf8'))

We can format it with indent of two spaces using JSON.stringify method. The second argument is a replacer function, we are not using that so we are passing null to it. Further, we will write the output, using writeFileSync method from fs module.

fs.writeFileSync(
  'formatted-' + path.basename(file),
  JSON.stringify(content, null, 2)
)

Note that before writing a file, we are appending formatted- before our file name.

Combining these, We can wrap entire read-format-write thing in a try-catch block, and print a nice message on error if something goes wrong.

Assuming we have a file path named file, we can write —

try {
  let content = JSON.parse(fs.readFileSync(file, 'utf8'))
  fs.writeFileSync(
    'formatted-' + path.basename(file),
    JSON.stringify(content, null, 2)
  )
} catch (err) {
  console.log('Failed to read file ' + file + '\n' + err)
  process.exit(1)
}

Our Final Program

Now, looping over our files array and utilizing code from previous steps, We can write our final program as —

#!/usr/bin/env node

const path = require('path')
const fs = require('fs')

const isJSON = file => path.extname(file) === '.json'
const exists = file => fs.existsSync(path.join(process.cwd(), file))

if (process.argv.length > 2) {
const files = process.argv
.slice(2, process.argv.length)
.filter(isJSON)
.filter(exists)

files.forEach(file => {
try {
let content = JSON.parse(fs.readFileSync(file, 'utf8'))
fs.writeFileSync(
'formatted-' + path.basename(file),
JSON.stringify(content, null, 2)
)
} catch (err) {
console.log('Failed to read file ' + file + '\n' + err)
process.exit(1)
}
})
} else {
console.error('No argument passed')
process.exit(1)
}

Finally, we created a command line tool to format our JSON files and we can use it by passing JSON files to it.

./jf.js a.json b.json data.json

Only correct files are parsed, formatted. For example, if only data.json exists, we will end up having a formatted-data.json file.

Further

We used synchronous methods of files system module, so you can try building asynchronous one using callback, promises or async/await. Possibilities are unlimited. Look for your tiny problems you can solve, hunt API docs, try building tiny tools and gradually increase complexity. Share it on GitHuband If no-one cares, at least you don’t have to look again somewhere else to solve that problem.


by MOHIT SINGH



Top 7 Most Popular Node.js Frameworks You Should Know

Top 7 Most Popular Node.js Frameworks You Should Know

Node.js is an open-source, cross-platform, runtime environment that allows developers to run JavaScript outside of a browser. In this post, you'll see top 7 of the most popular Node frameworks at this point in time (ranked from high to low by GitHub stars).

Node.js is an open-source, cross-platform, runtime environment that allows developers to run JavaScript outside of a browser.

One of the main advantages of Node is that it enables developers to use JavaScript on both the front-end and the back-end of an application. This not only makes the source code of any app cleaner and more consistent, but it significantly speeds up app development too, as developers only need to use one language.

Node is fast, scalable, and easy to get started with. Its default package manager is npm, which means it also sports the largest ecosystem of open-source libraries. Node is used by companies such as NASA, Uber, Netflix, and Walmart.

But Node doesn't come alone. It comes with a plethora of frameworks. A Node framework can be pictured as the external scaffolding that you can build your app in. These frameworks are built on top of Node and extend the technology's functionality, mostly by making apps easier to prototype and develop, while also making them faster and more scalable.

Below are 7of the most popular Node frameworks at this point in time (ranked from high to low by GitHub stars).

Express

With over 43,000 GitHub stars, Express is the most popular Node framework. It brands itself as a fast, unopinionated, and minimalist framework. Express acts as middleware: it helps set up and configure routes to send and receive requests between the front-end and the database of an app.

Express provides lightweight, powerful tools for HTTP servers. It's a great framework for single-page apps, websites, hybrids, or public HTTP APIs. It supports over fourteen different template engines, so developers aren't forced into any specific ORM.

Meteor

Meteor is a full-stack JavaScript platform. It allows developers to build real-time web apps, i.e. apps where code changes are pushed to all browsers and devices in real-time. Additionally, servers send data over the wire, instead of HTML. The client renders the data.

The project has over 41,000 GitHub stars and is built to power large projects. Meteor is used by companies such as Mazda, Honeywell, Qualcomm, and IKEA. It has excellent documentation and a strong community behind it.

Koa

Koa is built by the same team that built Express. It uses ES6 methods that allow developers to work without callbacks. Developers also have more control over error-handling. Koa has no middleware within its core, which means that developers have more control over configuration, but which means that traditional Node middleware (e.g. req, res, next) won't work with Koa.

Koa already has over 26,000 GitHub stars. The Express developers built Koa because they wanted a lighter framework that was more expressive and more robust than Express. You can find out more about the differences between Koa and Express here.

Sails

Sails is a real-time, MVC framework for Node that's built on Express. It supports auto-generated REST APIs and comes with an easy WebSocket integration.

The project has over 20,000 stars on GitHub and is compatible with almost all databases (MySQL, MongoDB, PostgreSQL, Redis). It's also compatible with most front-end technologies (Angular, iOS, Android, React, and even Windows Phone).

Nest

Nest has over 15,000 GitHub stars. It uses progressive JavaScript and is built with TypeScript, which means it comes with strong typing. It combines elements of object-oriented programming, functional programming, and functional reactive programming.

Nest is packaged in such a way it serves as a complete development kit for writing enterprise-level apps. The framework uses Express, but is compatible with a wide range of other libraries.

LoopBack

LoopBack is a framework that allows developers to quickly create REST APIs. It has an easy-to-use CLI wizard and allows developers to create models either on their schema or dynamically. It also has a built-in API explorer.

LoopBack has over 12,000 GitHub stars and is used by companies such as GoDaddy, Symantec, and the Bank of America. It's compatible with many REST services and a wide variety of databases (MongoDB, Oracle, MySQL, PostgreSQL).

Hapi

Similar to Express, hapi serves data by intermediating between server-side and client-side. As such, it's can serve as a substitute for Express. Hapi allows developers to focus on writing reusable app logic in a modular and prescriptive fashion.

The project has over 11,000 GitHub stars. It has built-in support for input validation, caching, authentication, and more. Hapi was originally developed to handle all of Walmart's mobile traffic during Black Friday.

Difference between Node.js and React.js and Angular.js

AngularJS turned the whole definition of web development through its existence, what was lacking in previous methods of development was inappropriate structure. It merges use of JavaScript with MVC and HTML with DOM. ReactJS is a JavaScript library and has a view, but lacks model &amp; controller components. React introduced the concept of virtual DOM which is regarded as one of the greatest advantages of React in comparison of AngularJS. Node.js is a JavaScript-based platform used for developing real-time network applications like a chat software.

AngularJS turned the whole definition of web development through its existence, what was lacking in previous methods of development was inappropriate structure. It merges use of JavaScript with MVC and HTML with DOM. ReactJS is a JavaScript library and has a view, but lacks model & controller components. React introduced the concept of virtual DOM which is regarded as one of the greatest advantages of React in comparison of AngularJS. Node.js is a JavaScript-based platform used for developing real-time network applications like a chat software.