Skip to content

Commit

Permalink
Merge pull request #3 from cristiammercado/develop
Browse files Browse the repository at this point in the history
Bug fixex
  • Loading branch information
cristiammercado authored Dec 19, 2019
2 parents 1b92893 + 3c9fe12 commit f77a1d6
Show file tree
Hide file tree
Showing 16 changed files with 255 additions and 342 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
language: node_js
sudo: false
os:
- linux
- osx
node_js:
- 13
- 12
Expand Down
40 changes: 1 addition & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,7 @@ Node module to get disk information in Windows, Linux & Mac. It works with Elect

## Usage

Import it and just call the main function:

```javascript
const nodeDiskInfo = require('node-disk-info');

// async mode
nodeDiskInfo.getDiskInfo().then(disks => {

for (const disk of disks) {
console.log('Filesystem:', disk.filesystem);
console.log('Blocks:', disk.blocks);
console.log('Used:', disk.used);
console.log('Available:', disk.available);
console.log('Capacity:', disk.capacity);
console.log('Mounted:', disk.mounted);
console.log('\n');
}

}).catch(reason => {
console.error(reason);
});

// sync mode
try {
const disks = nodeDiskInfo.getDiskInfoSync();

for (const disk of disks) {
console.log('Filesystem:', disk.filesystem);
console.log('Blocks:', disk.blocks);
console.log('Used:', disk.used);
console.log('Available:', disk.available);
console.log('Capacity:', disk.capacity);
console.log('Mounted:', disk.mounted);
console.log('\n');
}
} catch (e) {
console.error(e);
}
```
See the [example script](example/index.js) for usage. You can run it with `node example/index.js`.

## License

