# node 内置模块常用API

# 1. path

# 1.1. 获取路径的最后一节

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

path.basename('/foo/bar/baz/asdf/quux.html', '.html');
// Returns: 'quux'

# 1.2. 路径分隔符

path.sep

# 2. fs

# 2.1. 重命名

fs.renameSync(oldPath, newPath)

  • oldPath: String | Buffer | URL
  • newPath: String | Buffer | URL

Renames the file from oldPath to newPath. Returns undefined.

# 2.2. 删除文件或目录

fs.rmSync(path[, options])

  • path: String | Buffer | URL
  • options: Object
    • recursive: boolean. If true, perform a recursive directory removal. In recursive mode, operations are retried on failure. Default: false. Deprecated.

Synchronously removes files and directories. Returns undefined.

# 2.3. 拷贝文件

fs.copyFileSync(src, dest[, mode])

  • src: string.
  • dest: string.

Synchronously copies src to dest. By default, dest is overwritten if it already exists. Returns undefined.

# 2.4. 读取目录下的文件(夹)

fs.readdirSync(path[, options])

Reads the contents of the directory.

fs.readdirSync(dirPath);
/*
[
    'dir1',
    'dir2',
    '1.txt',
    '2.txt'
]
*/

# 2.5. 判断是否为目录

fs.lstatSync(path_string).isDirectory() 

// Objects returned from fs.stat() and fs.lstat() are of this type.

stats.isFile()
stats.isDirectory()
stats.isBlockDevice()
stats.isCharacterDevice()
stats.isSymbolicLink() // (only valid with fs.lstat())
stats.isFIFO()
stats.isSocket()

# 2.6. 判断是否存在(文件/目录)

说明:

/*
Returns true if the path exists, false otherwise.

fs.exists() is deprecated, but fs.existsSync() is not. 
The callback parameter to fs.exists() accepts parameters that are inconsistent with other Node.js callbacks. 
fs.existsSync() does not use a callback.
 */
export function existsSync(path: PathLike): boolean

示例:

import { existsSync } from 'node:fs';  

if (existsSync('/ etc/ passwd')) {
  console. log('The path exists.');
}

# 2.7. 创建目录

// fs.mkdirSync(path[, options])

// 创建单层目录
fs.mkdirSync('/a');

// 创建多层目录
fs.mkdirSync('a/b/c', { recursive: true });

# 2.8. 递归遍历目录中所有文件,并拷贝到其它目录

const path = require('path');
const fs = require('fs');

// 遍历
const traverse = (dir, callback) => {
  const filenameList = fs.readdirSync(dir);

  for (let i = 0; i < filenameList.length; i++) {
    const filename = filenameList[i];

    const filePath = path.join(dir, filename);

    const isDir = fs.lstatSync(filePath).isDirectory();
    
    callback({ filePath, isDir });

    if (isDir) {
      const subDirPath = path.join(dir, filename);
      traverse(subDirPath, callback);
    }
  }
};

// 拷贝:src/** -> dest/**
const srcRootPath = PROJ_DIST_PATH;
const destRootPath = WWW_PATH;

const copyFile = (filePath) => {
  const srcFilePath = filePath;
  const destFilePath = filePath.replace(srcRootPath, destRootPath);

  fs.copyFileSync(srcFilePath, destFilePath);
};

const copyDir = (dir) => {
  fs.mkdirSync(dir, { recursive: true });
};

traverse(srcRootPath, ({ filePath, isDir}) => {
  if (isDir) {
    copyDir(filePath);
    return;
  }

  copyFile(filePath);
});

# 3. os

# 3.1. 获取IPv4

const os = require('os');

const v4Ips = [];
const networkInterfaces = os.networkInterfaces();

Reflect.ownKeys(networkInterfaces).forEach((netName) => {
  const networkInterfaceInfoList = networkInterfaces[netName];

  networkInterfaceInfoList.forEach((networkInterfaceInfo) => {
    const { family, internal, address } = networkInterfaceInfo;

    if (family === 'IPv4' && !internal) {
      v4Ips.push(address);
    }
  });
});

# 3.2. 获取 Home 目录

os.homedir();// C:\Users\wuqinfei

# 4. child_process

每一条指令是在不同的进程中执行的,exec('cd ../../') 是不能切换工作目录的

# 4.1. 执行脚本命令

const { exec } = require("child_process");

// options.cwd : 指定工作目录
function execCommand(command, options = {}) {
  console.log(`start executing command: ${command}`);

  return new Promise((resolve, reject) => {
    exec(command, options, (error, stdout, stderr) => {
        if (error) {
          console.error(`~~~~~~~~~~~~ [ ${command} ] error start ~~~~~~~~~~~~`);
          console.error(error.message);
          console.error(`~~~~~~~~~~~~ [ ${command} ] error end ~~~~~~~~~~~~~~`);
          reject(error);
          return;
        }

        if (stderr) {
          console.warn(`~~~~~~~~~~~~ [ ${command} ] stderr start ~~~~~~~~~~~~`);
          console.warn(stderr);
          console.warn(`~~~~~~~~~~~~ [ ${command} ] stderr end ~~~~~~~~~~~~~~`);
        }
        
        if (stdout) {
          console.log(`~~~~~~~~~~~~ [ ${command} ] stdout start ~~~~~~~~~~~~`);
          console.log(stdout);
          console.log(`~~~~~~~~~~~~ [ ${command} ] stdout end ~~~~~~~~~~~~~~`);
        }

        resolve(stdout);
      }, 
    );
  });
}

async function run() {
  await execCommand('git pull', { cwd: 'xxx' });
  await execCommand('npm run build', { cwd: 'xxx' });
}

# 5. process

# 5.1. 获取用户 home 目录

/*
  mac、linux:
    process.env.HOME
  windows:
    process.env.USERPROFILE
 */
const USER_HOME = process.env.HOME || process.env.USERPROFILE; // C:\Users\wuqinfei
本章目录