@latentsearch/timemachine-cli
Version:
CLI tool for TimeMachine API. Generates time entries, lists users/projects, and features an enhanced dry-run output for generation.
77 lines (76 loc) • 3.17 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.listProjects = listProjects;
const chalk_1 = __importDefault(require("chalk"));
const console_table_printer_1 = require("console-table-printer");
const api_client_1 = require("../utils/api-client");
/**
* Lists all projects with their details
*/
async function listProjects(options) {
try {
console.log(chalk_1.default.blue("Fetching projects..."));
const api = (0, api_client_1.createApiClient)(options);
const response = await api.get("/api/projects");
if (response.data && response.data.length > 0) {
const table = new console_table_printer_1.Table({
title: "Code Clock Projects",
columns: [
{ name: "id", title: "ID", alignment: "right" },
{ name: "name", title: "Name" },
{ name: "company", title: "Company" }
]
});
// Group projects by company for better organization
const projectsByCompany = {};
response.data.forEach(project => {
const companyName = project.company_name || "No Company";
if (!projectsByCompany[companyName]) {
projectsByCompany[companyName] = [];
}
projectsByCompany[companyName].push(project);
});
// Add projects to table, grouped by company
Object.entries(projectsByCompany).forEach(([companyName, projects], index) => {
// Add company separator if not the first company
if (index > 0) {
table.addRow({
id: "",
name: "",
company: ""
});
}
// Add company header
table.addRow({
id: "",
name: chalk_1.default.bold(companyName),
company: ""
});
// Add company's projects
projects.forEach(project => {
table.addRow({
id: project.id,
name: project.name || "N/A",
company: "" // Company already shown in group header
});
});
});
table.printTable();
console.log(`\nTotal projects: ${response.data.length}`);
console.log(`Companies: ${Object.keys(projectsByCompany).length}`);
}
else {
console.log(chalk_1.default.yellow("No projects found"));
}
}
catch (error) {
console.error(chalk_1.default.red("Error fetching projects:"), error.response?.data?.error || error.message);
if (options.verbose && error.response?.data) {
console.error(chalk_1.default.gray(JSON.stringify(error.response.data, null, 2)));
}
process.exit(1);
}
}