-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist-waked.js
More file actions
executable file
·70 lines (60 loc) · 1.82 KB
/
Copy pathlist-waked.js
File metadata and controls
executable file
·70 lines (60 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env node
const spawn = require("node:child_process").spawn;
const process = require("node:process");
const readline = require("node:readline");
const parseLine = (line) => {
const [dtAndType, ...rest] = line.split(" ");
const [date, time, tz, eventType] = dtAndType.split(" ");
const dt = new Date(`${date} ${time} ${tz}`);
return {
dt,
eventType,
log: rest.join(" ").trim(),
}
}
const dateFormatter = new Intl.DateTimeFormat("ja-JP", {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
timeZone: "Asia/Tokyo"
});
const formatToDisplay = (event) => {
return `${dateFormatter.format(event.dt)} ${event.eventType.padEnd(6, " ")} - ${event.log}`;
}
const outputEvents = [
"Sleep",
"Wake",
];
/**
* macOSのスリープからの復帰履歴を表示する
*/
const main = async () => {
const ps = spawn("pmset", ["-g", "log"], { stdio: ["ignore", "pipe", "pipe"] });
ps.stderr.on("data", chunk => {
process.stderr.write(chunk);
});
const rl = readline.createInterface({ input: ps.stdout, terminal: false });
const parsedLines = [];
rl.on("line", (line) => {
if (line.includes("Entering Sleep") || line.includes("Wake from")) {
const parsedLine = parseLine(line)
if (
outputEvents.includes(parsedLine.eventType)
&& !(parsedLine.log.includes("Maintenance Sleep") || parsedLine.log.includes("Sleep Service Back to Sleep"))
) {
parsedLines.push(parsedLine);
}
}
})
const { code } = await new Promise((resolve) => ps.on("close", c => resolve({ code: c })));
if (code !== 0) {
throw new Error(`exit ${code}`);
}
console.log("Wake/Sleep Events:");
console.log(parsedLines.length);
parsedLines.forEach(event => console.log(formatToDisplay(event)));
}
main();