Options
All
  • Public
  • Public/Protected
  • All
Menu

Module @tomato-node/fs

Index

Functions

exists

  • exists(filename: fs.PathLike, callback?: undefined | ((error: Error | null) => void)): void | Promise<unknown>
  • 判断文件或者目录是否存在

    新增于v0.0.7

    脚本举例

      import { exists } from '@tomato-node/fs';
      //callback style
      const callback=(error)=>{
        console.log(!error?true:false)
      };
      exists('file',callback);
      //promise style
      async(()=>{
        const isExist = await exists('file');
      })()

    Parameters

    • filename: fs.PathLike
    • Optional callback: undefined | ((error: Error | null) => void)

    Returns void | Promise<unknown>

isDir

  • isDir(filePath: string): boolean
  • 判断是否是目录

    新增于v0.0.7

    脚本举例

      import { isDir } from '@tomato-node/fs'
      isDir(process.cwd())//false

    Parameters

    • filePath: string

    Returns boolean

    是否为目录

isFile

  • isFile(filePath: string): boolean
  • 判断是否是文件

    新增于v0.0.7

    脚本举例

      import { isFile } from '@tomato-node/fs'
      isFile(process.cwd())//true

    Parameters

    • filePath: string

    Returns boolean

    是否为文件

isModuleExist

  • isModuleExist(name: string): boolean
  • 判断一个模块是否可以被加载

    新增于v0.0.5

    脚本举例

      import { isModuleExist } from '@tomato-node/fs'
      isModuleExist('fs');//true
      isModuleExist('fs2');//false

    Parameters

    • name: string

    Returns boolean

mkdirP

  • mkdirP(path: fs.PathLike, callback?: undefined | ((error: Error | null) => void)): void | Promise<boolean>
  • 递归创建文件路径及文件

    新增于v0.0.7

    脚本举例

      import { mkdirP } from '@tomato-node/fs';
      //promise style
      async(()=>{
        await mkdirP(path.join(process.cwd(),'/create/dir'));
        // return true when success
      })()
      //callback style
      const callback=(error)=>{
        console.log(!error?true:false)
      };
      mkdirP(path.join(process.cwd(),'/create/dir'),callback);

    Parameters

    • path: fs.PathLike

      文件路径

    • Optional callback: undefined | ((error: Error | null) => void)

      回调函数,如果没有则走promise风格

    Returns void | Promise<boolean>

    是否创建成功

readJson

  • readJson(fileName: string): Promise<object>
  • 函数描述

    新增于v

    脚本举例

      import { readJson } from '@tomato-node/fs'
      const result = await readJson('a.json');

    Parameters

    • fileName: string

    Returns Promise<object>

readJsonSync

  • readJsonSync(fileName: string): object
  • 函数描述

    新增于v

    脚本举例

      import { readJsonSync } from '@tomato-node/fs'
      const resultSync = readJsonSync('b.json');

    Parameters

    • fileName: string

    Returns object

requireModule

  • requireModule(name: string): any
  • 安全的加载模块,并确保其只被加载一次

    新增于v0.0.5

    脚本举例

      import { requireModule } from '@tomato-node/fs'
      const fs = requireModule('fs');

    Parameters

    • name: string

    Returns any

rimraf

  • rimraf(path: string, callback?: undefined | ((error: Error | null) => void)): void | Promise<unknown>
  • 删除文件,基于通配符

    新增于v0.0.7

    脚本举例

      import { rimraf } from '@tomato-node/fs'
      //promise style
      async(()=>{
        await rimraf('./create/dir'));
        await rimraf('./*.json'));
        // return true when success
      })()
      //callback style
      const callback=(error)=>{
        console.log(!error?true:false)
      };
      rimraf(path.join(process.cwd(),'/create/dir'),callback);

    Parameters

    • path: string

      文件路径

    • Optional callback: undefined | ((error: Error | null) => void)

      回调函数,如果没有则走promise风格

    Returns void | Promise<unknown>

    是否删除成功

writeJson

  • writeJson(fileName: string, jsonContent: object): Promise<void>
  • 函数描述

    新增于v

    脚本举例

      import { writeJson } from '@tomato-node/fs'
      const obj = { firstName: 'aaa', lastName: 'bbb'};
      await writeJson('name.json', obj);

    Parameters

    • fileName: string
    • jsonContent: object

    Returns Promise<void>

writeJsonSync

  • writeJsonSync(fileName: string, jsonContent: object): void
  • 函数描述

    新增于v

    脚本举例

      import { writeJsonSync } from '@tomato-node/fs'
      const obj = { firstName: 'aaa', lastName: 'bbb'};
      writeJsonSync('name.json', obj);

    Parameters

    • fileName: string
    • jsonContent: object

    Returns void

Generated using TypeDoc