Fix comma insertion logic to prevent undefined entries
Improvements: - Check for existing trailing comma before adding separator - Validate generated entry contains required fields - Prevents duplicate commas when inserting multiple claims sequentially This fixes the "Type 'undefined' is not assignable" error 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
32696a9aca
commit
f5c6953d73
|
|
@ -96,13 +96,21 @@ export function addClaimToDataFile(claim: NewClaimInput): void {
|
||||||
// Generate new entry
|
// Generate new entry
|
||||||
const newEntry = generateClaimEntry(claim);
|
const newEntry = generateClaimEntry(claim);
|
||||||
|
|
||||||
|
// Validate the generated entry contains required fields
|
||||||
|
if (!newEntry.includes('slug:') || !newEntry.includes('title:') || !newEntry.includes('claims:')) {
|
||||||
|
throw new Error('Generated entry is missing required fields');
|
||||||
|
}
|
||||||
|
|
||||||
// Insert before the closing bracket
|
// Insert before the closing bracket
|
||||||
const insertPosition = match.index;
|
const insertPosition = match.index;
|
||||||
const before = fileContent.substring(0, insertPosition);
|
const before = fileContent.substring(0, insertPosition);
|
||||||
const after = fileContent.substring(insertPosition);
|
const after = fileContent.substring(insertPosition);
|
||||||
|
|
||||||
// Add comma after last entry if needed
|
// Add comma after last entry if needed (check if there's already a trailing comma)
|
||||||
const newContent = before.trimEnd() + ',\n' + newEntry + after;
|
const trimmedBefore = before.trimEnd();
|
||||||
|
const needsComma = !trimmedBefore.endsWith(',') && !trimmedBefore.endsWith('[');
|
||||||
|
const separator = needsComma ? ',\n' : '\n';
|
||||||
|
const newContent = trimmedBefore + separator + newEntry + after;
|
||||||
|
|
||||||
// Write back to file
|
// Write back to file
|
||||||
fs.writeFileSync(claimsFilePath, newContent, 'utf-8');
|
fs.writeFileSync(claimsFilePath, newContent, 'utf-8');
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue