Skip to content

Commit

Permalink
Merge pull request #1 from vzenix/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
vzenix authored Jan 20, 2019
2 parents 0a6a698 + ee41d5e commit 63134cc
Show file tree
Hide file tree
Showing 14 changed files with 930 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
images/
node_modules/
bitbucket.md
sample.json
package-lock.json
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
# fs-hard-drive
# NodeJS library "fs-hard-drive"

Hard drive functions not incluyed in "fs" package

# Functionalities

* List all hard drive devices mounted by windows, mac or linux
* Scan all files and folder tree (incluyed size) from a folder start point
* A little utils class for work with this library

# Documentation

* [English](https://github.com/vzenix/fs-hard-drive/wiki)
* [Español](https://github.com/vzenix/fs-hard-drive/wiki/ES_Inicio)

# Test

Run `npm test` or `gulp e2e:test`

# Licence

* [MIT](https://github.com/vzenix/fs-hard-drive/blob/master/LICENSE)

# Author

* [Webpage (only spanish)](https://vzenix.es)
29 changes: 29 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const gulp = require('gulp');
const path = require('path');
const wdioCli = require('@wdio/cli');
let wdio = new wdioCli.default(path.join(__dirname, 'wdio.conf.js'), {});

gulp.task('e2e:start', e2eStart);
gulp.task('e2e:end', e2eEnd);
gulp.task('e2e:test', gulp.series(['e2e:start', 'e2e:end']));

// Full task
gulp.task('default', gulp.series(['e2e:start', 'e2e:end']));

//
// Test
//

function e2eStart(done) {
let run = wdio.run();
run.then((code) => done(), (error) => {
console.error('Launcher failed to start the test', error.stacktrace);
done();
process.exit(1);
});
}

function e2eEnd(done) {
done();
process.exit(0);
}
47 changes: 47 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "fs-hard-drive",
"version": "1.0.0",
"description": "Hard drive functions not incluyed in fs package",
"homepage": "https://github.com/vzenix/fs-hard-drive/wiki",
"author": "Francisco M. E. <[email protected]>",
"license": "MIT",
"main": "src/index.js",
"scripts": {
"test": "gulp e2e:test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/vzenix/fs-hard-drive.git"
},
"keywords": [
"disk",
"disks",
"free",
"space",
"volumes",
"harddisk",
"library",
"module"
],
"bugs": {
"url": "https://github.com/vzenix/fs-hard-drive/issues"
},
"devDependencies": {
"@wdio/cli": "^5.3.5",
"@wdio/dot-reporter": "^5.2.3",
"@wdio/local-runner": "^5.3.5",
"@wdio/mocha-framework": "^5.3.2",
"@wdio/selenium-standalone-service": "^5.2.2",
"@wdio/spec-reporter": "^5.2.3",
"@wdio/sync": "^5.3.2",
"connect": "^3.6.6",
"gulp": "^4.0.0",
"gulp-concat": "^2.6.1",
"gulp-uglify": "^3.0.1",
"gulp-util": "^3.0.8",
"selenium-standalone": "^6.15.4",
"serve-static": "^1.13.2",
"webdriverio": "^5.3.5"
},
"dependencies": {}
}
10 changes: 10 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// List devices
exports.DriveType = require('./ls-devices').DriveType;
exports.lsDevices = require('./ls-devices').lsDevices;

// Scan folders
exports.scanFolders = require('./scan-folders').scanFolders;
exports.scanFoldersInfo = require('./scan-folders').scanFoldersInfo;

// Utils
exports.Utils = require('./utils').Utils;
121 changes: 121 additions & 0 deletions src/linux/ls-devices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
const exec = require('child_process').exec;
const os = require('os');

exports.lsDevices = lsDevicesDF;

function lsDevicesDF() {

let promise = new Promise(function (resolve, reject) {
exec('df -T -BK', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
reject({
error: error,
stderr: stderr
});

return;
}

let info = CMDFormatResponseSpaces(stdout);

let response = new Array();
for (let j = 0; j < info.length; j++) {

if (info[j].length === 7) {
let ln = CMDComposeLsDeviceLineDf(info[j]);
if (typeof ln.file_system === typeof 'string' && ln.file_system.trim().indexOf('ext') >= 0) {
response.push(ln);
}
}

}

resolve(response);
});

});

return promise;
}

function CMDComposeLsDeviceLineDf(ln) {
return {
drive_type: 0,
caption: ln[6].trim(),
description: ln[0].trim(),
file_system: ln[1].trim(),
size: parseInt(ln[2].trim().substring(0, ln[2].trim().length - 1), 10) * 1000,
free_space: parseInt(ln[4].trim().substring(0, ln[4].trim().length - 1), 10) * 1000,
so: {
file_system: ln[0],
type: ln[1],
one_k_blocks: ln[2],
used: ln[3],
available: ln[4],
use: ln[5],
mounted_on: ln[6]
}
};
}

/**
* Format a traditional response of shell cmd with headers
* @param {*} stdout
*/
function CMDFormatResponseSpaces(stdout) {
let result = stdout.split(os.EOL);
let ret = new Array();
for (let i = 1; i < result.length; i++) {
ret.push(CMDFormatResponseSpacesCalculateInfo(result[i].trim()));
}

return ret;
}


/**
* Helper of CMDFormatResponseSpaces
* @param {*} ln
*/
function CMDFormatResponseSpacesCalculateInfo(ln) {
let chars = ln.split('');
let current = {
title: '',
position_start: 0,
position_end: 0
};

let ret = new Array();
let add = false;
for (let i = 0; i < chars.length; i++) {
if (chars[i] === ' ') {
add = true;
}

if (chars[i] !== ' ' && add) {
current.position_end = i - 1;
current.title = current.title;
add = false;
ret.push(current);

current = {
title: '',
position_start: i,
position_end: i
};
}

current.title += chars[i];
}

current.position_end = ln.length;
ret.push(current);

let final = new Array();
for (let i = 0; i < ret.length; i++) {
final.push(ret[i].title);
}

return final;
}
32 changes: 32 additions & 0 deletions src/ls-devices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const os = require('os');

exports.DriveType = {
Unknown: 0,
NoRootDirectory: 1,
RemovableDisk: 2,
LocalDisk: 3,
NetworkDrive: 4,
CompactDisc: 5,
RAMDisk: 6
};

const isWindows = os.type().indexOf('Window') >= 0;
const isLinux = os.type().indexOf('Linux') >= 0;
const isMac = os.type().indexOf('Darwin') >= 0;

if (isWindows) {
const lsDevices = require('./windows/ls-devices').lsDevices;
exports.lsDevices = lsDevices;
} else if (isLinux || isMac) {
const lsDevices = require('./linux/ls-devices').lsDevices;
exports.lsDevices = lsDevices;
} else {
exports.lsDevices = () => {
return new Promise((resolve, reject) => {
reject({
error: `OS ${os.type()} not supported`,
stderr: null
});
});
}
}
Loading

0 comments on commit 63134cc

Please sign in to comment.