Detailed Description: use Node. js to write a simple command line tool, node. js command line
This article describes how to use Node. js to write a simple command line tool:
The operating system must be Linux
1. Objectives
- Enter a self-written command on the command line to complete the target task.
- The command line must be globally valid
- Command Line requirements can be deleted
- Command line function, generate a file, display the current date
2. Code Section
- Create a new file named sherryFile
- Content of file sherryFile
Introduction: generate a file whose content is the current date and creator
#! /usr/bin/env nodeconsole.log('command start');const fs = require('fs');let date = new Date().toLocaleDateString();let data = date + '\n\t' + '——create By karuru';fs.writeFile('./date.txt', data, 'utf8', (err) => { if (err) { console.log('sherryFile command wrong', err); return false; } console.log('writeFile success!!!!'); console.log('command end');});
- Grant the execution permission to the file chmod 755 sherryFile
- Enter./sherryFile in the file path where the file is located.
- If the following content is output, the command is successfully executed.
Command start
WriteFile success !!!!
Command end
Under this directory, a new date.txt file will be generated. The content is as follows:
2/28/2018
Create By karuru
Change the command to globally valid
ln sherryFile /usr/local/bin/sherryFile
DELETE command
rm /usr/local/bin/sherryFile
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.