What household remotes taught me about User Experience

This is a tale of 2 remote controls. It’s also an awesome lesson about effective UX and that newer doesn’t always make it better. Press on (see what I did there?) and learn something about building…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How to Build a Command Line Tool with NPM

in the command line (or terminal, whatever you want to call it) and it will create that file in whatever directory (folder) you’re in with some basic starter HTML.

In this article, you will learn how to build a command line tool with the same functionality.

What the finished product will look like

First, run npm init and entered the package info. You can just press enter a bunch of times to get a default package, or you can fill out the fields.

Next, edit your package.json file to look like this:

preferGlobal is set to true because it’s meant to be installed globally (meaning that the npm package isn’t just installed in one repository, so a user can access it from anywhere on the computer.) If somebody tries to install your module without the --global tag, they’ll receive a warning that it’s meant to be installed globally.

You’ll also notice that under bin there is the following code:

This is extremely important. This is saying that when a user runs html-init via the command line, index.js will run. THIS IS THE BASIC SETUP for our command line tool. Of course, html-init could be substituted for any other script name, and the same with index.js .

There is just one thing left to do to get your module working: edit your index.js file. My file looks like this:

The top line, sometimes called the “shebang line”, just has to be there — don’t worry about that right now. You’ll notice that I require fs-extra . This is an extremely useful NPM module for editing/creating files.

process.argv is an array that contains all of the command line arguments passed into index.js . The first 2 arguments don’t matter, just look at the later ones. process.argv[2] contains the parameter right after the html-init call. So, if I type html-init index.html , process.argv[2] will equal index.html .

process.cwd() gets the directory you’re in on the command line. So if your command line looks like this: C:/Users/username> (that’s on a Windows computer) then that’s what process.cwd() will evaluate to.

The rest of the code is fairly simple. var basicHTML is set to a template literal containing the contents of a basic HTML file. Don’t worry if you don’t know what that is, just treat it like a string for now.

const file = process.cwd() + “/" + process.argv[2] is setting the filepath to the current working directory plus a path to a file which is set in the argument after html-init (or whatever command you choose to have).

Then, we use fse (that’s what I required it as, but remember to call the function with whatever name you require it as) to output the basicHTML content to file (where we want the document to go.)

Now you have a command line tool! Keep in mind: when you output with fs-extra , it will overwrite file data there if that file exists, or create a new file. Be careful!

Add a comment

Related posts:

The SPE Sponsors Workshop on Slim Hole Drilling and Completion

As a senior vice president of asset valuation and operations with West Energy Capital/Offshore in Los Angeles, California, William Kelleher builds upon more than 3–1/2 decades of experience in the…

Healthcare Snake Oil

I happen to believe that, once we actually actively work to save the democracy from collapse by encouraging voters to show up instead of helping the current Republican Party in their mission to…

Dynamic Cell in iOS

UITableView is prevalent in our iOS apps. We could see it anywhere in an app. But as a freshman, it looks bothersome in developing. I want to share some methods to perform the dynamic cell in…