feat: Modernize package with ES modules and improve error handling

- Update to ES modules syntax throughout the codebase
- Add fileURLToPath and dirname imports to handle __dirname in ES modules
- Upgrade Anthropic SDK from 0.10.0 to 0.39.0
- Fix JSON parsing to handle responses wrapped in Markdown code blocks
- Improve documentation with clearer installation instructions
- Add important notes about ES modules and SDK requirements
- Update environment examples with API key format and model recommendations
- Include index.js in package files list
- Bump version to 1.0.2
This commit is contained in:
Eyal Toledano
2025-03-04 14:11:57 -05:00
parent 07648a1417
commit c72373d761
10 changed files with 100 additions and 51 deletions

View File

@@ -42,6 +42,11 @@
import fs from 'fs';
import path from 'path';
import dotenv from 'dotenv';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Load environment variables from .env file
dotenv.config();
@@ -169,9 +174,17 @@ async function callClaude(prdContent, prdPath, numTasks) {
log('debug', `Response length: ${textContent.length} characters`);
try {
// Try to parse the response as JSON
// Check if the response is wrapped in a Markdown code block and extract the JSON
log('info', "Parsing response as JSON...");
const parsedJson = JSON.parse(textContent);
let jsonText = textContent;
const codeBlockMatch = textContent.match(/```(?:json)?\s*([\s\S]*?)\s*```/);
if (codeBlockMatch) {
log('debug', "Detected JSON wrapped in Markdown code block, extracting...");
jsonText = codeBlockMatch[1];
}
// Try to parse the response as JSON
const parsedJson = JSON.parse(jsonText);
log('info', `Successfully parsed JSON with ${parsedJson.tasks?.length || 0} tasks`);
return parsedJson;
} catch (error) {

View File

@@ -1,9 +1,14 @@
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const readline = require('readline');
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
import readline from 'readline';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Create readline interface for user input
const rl = readline.createInterface({
@@ -143,6 +148,7 @@ function createProjectStructure(projectName, projectDescription, projectVersion,
version: projectVersion,
description: projectDescription,
author: authorName,
type: "module",
scripts: {
"dev": "node scripts/dev.js",
"list": "node scripts/dev.js list",
@@ -150,7 +156,7 @@ function createProjectStructure(projectName, projectDescription, projectVersion,
"parse-prd": "node scripts/dev.js parse-prd"
},
dependencies: {
"@anthropic-ai/sdk": "^0.10.0",
"@anthropic-ai/sdk": "^0.39.0",
"chalk": "^4.1.2",
"commander": "^11.1.0",
"dotenv": "^16.3.1"
@@ -230,7 +236,7 @@ function createProjectStructure(projectName, projectDescription, projectVersion,
}
// Run the initialization if this script is executed directly
if (require.main === module) {
if (process.argv[1] === fileURLToPath(import.meta.url)) {
(async function main() {
try {
await initializeProject();
@@ -242,7 +248,7 @@ if (require.main === module) {
}
// Export functions for programmatic use
module.exports = {
export {
initializeProject,
createProjectStructure,
log

View File

@@ -5,9 +5,14 @@
* It ensures all necessary files are included and properly configured.
*/
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Define colors for console output
const COLORS = {