Generic JSON and YAML file processors with git diff extraction and format preservation
npm install @yukiflowstack/json-yaml-processorsbash
npm install @alpnpmrepo/json-yaml-processors
`
Usage
$3
`typescript
import { FileProcessorFactory } from '@alpnpmrepo/json-yaml-processors';
// Create a processor for your file
const processor = FileProcessorFactory.createProcessor('config.json');
// Extract changes from a git diff
const result = processor.extractChanges(
'config.json', // source file path
sourceContent, // source file content
targetContent, // target file content
gitDiff // git diff output
);
// Process the extracted changes
result.changes.forEach(change => {
console.log(Property: ${change.propertyPath.join('.')});
console.log(Change type: ${change.changeType});
console.log(Old value: ${change.oldValue});
console.log(New value: ${change.newValue});
});
// Apply changes to target content
const updatedContent = processor.applyChanges(targetContent, result.changes);
`
$3
`typescript
import { JsonProcessor } from '@alpnpmrepo/json-yaml-processors';
const jsonProcessor = new JsonProcessor();
// Extract changes
const result = jsonProcessor.extractChanges(
'package.json',
sourceJson,
targetJson,
gitDiff
);
// Changes include full property paths
result.changes.forEach(change => {
console.log(change.propertyPath); // e.g., ["dependencies", "typescript"]
console.log(change.newValue); // e.g., "^5.0.0"
});
`
$3
`typescript
import { YamlProcessor } from '@alpnpmrepo/json-yaml-processors';
const yamlProcessor = new YamlProcessor();
// Extract changes (preserves YAML formatting)
const result = yamlProcessor.extractChanges(
'config.yaml',
sourceYaml,
targetYaml,
gitDiff
);
// Supports multi-line blocks
result.changes.forEach(change => {
if (change.sourceBlockContent) {
console.log('Multi-line block change detected');
}
});
`
API
$3
Factory for creating the appropriate processor based on file extension.
`typescript
static createProcessor(filePath: string): IFileProcessor
`
$3
Common interface implemented by all processors.
#### Methods
- canProcess(filePath: string): boolean - Check if processor can handle file
- getFileType(): 'json' | 'yaml' | 'text' | 'binary' - Get file type identifier
- parseContent(content: string): unknown - Parse file content
- serializeContent(data: unknown, originalContent?: string): string - Serialize data
- extractChanges(sourceFilePath: string, sourceContent: string, targetContent: string, gitDiff: string): IExtractChangesResult - Extract changes from git diff
- applyChanges(targetContent: string, changes: IFileChange[]): string - Apply changes to content
$3
Represents a single property change.
`typescript
interface IFileChange {
id: string; // Unique identifier
file: string; // File path
fileType: 'json' | 'yaml'; // File type
changeType: 'add' | 'modify' | 'delete';
propertyPath: string[]; // Hierarchical path (e.g., ["server", "port"])
lineNumber: number; // Line number in file
oldValue?: unknown; // Previous value (for modify/delete)
newValue: unknown; // New value (for add/modify)
sourceBlockContent?: string; // For YAML multi-line blocks
}
``