diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..4ccc916
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,11 @@
+root = true
+
+[*]
+charset = utf-8
+indent_size = 2
+indent_style = space
+insert_final_newline = true
+trim_trailing_whitespace = true
+
+[*.md]
+trim_trailing_whitespace = false
\ No newline at end of file
diff --git a/.eslintrc b/.eslintrc
new file mode 100644
index 0000000..7ec2409
--- /dev/null
+++ b/.eslintrc
@@ -0,0 +1,14 @@
+{
+ "env": {
+ "es6": true,
+ "browser": false,
+ "commonjs": true
+ },
+ "extends": "eslint:recommended",
+ "parserOptions": {
+ "ecmaVersion": 2018
+ },
+ "rules": {
+ "semi": ["error", "always"]
+ }
+}
\ No newline at end of file
diff --git a/README.md b/README.md
index f6c5fb4..db7fa5c 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,12 @@
node-diskusage
==============
+
+
+
+
+
+
This module implements platform specific bindings to obtain disk usage information on Windows and POSIX platforms. Windows support is backed by [GetDiskFreeSpaceEx](http://msdn.microsoft.com/en-us/library/windows/desktop/aa364937/) and POSIX is implemented with [statvfs](http://www.freebsd.org/cgi/man.cgi?query=statvfs).
Installation
@@ -17,6 +23,8 @@ 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` 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)
@@ -32,24 +40,43 @@ 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);
- }
+ 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);
+ let info = disk.checkSync(path);
+ console.log(info.available);
+ console.log(info.free);
+ console.log(info.total);
}
catch (err) {
- console.log(err);
+ console.log(err);
}
```
@@ -58,13 +85,25 @@ TypeScript
The module has an embedded .d.ts file. You can use `import * as diskusage from 'diskusage'`.
-```
+```ts
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, callback: (error?: Error, result?: DiskUsage) => void): void;
+export function check(path: string): Promise
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)
+
+```bash
+node ./demo/
+```
diff --git a/binding.gyp b/binding.gyp
index 10d09d5..9cd596d 100644
--- a/binding.gyp
+++ b/binding.gyp
@@ -36,7 +36,8 @@
}],
['OS=="mac"', {
'xcode_settings': {
- 'GCC_ENABLE_CPP_EXCEPTIONS': 'YES'
+ 'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
+ 'CLANG_CXX_LIBRARY': 'libc++'
}
}]
]
diff --git a/demo/index.js b/demo/index.js
new file mode 100644
index 0000000..480a0d0
--- /dev/null
+++ b/demo/index.js
@@ -0,0 +1,44 @@
+const { check, checkSync } = require("../");
+const os = require("os");
+
+const targetPath = os.platform() === "win32" ? "c:" : "/";
+
+function printResults(type, { available, free, total }) {
+ console.log(`${type}
+ Available: ${available}
+ Free: ${free}
+ Total: ${total}
+ `);
+}
+
+async function getFreeSpacePromise(path) {
+ try {
+ const info = await check(path);
+ printResults("PROMISE", info);
+ } catch (err) {
+ console.error(err);
+ }
+}
+
+function getFreeSpaceCallback(path) {
+ check(path, (err, info) => {
+ if (err) {
+ console.error(err);
+ } else {
+ printResults("CALLBACK", info);
+ }
+ });
+}
+
+function getFreeSpaceSync(path) {
+ const info = checkSync(path);
+ printResults("SYNC", info);
+}
+
+async function start() {
+ await getFreeSpacePromise(targetPath);
+ getFreeSpaceCallback(targetPath);
+ getFreeSpaceSync(targetPath);
+}
+
+start()
diff --git a/index.d.ts b/index.d.ts
index 138bcd0..9f24300 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -4,5 +4,6 @@ type DiskUsage = {
total: number;
}
-export function check(path: string, callback: (error: Error, result: DiskUsage) => void): void;
+export function check(path: string, callback: (error?: Error, result?: DiskUsage) => void): void;
+export function check(path: string): Promise
export function checkSync(path: string): DiskUsage;
diff --git a/index.js b/index.js
index 03ac0b2..a0eb17e 100644
--- a/index.js
+++ b/index.js
@@ -1,15 +1,29 @@
-var native = require('./build/Release/diskusage.node');
+var native = require("./build/Release/diskusage.node");
+var promise = typeof Promise !== "undefined" ? Promise : require("es6-promise").Promise;
exports.check = function(path, callback) {
- var result = undefined;
- var error = undefined;
- try {
- result = native.getDiskUsage(path);
- }
- catch (error_) {
- error = error_
- }
- callback(error, result);
+ if (callback) {
+ return check(path, callback);
+ }
+
+ return new promise(function (resolve, reject) {
+ check(path, function (err, result) {
+ err ? reject(err) : resolve(result);
+ });
+ });
};
exports.checkSync = native.getDiskUsage;
+
+function check(path, callback) {
+ var result = undefined;
+ var error = undefined;
+
+ try {
+ result = native.getDiskUsage(path);
+ } catch (error_) {
+ error = error_;
+ }
+
+ callback(error, result);
+}
diff --git a/package.json b/package.json
index 2170650..e7af5f7 100644
--- a/package.json
+++ b/package.json
@@ -1,12 +1,16 @@
{
"name": "diskusage",
- "version": "0.2.4-hola.0",
+ "version": "1.1.3-hola.0",
"description": "Get total diskspace and free diskspace using bindings around platform specific calls.",
"main": "index.js",
"typings": "index.d.ts",
"gypfile": true,
"dependencies": {
- "nan": "^2.5.0"
+ "es6-promise": "^4.2.5",
+ "nan": "^2.14.0"
+ },
+ "devDependencies": {
+ "eslint": "^6.0.1"
},
"repository": {
"type": "git",
diff --git a/src/main.cpp b/src/main.cpp
index 619ffab..807c8a3 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -6,12 +6,12 @@
static v8::Local ConvertDiskUsage(const DiskUsage& usage)
{
v8::Local obj = Nan::New();
- obj->Set(Nan::New("available").ToLocalChecked(), Nan::New(static_cast(usage.available)));
- obj->Set(Nan::New("free").ToLocalChecked(), Nan::New(static_cast(usage.free)));
- obj->Set(Nan::New("total").ToLocalChecked(), Nan::New(static_cast(usage.total)));
- obj->Set(Nan::New("inodes_available").ToLocalChecked(), Nan::New(static_cast(usage.inodes_available)));
- obj->Set(Nan::New("inodes_free").ToLocalChecked(), Nan::New(static_cast(usage.inodes_free)));
- obj->Set(Nan::New("inodes_total").ToLocalChecked(), Nan::New(static_cast(usage.inodes_total)));
+ Nan::Set(obj, Nan::New("available").ToLocalChecked(), Nan::New(static_cast(usage.available)));
+ Nan::Set(obj, Nan::New("free").ToLocalChecked(), Nan::New(static_cast(usage.free)));
+ Nan::Set(obj, Nan::New("total").ToLocalChecked(), Nan::New(static_cast(usage.total)));
+ Nan::Set(obj, Nan::New("inodes_available").ToLocalChecked(), Nan::New(static_cast(usage.inodes_available)));
+ Nan::Set(obj, Nan::New("inodes_free").ToLocalChecked(), Nan::New(static_cast(usage.inodes_free)));
+ Nan::Set(pbj, Nan::New("inodes_total").ToLocalChecked(), Nan::New(static_cast(usage.inodes_total)));
return obj;
}
@@ -26,7 +26,7 @@ static NAN_METHOD(GetDiskUsage)
Nan::HandleScope scope;
try {
- DiskUsage result = GetDiskUsage(*v8::String::Utf8Value(info[0]));
+ DiskUsage result = GetDiskUsage(*Nan::Utf8String(info[0]));
info.GetReturnValue().Set(ConvertDiskUsage(result));
}
catch (const SystemError &error) {
@@ -37,7 +37,7 @@ static NAN_METHOD(GetDiskUsage)
}
}
-void Init(v8::Handle exports)
+void Init(v8::Local exports)
{
Nan::SetMethod(exports, "getDiskUsage", GetDiskUsage);
}