npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@zhangbao/fs-lite

v0.1.0

Published

A lightweight alternative to fs-extra

Downloads

9

Readme

fs-lite ⚡

A lightweight alternative to fs-extra.

NPM version Node Compatibility NPM downloads npm

✨ Features

  • 💡 ESM-based Source Code
  • 🎉 No Dependencies
  • 🪄 Sync-based Async Function
  • 🔑 Fully Typed APIs

📦 Install

$ npm install --save @zhangbao/fs-lite
# or
$ yarn add @zhangbao/fs-lite
# or
$ pnpm add @zhangbao/fs-lite

🔨 Usage

ES Module:

import * as fs from '@zhangbao/fs-lite'

fs.readJson('./package.json')

// OR

import { readJson } from '@zhangbao/fs-lite'

readJson('./package.json')

CommonJS:

const fs = require('@zhangbao/fs-lite')

fs.readJson('./package.json')

// OR

const { readJson } = require('@zhangbao/fs-lite')

readJson('./package.json')

🛠️ Methods

Async

  • copy(alias: ncp)
  • move
  • remove
  • mkdirs(alias: mkdirp, ensureDir)
  • emptyDir
  • createFile
  • readFile
  • writeFile
  • readJson
  • writeJson
  • pathExists

Sync

  • copySync(alias: ncpSync)
  • moveSync
  • removeSync
  • mkdirsSync(alias: mkdirpSync, ensureDirSync)
  • emptyDirSync
  • createFileSync
  • readFileSync
  • writeFileSync
  • readJsonSync
  • writeJsonSync
  • pathExistsSync

📚 Documentation

fs-lite's implementation is sync-first, the async method is just the result of asynchronization of the corresponding sync method(via internal toAsync function).Therefore, the following only lists the API of sync method.

// sync method
fs.copySync('/tmp/myfile', '/tmp/mynewfile')
// corresponding to the async method, just add an `await` keyword in front.
await fs.copy('/tmp/myfile', '/tmp/mynewfile')

copySync(alias: ncpSync)

// Syntax: copySync(source: string, destination: string)
// Description: Copy a file or directory. The directory can have contents.
//              Overwrite if file or directory exists.

// Examples

// copy file
fs.copySync('/tmp/myfile', '/tmp/mynewfile')

// copy directory, even if it has subdirectories or files
fs.copySync('/tmp/mydir', '/tmp/mynewdir')

moveSync

// Syntax: moveSync(source: string, destination: string)
// Description: Moves a file or directory
//              Overwrite if file or directory exists.

fs.moveSync('/tmp/somefile', '/tmp/does/not/exist/yet/somefile')

removeSync

// Syntax: removeSync(path: string)
// Description: Removes a file or directory. The directory can have contents. 
//              If the path does not exist, silently does nothing.

// Examples

// remove file
fs.removeSync('/tmp/myfile')

fs.removeSync('/home/zhangbao') // I just deleted my entire HOME directory.

mkdirsSync(alias: mkdirpSync, ensureDirSync)

// Syntax: mkdirsSync(dir: string)
// Description: Ensures that the directory exists. 
//              If the directory structure does not exist, it is created.
//              If provided, silently do nothing.

// Examples

fs.ensureDirSync('path/to/dir')
// dir has now been created, including the directory it is to be placed in

emptyDirSync

// Syntax: emptyDirSync(dir: string)
// Description: Emptys a directory.
//              Deletes directory contents if the directory is not empty. The directory itself is not deleted.
//              If the directory does not exist, silently do nothing.

// Examples

// assume this directory has a lot of files and folders
fs.emptyDirSync('/tmp/some/dir')

createFileSync

// Syntax: createFileSync(file: string, content: string = '')
// Description: Create a file using optional content string
//              If provided, silently do nothing.

// Examples

createFileSync('path/to/file')
// Create a file without content(default use a empty string(`''`))
createFileSync('path/to/file', 'hello world')
// Create a file with content

readFileSync

// Syntax: readFileSync(file: string): string | null
// Description: Reads a file content.
//              If the file does not exist, return null.

// Examples

readFileSync('path/to/file')
// return file content, a string value

readFileSync('path/to/not/exist/file')
// return `null`

writeFileSync

// Syntax: writeFileSync(file: string, content: string)
// Description: Writes content to a file.

// Examples

fs.writeJsonSync('./hello.txt', 'hello world')

readJsonSync

// Syntax: readJsonSync(file: string): Record<string, any> | null
// Description: Reads a JSON file and then parses it into an object.
//              If the file does not exist, return null.

// Examples

const packageObj = fs.readJsonSync('./package.json')
console.log(packageObj.version) // => 2.0.0

writeJsonSync

// Syntax: writeJsonSync(file: string, object: Record<string, any>)
// Description: Writes an object to a JSON file.

// Examples

fs.writeJsonSync('./package.json', {name: 'fs-extra'})

pathExistsSync

// Syntax: pathExistsSync(path: string)
// Description: Test whether or not the given path exists by checking with the file system.
//              An alias for fs.existsSync().

// Examples

fs.pathExists('/tmp/this/path/does/not/exist/file.txt')
// false
fs.pathExists('/tmp/this/path/does/exist/file.txt')
// true

🤝 Contributing

download:

$ git clone https://github.com/baooab/node-fs-lite.git
$ cd node-fs-lite

commit changes:

# then commit to git
$ git add .
# see [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/)
$ git commit -m 'message to show your changes'

publish:

$ npm version patch # or minor/major
$ npm publish
# push changes to remote branch(Github)
$ git push
$ git push --tags

📓 License

Licensed under MIT

Copyright (c) 2023-present Bao Zhang