diff --git a/src/actions/components.js b/src/actions/components.js index 2b5413e..28f65a4 100644 --- a/src/actions/components.js +++ b/src/actions/components.js @@ -1,3 +1,4 @@ +import _ from 'lodash'; import api from './api.js'; import { loadBabelCore } from './babel.js'; import { loadConfig } from '../config.js'; @@ -88,8 +89,20 @@ export async function updateComponentConfig({ file, config }, opts = {}) { }); } +export function hasComponentChanged(remoteComponent, { file, config }) { + const remoteHtml = remoteComponent?.latestHtml; + const remoteEditable = remoteComponent?.latestSchema?.data?.editable; + return remoteHtml !== file || !_.isEqual(remoteEditable, config.fields); +} + export async function updateComponentFile({ file, config }, opts = {}) { const component = await getComponent(config.uuid, opts); + if ( + opts.skipUnchanged && + !hasComponentChanged(component?.data, { file, config }) + ) { + return { skipped: true }; + } return await api({ path: `/components/${config.uuid}?private=1`, method: 'PATCH', @@ -145,7 +158,8 @@ export async function compileComponents() { const componentsDir = path.join(process.cwd(), 'components'); const components = []; for (const file of fs.readdirSync(componentsDir)) { - if (!fs.statSync(path.join(componentsDir, file)).isDirectory()) continue; + if (!fs.statSync(path.join(componentsDir, file)).isDirectory()) + continue; const component = { name: file, latestHtml: fs.readFileSync( diff --git a/src/deploy.js b/src/deploy.js index 893e80e..561930a 100644 --- a/src/deploy.js +++ b/src/deploy.js @@ -14,24 +14,22 @@ import { shouldRefuseLayoutForCommand, getLegacyLayoutRefusalMessage, } from './actions/layout.js'; -import { - updateComponentFile, - updateComponentConfig, -} from './actions/components.js'; +import { updateComponentFile } from './actions/components.js'; import { uploadPage } from './actions/pages.js'; import { loadConfig } from './config.js'; import { getToken } from './actions/auth.js'; -import { - validateCampaignSass, - validateComponent, -} from './actions/validate.js'; +import { validateCampaignSass, validateComponent } from './actions/validate.js'; function formatValidationErrors(errors) { return errors.map(({ context, error }) => `${context}: ${error}`); } function normalizeValidationResult(result, fallbackError) { - if (result && typeof result === 'object' && typeof result.ok === 'boolean') { + if ( + result && + typeof result === 'object' && + typeof result.ok === 'boolean' + ) { return { ok: result.ok, error: @@ -49,7 +47,8 @@ function normalizeValidationResult(result, fallbackError) { function toErrorMessage(error, fallback) { if (typeof error === 'string' && error.trim()) return error.trim(); - if (error instanceof Error && error.message.trim()) return error.message.trim(); + if (error instanceof Error && error.message.trim()) + return error.message.trim(); if (error && typeof error.message === 'string' && error.message.trim()) { return error.message.trim(); } @@ -226,16 +225,24 @@ export default async function deploy(options = {}) { const componentsDir = path.join(cwd, 'components'); if (fs.existsSync(componentsDir)) { for (const file of fs.readdirSync(componentsDir)) { - if (!fs.statSync(path.join(componentsDir, file)).isDirectory()) continue; + if (!fs.statSync(path.join(componentsDir, file)).isDirectory()) + continue; const data = { - file: fs.readFileSync(path.join(componentsDir, file, `${file}.js`), 'utf8'), + file: fs.readFileSync( + path.join(componentsDir, file, `${file}.js`), + 'utf8' + ), config: JSON.parse( - fs.readFileSync(path.join(componentsDir, file, `${file}.json`), 'utf8') + fs.readFileSync( + path.join(componentsDir, file, `${file}.json`), + 'utf8' + ) ), }; - components.push(limit(() => updateComponentConfig(data))); - components.push(limit(() => updateComponentFile(data))); + components.push( + limit(() => updateComponentFile(data, { skipUnchanged: true })) + ); } } @@ -246,12 +253,26 @@ export default async function deploy(options = {}) { const rejected = deployResult .filter((result) => result.status === 'rejected') .map((result) => result.reason); + const skippedComponents = deployResult.filter( + (result) => + result.status === 'fulfilled' && result.value?.skipped === true + ).length; + const uploadedComponents = + deployResult.length - rejected.length - skippedComponents; if (rejected.length > 0) { loader.warn('The following errors occured while uploading components:'); rejected.forEach((error) => { log(error, 'red'); }); + } else if (skippedComponents > 0 && uploadedComponents === 0) { + loader.succeed( + `Components up to date (${skippedComponents} unchanged skipped)` + ); + } else if (skippedComponents > 0) { + loader.succeed( + `Uploaded ${uploadedComponents} component(s) (${skippedComponents} unchanged skipped)` + ); } else { loader.succeed(); } diff --git a/tests/components.test.js b/tests/components.test.js index 33eda91..0691bce 100644 --- a/tests/components.test.js +++ b/tests/components.test.js @@ -13,7 +13,11 @@ vi.mock('../src/config.js', () => ({ loadConfig: mocks.loadConfig, })); -import { createComponent } from '../src/actions/components.js'; +import { + createComponent, + hasComponentChanged, + updateComponentFile, +} from '../src/actions/components.js'; const USERS_ME_404 = 'https://api.raisely.com/v3/users/me (404) failed with message: user with uuid 5be64da0-xxxx... was not found'; @@ -179,3 +183,154 @@ describe('createComponent', () => { ); }); }); + +describe('hasComponentChanged', () => { + test('returns false when both latestHtml and editable fields match', () => { + const remote = { + latestHtml: 'export default () => null;', + latestSchema: { + data: { + editable: [{ id: 'title', type: 'text' }], + }, + }, + }; + + expect( + hasComponentChanged(remote, { + file: 'export default () => null;', + config: { fields: [{ id: 'title', type: 'text' }] }, + }) + ).toBe(false); + }); + + test('returns true when latestHtml differs', () => { + const remote = { + latestHtml: 'export default () => "old";', + latestSchema: { + data: { + editable: [{ id: 'title', type: 'text' }], + }, + }, + }; + + expect( + hasComponentChanged(remote, { + file: 'export default () => "new";', + config: { fields: [{ id: 'title', type: 'text' }] }, + }) + ).toBe(true); + }); + + test('returns true when editable fields differ', () => { + const remote = { + latestHtml: 'export default () => null;', + latestSchema: { + data: { + editable: [{ id: 'title', type: 'text' }], + }, + }, + }; + + expect( + hasComponentChanged(remote, { + file: 'export default () => null;', + config: { + fields: [ + { id: 'title', type: 'text' }, + { id: 'subtitle', type: 'text' }, + ], + }, + }) + ).toBe(true); + }); +}); + +describe('updateComponentFile', () => { + beforeEach(() => { + vi.clearAllMocks(); + }); + + test('skips PATCH when skipUnchanged is true and remote matches local', async () => { + mocks.api.mockResolvedValueOnce({ + data: { + uuid: 'comp-1', + latestVersion: 3, + latestHtml: 'export default () => null;', + latestSchema: { + data: { + editable: [{ id: 'title', type: 'text' }], + }, + }, + }, + }); + + const result = await updateComponentFile( + { + file: 'export default () => null;', + config: { + uuid: 'comp-1', + fields: [{ id: 'title', type: 'text' }], + }, + }, + { skipUnchanged: true } + ); + + expect(result).toEqual({ skipped: true }); + expect(mocks.api).toHaveBeenCalledTimes(1); + expect(mocks.api).toHaveBeenCalledWith( + expect.objectContaining({ + path: '/components/comp-1', + }) + ); + }); + + test('sends a single PATCH with latestHtml and editable fields when changed', async () => { + mocks.api + .mockResolvedValueOnce({ + data: { + uuid: 'comp-1', + latestVersion: 3, + latestHtml: 'export default () => "old";', + latestSchema: { + data: { + editable: [{ id: 'title', type: 'text' }], + }, + }, + }, + }) + .mockResolvedValueOnce({ + data: { uuid: 'comp-1', latestVersion: 4 }, + }); + + await updateComponentFile( + { + file: 'export default () => "new";', + config: { + uuid: 'comp-1', + fields: [{ id: 'title', type: 'rich-text' }], + }, + }, + { skipUnchanged: true } + ); + + expect(mocks.api).toHaveBeenCalledTimes(2); + expect(mocks.api).toHaveBeenNthCalledWith( + 2, + expect.objectContaining({ + path: '/components/comp-1?private=1', + method: 'PATCH', + json: { + data: { + latestHtml: 'export default () => "new";', + latestSchema: { + data: { + editable: [{ id: 'title', type: 'rich-text' }], + }, + }, + latestVersion: 4, + }, + }, + }) + ); + }); +}); diff --git a/tests/deploy.test.js b/tests/deploy.test.js index 0a216d7..92107ab 100644 --- a/tests/deploy.test.js +++ b/tests/deploy.test.js @@ -290,8 +290,42 @@ describe('deploy command', () => { expect.stringContaining('.DS_Store'), expect.anything() ); - expect(mocks.updateComponentConfig).toHaveBeenCalledTimes(1); + expect(mocks.updateComponentConfig).not.toHaveBeenCalled(); expect(mocks.updateComponentFile).toHaveBeenCalledTimes(1); + expect(mocks.updateComponentFile).toHaveBeenCalledWith( + { + file: '', + config: {}, + }, + { skipUnchanged: true } + ); + expect(process.exitCode).toBeUndefined(); + }); + + test('reports skipped unchanged components without calling updateComponentConfig', async () => { + mocks.fs.readdirSync.mockImplementation((target, options) => { + if (options && options.withFileTypes) return []; + if (target === '/repo/components') return ['hero', 'footer']; + return []; + }); + mocks.fs.readFileSync.mockImplementation((target) => { + if (String(target).endsWith('.json')) return '{}'; + return 'export default () => null;'; + }); + mocks.updateComponentFile + .mockResolvedValueOnce({ skipped: true }) + .mockResolvedValueOnce({ data: { uuid: 'footer-uuid' } }); + + await deploy({}); + + expect(mocks.updateComponentConfig).not.toHaveBeenCalled(); + expect(mocks.updateComponentFile).toHaveBeenCalledTimes(2); + const componentsLoader = mocks.ora.mock.results.find( + (r) => r.value?.label === 'Uploading components' + )?.value; + expect(componentsLoader.succeed).toHaveBeenCalledWith( + 'Uploaded 1 component(s) (1 unchanged skipped)' + ); expect(process.exitCode).toBeUndefined(); });