In the case of an error, the value of process.exit must be greater than 0, whereas a successful exit should equal 0. It is important to make sure you exit your scripts correctly, once again this uses the process object. Now running the command gitsearch jquery -o jquery -l JavaScript will return repositories that mention jquery, are owned by jquery, and use JavaScript. In order to make a NodeJS script installable via npm we need to create an accompanying package.json file in the same directory as your gitsearch.js script. Discussing package managers is way beyond the scope of this post - if you want to learn more about npm, read this excellent article: Package Managers: An Introductory Guide For The Uninitiated Front-End Developer Don’t worry if you don’t really know what npm is. ![]() Doing this will mean you can just type your script’s name, without having to worry about where you are (and where it is). In this example we will be using the command gitsearch, If which gitsearch returns blank then the command is not in use.īecause this is a NodeJS script, we’re going to make this script installable using Node’s package manager, because that’s a normal thing to do if writing NodeJS scripts. This can be checked by using the which command and the name of our command which commandName. It is good practice with command-line utilities to make sure there are no other commands already in use on your system with the same name. The simplest way to create the command would be to call it’s file path and file name which would execute your script./gitsearch.js In order to make it executable, run chmod +x gitsearch.js, which modifies your script’s access permissions so that the program loader can execute it. Your script needs to be “ executable” (so it can be run by the program loader). In our case, we want to run the file with the node interpreter (see this discussion on StackOverflow as to why we use /usr/bin/env node instead of something like /usr/local/bin/node). This informs the system which interpreter to use to run our file. To start, create a new JavaScript file named gitsearch.js and add a shebang to the first line of it. If you already have node installed you should receive a similar response to below, else if the response is blank then node is not installed and you can download the installer from. You can run which node from the command-line to find out if it’s already installed. The only dependency to getting started is Node.js. Everything should work on other unix-like operating systems but this may require some extra research. Creating a command-line utilityīefore I dive too deep into any implementation it’s worth pointing out that this tutorial has been written and tested on a unix-like operating system (OSX). Arguments are often used to identify sources of information, or alter the operation of the command. ArgumentsĪ command-line argument is an item of information passed to the command when run. Options on Unix like systems are often indicated by a hyphen and separated by a space. ![]() Included - a command that requires a separate executable file that is always included with the OS and generally considered part of the OS.Įxternal - a command that requires an external executable file which is not part of the OS and instead added by a third party.Ĭommand-line options can be used to modify the operation of a command. Internal - a command recognized and processed by the command-line which is not dependent upon any external executable file. CommandĬommands are categorized into three types: ![]() The most basic pattern consists of three main components: the command, options and arguments. Understanding the command-lineīefore writing anything for the Unix command-line, no matter what scripting language you are using, it’s important to understand common patterns used for input. If you’d like to jump straight into the source code, it’s available here. I’m going to create a small utility which allows for quick searching of GitHub for reposities based on keywords, owners and languages. Here I want to demonstrate how simple it can be to create useful command-line utilities with the help of node.js. One often overlooked feature of node.js is the ability to create command-line utilities.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |