Skip to content

Commit

Permalink
Read system partition cross-platform
Browse files Browse the repository at this point in the history
  • Loading branch information
giapdong committed Mar 31, 2021
1 parent f055fd0 commit 6498f53
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 41 deletions.
27 changes: 27 additions & 0 deletions build/lib/bean/queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class Queue {
constructor() {
this.data = new Array();
}
isEmpty() {
return this.data.length == 0;
}
enqueue(newItem) {
this.data = this.data.concat(newItem);
return this.size();
}
dequeue() {
return this.data.shift();
}
peek() {
return !this.isEmpty() ? this.data[0] : undefined;
}
clear() {
this.data = new Array();
}
size() {
return this.data.length;
}
}
exports.default = Queue;
27 changes: 27 additions & 0 deletions build/lib/bean/stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class Stack {
constructor() {
this.data = new Array();
}
isEmpty() {
return this.data.length == 0;
}
push(newItem) {
this.data = this.data.concat(newItem);
return this.size();
}
pop() {
return this.data.pop();
}
peek() {
return !this.isEmpty() ? this.data[this.data.length - 1] : undefined;
}
clear() {
this.data = new Array();
}
size() {
return this.data.length;
}
}
exports.default = Stack;
12 changes: 8 additions & 4 deletions build/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const global_helper_1 = require("./helper/global-helper");
const CompareHelper = __importStar(require("./helper/compare-helper"));
const ScanHelper = __importStar(require("./helper/scan-helper"));
const os_1 = __importDefault(require("os"));
const win32 = __importStar(require("./os/win32"));
const win32_1 = require("./os/win32");
const darwin_1 = require("./os/darwin");
const scanDir = path_1.default.join(__dirname, "..", "..", "scan");
const compareDir = path_1.default.join(__dirname, "..", "..", "compare");
async function Scan(root = __dirname, threshold = 1000000, mode = interface_1.ScanMode.Normal) {
Expand Down Expand Up @@ -67,9 +68,12 @@ function readSystemPartition() {
return new Promise(async (resolve, reject) => {
switch (os_1.default.platform()) {
case "win32": {
let data = await win32.readPartition();
resolve(data);
break;
const data = await new win32_1.win32().readSystemPartition();
return resolve(data);
}
case "darwin": {
const data = await new darwin_1.darwin().readSystemPartition();
return resolve(data);
}
default:
resolve(null);
Expand Down
6 changes: 6 additions & 0 deletions build/lib/inheritable/ASystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DiskSystem = void 0;
class DiskSystem {
}
exports.DiskSystem = DiskSystem;
55 changes: 55 additions & 0 deletions build/lib/os/darwin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.darwin = exports.parseData = void 0;
const child_process_1 = require("child_process");
const ASystem_1 = require("../inheritable/ASystem");
const spawnCommand = function (command, options) {
return new Promise((resolve, reject) => {
const ps = child_process_1.spawn(command, options);
if (!ps || !ps.stdout) {
const err = new Error("Cannot spawn command!");
return reject(err);
}
let ret = "";
ps.stdout.on("data", function (data) {
ret = data.toString();
});
ps.on("error", function (err) {
reject(err);
});
ps.on("close", function (code, signal) {
resolve(ret);
});
});
};
function parseData(stdout) {
const listRawPartition = stdout.split("\n").filter(Boolean);
listRawPartition.shift();
return listRawPartition.map(partition => {
const arr = partition
.replace(/\s+(\d+)/g, "\t$1")
.replace(/\s+\//g, "\t/")
.split(/\t/);
const usedSize = parseInt(arr[2].replace(/k|K/g, "")) * 1024;
const freespace = parseInt(arr[3].replace(/k|K/g, "")) * 1024;
const size = freespace + usedSize;
const caption = arr[5];
return { caption, size, freespace };
});
}
exports.parseData = parseData;
class darwin extends ASystem_1.DiskSystem {
readSystemPartition() {
return new Promise(async (resolve, reject) => {
try {
const rawPartition = await spawnCommand("df", ["-bk"]);
const partitions = parseData(rawPartition);
resolve(partitions);
}
catch (error) {
reject(error);
}
});
}
}
exports.darwin = darwin;
78 changes: 42 additions & 36 deletions build/lib/os/win32.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,49 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.caseToPartitionNode = exports.readPartition = void 0;
exports.win32 = exports.castToPartitionNode = exports.parseData = exports.execCommand = void 0;
const child_process_1 = require("child_process");
function readPartition() {
return new Promise(async (resolve, reject) => {
var _a;
const getPartition = child_process_1.exec("wmic logicaldisk get deviceid, freespace, size");
(_a = getPartition.stdout) === null || _a === void 0 ? void 0 : _a.on("data", (data) => {
let rawDataPartition = data.split("\r\r\n").filter((item) => item);
let tablePartition = rawDataPartition.map((item) => item.trim().split(/\s+/gm));
let headerTable = tablePartition.length ? tablePartition.shift() : [];
headerTable = headerTable ? headerTable : [];
headerTable = headerTable.map((item) => item.toLowerCase());
let listPartition = new Array();
tablePartition.forEach((partition) => {
let temp = Object.fromEntries(partition.map((value, index) => {
return [headerTable[index], value];
}));
let node = caseToPartitionNode(temp);
if (node)
listPartition.push(node);
});
resolve(listPartition);
const ASystem_1 = require("../inheritable/ASystem");
function execCommand(command) {
return new Promise((resolve, reject) => {
child_process_1.exec(command, (error, stdout) => {
if (error)
return reject(error);
try {
resolve(stdout);
}
catch (error) {
reject(error);
}
});
});
}
exports.readPartition = readPartition;
function caseToPartitionNode(partition) {
if (!Object.prototype.hasOwnProperty.call(partition, "deviceid"))
return null;
if (!Object.prototype.hasOwnProperty.call(partition, "freespace"))
return null;
if (!Object.prototype.hasOwnProperty.call(partition, "size"))
return null;
if (isNaN(+partition.freespace))
return null;
if (isNaN(+partition.size))
return null;
let node = { deviceid: partition.deviceid, freespace: +partition.freespace, size: +partition.size };
return node;
exports.execCommand = execCommand;
function parseData(stdout) {
const parsed = stdout
.trim()
.split("\n")
.slice(1)
.map(line => {
return line.trim().split(/\s+(?=[\d/])/);
});
const listPartition = parsed.map(partition => castToPartitionNode(partition));
return listPartition;
}
exports.parseData = parseData;
function castToPartitionNode(partition) {
const caption = partition[0];
const freespace = parseInt(partition[1]);
const size = parseInt(partition[2]);
return { caption, freespace, size };
}
exports.castToPartitionNode = castToPartitionNode;
class win32 extends ASystem_1.DiskSystem {
readSystemPartition() {
return new Promise(async (resolve, reject) => {
const stdout = await execCommand("wmic logicaldisk get size,freespace,caption");
const listPartition = parseData(stdout);
resolve(listPartition);
});
}
}
exports.caseToPartitionNode = caseToPartitionNode;
exports.win32 = win32;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "disk-management",
"version": "0.4.0",
"version": "0.4.1",
"description": "Module/application for management your disk",
"main": "index.js",
"license": "MIT",
Expand Down

0 comments on commit 6498f53

Please sign in to comment.