Home > Article > Web Front-end > Detailed explanation of using Node.js to write a simple command line tool (detailed tutorial)
This article mainly introduces the detailed explanation of writing a simple command line tool using Node.js. Now I share it with you and give it as a reference.
This article introduces how to write a simple command line tool using Node.js and share it with everyone. The details are as follows:
The operating system needs to be Linux
1. Goal
#Enter the command you wrote on the command line to complete the target task
The command line requires global validity
Command line requirements can delete the
command line function and generate a file showing the current date
2. Code part
Create a new file and name it sherryFile
Contents of file sherryFile
Introduction: Generate a file with the current date and creator
#! /usr/bin/env node console.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'); });
Give execution permissions to the file chmod 755 sherryFile
Enter ./sherryFile in the file path where the file is located
If the following content is output, it means the command execution is successful
command start
writeFile success! !!!
command end
In this file directory, a new date.txt file will be generated with the following content
2/28/2018
create By karuru
Modify the command to be globally valid
ln sherryFile /usr/local/bin/sherryFile
Delete command
rm /usr/local/bin/sherryFile
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Vue.js dynamically assigns value to img's src
Example of defining your own angular time component based on datepicker
vue filter filter instance detailed explanation
The above is the detailed content of Detailed explanation of using Node.js to write a simple command line tool (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!