Skip to content

Commit f6a86ec

Browse files
committed
feat: show per-field diffs in update logging for dry run and sync
1 parent bbc7778 commit f6a86ec

2 files changed

Lines changed: 32 additions & 9 deletions

File tree

scripts/fetch-models.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,19 @@ export function diffModels(apiModels, webflowItems) {
116116
continue;
117117
}
118118

119-
const hasChanges = Object.keys(model.fields).some((key) => {
120-
if (FIELDS_MANAGED_OUTSIDE_DIFF.has(key)) return false;
119+
const changedFields = [];
120+
for (const key of Object.keys(model.fields)) {
121+
if (FIELDS_MANAGED_OUTSIDE_DIFF.has(key)) continue;
121122
const apiVal = model.fields[key];
122123
const wfVal = existing.fieldData[key];
123-
if ((apiVal === '' || apiVal == null) && (wfVal === '' || wfVal == null)) return false;
124-
return JSON.stringify(apiVal) !== JSON.stringify(wfVal);
125-
});
124+
if ((apiVal === '' || apiVal == null) && (wfVal === '' || wfVal == null)) continue;
125+
if (JSON.stringify(apiVal) !== JSON.stringify(wfVal)) {
126+
changedFields.push({ field: key, from: wfVal, to: apiVal });
127+
}
128+
}
126129

127-
if (hasChanges) {
128-
toUpdate.push({ id: existing.id, fieldData: model.fields });
130+
if (changedFields.length > 0) {
131+
toUpdate.push({ id: existing.id, fieldData: model.fields, changedFields });
129132
} else {
130133
unchanged++;
131134
}
@@ -295,6 +298,17 @@ async function buildModelFieldData(models, categoryMap, existingItems) {
295298
return apiModels;
296299
}
297300

301+
function truncate(val, maxLen = 80) {
302+
const str = typeof val === 'string' ? val : JSON.stringify(val);
303+
return str.length > maxLen ? str.slice(0, maxLen) + '...' : str;
304+
}
305+
306+
function logChangedFields(changedFields) {
307+
for (const { field, from, to } of changedFields) {
308+
console.log(` ${field}: ${truncate(from)}${truncate(to)}`);
309+
}
310+
}
311+
298312
async function applyChanges(collectionId, { toCreate, toUpdate, toDelete }) {
299313
if (toCreate.length > 0) {
300314
console.log(`Creating ${toCreate.length} models...`);
@@ -304,7 +318,10 @@ async function applyChanges(collectionId, { toCreate, toUpdate, toDelete }) {
304318

305319
if (toUpdate.length > 0) {
306320
console.log(`Updating ${toUpdate.length} models...`);
307-
for (const item of toUpdate) console.log(` Updating: ${item.fieldData.slug}`);
321+
for (const item of toUpdate) {
322+
console.log(` Updating: ${item.fieldData.slug}`);
323+
logChangedFields(item.changedFields);
324+
}
308325
await wf.updateItems(collectionId, toUpdate);
309326
}
310327

@@ -321,7 +338,10 @@ function logDryRunSummary({ toCreate, toUpdate, toDelete, unchanged }) {
321338
}
322339
if (toUpdate.length > 0) {
323340
console.log(`[dry run] Would update ${toUpdate.length} models:`);
324-
for (const item of toUpdate) console.log(` ${item.fieldData.slug}`);
341+
for (const item of toUpdate) {
342+
console.log(` ${item.fieldData.slug}`);
343+
logChangedFields(item.changedFields);
344+
}
325345
}
326346
if (toDelete.length > 0) {
327347
console.log(`[dry run] Would delete ${toDelete.length} models`);

tests/fetch-models/diff.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ describe('diffModels', () => {
4646
assert.equal(result.toUpdate.length, 1);
4747
assert.equal(result.toUpdate[0].id, 'wf-id-2');
4848
assert.deepEqual(result.toUpdate[0].fieldData, { slug: 'existing-model', name: 'Updated Name' });
49+
assert.deepEqual(result.toUpdate[0].changedFields, [
50+
{ field: 'name', from: 'Old Name', to: 'Updated Name' },
51+
]);
4952
assert.equal(result.toCreate.length, 0);
5053
assert.equal(result.toDelete.length, 0);
5154
assert.equal(result.unchanged, 0);

0 commit comments

Comments
 (0)