-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdelayed_sleep.ts
More file actions
64 lines (55 loc) · 1.68 KB
/
delayed_sleep.ts
File metadata and controls
64 lines (55 loc) · 1.68 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
/**
* @license
* Copyright 2026 Steven A. Thompson
* SPDX-License-Identifier: MIT
*/
import { SESSION_NAME, sendNotification } from './tmux_utils.js';
import * as fs from 'fs';
interface SleepState {
pid: number;
wakeTime: number;
recurring: boolean;
interval: number;
}
async function main() {
const args = process.argv.slice(2);
if (args.length < 2) {
process.exit(1);
}
const seconds = parseInt(args[0], 10);
const id = args[1];
// args[2] is 'true'/'false' for recurring
const isRecurring = args[2] === 'true';
// args[3] is the path to the state file
const stateFilePath = args[3];
const target = `${SESSION_NAME}:0.0`;
if (isRecurring) {
while (true) {
await new Promise(resolve => setTimeout(resolve, seconds * 1000));
await sendNotification(target, `[${id}] Recurring sleep interval (${seconds}s) reached. Waking up.`);
// Update state file with next wake time
if (stateFilePath && fs.existsSync(stateFilePath)) {
try {
const nextWake = Date.now() + (seconds * 1000);
const state: SleepState = JSON.parse(fs.readFileSync(stateFilePath, 'utf8'));
state.wakeTime = nextWake;
fs.writeFileSync(stateFilePath, JSON.stringify(state));
} catch (e) {
// Ignore errors updating state file
}
}
}
} else {
await new Promise(resolve => setTimeout(resolve, seconds * 1000));
await sendNotification(target, `[${id}] Sleep complete. Waking up.`);
// Clean up state file
if (stateFilePath && fs.existsSync(stateFilePath)) {
try {
fs.unlinkSync(stateFilePath);
} catch (e) {
// Ignore
}
}
}
}
main();