Skip to content

Commit 8722363

Browse files
committed
New reordering algorithm
1 parent ec3014c commit 8722363

28 files changed

+916
-1335
lines changed

biome.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,13 @@
2929
"formatter": {
3030
"quoteStyle": "single"
3131
}
32-
}
32+
},
33+
"overrides": [
34+
{
35+
"include": ["package.json"],
36+
"formatter": {
37+
"lineWidth": 1
38+
}
39+
}
40+
]
3341
}

packages/cli/package-lock.json

Lines changed: 63 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"@oclif/plugin-warn-if-update-available": "^2.0.4",
3535
"lodash": "^4.17.21",
3636
"mkdirp": "^1.0.4",
37-
"prettier": "^2.7.1",
37+
"prettier": "^3.3.3",
3838
"ts-node": "^10.7.0",
3939
"typescript": "~4.5.0"
4040
},

packages/cli/src/utils/environments-diff/fetch-schema.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export async function fetchSchema(client: CmaClient.Client): Promise<Schema> {
1010
workflowsResponse,
1111
itemTypeFiltersResponse,
1212
uploadFiltersResponse,
13+
uploadCollectionsResponse,
1314
] = await Promise.all([
1415
client.site.rawFind({
1516
include: 'item_types,item_types.fields,item_types.fieldsets',
@@ -20,6 +21,7 @@ export async function fetchSchema(client: CmaClient.Client): Promise<Schema> {
2021
client.workflows.rawList(),
2122
client.itemTypeFilters.rawList(),
2223
client.uploadFilters.rawList(),
24+
client.uploadCollections.rawList(),
2325
]);
2426

2527
const includedResources = siteResponse.included || [];
@@ -70,6 +72,12 @@ export async function fetchSchema(client: CmaClient.Client): Promise<Schema> {
7072
schemaMenuItem,
7173
]),
7274
),
75+
uploadCollectionsById: Object.fromEntries(
76+
uploadCollectionsResponse.data.map((uploadCollection) => [
77+
uploadCollection.id,
78+
uploadCollection,
79+
]),
80+
),
7381
pluginsById: Object.fromEntries(
7482
pluginsResponse.data.map((plugin) => [plugin.id, plugin]),
7583
),

packages/cli/src/utils/environments-diff/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { manageItemTypeFilters } from './resources/manage-item-type-filters';
1010
import { manageMenuItems } from './resources/manage-menu-items';
1111
import { managePlugins } from './resources/manage-plugins';
1212
import { manageSchemaMenuItems } from './resources/manage-schema-menu-items';
13+
import { manageUploadCollections } from './resources/manage-upload-collections';
1314
import { manageUploadFilters } from './resources/manage-upload-filters';
1415
import { manageWorkflows } from './resources/manage-workflows';
1516
import { updateFieldsAndFieldsets } from './resources/update-fields-and-fieldsets';
@@ -51,10 +52,15 @@ export async function diffEnvironments({
5152
...manageItemTypeFilters(newSchema, oldSchema),
5253
...manageMenuItems(newSchema, oldSchema),
5354
...manageSchemaMenuItems(newSchema, oldSchema),
55+
...manageUploadCollections(newSchema, oldSchema),
5456
...updateRoles(roles, newEnvironmentId, oldEnvironmentId),
5557
];
5658

5759
const options = await resolveConfig(migrationFilePath);
5860

59-
return write(commands, { ...options, format, filepath: migrationFilePath });
61+
return write(commands, {
62+
...options,
63+
format,
64+
filepath: migrationFilePath,
65+
});
6066
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { isEqual } from 'lodash';
2+
import { buildLog } from './comments';
3+
4+
type ReorderableEntity<EntityType extends string> = {
5+
id: string;
6+
type: EntityType;
7+
/**
8+
* JSON API attributes
9+
*/
10+
attributes: {
11+
/**
12+
* Ordering index
13+
*/
14+
position: number;
15+
};
16+
relationships: {
17+
/**
18+
* Parent menu item
19+
*/
20+
parent: {
21+
data: null | {
22+
id: string;
23+
type: EntityType;
24+
};
25+
};
26+
};
27+
};
28+
29+
type SingleReorderCommand<EntityType extends string> = {
30+
id: string;
31+
type: EntityType;
32+
/**
33+
* JSON API attributes
34+
*/
35+
attributes: {
36+
/**
37+
* Ordering index
38+
*/
39+
position: number;
40+
};
41+
relationships: {
42+
/**
43+
* Parent menu item
44+
*/
45+
parent: {
46+
data: null | {
47+
id: string;
48+
type: EntityType;
49+
};
50+
};
51+
};
52+
};
53+
54+
export function buildReorderCommand<
55+
T extends string,
56+
Command extends {
57+
type: 'apiCallClientCommand';
58+
call: string;
59+
arguments: [{ data: SingleReorderCommand<T>[] }];
60+
},
61+
>(
62+
call: Command['call'],
63+
keptEntities: [ReorderableEntity<T>, ReorderableEntity<T>][],
64+
newEntities: ReorderableEntity<T>[],
65+
) {
66+
const commands: SingleReorderCommand<T>[] = [
67+
...newEntities,
68+
...keptEntities
69+
.filter(
70+
([newEntity, oldEntity]) =>
71+
!isEqual(
72+
newEntity.attributes.position,
73+
oldEntity.attributes.position,
74+
) ||
75+
!isEqual(
76+
newEntity.relationships.parent.data?.id,
77+
oldEntity.relationships.parent.data?.id,
78+
),
79+
)
80+
.map((tuple) => tuple[0]),
81+
].map((entity) => ({
82+
id: entity.id,
83+
type: entity.type,
84+
attributes: { position: entity.attributes.position },
85+
relationships: {
86+
parent: {
87+
data: entity.relationships.parent.data?.id
88+
? {
89+
id: entity.relationships.parent.data.id,
90+
type: entity.type,
91+
}
92+
: null,
93+
},
94+
},
95+
}));
96+
97+
if (commands.length === 0) {
98+
return [];
99+
}
100+
101+
return [
102+
buildLog('Reorder elements in the tree'),
103+
{
104+
type: 'apiCallClientCommand',
105+
call,
106+
arguments: [
107+
{
108+
data: commands,
109+
},
110+
],
111+
},
112+
] as Command[];
113+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { Comment } from '../types';
1+
import type { LogCommand } from '../types';
22

3-
export function buildComment(message: string): Comment {
4-
return { type: 'comment', message };
3+
export function buildLog(message: string): LogCommand {
4+
return { type: 'log', message };
55
}

0 commit comments

Comments
 (0)