logggai
Version:
AI-powered CLI for transforming your development work into professional content
217 lines • 8.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.syncCommand = syncCommand;
const chalk = require("chalk");
const project_1 = require("../lib/project");
const git_1 = require("../lib/git");
const inquirer_1 = require("inquirer");
const api_1 = require("../lib/api");
const ora_1 = require("ora");
const sync_helpers_1 = require("../lib/sync-helpers");
const auth_1 = require("../lib/auth");
async function syncCommand(options) {
return (0, auth_1.withAuth)(async () => {
await _syncCommand(options);
});
}
async function _syncCommand(options) {
const nonInteractive = options && options.nonInteractive;
// 1. Read local project mapping
const projectConfig = (0, project_1.readProjectConfig)();
if (!projectConfig) {
console.log(chalk.default.red('No Logggai project found in this directory.'));
console.log(chalk.default.yellow('Run: logggai project'));
return;
}
// 2. Get cloud project ID
const projectId = (0, project_1.getCloudProjectId)();
if (!projectId) {
console.log(chalk.default.red('Invalid project mapping: missing projectId.'));
return;
}
// 3. Show current project info (only in interactive mode)
if (!nonInteractive) {
console.log(chalk.default.blue.bold('\nCurrent project:'));
console.log(chalk.default.cyan(` Name : ${projectConfig.name}`));
if (projectConfig.repo) {
console.log(chalk.default.cyan(` Repo : ${projectConfig.repo}`));
}
console.log(chalk.default.cyan(` ID : ${projectId}`));
console.log(chalk.default.cyan(` Context: ${projectConfig.contextType}${projectConfig.contextId ? ' (' + projectConfig.contextId + ')' : ''}`));
}
// 4. Scan git commits since last sync
let lastSyncedCommit = projectConfig.lastSyncedCommit || null;
let commits = await (0, git_1.getCommitsSince)(lastSyncedCommit);
if (!commits.length) {
if (!nonInteractive) {
console.log(chalk.default.green('\nNo new commits to sync!'));
}
return;
}
if (!nonInteractive) {
console.log(chalk.default.yellow(`\n${commits.length} commit(s) to sync as a single changelog post.`));
}
// Normalize tags in each commit (future-proof, in case tags are added)
if (Array.isArray(commits)) {
for (const commit of commits) {
if ('tags' in commit) {
let tagsAny = commit.tags;
if (!Array.isArray(tagsAny)) {
if (typeof tagsAny === 'string' && tagsAny.length > 0) {
commit.tags = [tagsAny];
}
else if (tagsAny && typeof tagsAny !== 'string' && typeof tagsAny !== 'undefined') {
commit.tags = [String(tagsAny)];
}
else {
commit.tags = [];
}
}
}
}
}
// === Non-interactive mode: use defaults ===
let useAI = true;
let promptId = undefined;
let postTitle = `Changelog${projectConfig.name ? ' – ' + projectConfig.name : ''}`;
if (nonInteractive) {
// Get prompts and select 'IT Report/Technical Blog' or first
try {
const promptsData = await api_1.apiClient.getPrompts();
const prompts = promptsData.prompts || [];
if (prompts.length > 0) {
const techPrompt = prompts.find((p) => p.name.toLowerCase().includes('it report') || p.name.toLowerCase().includes('technical blog'));
promptId = techPrompt ? techPrompt.id : prompts[0].id;
}
}
catch (error) {
// fallback: no promptId
}
}
else {
// 5. Global AI Enhance prompt
const aiPrompt = await inquirer_1.default.prompt([
{
type: 'confirm',
name: 'useAI',
message: 'Do you want to enhance the changelog with AI?',
default: true
}
]);
useAI = aiPrompt.useAI;
// 6. If AI, prompt for prompt style
if (useAI) {
try {
const promptsData = await api_1.apiClient.getPrompts();
const prompts = promptsData.prompts || [];
if (prompts.length > 0) {
const { chosenPromptId } = await inquirer_1.default.prompt([
{
type: 'list',
name: 'chosenPromptId',
message: 'Choose an AI prompt style:',
choices: prompts.map((prompt) => ({
name: prompt.name,
value: prompt.id
})),
default: prompts[0].id
}
]);
promptId = chosenPromptId;
}
}
catch (error) {
console.log(chalk.default.yellow('Could not load prompts, using default AI enhancement.'));
}
}
}
// 7. Build changelog content from all commits
const changelogContent = commits.map(commit => {
return `- [${commit.hash.slice(0, 7)}] ${commit.date} ${commit.author}: ${commit.message}`;
}).join('\n');
let content = `## Changelog\n\n${changelogContent}`;
// 8. If AI, generate enhanced content
let aiContent = content;
if (useAI) {
if (!nonInteractive) {
const spinner = (0, ora_1.default)('Enhancing changelog with AI...').start();
try {
const aiResult = await api_1.apiClient.createPost({
title: 'Changelog',
content,
useAI: true,
promptId
});
aiContent = aiResult?.post?.content || content;
spinner.succeed('AI enhancement complete.');
}
catch (err) {
spinner.fail('AI enhancement failed, using raw changelog.');
aiContent = content;
}
}
else {
// No spinner in non-interactive mode
try {
const aiResult = await api_1.apiClient.createPost({
title: 'Changelog',
content,
useAI: true,
promptId
});
aiContent = aiResult?.post?.content || content;
}
catch (err) {
aiContent = content;
}
}
}
// 9. Ask for post title (interactive only)
if (!nonInteractive) {
const titlePrompt = await inquirer_1.default.prompt([
{
type: 'input',
name: 'postTitle',
message: 'Enter a title for your changelog post:',
default: 'Changelog'
}
]);
postTitle = titlePrompt.postTitle;
}
// 10. (Removed manual edit prompt and logic)
let finalContent = aiContent;
// 11. Create the post via API
let result;
try {
result = await api_1.apiClient.createPost({
title: postTitle,
content: finalContent,
useAI,
promptId
});
if (!nonInteractive) {
console.log(chalk.default.green('✓ Changelog post created successfully!'));
}
// Note: Tinybird push is handled server-side in the API endpoint
}
catch (error) {
if (!nonInteractive) {
console.log(chalk.default.red('Failed to create changelog post.'));
console.log(chalk.default.red(error.message));
}
return;
}
// 12. Offer to publish to integrations (interactive only)
if (!nonInteractive && result && result.post && result.post.id) {
await (0, sync_helpers_1.handleSocialPublishing)(result.post.id);
}
// 13. Update local mapping (last synced commit)
if (commits.length > 0) {
projectConfig.lastSyncedCommit = commits[commits.length - 1].hash;
(0, project_1.writeProjectConfig)(projectConfig);
if (!nonInteractive) {
console.log(chalk.default.green('Local mapping updated (last synced commit).'));
}
}
}
//# sourceMappingURL=sync.js.map