-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·129 lines (109 loc) · 3.7 KB
/
index.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env node
'use strict';
const process = require('process');
const path = require('path');
const notifier = require('node-notifier');
const progress = require('progress');
const colors = require('colors');
const program = require('commander');
const pomodoro = require('./models/pomodoro');
const { spawn } = require('child_process');
const APP = {
getPomodoroType: ({ shortbreak, longbreak, timer }) => {
let timerTypeKey = 'default';
const types = [
{shortbreak},
{longbreak},
{timer}
];
types.filter((type) => {
const key = Object.keys(type)[0];
if (type[key]) {
timerTypeKey = key;
}
});
return timerTypeKey;
},
}
program
.version(process.env.npm_package_version)
.usage('Pomodoro cli - a simple pomodoro for terminal')
.option('-s, --shortbreak', 'Add short break timer')
.option('-l, --longbreak', 'Add long break timer')
.option('-t, --timer <time>', 'Add specific time in minutes', parseInt)
.option('-p, --play-sound <filepath>', 'Play a sound file when the timer expires')
.option('--player-binary <name>', 'System binary to use for playing sounds, default auto selects')
.option('--start-command <filepath>', 'Execute a shell command ansynchronously at the start of the timer. WARNING: The command is passed directly to a shell with the same user permissions this program runs under -- use with caution!')
.option('--end-command <filepath>', 'Execute a shell command ansynchronously at the end of the timer. WARNING: The command is passed directly to a shell with the same user permissions this program runs under -- use with caution!')
.option('--interrupt-command <filepath>', 'Execute a shell command prior to exiting from a SIGINT signal. WARNING: The command is passed directly to a shell with the same user permissions this program runs under -- use with caution!')
.option('-c, --progress-color <color>', 'Color of the progress bar, as a valid colors.js text color', 'red')
.option('-a, --add-task <task>', 'Add a new task', 'task')
.parse(process.argv);
const playerArgs = {};
if (program.playerBinary) {
playerArgs.player = program.playerBinary;
}
const player = require('play-sound')(playerArgs);
const init = () => {
if (program.startCommand) {
runCommand(program.startCommand);
}
const pomodoroType = APP.getPomodoroType(program);
pomodoro.setConfig(pomodoroType, program.timer);
var bar = new progress(':timerFrom [:bar] :timerTo'[program.progressColor], {
complete: '=',
incomplete: ' ',
width: process.stdout.columns,
total: pomodoro.totalSeconds(),
timerTo: pomodoro.getTime('timerTo'),
timerFrom: pomodoro.getTime('timerFrom'),
callback: notify
});
if (program.interruptCommand) {
process.on('SIGINT', function() {
runCommand(program.interruptCommand);
// 130 is the exit code for SIGINT.
process.exit(130);
});
}
setInterval(() => {
tick(bar);
},1000);
};
const tick = (bar) => {
bar.tick(1, {
timerFrom: pomodoro.getTime('timerFrom'),
timerTo: pomodoro.getTime('timerTo')
});
pomodoro.tick();
};
const notify = () => {
notifier.notify({
title: 'Pomodoro Cli',
message: pomodoro.getMessage(),
icon: path.join(__dirname, 'images/pomodoro.png'),
sound: program.playSound ? 'false' : 'true',
});
if (program.playSound) {
player.play(program.playSound, function(err) {
if (err) {
throw err;
}
});
}
if (program.endCommand) {
runCommand(program.endCommand);
}
process.exit(0);
};
const runCommand = (command) => {
const child = spawn(command, {
stdio: 'ignore',
shell: true,
detached: true,
});
child.on('error', function (err) {
throw err;
});
}
init();