Skip to content

Commit

Permalink
Improve code read partition in darwin system
Browse files Browse the repository at this point in the history
  • Loading branch information
giapdong committed Mar 30, 2021
1 parent 72e0e44 commit f055fd0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 45 deletions.
6 changes: 3 additions & 3 deletions src/lib/interface/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ describe("Interface test", () => {
});

test("PartitionNode interface", () => {
const partition: PartitionNode = { deviceid: "C", freespace: 2000, size: 5000 };
const partition: PartitionNode = { caption: "C", freespace: 2000, size: 5000 };

expect(typeof partition.deviceid).toEqual("string");
expect(typeof partition.caption).toEqual("string");
expect(typeof partition.size).toEqual("number");
expect(typeof partition.freespace).toEqual("number");

expect(partition.deviceid).toEqual("C");
expect(partition.caption).toEqual("C");
expect(partition.size).toEqual(5000);
expect(partition.freespace).toEqual(2000);
});
Expand Down
99 changes: 57 additions & 42 deletions src/lib/os/darwin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,66 @@ import { ChildProcess, spawn } from "child_process";
import { DiskSystem } from "../inheritable/ASystem";
import { PartitionNode } from "../interface";

const spawnCommand = function (command: string, options: string[]): Promise<any> {
return new Promise((resolve, reject) => {
// https://en.wikipedia.org/wiki/Df_(Unix)#Specification
const ps: ChildProcess = 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: any) {
ret = data.toString();
});

ps.on("error", function (err: Error) {
reject(err);
});

ps.on("close", function (code: number | null, signal: NodeJS.Signals | null) {
resolve(ret);
});
});
};

export function parseData(stdout: string): PartitionNode[] {
const listRawPartition = stdout.split("\n").filter(Boolean);
listRawPartition.shift();

return listRawPartition.map(partition => {
// Parse correct format of df result
// https://github.com/adriano-di-giovanni/node-df/blob/master/lib/parse.js
const arr = partition
// one or more whitespaces followed by one or more digits
// must be interpreted as column delimiter
.replace(/\s+(\d+)/g, "\t$1")
// one or more whitespaces followed by a slash
// must be interpreted as the last column delimiter
.replace(/\s+\//g, "\t/")
// split into columns
.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 };
});
}

export class darwin extends DiskSystem {
readSystemPartition(): Promise<PartitionNode[]> {
return new Promise((resolve, reject) => {
// https://en.wikipedia.org/wiki/Df_(Unix)#Specification
const ps: ChildProcess = spawn("df", ["-bk"]);
if (!ps || !ps.stdout) {
const err = new Error("Cannot spawn command!");
return reject(err);
}
let ret = "";

ps.stdout.on("data", function (data: any) {
ret = data.toString();
});

ps.on("error", function (err: Error) {
reject(err);
});

ps.on("close", function (code: number | null, signal: NodeJS.Signals | null) {
const listRawPartition = ret.split("\n").filter(Boolean);
listRawPartition.shift();

const partitions = listRawPartition.map(partition => {
// Parse correct format of df result
// https://github.com/adriano-di-giovanni/node-df/blob/master/lib/parse.js
const arr = partition
// one or more whitespaces followed by one or more digits
// must be interpreted as column delimiter
.replace(/\s+(\d+)/g, "\t$1")
// one or more whitespaces followed by a slash
// must be interpreted as the last column delimiter
.replace(/\s+\//g, "\t/")
// split into columns
.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 };
});
return new Promise(async (resolve, reject) => {
try {
const rawPartition = await spawnCommand("df", ["-bk"]);
const partitions = parseData(rawPartition);
resolve(partitions);
});
} catch (error) {
reject(error);
}
});
}
}

0 comments on commit f055fd0

Please sign in to comment.