@apet97/clockify-cli-115 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/dist/index.js ADDED
@@ -0,0 +1,171 @@
1
+ #!/usr/bin/env node
2
+ import { Command } from "commander";
3
+ import { buildClient } from "./client.js";
4
+ import { registerApiCommand } from "./commands/api.js";
5
+ import { registerAuditLogCommand } from "./commands/auditlog.js";
6
+ import { registerClientsCommand } from "./commands/clients.js";
7
+ import { registerDoctorCommand } from "./commands/doctor.js";
8
+ import { registerEntriesCommand } from "./commands/entries.js";
9
+ import { registerExpensesCommand } from "./commands/expenses.js";
10
+ import { registerInvoicesCommand } from "./commands/invoices.js";
11
+ import { registerLogCommand } from "./commands/log.js";
12
+ import { registerProjectsCommand } from "./commands/projects.js";
13
+ import { registerReportsCommand } from "./commands/reports.js";
14
+ import { registerSchedulingCommand } from "./commands/scheduling.js";
15
+ import { registerSharedReportsCommand } from "./commands/sharedReports.js";
16
+ import { registerStartCommand } from "./commands/start.js";
17
+ import { registerStatusCommand } from "./commands/status.js";
18
+ import { registerStopCommand } from "./commands/stop.js";
19
+ import { registerTagsCommand } from "./commands/tags.js";
20
+ import { registerTasksCommand } from "./commands/tasks.js";
21
+ import { registerTimeOffCommand } from "./commands/timeoff.js";
22
+ import { registerUsersCommand } from "./commands/users.js";
23
+ import { registerWebhooksCommand } from "./commands/webhooks.js";
24
+ import { parseCompletionShell, renderCompletion } from "./completions.js";
25
+ import { loadConfig } from "./config.js";
26
+ import { printError } from "./output.js";
27
+ export function buildProgram() {
28
+ const program = new Command();
29
+ program
30
+ .name("clockify115")
31
+ .description("Clockify CLI from @apet97/clockify-cli-115, built on clockify-sdk-ts-115.")
32
+ .version("0.1.0")
33
+ .option("--api-key <key>", "Clockify personal API key (or CLOCKIFY_API_KEY env var).")
34
+ .option("--workspace <id>", "Clockify workspace ID (or CLOCKIFY_WORKSPACE_ID env var).")
35
+ .option("--base-url <url>", "Override Clockify API base URL (or CLOCKIFY_BASE_URL env var). Only a Clockify host or a loopback host is accepted; arbitrary hosts are rejected.")
36
+ .option("--json", "Emit machine-readable JSON instead of human-friendly tables.", false)
37
+ .option("--output <mode>", "Output mode: table, json, or ndjson.")
38
+ .option("--compact", "Print compact JSON without indentation.", false)
39
+ .option("--select <path>", "Select a dot-path before printing JSON or NDJSON.")
40
+ .option("--no-color", "Disable ANSI color output.")
41
+ .showHelpAfterError(true);
42
+ const services = {
43
+ loadConfig,
44
+ buildClient,
45
+ };
46
+ registerApiCommand(program, services);
47
+ registerStatusCommand(program, services);
48
+ registerDoctorCommand(program, services);
49
+ registerStartCommand(program, services);
50
+ registerStopCommand(program, services);
51
+ registerLogCommand(program, services);
52
+ registerEntriesCommand(program, services);
53
+ registerProjectsCommand(program, services);
54
+ registerClientsCommand(program, services);
55
+ registerTasksCommand(program, services);
56
+ registerTagsCommand(program, services);
57
+ registerWebhooksCommand(program, services);
58
+ registerInvoicesCommand(program, services);
59
+ registerExpensesCommand(program, services);
60
+ registerTimeOffCommand(program, services);
61
+ registerSchedulingCommand(program, services);
62
+ registerAuditLogCommand(program, services);
63
+ registerReportsCommand(program, services);
64
+ registerSharedReportsCommand(program, services);
65
+ registerUsersCommand(program, services);
66
+ program
67
+ .command("completion")
68
+ .argument("[shell]", "Shell to generate completion for: zsh, bash, or fish.", "zsh")
69
+ .description("Print shell completion script for zsh, bash, or fish.")
70
+ .action((shell) => {
71
+ console.log(renderCompletion(parseCompletionShell(shell)));
72
+ });
73
+ return program;
74
+ }
75
+ export function resolveFlags(program) {
76
+ const opts = program.opts();
77
+ const mode = resolveMode(opts.output, opts.json);
78
+ const resolved = {
79
+ mode,
80
+ color: opts.color !== false && process.stdout.isTTY === true,
81
+ };
82
+ if (opts.compact)
83
+ resolved.compact = true;
84
+ if (opts.select !== undefined)
85
+ resolved.select = opts.select;
86
+ return resolved;
87
+ }
88
+ function resolveFlagsSafe(program) {
89
+ try {
90
+ return resolveFlags(program);
91
+ }
92
+ catch {
93
+ const opts = program.opts();
94
+ return {
95
+ mode: "table",
96
+ color: opts.color !== false && process.stdout.isTTY === true,
97
+ };
98
+ }
99
+ }
100
+ function resolveMode(output, json) {
101
+ if (output === undefined) {
102
+ return json ? "json" : "table";
103
+ }
104
+ const modes = ["table", "json", "ndjson"];
105
+ const match = modes.find((mode) => mode === output);
106
+ if (!match) {
107
+ throw new Error(`Unsupported output mode "${output}". Use table, json, or ndjson.`);
108
+ }
109
+ return match;
110
+ }
111
+ export function globalFlags(program) {
112
+ const opts = program.opts();
113
+ const out = {};
114
+ if (opts.apiKey)
115
+ out.apiKey = opts.apiKey;
116
+ if (opts.workspace)
117
+ out.workspace = opts.workspace;
118
+ if (opts.baseUrl)
119
+ out.baseUrl = opts.baseUrl;
120
+ return out;
121
+ }
122
+ export async function main(argv) {
123
+ const program = buildProgram();
124
+ const applyExitOverride = (cmd) => {
125
+ cmd.exitOverride();
126
+ for (const sub of cmd.commands)
127
+ applyExitOverride(sub);
128
+ };
129
+ applyExitOverride(program);
130
+ try {
131
+ await program.parseAsync(argv);
132
+ return 0;
133
+ }
134
+ catch (err) {
135
+ if (isCommanderHelpError(err)) {
136
+ return Number(err.exitCode ?? 0);
137
+ }
138
+ const message = err instanceof Error ? err.message : String(err);
139
+ const statusCode = err.statusCode;
140
+ const flags = resolveFlagsSafe(program);
141
+ printError(message, { mode: flags.mode, color: flags.color }, statusCode);
142
+ return isCommanderUsageError(err) ? 2 : 1;
143
+ }
144
+ }
145
+ function isCommanderHelpError(err) {
146
+ return (typeof err === "object" &&
147
+ err !== null &&
148
+ "code" in err &&
149
+ typeof err.code === "string" &&
150
+ (err.code === "commander.helpDisplayed" ||
151
+ err.code === "commander.help" ||
152
+ err.code === "commander.version"));
153
+ }
154
+ function isCommanderUsageError(err) {
155
+ return (typeof err === "object" &&
156
+ err !== null &&
157
+ "code" in err &&
158
+ typeof err.code === "string" &&
159
+ err.code.startsWith("commander."));
160
+ }
161
+ const invokedDirectly = typeof process !== "undefined" &&
162
+ Array.isArray(process.argv) &&
163
+ process.argv[1] !== undefined &&
164
+ /(?:^|\/)(?:clockify115|clk115|index\.[jt]s)$/.test(process.argv[1]);
165
+ if (invokedDirectly) {
166
+ main(process.argv).then((code) => process.exit(code), (err) => {
167
+ const message = err instanceof Error ? err.message : String(err);
168
+ console.error(`fatal: ${message}`);
169
+ process.exit(1);
170
+ });
171
+ }
package/dist/output.js ADDED
@@ -0,0 +1,144 @@
1
+ import Table from "cli-table3";
2
+ import pc from "picocolors";
3
+ import { errorCodeForMessage, errorCodeForStatus, recoveryForCode, retryableForCode, } from "./error-codes.js";
4
+ export function printRecords(rows, opts) {
5
+ if (opts.mode === "json") {
6
+ printJson(rows, opts);
7
+ return;
8
+ }
9
+ if (opts.mode === "ndjson") {
10
+ printNdjson(rows, opts);
11
+ return;
12
+ }
13
+ if (rows.length === 0) {
14
+ console.log(opts.color ? pc.dim("(no rows)") : "(no rows)");
15
+ return;
16
+ }
17
+ const headers = collectHeaders(rows);
18
+ const table = new Table({
19
+ head: opts.color ? headers.map((h) => pc.bold(h)) : headers,
20
+ style: { head: [], border: [] },
21
+ });
22
+ for (const row of rows) {
23
+ table.push(headers.map((h) => stringifyCell(row[h])));
24
+ }
25
+ console.log(table.toString());
26
+ }
27
+ export function printObject(obj, opts) {
28
+ if (opts.mode === "json") {
29
+ printJson(obj, opts);
30
+ return;
31
+ }
32
+ if (opts.mode === "ndjson") {
33
+ printNdjson(obj, opts);
34
+ return;
35
+ }
36
+ const table = new Table({
37
+ head: opts.color ? [pc.bold("field"), pc.bold("value")] : ["field", "value"],
38
+ style: { head: [], border: [] },
39
+ });
40
+ for (const [key, value] of Object.entries(obj)) {
41
+ table.push([key, stringifyCell(value)]);
42
+ }
43
+ console.log(table.toString());
44
+ }
45
+ export function printSuccess(message, opts) {
46
+ if (opts.mode === "ndjson") {
47
+ printNdjson({ ok: true, message }, opts);
48
+ return;
49
+ }
50
+ if (opts.mode === "json") {
51
+ printJson({ ok: true, message }, opts);
52
+ return;
53
+ }
54
+ const prefix = opts.color ? pc.green("OK") : "OK";
55
+ console.log(`${prefix} ${message}`);
56
+ }
57
+ export function printError(message, opts, statusCode) {
58
+ const messageCode = errorCodeForMessage(message);
59
+ const code = messageCode === "not_found" && statusCode === 400
60
+ ? "not_found"
61
+ : (errorCodeForStatus(statusCode) ?? messageCode);
62
+ if (opts.mode !== "table") {
63
+ console.error(JSON.stringify({
64
+ ok: false,
65
+ error: message,
66
+ code,
67
+ recovery: recoveryForCode(code),
68
+ retryable: retryableForCode(code),
69
+ }));
70
+ return;
71
+ }
72
+ const prefix = opts.color ? pc.red("ERR") : "ERR";
73
+ console.error(`${prefix} ${message}`);
74
+ const recovery = recoveryForCode(code);
75
+ if (recovery) {
76
+ console.error(`${opts.color ? pc.dim("→") : "→"} ${recovery}`);
77
+ }
78
+ }
79
+ export function selectValue(value, selector) {
80
+ if (!selector) {
81
+ return value;
82
+ }
83
+ let current = value;
84
+ for (const part of selector.split(".").filter(Boolean)) {
85
+ if (Array.isArray(current)) {
86
+ const index = Number(part);
87
+ if (!Number.isInteger(index) || index < 0 || index >= current.length) {
88
+ return undefined;
89
+ }
90
+ current = current[index];
91
+ continue;
92
+ }
93
+ if (current !== null && typeof current === "object" && part in current) {
94
+ current = current[part];
95
+ continue;
96
+ }
97
+ return undefined;
98
+ }
99
+ return current;
100
+ }
101
+ export function printJson(value, options = {}) {
102
+ const selected = selectValue(value, options.select);
103
+ console.log(JSON.stringify(selected, null, options.compact ? 0 : 2));
104
+ }
105
+ export function printNdjson(value, options = {}) {
106
+ const selected = selectValue(value, options.select);
107
+ if (Array.isArray(selected)) {
108
+ for (const item of selected) {
109
+ console.log(JSON.stringify(item));
110
+ }
111
+ return;
112
+ }
113
+ console.log(JSON.stringify(selected));
114
+ }
115
+ function collectHeaders(rows) {
116
+ const seen = new Set();
117
+ const ordered = [];
118
+ for (const row of rows) {
119
+ for (const key of Object.keys(row)) {
120
+ if (!seen.has(key)) {
121
+ seen.add(key);
122
+ ordered.push(key);
123
+ }
124
+ }
125
+ }
126
+ return ordered;
127
+ }
128
+ function stringifyCell(value) {
129
+ if (value === null || value === undefined) {
130
+ return "";
131
+ }
132
+ if (typeof value === "string") {
133
+ return value;
134
+ }
135
+ if (typeof value === "number" || typeof value === "boolean") {
136
+ return String(value);
137
+ }
138
+ try {
139
+ return JSON.stringify(value);
140
+ }
141
+ catch {
142
+ return typeof value === "bigint" ? value.toString() : "[unserializable]";
143
+ }
144
+ }
@@ -0,0 +1,22 @@
1
+ import { printJson, printNdjson, printObject, } from "./output.js";
2
+ export function printReceipt(receipt, output) {
3
+ const payload = {
4
+ ...receipt.data,
5
+ ok: receipt.ok,
6
+ action: receipt.action,
7
+ entity: receipt.entity,
8
+ ids: receipt.ids,
9
+ changed: receipt.changed ?? {},
10
+ warnings: receipt.warnings ?? [],
11
+ next: receipt.next ?? [],
12
+ };
13
+ if (output.mode === "json") {
14
+ printJson(payload, output);
15
+ return;
16
+ }
17
+ if (output.mode === "ndjson") {
18
+ printNdjson(payload, output);
19
+ return;
20
+ }
21
+ printObject(receipt.data, output);
22
+ }
package/package.json ADDED
@@ -0,0 +1,72 @@
1
+ {
2
+ "name": "@apet97/clockify-cli-115",
3
+ "version": "0.1.0",
4
+ "description": "Unofficial, community-built — not affiliated with CAKE.com or Clockify. Command-line interface for Clockify, built on clockify-sdk-ts-115.",
5
+ "license": "MIT",
6
+ "author": "Aleksandar Petković <petkovic.aleksandar037@gmail.com>",
7
+ "homepage": "https://github.com/apet97/clockify-ts-sdk/tree/main/cli",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/apet97/clockify-ts-sdk.git",
11
+ "directory": "cli"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/apet97/clockify-ts-sdk/issues"
15
+ },
16
+ "keywords": [
17
+ "clockify",
18
+ "cli",
19
+ "time-tracking",
20
+ "timer"
21
+ ],
22
+ "type": "module",
23
+ "engines": {
24
+ "node": ">=20"
25
+ },
26
+ "bin": {
27
+ "clockify115": "./dist/index.js",
28
+ "clk115": "./dist/index.js"
29
+ },
30
+ "exports": {
31
+ ".": "./dist/index.js"
32
+ },
33
+ "files": [
34
+ "dist",
35
+ "README.md",
36
+ "LICENSE"
37
+ ],
38
+ "scripts": {
39
+ "build": "tsc -p tsconfig.json",
40
+ "dev": "tsx src/index.ts",
41
+ "test": "vitest run",
42
+ "test:coverage": "vitest run --coverage",
43
+ "test:watch": "vitest",
44
+ "type-check": "tsc --noEmit",
45
+ "lint": "eslint .",
46
+ "lint:fix": "eslint . --fix",
47
+ "prepublishOnly": "npm run type-check && npm test && npm run build"
48
+ },
49
+ "dependencies": {
50
+ "cli-table3": "^0.6.5",
51
+ "commander": "^12.1.0",
52
+ "picocolors": "^1.1.1"
53
+ },
54
+ "peerDependencies": {
55
+ "clockify-sdk-ts-115": ">=0.9.0"
56
+ },
57
+ "devDependencies": {
58
+ "@types/node": "^25",
59
+ "@vitest/coverage-v8": "^4.1.9",
60
+ "clockify-sdk-ts-115": "*",
61
+ "eslint": "^10.5.0",
62
+ "eslint-plugin-import-x": "^4.16.2",
63
+ "tsx": "^4.19.2",
64
+ "typescript": "^5.7.0",
65
+ "typescript-eslint": "^8.59.4",
66
+ "vitest": "^4.1.4"
67
+ },
68
+ "publishConfig": {
69
+ "access": "public",
70
+ "provenance": true
71
+ }
72
+ }