|
| 1 | +import chalk from 'chalk'; |
| 2 | +import { ResourceConfig } from '../entities/resource-config.js'; |
| 3 | +import * as Diff from 'diff' |
| 4 | +import { FileType, InMemoryFile } from '../parser/entities.js'; |
| 5 | + |
| 6 | +export enum ModificationType { |
| 7 | + INSERT_OR_UPDATE, |
| 8 | + DELETE |
| 9 | +} |
| 10 | + |
| 11 | +export interface ModifiedResource { |
| 12 | + resource: ResourceConfig; |
| 13 | + modification: ModificationType |
| 14 | +} |
| 15 | + |
| 16 | +export interface FileModificationResult { |
| 17 | + newFile: string; |
| 18 | + diff: string; |
| 19 | +} |
| 20 | + |
| 21 | +export class FileModificationCalculator { |
| 22 | + private existingFile?: InMemoryFile; |
| 23 | + private existingResources: ResourceConfig[]; |
| 24 | + |
| 25 | + constructor(existingResources: ResourceConfig[], existingFile: InMemoryFile) { |
| 26 | + this.existingFile = existingFile; |
| 27 | + this.existingResources = existingResources; |
| 28 | + } |
| 29 | + |
| 30 | + async calculate(modifications: ModifiedResource[]): Promise<FileModificationResult> { |
| 31 | + const resultResources = [...this.existingResources] |
| 32 | + |
| 33 | + if (this.existingResources.length === 0 || !this.existingFile) { |
| 34 | + const newFile = JSON.stringify( |
| 35 | + modifications |
| 36 | + .filter((r) => r.modification === ModificationType.INSERT_OR_UPDATE) |
| 37 | + .map((r) => r.resource.raw), |
| 38 | + null, 2) |
| 39 | + |
| 40 | + return { |
| 41 | + newFile, |
| 42 | + diff: this.diff('', newFile), |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + this.validate(modifications); |
| 47 | + |
| 48 | + for (const modified of modifications) { |
| 49 | + const duplicateIndex = this.existingResources.findIndex((existing) => existing.isSameOnSystem(modified.resource)) |
| 50 | + |
| 51 | + if (duplicateIndex === -1) { |
| 52 | + if (modified.modification === ModificationType.INSERT_OR_UPDATE) { |
| 53 | + resultResources.push(modified.resource); |
| 54 | + } |
| 55 | + |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + if (modified.modification === ModificationType.DELETE) { |
| 60 | + resultResources.splice(duplicateIndex, 1); |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + const duplicate = resultResources[duplicateIndex]; |
| 65 | + for (const [key, newValue] of Object.entries(modified.resource.parameters)) { |
| 66 | + duplicate.setParameter(key, newValue); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + const newFile = JSON.stringify( |
| 71 | + resultResources.map((r) => r.raw), |
| 72 | + null, 2 |
| 73 | + ); |
| 74 | + |
| 75 | + return { |
| 76 | + newFile, |
| 77 | + diff: this.diff(this.existingFile.contents, newFile), |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + validate(modifiedResources: ModifiedResource[]): void { |
| 82 | + if (!this.existingFile) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + if (this.existingFile?.fileType !== FileType.JSON) { |
| 87 | + throw new Error(`Only updating .json files are currently supported. Found ${this.existingFile?.filePath}`); |
| 88 | + } |
| 89 | + |
| 90 | + if (this.existingResources.some((r) => !r.resourceInfo)) { |
| 91 | + const badResources = this.existingResources |
| 92 | + .filter((r) => !r.resourceInfo) |
| 93 | + .map((r) => r.id); |
| 94 | + |
| 95 | + throw new Error(`All resources must have resource info attached to generate diff. Found bad resources: ${badResources}`); |
| 96 | + } |
| 97 | + |
| 98 | + if (modifiedResources.some((r) => !r.resource.resourceInfo)) { |
| 99 | + const badResources = modifiedResources |
| 100 | + .filter((r) => !r.resource.resourceInfo) |
| 101 | + .map((r) => r.resource.id); |
| 102 | + |
| 103 | + throw new Error(`All resources must have resource info attached to generate diff. Found bad resources: ${badResources}`); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + diff(a: string, b: string): string { |
| 108 | + const diff = Diff.diffLines(a, b); |
| 109 | + |
| 110 | + let result = ''; |
| 111 | + diff.forEach((part) => { |
| 112 | + result += part.added ? chalk.green(part.value) : |
| 113 | + part.removed ? chalk.red(part.value) : |
| 114 | + part.value; |
| 115 | + }); |
| 116 | + |
| 117 | + return result; |
| 118 | + } |
| 119 | +} |
0 commit comments