0.2.0 • Published 1 month ago

fs-un v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

fs-un

unified util to manage files and directories inside a directory on different platforms

Usage

Support platforms

Node

Nodejs fs module

import { NodeIFS, walk } from 'fs-un'

const ifs = new NodeIFS('/path/to/dir')

walk(ifs.root)

Web

File Systen Access API

import { WebIFS, getOpfsRoot, getUserRoot, isSupportOpfsRoot, isSupportUserRoot, walk } from 'fs-un/web'

let root
if (isSupportUserRoot()) {
  root = await getUserRoot()
} else if (isSupportOpfsRoot()) {
  root = await getOpfsRoot()
} else {
  throw new Error('not support')
}

const ifs = new WebIFS(root)

walk(ifs.root)

Types

export interface IReadonlyFS {
  /**
   * check file or directory
   */
  exists: (path: string) => Promise<PathType>

  /**
   * get file attributes
   */
  fileAttr: (path: string) => Promise<FileAttr | undefined>

  /**
   * list directory
   * @throws no such dir
   */
  list: (path: string) => AsyncIterable<ListState>

  /**
   * read file data as Buffer or Uint8Array
   */
  readByte: (path: string) => Promise<Uint8Array | undefined>
  /**
   * read file data as string
   */
  readText: (path: string) => Promise<string | undefined>
}

export interface IFS extends IReadonlyFS {
  /**
   * ensure directory exists, auto create parent directory
   */
  mkdir: (path: string) => Promise<void>

  /**
   * write data to file
   */
  writeFile: (path: string, data: string | ArrayBuffer | ArrayBufferView, options?: OverwriteOptions) => Promise<void>

  /**
   * move or rename file or dir, in default, throw error when overwrite by default
   */
  move: (from: string, to: string, options?: MoveOptions) => Promise<void>

  /**
   * copy file or dir, throw error when overwrite by default
   */
  copy: (from: string, to: string, options?: OverwriteOptions) => Promise<void>

  /**
   * remove directory and file recursively
   */
  remove: (path: string) => Promise<void>
}

Reference

0.2.0

1 month ago

0.1.0

2 months ago

0.0.1

6 months ago