Expand Down
65 changes: 40 additions & 25 deletions dist/classes/drive.d.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,33 @@
import { DriveBuilder } from './drive-builder';
import { IDriveJson } from './drive-json';
/**
* Class with drive information.
*
* @author Cristiam Mercado
*/
export default class Drive {
/**
* Creates a new class instance from JSON.
*
* @param {IDriveJson} json JSON drive data.
* @return {Drive} Drive class instance.
*/
static fromJSON(json: IDriveJson): Drive;
/**
* Builder to generate a instance of Drive class.
*
* @param {Drive} drive Drive instance to copy of.
*/
static builder(drive?: Drive): DriveBuilder;
/**
* Drive filesystem.
*/
readonly filesystem: string;
private readonly _filesystem;
/**
* Blocks associated to disk.
*/
readonly blocks: number;
private readonly _blocks;
/**
* Used disk space.
*/
readonly used: number;
private readonly _used;
/**
* Available disk space.
*/
readonly available: number;
private readonly _available;
/**
* Disk capacity.
*/
readonly capacity: string;
private readonly _capacity;
/**
* Indicates the mount point of the disk.
*/
readonly mounted: string;
private readonly _mounted;
/**
* Constructor for Drive class.
*
Expand All @@ -53,11 +38,41 @@ export default class Drive {
* @param {string} capacity Disk capacity.
* @param {string} mounted Indicates the mount point of the disk.
*/
private constructor();
constructor(filesystem: string, blocks: number, used: number, available: number, capacity: string, mounted: string);
/**
* Drive filesystem.
*
* @return Gets drive filesystem.
*/
get filesystem(): string;
/**
* Blocks associated to disk.
*
* @return Gets blocks associated to disk.
*/
get blocks(): number;
/**
* Generates JSON of this class.
* Used disk space.
*
* @return Gets used disk space.
*/
get used(): number;
/**
* Available disk space.
*
* @return Gets available disk space.
*/
get available(): number;
/**
* Disk capacity.
*
* @return Gets disk capacity.
*/
get capacity(): string;
/**
* Indicates the mount point of the disk.
*
* @return {object} A JSON object.
* @return Gets the mount point of the disk.
*/
toJSON(): IDriveJson;
get mounted(): string;
}
117 changes: 78 additions & 39 deletions dist/classes/drive.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var drive_builder_1 = require("./drive-builder");
/**
* Class with drive information.
*
Expand All @@ -18,45 +17,85 @@ var Drive = /** @class */ (function () {
* @param {string} mounted Indicates the mount point of the disk.
*/
function Drive(filesystem, blocks, used, available, capacity, mounted) {
this.filesystem = filesystem;
this.blocks = blocks;
this.used = used;
this.available = available;
this.capacity = capacity;
this.mounted = mounted;
this._filesystem = filesystem;
this._blocks = blocks;
this._used = used;
this._available = available;
this._capacity = capacity;
this._mounted = mounted;
}
/**
* Creates a new class instance from JSON.
*
* @param {IDriveJson} json JSON drive data.
* @return {Drive} Drive class instance.
*/
Drive.fromJSON = function (json) {
return new Drive(json.filesystem, json.blocks, json.used, json.available, json.capacity, json.mounted);
};
/**
* Builder to generate a instance of Drive class.
*
* @param {Drive} drive Drive instance to copy of.
*/
Drive.builder = function (drive) {
return new drive_builder_1.DriveBuilder(drive);
};
/**
* Generates JSON of this class.
*
* @return {object} A JSON object.
*/
Drive.prototype.toJSON = function () {
return {
available: this.available,
blocks: this.blocks,
capacity: this.capacity,
filesystem: this.filesystem,
mounted: this.mounted,
used: this.used,
};
};
Object.defineProperty(Drive.prototype, "filesystem", {
/**
* Drive filesystem.
*
* @return Gets drive filesystem.
*/
get: function () {
return this._filesystem;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Drive.prototype, "blocks", {
/**
* Blocks associated to disk.
*
* @return Gets blocks associated to disk.
*/
get: function () {
return this._blocks;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Drive.prototype, "used", {
/**
* Used disk space.
*
* @return Gets used disk space.
*/
get: function () {
return this._used;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Drive.prototype, "available", {
/**
* Available disk space.
*
* @return Gets available disk space.
*/
get: function () {
return this._available;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Drive.prototype, "capacity", {
/**
* Disk capacity.
*
* @return Gets disk capacity.
*/
get: function () {
return this._capacity;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Drive.prototype, "mounted", {
/**
* Indicates the mount point of the disk.
*
* @return Gets the mount point of the disk.
*/
get: function () {
return this._mounted;
},
enumerable: true,
configurable: true
});
return Drive;
}());
exports.default = Drive;
9 changes: 1 addition & 8 deletions dist/platforms/darwin.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,7 @@ var Darwin = /** @class */ (function () {
if (value !== '') {
var line = value.replace(/ +(?= )/g, '');
var tokens = line.split(' ');
var d = drive_1.default.builder()
.filesystem(tokens[0])
.blocks(isNaN(parseFloat(tokens[1])) ? +tokens[1] : 0)
.used(isNaN(parseFloat(tokens[2])) ? +tokens[2] : 0)
.available(isNaN(parseFloat(tokens[3])) ? +tokens[3] : 0)
.capacity(tokens[4])
.mounted(tokens[5])
.build();
var d = new drive_1.default(tokens[0], isNaN(parseFloat(tokens[1])) ? 0 : +tokens[1], isNaN(parseFloat(tokens[2])) ? 0 : +tokens[2], isNaN(parseFloat(tokens[3])) ? 0 : +tokens[3], tokens[4], tokens[5]);
drives.push(d);
}
});
Expand Down
9 changes: 1 addition & 8 deletions dist/platforms/linux.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,7 @@ var Linux = /** @class */ (function () {
if (value !== '') {
var line = value.replace(/ +(?= )/g, '');
var tokens = line.split(' ');
var d = drive_1.default.builder()
.filesystem(tokens[0])
.blocks(isNaN(parseFloat(tokens[1])) ? +tokens[1] : 0)
.used(isNaN(parseFloat(tokens[2])) ? +tokens[2] : 0)
.available(isNaN(parseFloat(tokens[3])) ? +tokens[3] : 0)
.capacity(tokens[4])
.mounted(tokens[5])
.build();
var d = new drive_1.default(tokens[0], isNaN(parseFloat(tokens[1])) ? 0 : +tokens[1], isNaN(parseFloat(tokens[2])) ? 0 : +tokens[2], isNaN(parseFloat(tokens[3])) ? 0 : +tokens[3], tokens[4], tokens[5]);
drives.push(d);
}
});
Expand Down
20 changes: 7 additions & 13 deletions dist/platforms/windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var Windows = /** @class */ (function () {
var newDiskIteration = false;
var caption = '';
var description = '';
var fresSpace = 0;
var freeSpace = 0;
var size = 0;
lines.forEach(function (value) {
if (value !== '') {
Expand All @@ -40,32 +40,26 @@ var Windows = /** @class */ (function () {
description = data;
break;
case 'FreeSpace':
fresSpace = isNaN(parseFloat(data)) ? +data : 0;
freeSpace = isNaN(parseFloat(data)) ? 0 : +data;
break;
case 'Size':
size = isNaN(parseFloat(data)) ? +data : 0;
size = isNaN(parseFloat(data)) ? 0 : +data;
break;
}
}
else {
if (newDiskIteration) {
var used = (size - fresSpace);
var used = (size - freeSpace);
var percent = '0%';
if (size > 0) {
percent = Math.round((used / size) * 100) + '%';
}
var d = drive_1.default.builder()
.filesystem(description)
.blocks(size)
.used(used)
.available(fresSpace)
.capacity(percent)
.mounted(caption)
.build();
var d = new drive_1.default(description, size, used, freeSpace, percent, caption);
drives.push(d);
newDiskIteration = false;
caption = '';
description = '';
fresSpace = 0;
freeSpace = 0;
size = 0;
}
}
Expand Down
Loading

0 comments on commit f77a1d6

Please sign in to comment.