-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworky.mjs
executable file
·117 lines (101 loc) · 3.05 KB
/
worky.mjs
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
#!/usr/bin/env node
import Worky from './api.mjs'
import minimist from 'minimist'
import 'dotenv/config'
import { readFile, writeFile } from 'fs/promises'
import dayjs from 'dayjs'
import customParseFormat from 'dayjs/plugin/customParseFormat.js'
import relativeTime from 'dayjs/plugin/relativeTime.js'
dayjs.extend(customParseFormat)
dayjs.extend(relativeTime)
function usage() {
console.error(`Usage: node worky.mjs ARGS
ARGS:
[report] Shows a status report
[checkin] Executes a check-in
[checkout] Executes a check-out
[--tokenFile {filename}] file where the token is stored
This script expects an .env file in the current directory, this file
should contain the Worky credentials like this:
WORKY_USER=email@company
WORKY_PASS=plain_text_password
By default the authentication token is saved to a file called .token
in the current directory.
`)
process.exit(1)
}
import { readSync } from 'fs'
function getChar() {
let buffer = Buffer.alloc(1)
readSync(0, buffer, 0, 1)
return buffer.toString('utf8')
}
var args = minimist(process.argv.slice(2));
const commands = args._
const username = process.env.WORKY_USER
const password = process.env.WORKY_PASS
const tokenFile = process.env.TOKEN_FILE || ".token"
if (!username || commands.length == 0) {
usage()
}
async function login() {
let token
if (tokenFile) {
try {
token = await readFile(tokenFile, {encoding: 'utf8'})
} catch(err) { }
}
let newtoken = await worky.loadOrLogin(token, username, password)
if (tokenFile && token != newtoken) {
await writeFile(tokenFile, newtoken)
}
}
const worky = new Worky()
try {
await login();
if (args.timework) {
console.log(await worky.status_timework())
}
let timework = await worky.status_timework()
while(commands.length > 0) {
let command = commands.shift()
if (command === 'timework') {
console.log(timework)
}
if (command === 'status') {
let status = await worky.status(timework)
if(status=='unknown') console.log(timework)
let action_available = worky.action_available(status)
console.log('status:', status)
console.log('action available', action_available)
if(action_available != 'none') {
console.log('Perform that? [yN]');
let response = getChar().toLowerCase()
if(response=='y') {
if(action_available == 'checkout') await worky.checkout(timework);
else if(action_available == 'checkin') await worky.checkin(timework);
let timework = await worky.status_timework()
let status = await worky.status(timework)
console.log('status:', status)
}
}
}
if (command === 'checkin') {
console.log(`Checking in`)
timework = await worky.checkin(timework)
console.log('Ok')
}
if (command === 'checkout') {
console.log(`Checking out`)
timework = await worky.checkout(timework)
console.log('Ok')
}
}
process.exit(0)
} catch (errors) {
console.error('ERROR', errors)
/* for (let err of errors) {
console.log(err)
}*/
process.exit(1)
}