Home >Web Front-end >JS Tutorial >Make command line tools using Node.js

Make command line tools using Node.js

php中世界最好的语言
php中世界最好的语言Original
2018-04-12 15:14:501381browse

This time I will bring you the Notes on using Node.js to make command line tools and using Node.js to make command line toolsWhat are they? The following is a practical case. Let’s take a look.

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. Command line requirements are globally valid

  3. Command line requirements You candelete

  4. command line function, 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');
});
  1. Give the file execution permission chmod 755 sherryFile

  2. Enter ./sherryFile

  3. ## in the file path where the file is located. #If the following content is output, it means the command execution was 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

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on php Chinese website!

Recommended reading:

##readlineHow to read and write content line by line

Detailed explanation of the use of mutations and actions in Vuex

The above is the detailed content of Make command line tools using Node.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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