Home > Web Front-end > JS Tutorial > Detailed explanation of using Node.js to write a simple command line tool (detailed tutorial)

Detailed explanation of using Node.js to write a simple command line tool (detailed tutorial)

亚连
Release: 2018-06-02 12:04:31
Original
1646 people have browsed it

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

  1. #Enter the command you wrote on the command line to complete the target task

  2. The command line requires global validity

  3. Command line requirements can delete the

  4. command line function and generate a file showing the current date

2. Code part

  1. Create a new file and name it sherryFile

  2. 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');
});
Copy after login
  1. Give execution permissions to the file chmod 755 sherryFile

  2. Enter ./sherryFile in the file path where the file is located

  3. 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
Copy after login

Delete command

rm /usr/local/bin/sherryFile
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template