Git Product home page Git Product logo

node-diskusage's Introduction

node-diskusage

npm Dependencies npm Version npm Downloads

This module implements platform specific bindings to obtain disk usage information on Windows and POSIX platforms. Windows support is backed by GetDiskFreeSpaceEx and POSIX is implemented with statvfs.

Installation

Install with npm:

$ npm install diskusage

Usage

The module exposes two functions. check takes a path/mount point as the first argument and a callback as the second. The callback takes two arguments err and info. err will be an Error if something went wrong. info contains three members: available, free and total in bytes.

If no callback is supplied check will instead return a Promise<DiskUsage> that you can await.

  • available: Disk space available to the current user (i.e. Linux reserves 5% for root)
  • free: Disk space physically free
  • total: Total disk space (free + used)

checkSync only takes the path argument. It returns the same info on success, throws an Error on failure.

Examples

const disk = require('diskusage');
const os = require('os');

let path = os.platform() === 'win32' ? 'c:' : '/';

// Callbacks
disk.check(path, function(err, info) {
  if (err) {
    console.log(err);
  } else {
    console.log(info.available);
    console.log(info.free);
    console.log(info.total);
  }
});

// Promise
async function getFreeSpace(path) {
  try {
    const { free } = await disk.check(path);
    console.log(`Free space: ${free}`);
    return free
  } catch (err) {
    console.error(err)
    return 0
  }
}

// Or without using async/await
disk.check(path)
  .then(info => console.log(`free: ${info.free}`))
  .catch(err => console.error(err))

// Synchronous
try {
  let info = disk.checkSync(path);
  console.log(info.available);
  console.log(info.free);
  console.log(info.total);
}
catch (err) {
  console.log(err);
}

TypeScript

The module has an embedded .d.ts file. You can use import * as diskusage from 'diskusage'.

type DiskUsage = {
    available: number;
    free: number;
    total: number;
}

export function check(path: string, callback: (error?: Error, result?: DiskUsage) => void): void;
export function check(path: string): Promise<DiskUsage>
export function checkSync(path: string): DiskUsage;

Demo

To see a demo of this library see the demo/ folder.

You can run it with node: (node 8+ required)

node ./demo/

node-diskusage's People

Contributors

abrarahmad avatar amilajack avatar blagoev avatar braydonf avatar brknstrngz avatar jacobq avatar jduncanator avatar jordond avatar kneth avatar miniak avatar not-implemented avatar omgimalexis avatar ubershmekel avatar weiyin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.