Skip to content

Commit 51aa606

Browse files
committed
feat: Added parameter key sorting so that the order of parameters remains the same. For new resources, it will default to alphabetical order. Modified the import result to not show the JSON. Fixed bug with not adding trimmed string back and resource ordering issues.
1 parent c29f24d commit 51aa606

File tree

4 files changed

+54
-20
lines changed

4 files changed

+54
-20
lines changed

src/entities/resource-config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ export class ResourceConfig implements ConfigBlock {
5858
return this.name ? `${this.type}.${this.name}` : this.type;
5959
}
6060

61+
core(excludeName?: boolean): SchemaResourceConfig {
62+
return {
63+
type: this.type,
64+
...(excludeName || !this.name ? {} : { name: this.name }),
65+
...(this.dependsOn.length > 0 ? { dependsOn: this.dependsOn } : {})
66+
};
67+
}
68+
6169
toJson(): ResourceJson {
6270
return {
6371
core: {

src/ui/components/import/import-result.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,20 @@ export function ImportResultComponent(props: {
1010
const { result, errors } = props.importResult
1111

1212
return <Box flexDirection="column">
13-
<Box borderColor="green" borderStyle="round">
14-
<Text>Codify Import</Text>
15-
</Box>
16-
<br/>
17-
<Text>{ JSON.stringify(result.map((r) => r.raw), null, 2)}</Text>
13+
<Text> </Text>
14+
{
15+
result.length > 0 && (<Box flexDirection="column">
16+
<Text bold={true} color={'green'}>Successfully imported the following configs:</Text>
17+
<OrderedList>
18+
{
19+
result.map((r, idx) => <OrderedList.Item key={idx}>
20+
<Text color={'green'}>{r.type}</Text>
21+
</OrderedList.Item>)
22+
}
23+
</OrderedList>
24+
</Box>)
25+
}
26+
<Text> </Text>
1827
{
1928
errors.length > 0 && (<Box flexDirection="column">
2029
<Text bold={true} color={'yellow'}>The following configs failed to import:</Text>

src/ui/file-diff-pretty-printer.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import * as Diff from 'diff'
44
export function prettyFormatFileDiff(before: string, after: string): string {
55
const diff = Diff.diffLines(before, after);
66

7-
console.log(diff);
8-
97
const changeList: Array<{ added: boolean; removed: boolean; lineNumber: number }> = []
108
diff
119
.reduce((lineNumber, change) => {

src/utils/file-modification-calculator.ts

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import detectIndent from 'detect-indent';
88
import { Project } from '../entities/project.js';
99
import { ProjectConfig } from '../entities/project-config.js';
1010
import { prettyFormatFileDiff } from '../ui/file-diff-pretty-printer.js';
11+
import { deepEqual } from './index.js';
1112

1213
export enum ModificationType {
1314
INSERT_OR_UPDATE,
@@ -50,15 +51,20 @@ export class FileModificationCalculator {
5051

5152
// Reverse the traversal order so we edit from the back. This way the line numbers won't be messed up with new edits.
5253
for (const existing of this.existingConfigs.reverse()) {
53-
const duplicateIndex = modifications.findIndex((modified) => existing.isSameOnSystem(modified.resource))
54+
const duplicateIndex = updateCache.findIndex((modified) => existing.isSameOnSystem(modified.resource))
5455

55-
// The resource was not modified in any way. Skip.
56+
// The existing was not modified in any way. Skip.
5657
if (duplicateIndex === -1) {
5758
continue;
5859
}
60+
61+
const modified = updateCache[duplicateIndex];
5962
updateCache.splice(duplicateIndex, 1)
6063

61-
const modified = modifications[duplicateIndex];
64+
if (deepEqual(modified.resource.parameters, existing.parameters)) {
65+
continue;
66+
}
67+
6268
const duplicateSourceKey = existing.sourceMapKey?.split('#').at(1)!;
6369
const sourceIndex = Number.parseInt(duplicateSourceKey.split('/').at(1)!)
6470

@@ -71,7 +77,7 @@ export class FileModificationCalculator {
7177

7278
// Update an existing resource
7379
newFile = this.remove(newFile, this.sourceMap, sourceIndex);
74-
newFile = this.update(newFile, modified.resource, this.sourceMap, sourceIndex);
80+
newFile = this.update(newFile, modified.resource, existing, this.sourceMap, sourceIndex);
7581
}
7682

7783
// Insert new resources
@@ -82,6 +88,10 @@ export class FileModificationCalculator {
8288

8389
newFile = this.insert(newFile, newResourcesToInsert, insertionIndex);
8490

91+
const lastCharacterIndex = this.existingFile.contents.lastIndexOf(']')
92+
const ending = this.existingFile.contents.slice(Math.min(lastCharacterIndex + 1, this.existingFile.contents.length - 1));
93+
newFile += ending;
94+
8595
return {
8696
newFile: newFile,
8797
diff: prettyFormatFileDiff(this.existingFile.contents, newFile),
@@ -128,7 +138,9 @@ export class FileModificationCalculator {
128138
let result = file;
129139

130140
for (const newResource of resources.reverse()) {
131-
let content = JSON.stringify(newResource.raw, null, 2);
141+
const sortedResource = { ...newResource.core(true), ...this.sortKeys(newResource.parameters) }
142+
let content = JSON.stringify(sortedResource, null, 2);
143+
132144
content = content.split(/\n/).map((l) => `${this.indentString}${l}`).join('\n')
133145
content = `,\n${content}`;
134146

@@ -167,20 +179,21 @@ export class FileModificationCalculator {
167179
private update(
168180
file: string,
169181
resource: ResourceConfig,
182+
existing: ResourceConfig,
170183
sourceMap: SourceMap,
171184
sourceIndex: number,
172185
): string {
173186
// Updates: for now let's remove and re-add the entire object. Only two formatting availalbe either same line or multi-line
174187
const { value, valueEnd } = this.sourceMap.lookup(`/${sourceIndex}`)!;
175188
const isSameLine = value.line === valueEnd.line;
176-
177-
const isLast = sourceIndex === this.totalConfigLength - 1;
178189
const isFirst = sourceIndex === 0;
179190

180191
// We try to start deleting from the previous element to the next element if possible. This covers any spaces as well.
181192
const start = !isFirst ? this.sourceMap.lookup(`/${sourceIndex - 1}`)?.valueEnd : this.sourceMap.lookup(`/${sourceIndex}`)?.value;
182193

183-
let content = isSameLine ? JSON.stringify(resource.raw) : JSON.stringify(resource.raw, null, this.indentString);
194+
const sortedResource = this.sortKeys(resource.raw, existing.raw);
195+
196+
let content = isSameLine ? JSON.stringify(sortedResource) : JSON.stringify(sortedResource, null, this.indentString);
184197
content = this.updateParamsToOnelineIfNeeded(content, sourceMap, sourceIndex);
185198

186199
content = content.split(/\n/).map((l) => `${this.indentString}${l}`).join('\n');
@@ -216,12 +229,18 @@ export class FileModificationCalculator {
216229
return s.substring(0, start) + s.substring(end);
217230
}
218231

219-
/** Transforms a source key from global to file specific */
220-
private transformSourceKey(key: string): string {
221-
return key.split('#').at(1)!;
222-
}
223-
224232
private isResourceConfig(config: ProjectConfig | ResourceConfig): config is ResourceConfig {
225233
return config instanceof ResourceConfig;
226234
}
235+
236+
private sortKeys(obj: Record<string, unknown>, referenceOrder?: Record<string, unknown>): Record<string, unknown> {
237+
const reference = Object.keys(referenceOrder
238+
?? Object.fromEntries([...Object.keys(obj)].sort().map((k) => [k, undefined]))
239+
);
240+
241+
return Object.fromEntries(
242+
Object.entries(obj)
243+
.sort((a, b) => reference.indexOf(a[0]) - reference.indexOf(b[0]))
244+
)
245+
}
227246
}

0 commit comments

Comments
 (0)