-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
executable file
·90 lines (77 loc) · 2.29 KB
/
cli.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
#!/usr/bin/env node
"use strict";
const chalk = require("chalk");
const costpoint = require("./costpoint");
const program = require("commander");
require("dotenv").config();
const url = process.env.COSTPOINT_URL;
const username = process.env.COSTPOINT_USERNAME;
const password = process.env.COSTPOINT_PASSWORD;
const system = process.env.COSTPOINT_SYSTEM;
if (
typeof url === "undefined" ||
typeof username === "undefined" ||
typeof password === "undefined" ||
typeof system === "undefined"
) {
console.error(
chalk.red(
"Make sure that COSTPOINT_URL, COSTPOINT_USERNAME, COSTPOINT_PASSWORD, COSTPOINT_SYSTEM are set in the environment."
)
);
process.exit(1);
}
program
.name("costpoint")
.version(require("./package.json").version)
.description("A command line utility for Costpoint.");
program
.command("show")
.description("show timesheet")
.action(async () => {
const cp = await costpoint.launch(url, username, password, system);
cp.display();
await cp.close();
});
program
.command("set <line> <day> <hours>")
.description("set hours for given project line and day")
.action(async (line, day, hours) => {
const cp = await costpoint.launch(url, username, password, system);
await cp.set(line, day, hours);
await cp.save();
cp.display();
await cp.close();
});
program
.command("setm <values...>")
.description("set hours for multiple project lines and days")
.usage("<line> <day> <hours>, <line> <day> <hours>, ...")
.action(async xs => {
xs = xs.map(x => x.replace(/,/g, "")).filter(Boolean);
if (xs.length % 3 !== 0) {
console.error(chalk.red("Not enough values provided."));
process.exit(1);
}
const cp = await costpoint.launch(url, username, password, system);
for (let i = 0; i < xs.length; i += 3) {
const line = xs[i];
const day = xs[i + 1];
const hours = xs[i + 2];
await cp.set(line, day, hours);
}
await cp.save();
cp.display();
await cp.close();
});
program
.command("add <code>")
.description("add project code to timesheet")
.action(async code => {
const cp = await costpoint.launch(url, username, password, system);
await cp.add(code);
cp.display();
await cp.save();
await cp.close();
});
program.parse(process.argv);