@infaris/calendar-cli 0.9.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/README.md +426 -0
- package/dist/activity.js +165 -0
- package/dist/api.js +409 -0
- package/dist/auth.js +378 -0
- package/dist/browser.js +24 -0
- package/dist/config.js +196 -0
- package/dist/crm-api.js +37 -0
- package/dist/crm-index.js +198 -0
- package/dist/dates.js +164 -0
- package/dist/event-input.js +39 -0
- package/dist/freebusy.js +92 -0
- package/dist/hours-cli.js +335 -0
- package/dist/hours.js +276 -0
- package/dist/index.js +1047 -0
- package/dist/legacy.js +43 -0
- package/dist/selectors.js +49 -0
- package/dist/task-download.js +56 -0
- package/package.json +38 -0
package/dist/legacy.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compatibility with the names used before the CLI was renamed to
|
|
3
|
+
* `infaris-calendar` / `infaris-crm`. New names win; the old ones keep
|
|
4
|
+
* existing scripts and stored logins working.
|
|
5
|
+
*/
|
|
6
|
+
import { lstatSync, mkdirSync, renameSync, existsSync, symlinkSync } from "node:fs";
|
|
7
|
+
import { dirname } from "node:path";
|
|
8
|
+
const LEGACY = "PULSE";
|
|
9
|
+
/** Read `INFARIS_<name>` (e.g. `CALENDAR_URL`), falling back to the pre-rename variable. */
|
|
10
|
+
export function infarisEnv(name) {
|
|
11
|
+
return (process.env[`INFARIS_${name}`] ||
|
|
12
|
+
process.env[`${LEGACY}${name}`] ||
|
|
13
|
+
undefined);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Move a pre-rename config directory to its new location once and leave a
|
|
17
|
+
* symlink, so an older CLI still finds the same login. Moving (not copying)
|
|
18
|
+
* matters: two copies of a rotating refresh token would revoke each other.
|
|
19
|
+
*/
|
|
20
|
+
export function migrateLegacyDir(target, legacy) {
|
|
21
|
+
if (existsSync(target))
|
|
22
|
+
return;
|
|
23
|
+
try {
|
|
24
|
+
if (lstatSync(legacy).isSymbolicLink())
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
mkdirSync(dirname(target), { recursive: true, mode: 0o700 });
|
|
32
|
+
renameSync(legacy, target);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return; // another process migrated it first
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
symlinkSync(target, legacy, "dir");
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
/* best effort */
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
function normalize(value) {
|
|
2
|
+
return value.trim().toLocaleLowerCase();
|
|
3
|
+
}
|
|
4
|
+
function requestedValues(values) {
|
|
5
|
+
return values
|
|
6
|
+
.flatMap((value) => value.split(","))
|
|
7
|
+
.map((value) => value.trim())
|
|
8
|
+
.filter(Boolean);
|
|
9
|
+
}
|
|
10
|
+
export function resolveAttendeeIds(selectors, members) {
|
|
11
|
+
const resolved = [];
|
|
12
|
+
for (const selector of requestedValues(selectors)) {
|
|
13
|
+
const needle = normalize(selector);
|
|
14
|
+
const matches = members.filter((member) => {
|
|
15
|
+
const name = normalize(member.name ?? "");
|
|
16
|
+
const email = normalize(member.email ?? "");
|
|
17
|
+
const firstName = name.split(/\s+/)[0];
|
|
18
|
+
return member.id === selector || email === needle || name === needle || firstName === needle;
|
|
19
|
+
});
|
|
20
|
+
if (matches.length === 0) {
|
|
21
|
+
const available = members
|
|
22
|
+
.map((member) => member.name || member.email)
|
|
23
|
+
.filter(Boolean)
|
|
24
|
+
.join(", ");
|
|
25
|
+
throw new Error(`Unknown attendee "${selector}".${available ? ` Available: ${available}.` : ""}`);
|
|
26
|
+
}
|
|
27
|
+
if (matches.length > 1) {
|
|
28
|
+
const choices = matches
|
|
29
|
+
.map((member) => `${member.name || "Unknown"} <${member.email || member.id}>`)
|
|
30
|
+
.join(", ");
|
|
31
|
+
throw new Error(`Attendee "${selector}" is ambiguous. Use an email: ${choices}.`);
|
|
32
|
+
}
|
|
33
|
+
resolved.push(matches[0].id);
|
|
34
|
+
}
|
|
35
|
+
return [...new Set(resolved)];
|
|
36
|
+
}
|
|
37
|
+
export function resolveWritableCalendarId(selector, accounts) {
|
|
38
|
+
const writable = accounts.flatMap((account) => account.calendars.filter((calendar) => !calendar.isReadOnly));
|
|
39
|
+
const needle = normalize(selector);
|
|
40
|
+
const matches = writable.filter((calendar) => calendar.id === selector || normalize(calendar.name) === needle);
|
|
41
|
+
if (matches.length === 0) {
|
|
42
|
+
const available = writable.map((calendar) => calendar.name).join(", ");
|
|
43
|
+
throw new Error(`Unknown writable calendar "${selector}".${available ? ` Available: ${available}.` : ""}`);
|
|
44
|
+
}
|
|
45
|
+
if (matches.length > 1) {
|
|
46
|
+
throw new Error(`Calendar name "${selector}" is ambiguous. Use its id.`);
|
|
47
|
+
}
|
|
48
|
+
return matches[0].id;
|
|
49
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { authedFetch, MAX_ATTACHMENT_BYTES, } from "./api.js";
|
|
4
|
+
/** Download through the private ticket endpoint, never send tokens to an attachment URL. */
|
|
5
|
+
export async function downloadTaskAttachments(taskId, attachments, directory) {
|
|
6
|
+
mkdirSync(directory, { recursive: true });
|
|
7
|
+
const files = [];
|
|
8
|
+
for (const attachment of attachments) {
|
|
9
|
+
let { res, baseUrl } = await authedFetch(`/api/work/tasks/${encodeURIComponent(taskId)}/attachments/${encodeURIComponent(attachment.id)}`, { method: "GET", redirect: "manual" });
|
|
10
|
+
if (res.status === 307) {
|
|
11
|
+
const location = res.headers.get("location");
|
|
12
|
+
if (!location)
|
|
13
|
+
throw new Error("Missing attachment redirect");
|
|
14
|
+
const target = new URL(location, baseUrl);
|
|
15
|
+
if (target.origin !== new URL(baseUrl).origin ||
|
|
16
|
+
!target.pathname.startsWith("/api/public/tasks/"))
|
|
17
|
+
throw new Error("Unexpected attachment redirect");
|
|
18
|
+
res = await fetch(target, {
|
|
19
|
+
redirect: "error",
|
|
20
|
+
signal: AbortSignal.timeout(30_000),
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
if (!res.ok)
|
|
24
|
+
throw new Error(`Attachment ${attachment.id} download failed (${res.status})`);
|
|
25
|
+
// The id prefix avoids duplicate names; strip separators and control characters from server filenames.
|
|
26
|
+
const name = attachment.fileName.replace(/[^a-zA-Z0-9._-]/g, "_").slice(-150) ||
|
|
27
|
+
"attachment";
|
|
28
|
+
const id = attachment.id.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
29
|
+
const path = join(directory, `${id}-${name}`);
|
|
30
|
+
if (Number(res.headers.get("content-length")) > MAX_ATTACHMENT_BYTES)
|
|
31
|
+
throw new Error(`Attachment ${attachment.id} exceeds 10 MB`);
|
|
32
|
+
if (!res.body)
|
|
33
|
+
throw new Error("Empty attachment response");
|
|
34
|
+
const reader = res.body.getReader();
|
|
35
|
+
const chunks = [];
|
|
36
|
+
let size = 0;
|
|
37
|
+
try {
|
|
38
|
+
while (true) {
|
|
39
|
+
const { done, value } = await reader.read();
|
|
40
|
+
if (done)
|
|
41
|
+
break;
|
|
42
|
+
size += value.byteLength;
|
|
43
|
+
if (size > MAX_ATTACHMENT_BYTES)
|
|
44
|
+
throw new Error(`Attachment ${attachment.id} exceeds 10 MB`);
|
|
45
|
+
chunks.push(value);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
finally {
|
|
49
|
+
await reader.cancel();
|
|
50
|
+
}
|
|
51
|
+
// Never overwrite earlier evidence, including symlinks.
|
|
52
|
+
writeFileSync(path, Buffer.concat(chunks), { flag: "wx", mode: 0o600 });
|
|
53
|
+
files.push({ id: attachment.id, path });
|
|
54
|
+
}
|
|
55
|
+
return files;
|
|
56
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@infaris/calendar-cli",
|
|
3
|
+
"version": "0.9.0",
|
|
4
|
+
"description": "Command line interface for Calendar by Infaris: log in once on your device, read your agenda, manage work tasks, and fill the urenstaat from tasks, commits and PRs.",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": {
|
|
8
|
+
"infaris-calendar": "dist/index.js",
|
|
9
|
+
"infaris-crm": "dist/crm-index.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"README.md"
|
|
14
|
+
],
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=18"
|
|
17
|
+
},
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsc -p tsconfig.json",
|
|
23
|
+
"dev": "tsx src/index.ts",
|
|
24
|
+
"lint": "echo \"TODO: geen eslint in calendar-cli\" && exit 0",
|
|
25
|
+
"typecheck": "tsgo -p tsconfig.json --noEmit",
|
|
26
|
+
"typecheck:legacy": "tsc -p tsconfig.json --noEmit",
|
|
27
|
+
"test": "node --import tsx --test tests/*.test.ts",
|
|
28
|
+
"prepublishOnly": "npm run build"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"commander": "^12.1.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/node": "^22.0.0",
|
|
35
|
+
"tsx": "^4.20.5",
|
|
36
|
+
"typescript": "^5.6.0"
|
|
37
|
+
}
|
|
38
|
+
}
|