From 6498f53d80472e1764a9943d900fa47c8a2270e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gi=C3=A1p=20V=C4=83n=20=C4=90=C3=B4ng?= Date: Wed, 31 Mar 2021 21:08:10 +0700 Subject: [PATCH] Read system partition cross-platform --- build/lib/bean/queue.js | 27 +++++++++++ build/lib/bean/stack.js | 27 +++++++++++ build/lib/index.js | 12 +++-- build/lib/inheritable/ASystem.js | 6 +++ build/lib/os/darwin.js | 55 ++++++++++++++++++++++ build/lib/os/win32.js | 78 +++++++++++++++++--------------- package.json | 2 +- 7 files changed, 166 insertions(+), 41 deletions(-) create mode 100644 build/lib/bean/queue.js create mode 100644 build/lib/bean/stack.js create mode 100644 build/lib/inheritable/ASystem.js create mode 100644 build/lib/os/darwin.js diff --git a/build/lib/bean/queue.js b/build/lib/bean/queue.js new file mode 100644 index 0000000..a552af9 --- /dev/null +++ b/build/lib/bean/queue.js @@ -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; diff --git a/build/lib/bean/stack.js b/build/lib/bean/stack.js new file mode 100644 index 0000000..c082a86 --- /dev/null +++ b/build/lib/bean/stack.js @@ -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; diff --git a/build/lib/index.js b/build/lib/index.js index b207085..855150d 100644 --- a/build/lib/index.js +++ b/build/lib/index.js @@ -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) { @@ -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); diff --git a/build/lib/inheritable/ASystem.js b/build/lib/inheritable/ASystem.js new file mode 100644 index 0000000..abfbd22 --- /dev/null +++ b/build/lib/inheritable/ASystem.js @@ -0,0 +1,6 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.DiskSystem = void 0; +class DiskSystem { +} +exports.DiskSystem = DiskSystem; diff --git a/build/lib/os/darwin.js b/build/lib/os/darwin.js new file mode 100644 index 0000000..9239bf7 --- /dev/null +++ b/build/lib/os/darwin.js @@ -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; diff --git a/build/lib/os/win32.js b/build/lib/os/win32.js index 5caf1cf..28d7723 100644 --- a/build/lib/os/win32.js +++ b/build/lib/os/win32.js @@ -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; diff --git a/package.json b/package.json index 9bec68d..d32623e 100644 --- a/package.json +++ b/package.json @@ -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",