-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Read system partition cross-platform
- Loading branch information
Showing
7 changed files
with
166 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters