-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·122 lines (103 loc) · 2.5 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
#!/usr/bin/env node
const inquirer = require('inquirer');
const { table } = require('table');
const Browser = require('./lib/Browser');
let browser;
async function handleExit() {
if (browser) {
await browser.close();
}
process.exit();
}
function buildPrompt(projects) {
return [
{
type: 'list',
name: 'projectId',
message: 'Select a project',
choices: projects.map(item => ({ name: item.name, value: item.id })),
},
{
type: 'list',
name: 'taskId',
message: 'Select task',
choices: (prev) => {
return projects
.find(item => item.id === prev.projectId).tasks
.map(item => ({ name: item.name, value: item.id }))
}
},
{
type: 'input',
name: 'hours',
message: 'Enter hours',
filter: input => Number(input),
validate: input => !isNaN(input),
},
];
}
async function init() {
browser = new Browser();
console.log(`📅 ${new Date().toDateString()}`);
await browser.open();
if (await browser.needsLogin()) {
const answers = await inquirer.prompt([
{
type: 'input',
name: 'user',
message: 'Username:',
prefix: '👤',
},
{
type: 'password',
name: 'pass',
message: 'Password:',
prefix: '🔑',
}
]);
const { user, pass } = answers;
await browser.login(user, pass);
}
const { projects, totalDayHours, dayColumnIndex, summary } = await browser.load();
let dayHours = totalDayHours;
console.log(table(summary, {
columnDefault: {
alignment: 'center'
},
columns: {
0: { alignment: 'left' }
}
}));
console.log(`💻 ${projects.length} projects found`);
console.log(`🕑 ${dayHours} hours have been entered for today`);
while (dayHours < 8) {
const { taskId, hours } = await inquirer.prompt(buildPrompt(projects));
dayHours += hours;
console.log(`Total day hours: ${dayHours}`);
await browser.fill(taskId, dayColumnIndex, hours);
}
const answers = await inquirer.prompt([
{
type: 'confirm',
name: 'confirm',
message: 'Submit for approve?',
default: false,
}
]);
if (answers.confirm) {
await browser.submit();
} else {
await browser.save();
}
console.log('👌 Very nice!');
await browser.close();
}
process.on('SIGINT', handleExit);
process.on('SIGTERM', handleExit);
(async () => {
try {
await init();
} catch (e) {
console.error('Unexpected error: ', e);
}
})();