preface

Cli is developed on Node, so we need to have some prior knowledge.

This article mainly explains these necessary pre-knowledge.

Note: All the examples below use the Macos environment as an example

path

This module provides some small tools for handling file paths, for handling directory objects, to improve user development efficiency, let’s quickly recognize some common methods of path objects!

path.relative()

The method takes two arguments, both of which should be absolute paths.

This method returns the relative path of the second path relative to the first path.

Path.relative (from:string,to:string): String If from or to is not a string, TypeError is raised

import path from 'path'

console.log(path.relative('/write-cli/folder/aaa'.'/write-cli/anotherFolder/bbb'))

//   ../../anotherFolder/bbb

Copy the code

In the above code, if the current directory is ‘/write-cli/folder/aaa’, go to the relative path returned by path.relative and you will reach ‘/write-cli/anotherFolder/ BBB ‘.

If two parameters of the path.relative method are the same, an empty string is returned.

console.log(path.relative('/write-cli/folder/aaa'.'/write-cli/anotherFolder/bbb'))

/ /"
Copy the code

path.resolve

Method resolves a relative path to an absolute path.

It can take multiple arguments to indicate the path to enter, one after the other, until the last argument is converted to an absolute path.

Path.resolve ([…paths]:Arrary

):string Raises TypeError if any of the arguments are not strings

If the absolute path cannot be obtained based on the parameters, the current path is used as the baseline. Except for the root directory, the method returns values without trailing slashes.

/ / format
path.resolve([from ...], to)

/ / instance
console.log(path.resolve('folder/aaa'.'/tmp/file/'.'.. '.'a/.. /subfile'))

Copy the code

The above example actually does something like the following

cd folder/aaa cd /tmp/file/ cd .. cd a/.. /subfileCopy the code

More chestnuts 🌰🌰🌰

path.resolve('/foo/bar'.'./baz');
// Returns: '/foo/bar/baz'

path.resolve('/foo/bar'.'/tmp/file/');
// Returns: '/ TMP /file'

path.resolve('wwwroot'.'static_files/png/'.'.. /gif/image.gif');
// If the current working directory is /home/myself-/ node,
/ / returns'/home/myself/node/below/static_files/GIF image. GIF '
Copy the code

path.join

This method is used to connect paths.

The main purpose is to concatenate all given path fragments together using platform-specific separators as delimiters correctly, and then normalize the generated path.

For Linux/Macos it is ‘/’ and for Windows it is ”.

Path.join ([…path]:Array

):string Raises TypeError if any path fragment is not a string

path.join('/foo'.'bar'.'baz/asdf'.'quux'.'.. ');
// Returns: '/foo/bar/baz/asdf'

path.join('foo', {}, 'bar');
// Throws 'TypeError: Path must be a string. Received {}'
Copy the code

path.parse

This method returns information about each part of the path.

path.parse(path:string) : {dir:string.root:string.base:string.name:string.ext:string,}Copy the code

For example, 🌰

path.parse('/Users/joker/Documents/write-cli/folder/aaa/a.js')

{
  root: '/',
  dir: '/Users/joker/Documents/write-cli/folder/aaa',
  base: 'a.js',
  ext: '.js',
  name: 'a'
}
Copy the code

path.extname

This method can return the corresponding extension.

If there is no. In the last part of the path, or no. Except the first character of the path’s base name. Character, an empty string is returned.

Path. extName (PATH :string):string If path is not a string, TypeError is raised

To cite a few examples 🌰

path.extname('index.html');
// Return: '.html'

path.extname('index.coffee.md');
// return: '.md'

path.extname('index.');
// return: '.'

path.extname('index');
// return:"

path.extname('.index');
// return:"

path.extname('.index.md');
// return: '.md'
Copy the code

_dirname

Always point to the absolute path of the js file being executed.

So when you write __dirname in /write-cli/folder/aaa/a.js, its value is /write-cli/folder/aaa/

Also commonly used with Path is the very useful library slash, which will be covered later in this article.

process

Process is the global module of Node that provides information about and controls the current Node.js process.

process.env

The process.env attribute returns an object containing information about the user’s environment.

Adding an attribute to process.env converts the value of the attribute to a string.

At ordinary times in the work we use more general is the following kinds

if(process.env.NODE_ENV === 'production') {console.log('Production environment');
}else{
    console.log('Non-production environment');
}

const baseUrl = process.env.VUE_APP_BASE_URL
......
Copy the code

In cli development, we will also use the above usage in many places.

process.exit()

This method can be used to exit the process immediately.

If the program has an exception and must exit, we can specify an exitCode as the exitCode for the exception and throw an uncaught error to terminate the process.

process.exit(exitCode=0:number)

process.exitCode

When a process exits normally or via process.exit() without specifying a code, 0 is used as the default exit code for the process.

If there is process.exit(exitCode) it overrides any previous Settings of process.exitcode.

For example, 🌰

fun(){... . }/ / 0


fun(){... process.exitCode =1. process.exitCode =2
}
/ / 2

fun(){... process.exitCode =2. exit(1)}/ / 1
Copy the code

process.stdout

The process.stdout property returns the stream connected to stdout (file descriptor 1).

For example, to copy process.stdin to process.stdout:

import { stdin, stdout } from 'process';

stdin.pipe(stdout);
Copy the code

process.cwd()

The process.cwd() method returns the current working directory of the Node.js process.

process.cwd():string

The last

It doesn’t matter if the students can’t remember the above content temporarily. When used in the actual combat, they can come here to strengthen their memory.

This article refers to the Node.js documentation