@ever-teams/cli 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +240 -2
- package/dist/args.js +54 -0
- package/dist/command.js +1 -0
- package/dist/commands/export.js +102 -0
- package/dist/commands/index.js +21 -0
- package/dist/commands/log.js +86 -0
- package/dist/commands/login.js +76 -0
- package/dist/commands/logout.js +24 -0
- package/dist/commands/projects.js +26 -0
- package/dist/commands/start.js +34 -0
- package/dist/commands/status.js +39 -0
- package/dist/commands/stop.js +35 -0
- package/dist/commands/today.js +49 -0
- package/dist/config.js +91 -0
- package/dist/csv.js +48 -0
- package/dist/entries.js +78 -0
- package/dist/failures.js +170 -0
- package/dist/format.js +23 -0
- package/dist/help.js +52 -0
- package/dist/index.js +4 -0
- package/dist/io.js +14 -0
- package/dist/lookup.js +105 -0
- package/dist/output.js +42 -0
- package/dist/past-range.js +120 -0
- package/dist/prompt.js +84 -0
- package/dist/refusals.js +19 -0
- package/dist/run.js +81 -0
- package/dist/session.js +116 -0
- package/dist/version.js +4 -0
- package/package.json +36 -5
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { textOption } from '../args.js';
|
|
2
|
+
import { recordOfLog, workOf } from '../entries.js';
|
|
3
|
+
import { clockOf } from '../format.js';
|
|
4
|
+
import { namesOf, workNamed } from '../lookup.js';
|
|
5
|
+
import { openSession } from '../session.js';
|
|
6
|
+
async function start(context) {
|
|
7
|
+
const client = openSession(context);
|
|
8
|
+
const work = await workNamed(client, context.values);
|
|
9
|
+
const note = textOption(context.values, 'note') || undefined;
|
|
10
|
+
const log = await client.timer.start({ projectId: work.projectId, taskId: work.taskId, description: note });
|
|
11
|
+
const names = await namesOf(client, log, work);
|
|
12
|
+
const entry = recordOfLog(log, names, context.io.now());
|
|
13
|
+
const startedAt = clockOf(new Date(entry.startedAt));
|
|
14
|
+
context.output.result({ started: true, entry }, [`Timer started at ${startedAt}${workOf(entry)}.`]);
|
|
15
|
+
}
|
|
16
|
+
export const startCommand = {
|
|
17
|
+
name: 'start',
|
|
18
|
+
summary: 'Start your timer, on a project or a task',
|
|
19
|
+
usage: 'start [--project <name or id>] [--task <number or id>] [--note <text>]',
|
|
20
|
+
description: 'Starts your timer now, on the project or the task you name. A task given alone brings its project.\n' +
|
|
21
|
+
'A timer already running stops first, as in the apps.',
|
|
22
|
+
options: { project: { type: 'string' }, task: { type: 'string' }, note: { type: 'string' } },
|
|
23
|
+
optionHelp: [
|
|
24
|
+
['--project <name or id>', 'One of your projects (ever-teams projects lists them)'],
|
|
25
|
+
['--task <number or id>', 'One of your tasks: 12, #12, its number as the app shows it, or its id'],
|
|
26
|
+
['--note <text>', 'What you work on']
|
|
27
|
+
],
|
|
28
|
+
examples: [
|
|
29
|
+
'ever-teams start',
|
|
30
|
+
'ever-teams start --project Website --task 12',
|
|
31
|
+
'ever-teams start --project Website --note "Call with the client"'
|
|
32
|
+
],
|
|
33
|
+
run: start
|
|
34
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { recordOfLog, workOf } from '../entries.js';
|
|
2
|
+
import { clockOf, durationText } from '../format.js';
|
|
3
|
+
import { namesOf } from '../lookup.js';
|
|
4
|
+
import { openSession } from '../session.js';
|
|
5
|
+
function runningLogOf(timer) {
|
|
6
|
+
if (!timer.running) {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
return timer.lastLog;
|
|
10
|
+
}
|
|
11
|
+
async function status(context) {
|
|
12
|
+
const client = openSession(context);
|
|
13
|
+
const timer = await client.timer.status();
|
|
14
|
+
// The running entry lasts until the status was read: the reads of its names come after.
|
|
15
|
+
const observedAt = context.io.now();
|
|
16
|
+
const today = `Today: ${durationText(timer.duration)} tracked.`;
|
|
17
|
+
const log = runningLogOf(timer);
|
|
18
|
+
if (!log) {
|
|
19
|
+
context.output.result({ running: false, todaySeconds: timer.duration, entry: null }, [
|
|
20
|
+
`The timer is stopped. ${today}`
|
|
21
|
+
]);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const names = await namesOf(client, log);
|
|
25
|
+
const entry = recordOfLog(log, names, observedAt);
|
|
26
|
+
const since = clockOf(new Date(entry.startedAt));
|
|
27
|
+
const running = `The timer is running since ${since} (${durationText(entry.durationSeconds)})${workOf(entry)}.`;
|
|
28
|
+
context.output.result({ running: true, todaySeconds: timer.duration, entry }, [running, today]);
|
|
29
|
+
}
|
|
30
|
+
export const statusCommand = {
|
|
31
|
+
name: 'status',
|
|
32
|
+
summary: 'Show whether your timer runs, and the time tracked today',
|
|
33
|
+
usage: 'status',
|
|
34
|
+
description: 'Shows whether your timer runs, since when and on what, and the time you tracked today.',
|
|
35
|
+
options: {},
|
|
36
|
+
optionHelp: [],
|
|
37
|
+
examples: ['ever-teams status', 'ever-teams status --json'],
|
|
38
|
+
run: status
|
|
39
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { recordOfLog, workOf } from '../entries.js';
|
|
2
|
+
import { clockOf, durationText } from '../format.js';
|
|
3
|
+
import { namesOf } from '../lookup.js';
|
|
4
|
+
import { openSession } from '../session.js';
|
|
5
|
+
// A stop with no timer running leaves the timer as asked: the command succeeds and says so.
|
|
6
|
+
async function stop(context) {
|
|
7
|
+
const { output } = context;
|
|
8
|
+
const client = openSession(context);
|
|
9
|
+
const timer = await client.timer.status();
|
|
10
|
+
if (!timer.running) {
|
|
11
|
+
output.result({ stopped: false, entry: null }, ['The timer was not running: there was nothing to stop.']);
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
const log = await client.timer.stop();
|
|
15
|
+
if (!log) {
|
|
16
|
+
output.result({ stopped: true, entry: null }, ['Timer stopped.']);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const names = await namesOf(client, log);
|
|
20
|
+
const entry = recordOfLog(log, names, context.io.now());
|
|
21
|
+
const stoppedAt = clockOf(new Date(log.stoppedAt));
|
|
22
|
+
const line = `Timer stopped at ${stoppedAt} after ${durationText(entry.durationSeconds)}${workOf(entry)}.`;
|
|
23
|
+
output.result({ stopped: true, entry }, [line]);
|
|
24
|
+
}
|
|
25
|
+
export const stopCommand = {
|
|
26
|
+
name: 'stop',
|
|
27
|
+
summary: 'Stop your timer',
|
|
28
|
+
usage: 'stop',
|
|
29
|
+
description: 'Stops your timer, wherever it was started (here, the web app, the desktop app). With no timer running,\n' +
|
|
30
|
+
'it says so and succeeds.',
|
|
31
|
+
options: {},
|
|
32
|
+
optionHelp: [],
|
|
33
|
+
examples: ['ever-teams stop', 'ever-teams stop --json'],
|
|
34
|
+
run: stop
|
|
35
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { inStartOrder, labelOf, recordOfEntry } from '../entries.js';
|
|
2
|
+
import { clockOf, dayOf, durationText } from '../format.js';
|
|
3
|
+
import { openSession } from '../session.js';
|
|
4
|
+
function rangeOf(record) {
|
|
5
|
+
const from = clockOf(new Date(record.startedAt));
|
|
6
|
+
if (record.stoppedAt === null) {
|
|
7
|
+
return `${from}-now`;
|
|
8
|
+
}
|
|
9
|
+
return `${from}-${clockOf(new Date(record.stoppedAt))}`;
|
|
10
|
+
}
|
|
11
|
+
function whatOf(record) {
|
|
12
|
+
const label = labelOf(record) || '(no project)';
|
|
13
|
+
if (!record.note) {
|
|
14
|
+
return label;
|
|
15
|
+
}
|
|
16
|
+
return `${label}: ${record.note}`;
|
|
17
|
+
}
|
|
18
|
+
function lineOf(record) {
|
|
19
|
+
const range = rangeOf(record).padEnd(11);
|
|
20
|
+
const duration = durationText(record.durationSeconds).padStart(7);
|
|
21
|
+
return ` ${range} ${duration} ${whatOf(record)}`;
|
|
22
|
+
}
|
|
23
|
+
function linesOf(day, records, totalSeconds) {
|
|
24
|
+
if (records.length === 0) {
|
|
25
|
+
return [`Nothing tracked today (${day}).`];
|
|
26
|
+
}
|
|
27
|
+
const header = `Today (${day}): ${durationText(totalSeconds)} tracked.`;
|
|
28
|
+
return [header, ...records.map(lineOf)];
|
|
29
|
+
}
|
|
30
|
+
async function today(context) {
|
|
31
|
+
const client = openSession(context);
|
|
32
|
+
const now = context.io.now();
|
|
33
|
+
const day = dayOf(now);
|
|
34
|
+
const listed = await client.timeEntries.list({ from: day, to: day, mine: true });
|
|
35
|
+
const records = inStartOrder(listed.map((entry) => recordOfEntry(entry, now)));
|
|
36
|
+
const totalSeconds = records.reduce((total, record) => total + record.durationSeconds, 0);
|
|
37
|
+
context.output.result({ date: day, totalSeconds, entries: records }, linesOf(day, records, totalSeconds));
|
|
38
|
+
}
|
|
39
|
+
export const todayCommand = {
|
|
40
|
+
name: 'today',
|
|
41
|
+
summary: "List today's time entries, with their total",
|
|
42
|
+
usage: 'today',
|
|
43
|
+
description: 'Lists your time entries started today, in the time zone of this computer, with what each was on and the\n' +
|
|
44
|
+
'total. A running entry counts up to now.',
|
|
45
|
+
options: {},
|
|
46
|
+
optionHelp: [],
|
|
47
|
+
examples: ['ever-teams today', 'ever-teams today --json'],
|
|
48
|
+
run: today
|
|
49
|
+
};
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { Refusal } from './refusals.js';
|
|
5
|
+
const FOLDER = 'ever-teams';
|
|
6
|
+
const FILE = 'config.json';
|
|
7
|
+
/** %APPDATA%\ever-teams on Windows; $XDG_CONFIG_HOME/ever-teams, else ~/.config/ever-teams, elsewhere. */
|
|
8
|
+
export function configDirOf(place) {
|
|
9
|
+
if (place.platform === 'win32') {
|
|
10
|
+
const appData = place.env.APPDATA || path.join(place.homedir, 'AppData', 'Roaming');
|
|
11
|
+
return path.join(appData, FOLDER);
|
|
12
|
+
}
|
|
13
|
+
// The XDG specification asks to ignore a relative XDG_CONFIG_HOME.
|
|
14
|
+
const xdgHome = place.env.XDG_CONFIG_HOME;
|
|
15
|
+
if (xdgHome && path.isAbsolute(xdgHome)) {
|
|
16
|
+
return path.join(xdgHome, FOLDER);
|
|
17
|
+
}
|
|
18
|
+
return path.join(place.homedir, '.config', FOLDER);
|
|
19
|
+
}
|
|
20
|
+
export const configFileOf = (place) => path.join(configDirOf(place), FILE);
|
|
21
|
+
const isMissing = (error) => error?.code === 'ENOENT';
|
|
22
|
+
const damaged = (file) => new Refusal('SETTINGS_DAMAGED', `The settings file ${file} cannot be read: run ever-teams login again.`);
|
|
23
|
+
function isLogin(value) {
|
|
24
|
+
if (typeof value !== 'object' || value === null) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
const fields = value;
|
|
28
|
+
const hasApiUrl = typeof fields.apiUrl === 'string';
|
|
29
|
+
const hasToken = typeof fields.token === 'string';
|
|
30
|
+
return hasApiUrl && hasToken;
|
|
31
|
+
}
|
|
32
|
+
function loginOf(text, file) {
|
|
33
|
+
let read;
|
|
34
|
+
try {
|
|
35
|
+
read = JSON.parse(text);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
throw damaged(file);
|
|
39
|
+
}
|
|
40
|
+
if (!isLogin(read)) {
|
|
41
|
+
throw damaged(file);
|
|
42
|
+
}
|
|
43
|
+
return { apiUrl: read.apiUrl, token: read.token };
|
|
44
|
+
}
|
|
45
|
+
/** The stored login, or null before the first login and after logout. */
|
|
46
|
+
export function readLogin(place) {
|
|
47
|
+
const file = configFileOf(place);
|
|
48
|
+
let text;
|
|
49
|
+
try {
|
|
50
|
+
text = fs.readFileSync(file, 'utf8');
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
if (isMissing(error)) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
throw damaged(file);
|
|
57
|
+
}
|
|
58
|
+
return loginOf(text, file);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Stores the login in a file only its owner may read or write (0600). The file is written new, then moved over the
|
|
62
|
+
* old one: a mode given to an existing file would not change it.
|
|
63
|
+
*/
|
|
64
|
+
export function writeLogin(place, login) {
|
|
65
|
+
const dir = configDirOf(place);
|
|
66
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
67
|
+
const file = path.join(dir, FILE);
|
|
68
|
+
const written = path.join(dir, `.${FILE}.${randomUUID()}`);
|
|
69
|
+
try {
|
|
70
|
+
fs.writeFileSync(written, `${JSON.stringify(login, null, '\t')}\n`, { flag: 'wx', mode: 0o600 });
|
|
71
|
+
fs.renameSync(written, file);
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
fs.rmSync(written, { force: true });
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
return file;
|
|
78
|
+
}
|
|
79
|
+
/** Removes the stored login; false when there was none. */
|
|
80
|
+
export function removeLogin(place) {
|
|
81
|
+
try {
|
|
82
|
+
fs.unlinkSync(configFileOf(place));
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
if (isMissing(error)) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
}
|
package/dist/csv.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** The columns of an export, named and ordered as the keys of an entry in JSON. */
|
|
2
|
+
export const CSV_COLUMNS = [
|
|
3
|
+
'id',
|
|
4
|
+
'startedAt',
|
|
5
|
+
'stoppedAt',
|
|
6
|
+
'durationSeconds',
|
|
7
|
+
'running',
|
|
8
|
+
'projectId',
|
|
9
|
+
'projectName',
|
|
10
|
+
'taskId',
|
|
11
|
+
'taskNumber',
|
|
12
|
+
'taskTitle',
|
|
13
|
+
'note',
|
|
14
|
+
'logType',
|
|
15
|
+
'billable',
|
|
16
|
+
'memberId',
|
|
17
|
+
'memberName'
|
|
18
|
+
];
|
|
19
|
+
// A spreadsheet runs a cell that starts with one of these as a formula: a note such as =HYPERLINK(...) typed by a
|
|
20
|
+
// member would run in the sheet that imports the export. Such a text gets a leading ' and stays text.
|
|
21
|
+
const FORMULA_START = /^[=+\-@\t\r]/;
|
|
22
|
+
const NEEDS_QUOTES = /[",\r\n]/;
|
|
23
|
+
function textCell(text) {
|
|
24
|
+
let cell = text;
|
|
25
|
+
if (FORMULA_START.test(cell)) {
|
|
26
|
+
cell = `'${cell}`;
|
|
27
|
+
}
|
|
28
|
+
if (!NEEDS_QUOTES.test(cell)) {
|
|
29
|
+
return cell;
|
|
30
|
+
}
|
|
31
|
+
const escaped = cell.replaceAll('"', '""');
|
|
32
|
+
return `"${escaped}"`;
|
|
33
|
+
}
|
|
34
|
+
function cellOf(value) {
|
|
35
|
+
if (value === null) {
|
|
36
|
+
return '';
|
|
37
|
+
}
|
|
38
|
+
if (typeof value === 'string') {
|
|
39
|
+
return textCell(value);
|
|
40
|
+
}
|
|
41
|
+
return String(value);
|
|
42
|
+
}
|
|
43
|
+
const rowOf = (record) => CSV_COLUMNS.map((column) => cellOf(record[column])).join(',');
|
|
44
|
+
/** The entries as CSV (RFC 4180): a header row, then one row per entry, every line ending with CRLF. */
|
|
45
|
+
export function csvOf(records) {
|
|
46
|
+
const rows = [CSV_COLUMNS.join(','), ...records.map(rowOf)];
|
|
47
|
+
return rows.map((row) => `${row}\r\n`).join('');
|
|
48
|
+
}
|
package/dist/entries.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// A stopped entry lasts what the API counts. A running one counts up to now: the API moves its stop only at each
|
|
2
|
+
// heartbeat of its timer.
|
|
3
|
+
function secondsOf(log, now) {
|
|
4
|
+
if (!log.isRunning) {
|
|
5
|
+
return log.duration;
|
|
6
|
+
}
|
|
7
|
+
const startedAt = Date.parse(log.startedAt);
|
|
8
|
+
const seconds = Math.round((now.getTime() - startedAt) / 1000);
|
|
9
|
+
return Math.max(0, seconds);
|
|
10
|
+
}
|
|
11
|
+
function stoppedAtOf(log) {
|
|
12
|
+
if (log.isRunning) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
return log.stoppedAt;
|
|
16
|
+
}
|
|
17
|
+
/** The record of a time log, with the names read for it. */
|
|
18
|
+
export function recordOfLog(log, names, now) {
|
|
19
|
+
return {
|
|
20
|
+
id: log.id,
|
|
21
|
+
startedAt: log.startedAt,
|
|
22
|
+
stoppedAt: stoppedAtOf(log),
|
|
23
|
+
durationSeconds: secondsOf(log, now),
|
|
24
|
+
running: log.isRunning,
|
|
25
|
+
projectId: log.projectId,
|
|
26
|
+
projectName: names.project?.name ?? null,
|
|
27
|
+
taskId: log.taskId,
|
|
28
|
+
taskNumber: names.task?.taskNumber ?? null,
|
|
29
|
+
taskTitle: names.task?.title ?? null,
|
|
30
|
+
note: log.description || null,
|
|
31
|
+
logType: log.logType,
|
|
32
|
+
billable: log.isBillable,
|
|
33
|
+
memberId: log.employeeId,
|
|
34
|
+
memberName: null
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/** The record of a listed time entry, which carries its project, task and member. */
|
|
38
|
+
export function recordOfEntry(entry, now) {
|
|
39
|
+
const names = { project: entry.project, task: entry.task };
|
|
40
|
+
const record = recordOfLog(entry, names, now);
|
|
41
|
+
record.memberName = entry.employee?.fullName ?? null;
|
|
42
|
+
return record;
|
|
43
|
+
}
|
|
44
|
+
const byStart = (a, b) => a.startedAt.localeCompare(b.startedAt);
|
|
45
|
+
export function inStartOrder(records) {
|
|
46
|
+
return [...records].sort(byStart);
|
|
47
|
+
}
|
|
48
|
+
function taskLabelOf(record) {
|
|
49
|
+
if (record.taskTitle) {
|
|
50
|
+
return `${record.taskNumber} ${record.taskTitle}`;
|
|
51
|
+
}
|
|
52
|
+
if (record.taskId) {
|
|
53
|
+
return `task ${record.taskId}`;
|
|
54
|
+
}
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
function projectLabelOf(record) {
|
|
58
|
+
if (record.projectName) {
|
|
59
|
+
return record.projectName;
|
|
60
|
+
}
|
|
61
|
+
if (record.projectId) {
|
|
62
|
+
return `project ${record.projectId}`;
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
/** The project and the task of the entry, as a person reads them: "Website, WEB-12 Fix the sign-in page". */
|
|
67
|
+
export function labelOf(record) {
|
|
68
|
+
const labels = [projectLabelOf(record), taskLabelOf(record)].filter((label) => label !== null);
|
|
69
|
+
return labels.join(', ');
|
|
70
|
+
}
|
|
71
|
+
/** What the entry was on, for a sentence: " on Website, WEB-12 Fix the sign-in page", or nothing. */
|
|
72
|
+
export function workOf(record) {
|
|
73
|
+
const label = labelOf(record);
|
|
74
|
+
if (!label) {
|
|
75
|
+
return '';
|
|
76
|
+
}
|
|
77
|
+
return ` on ${label}`;
|
|
78
|
+
}
|
package/dist/failures.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { EverTeamsAuthError, EverTeamsConflictError, EverTeamsError, EverTeamsForbiddenError, EverTeamsNetworkError, EverTeamsNotFoundError, EverTeamsRateLimitError, EverTeamsServerError, EverTeamsValidationError } from '@ever-teams/api';
|
|
2
|
+
import { EXIT_FAILED, EXIT_USAGE, Refusal, UsageError } from './refusals.js';
|
|
3
|
+
const ISSUES = 'https://github.com/ever-co/ever-teams/issues';
|
|
4
|
+
const TOKEN_PAGE = 'Settings > API tokens';
|
|
5
|
+
const ADDRESS_STEP = "check that it is the API's address, ending with /api";
|
|
6
|
+
const NOT_A_MEMBER = 'Your account is not a member of an organization that tracks time: ask an administrator of your organization to ' +
|
|
7
|
+
'add you as a member.';
|
|
8
|
+
// The client's codes for a token whose user does not track time in an organization.
|
|
9
|
+
const NOT_A_MEMBER_CODES = new Set(['NO_EMPLOYEE', 'NO_ORGANIZATION', 'NO_TENANT']);
|
|
10
|
+
const SERVER_FAILED = 'The server failed on its side: try again in a moment, and tell its administrator if it keeps failing.';
|
|
11
|
+
function helpHint(command) {
|
|
12
|
+
if (command) {
|
|
13
|
+
return `Run ever-teams ${command} --help to see how to use it.`;
|
|
14
|
+
}
|
|
15
|
+
return 'Run ever-teams --help to see the commands.';
|
|
16
|
+
}
|
|
17
|
+
function messageOf(error) {
|
|
18
|
+
if (error instanceof Error) {
|
|
19
|
+
return error.message;
|
|
20
|
+
}
|
|
21
|
+
return String(error);
|
|
22
|
+
}
|
|
23
|
+
const addressOf = (trail) => trail.apiUrl ?? 'The API';
|
|
24
|
+
// The API's own words: a validation answers one message per field.
|
|
25
|
+
function apiWords(error) {
|
|
26
|
+
if (error instanceof EverTeamsValidationError && error.messages.length > 0) {
|
|
27
|
+
return error.messages.join('; ');
|
|
28
|
+
}
|
|
29
|
+
return error.message;
|
|
30
|
+
}
|
|
31
|
+
function tokenRefused(trail) {
|
|
32
|
+
const code = 'TOKEN_REFUSED';
|
|
33
|
+
if (trail.tokenFrom === 'environment') {
|
|
34
|
+
const message = `The token in EVER_TEAMS_TOKEN was refused: create a new one in ${TOKEN_PAGE}, then put it in ` +
|
|
35
|
+
'EVER_TEAMS_TOKEN.';
|
|
36
|
+
return { code, message };
|
|
37
|
+
}
|
|
38
|
+
if (trail.tokenFrom === 'typed') {
|
|
39
|
+
return {
|
|
40
|
+
code,
|
|
41
|
+
message: `This token was refused: create a new one in ${TOKEN_PAGE}, then run ever-teams login.`
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
return { code, message: `Your token was refused: create a new one in ${TOKEN_PAGE}, then run ever-teams login.` };
|
|
45
|
+
}
|
|
46
|
+
// The token exchange answers 404 when the server keeps personal API tokens off, as it does by default.
|
|
47
|
+
function notFound(trail) {
|
|
48
|
+
const address = addressOf(trail);
|
|
49
|
+
if (trail.exchangeStatus === 404) {
|
|
50
|
+
const message = `${address} does not accept personal API tokens: its administrator turns them on with ` +
|
|
51
|
+
`GAUZY_USER_API_TOKENS_ENABLED (or ${ADDRESS_STEP}).`;
|
|
52
|
+
return { code: 'PERSONAL_TOKENS_OFF', message };
|
|
53
|
+
}
|
|
54
|
+
return { code: 'NOT_FOUND', message: `${address} has nothing at this route: ${ADDRESS_STEP}.` };
|
|
55
|
+
}
|
|
56
|
+
function forbidden(error, trail) {
|
|
57
|
+
const code = error.code ?? 'FORBIDDEN';
|
|
58
|
+
const isEmbedToken = error.code?.startsWith('EMBED_') === true;
|
|
59
|
+
if (isEmbedToken) {
|
|
60
|
+
return { code, message: `This token reaches the timer only: use a personal API token, from ${TOKEN_PAGE}.` };
|
|
61
|
+
}
|
|
62
|
+
if (trail.command === 'log') {
|
|
63
|
+
const message = 'Your organization does not allow adding time by hand, or your role may not: ask an administrator of ' +
|
|
64
|
+
'your organization to allow manual time, or use ever-teams start and stop.';
|
|
65
|
+
return { code: 'MANUAL_TIME_REFUSED', message };
|
|
66
|
+
}
|
|
67
|
+
const message = 'Your account may not do this: ask an administrator of your organization for the permission.';
|
|
68
|
+
return { code, message };
|
|
69
|
+
}
|
|
70
|
+
function conflict(error) {
|
|
71
|
+
const code = error.code ?? 'CONFLICT';
|
|
72
|
+
if (code === 'RUNNING_TIMER_OVERLAP') {
|
|
73
|
+
const message = 'Your timer is running over this time: stop it with ever-teams stop, or pick another time.';
|
|
74
|
+
return { code, message };
|
|
75
|
+
}
|
|
76
|
+
if (code === 'BREAK_OVERLAP') {
|
|
77
|
+
return { code, message: 'This time overlaps a break: pick a time outside it.' };
|
|
78
|
+
}
|
|
79
|
+
const words = apiWords(error);
|
|
80
|
+
const message = `The server refused this in the current state (${words}): run ever-teams status, then try again.`;
|
|
81
|
+
return { code, message };
|
|
82
|
+
}
|
|
83
|
+
function refusedValues(error, trail) {
|
|
84
|
+
const code = error.code ?? 'REFUSED_VALUES';
|
|
85
|
+
const words = apiWords(error);
|
|
86
|
+
if (trail.command === 'log') {
|
|
87
|
+
const message = `The server refused this entry (${words}): check the times; an entry in the future needs your ` +
|
|
88
|
+
'organization to allow future dates.';
|
|
89
|
+
return { code, message };
|
|
90
|
+
}
|
|
91
|
+
return { code, message: `The server refused the values sent (${words}): check them, then try again.` };
|
|
92
|
+
}
|
|
93
|
+
function tooMany(error) {
|
|
94
|
+
let wait = 'a minute';
|
|
95
|
+
if (error.retryAfter !== undefined) {
|
|
96
|
+
wait = `${error.retryAfter} seconds`;
|
|
97
|
+
}
|
|
98
|
+
return { code: 'RATE_LIMITED', message: `Too many requests: wait ${wait}, then try again.` };
|
|
99
|
+
}
|
|
100
|
+
// The client's own codes, for a request it did not send or an answer it could not read.
|
|
101
|
+
function clientExplained(error, trail) {
|
|
102
|
+
const address = addressOf(trail);
|
|
103
|
+
const code = error.code;
|
|
104
|
+
if (code === 'NOT_JSON') {
|
|
105
|
+
return { code, message: `${address} did not answer as the API does: ${ADDRESS_STEP}.` };
|
|
106
|
+
}
|
|
107
|
+
if (code === 'UNEXPECTED_ANSWER') {
|
|
108
|
+
return { code, message: `${address} answered without your account: ${ADDRESS_STEP}.` };
|
|
109
|
+
}
|
|
110
|
+
if (code !== undefined && NOT_A_MEMBER_CODES.has(code)) {
|
|
111
|
+
return { code, message: NOT_A_MEMBER };
|
|
112
|
+
}
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
function explained(error, trail) {
|
|
116
|
+
const own = clientExplained(error, trail);
|
|
117
|
+
if (own) {
|
|
118
|
+
return own;
|
|
119
|
+
}
|
|
120
|
+
if (error instanceof EverTeamsAuthError) {
|
|
121
|
+
return tokenRefused(trail);
|
|
122
|
+
}
|
|
123
|
+
if (error instanceof EverTeamsNotFoundError) {
|
|
124
|
+
return notFound(trail);
|
|
125
|
+
}
|
|
126
|
+
if (error instanceof EverTeamsForbiddenError) {
|
|
127
|
+
return forbidden(error, trail);
|
|
128
|
+
}
|
|
129
|
+
if (error instanceof EverTeamsConflictError) {
|
|
130
|
+
return conflict(error);
|
|
131
|
+
}
|
|
132
|
+
if (error instanceof EverTeamsValidationError) {
|
|
133
|
+
return refusedValues(error, trail);
|
|
134
|
+
}
|
|
135
|
+
if (error instanceof EverTeamsRateLimitError) {
|
|
136
|
+
return tooMany(error);
|
|
137
|
+
}
|
|
138
|
+
if (error instanceof EverTeamsServerError) {
|
|
139
|
+
return { code: 'SERVER_FAILED', message: SERVER_FAILED };
|
|
140
|
+
}
|
|
141
|
+
if (error instanceof EverTeamsNetworkError) {
|
|
142
|
+
const message = `No answer from ${addressOf(trail)}: check your network and the address.`;
|
|
143
|
+
return { code: 'NO_ANSWER', message };
|
|
144
|
+
}
|
|
145
|
+
const message = `The server refused this (${error.message}): run ever-teams status, then try again.`;
|
|
146
|
+
return { code: error.code ?? 'REFUSED', message };
|
|
147
|
+
}
|
|
148
|
+
function apiFailure(error, trail) {
|
|
149
|
+
const { code, message } = explained(error, trail);
|
|
150
|
+
const failure = { code, message, exitCode: EXIT_FAILED };
|
|
151
|
+
if (error.status > 0) {
|
|
152
|
+
failure.status = error.status;
|
|
153
|
+
}
|
|
154
|
+
return failure;
|
|
155
|
+
}
|
|
156
|
+
/** The failure to report for what a command threw: one sentence with what to do next, and the exit code. */
|
|
157
|
+
export function failureOf(error, trail) {
|
|
158
|
+
if (error instanceof UsageError) {
|
|
159
|
+
return { code: 'USAGE', message: `${error.message} ${helpHint(trail.command)}`, exitCode: EXIT_USAGE };
|
|
160
|
+
}
|
|
161
|
+
if (error instanceof Refusal) {
|
|
162
|
+
return { code: error.code, message: error.message, exitCode: EXIT_FAILED };
|
|
163
|
+
}
|
|
164
|
+
if (error instanceof EverTeamsError) {
|
|
165
|
+
return apiFailure(error, trail);
|
|
166
|
+
}
|
|
167
|
+
const reason = messageOf(error);
|
|
168
|
+
const message = `Something went wrong (${reason}): try again, and report it at ${ISSUES} if it keeps happening.`;
|
|
169
|
+
return { code: 'UNEXPECTED', message, exitCode: EXIT_FAILED };
|
|
170
|
+
}
|
package/dist/format.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const pad = (value) => String(value).padStart(2, '0');
|
|
2
|
+
/** HH:MM, in the time zone the command line runs in. */
|
|
3
|
+
export function clockOf(date) {
|
|
4
|
+
const hours = pad(date.getHours());
|
|
5
|
+
const minutes = pad(date.getMinutes());
|
|
6
|
+
return `${hours}:${minutes}`;
|
|
7
|
+
}
|
|
8
|
+
/** YYYY-MM-DD, the day in the time zone the command line runs in. */
|
|
9
|
+
export function dayOf(date) {
|
|
10
|
+
const month = pad(date.getMonth() + 1);
|
|
11
|
+
const day = pad(date.getDate());
|
|
12
|
+
return `${date.getFullYear()}-${month}-${day}`;
|
|
13
|
+
}
|
|
14
|
+
/** 1h 05m, or 12m under an hour. */
|
|
15
|
+
export function durationText(seconds) {
|
|
16
|
+
const minutes = Math.floor(seconds / 60);
|
|
17
|
+
const hours = Math.floor(minutes / 60);
|
|
18
|
+
const restOfHour = minutes % 60;
|
|
19
|
+
if (hours === 0) {
|
|
20
|
+
return `${restOfHour}m`;
|
|
21
|
+
}
|
|
22
|
+
return `${hours}h ${pad(restOfHour)}m`;
|
|
23
|
+
}
|
package/dist/help.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
const COMMON_HELP = [
|
|
2
|
+
['--json', 'Answer in JSON (a failure too, on the error output)'],
|
|
3
|
+
['--api-url <url>', "The API's address, ending with /api (or EVER_TEAMS_API_URL)"],
|
|
4
|
+
['-h, --help', 'Show this help']
|
|
5
|
+
];
|
|
6
|
+
const ENVIRONMENT_HELP = [
|
|
7
|
+
['EVER_TEAMS_TOKEN', 'A personal API token, used in place of the one login stored'],
|
|
8
|
+
['EVER_TEAMS_API_URL', "The API's address, used in place of the one login stored"]
|
|
9
|
+
];
|
|
10
|
+
function columns(rows) {
|
|
11
|
+
const width = Math.max(0, ...rows.map(([left]) => left.length));
|
|
12
|
+
return rows.map(([left, right]) => ` ${left.padEnd(width)} ${right}`);
|
|
13
|
+
}
|
|
14
|
+
const textOf = (lines) => `${lines.join('\n')}\n`;
|
|
15
|
+
export function generalHelp(commands) {
|
|
16
|
+
const commandRows = commands.map((command) => [command.name, command.summary]);
|
|
17
|
+
return textOf([
|
|
18
|
+
'Usage: ever-teams <command> [options]',
|
|
19
|
+
'',
|
|
20
|
+
'Track your own time in Ever Teams from a terminal or a script.',
|
|
21
|
+
'',
|
|
22
|
+
'Commands:',
|
|
23
|
+
...columns(commandRows),
|
|
24
|
+
'',
|
|
25
|
+
'Options of every command:',
|
|
26
|
+
...columns(COMMON_HELP),
|
|
27
|
+
'',
|
|
28
|
+
'Environment:',
|
|
29
|
+
...columns(ENVIRONMENT_HELP),
|
|
30
|
+
'',
|
|
31
|
+
'Exit codes: 0 done, 1 refused or failed, 2 usage error.',
|
|
32
|
+
'Run ever-teams <command> --help for the options of a command, ever-teams --version for the version.'
|
|
33
|
+
]);
|
|
34
|
+
}
|
|
35
|
+
function examplesOf(command) {
|
|
36
|
+
if (command.examples.length === 0) {
|
|
37
|
+
return [];
|
|
38
|
+
}
|
|
39
|
+
const examples = command.examples.map((example) => ` ${example}`);
|
|
40
|
+
return ['', 'Examples:', ...examples];
|
|
41
|
+
}
|
|
42
|
+
export function commandHelp(command) {
|
|
43
|
+
return textOf([
|
|
44
|
+
`Usage: ever-teams ${command.usage}`,
|
|
45
|
+
'',
|
|
46
|
+
command.description,
|
|
47
|
+
'',
|
|
48
|
+
'Options:',
|
|
49
|
+
...columns([...command.optionHelp, ...COMMON_HELP]),
|
|
50
|
+
...examplesOf(command)
|
|
51
|
+
]);
|
|
52
|
+
}
|
package/dist/index.js
ADDED
package/dist/io.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import os from 'node:os';
|
|
2
|
+
export function processIo() {
|
|
3
|
+
return {
|
|
4
|
+
argv: process.argv.slice(2),
|
|
5
|
+
env: process.env,
|
|
6
|
+
platform: process.platform,
|
|
7
|
+
homedir: os.homedir(),
|
|
8
|
+
stdin: process.stdin,
|
|
9
|
+
writeOut: (text) => process.stdout.write(text),
|
|
10
|
+
writeErr: (text) => process.stderr.write(text),
|
|
11
|
+
fetch: (input, init) => globalThis.fetch(input, init),
|
|
12
|
+
now: () => new Date()
|
|
13
|
+
};
|
|
14
|
+
}
|