Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/cmd/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const _ = require('lodash'),
h = require('highland'),
yaml = require('js-yaml'),
utils = require('clayutils'),
split = require('split-lines'),
formatting = require('../formatting'),
prefixes = require('../prefixes'),
Expand All @@ -19,6 +20,19 @@ function isURI(url) {
return _.includes(url, 'uris/');
}

/**
* Decides which dispatches the publish option publishes directly.
*
* amphora's page publish creates the page's component @published versions itself; a solo
* component publish has no page context (no publishUrl), so it writes draft-shaped published
* data and races the page publish for last write
* @param {string} url
* @return {Boolean}
*/
function isPublishable(url) {
return utils.isPage(url) || utils.isLayout(url);
}

function sendDispatchToClay(dispatch, prefix, key, options) {
const rootURI = Object.keys(dispatch)[0],
url = prefixes.uriToUrl(prefix, rootURI),
Expand All @@ -33,9 +47,11 @@ function sendDispatchToClay(dispatch, prefix, key, options) {
// user wants to publish something, but isn't importing latest data
// so make it for them and add a warning
return h(rest.put(url.replace('@published', ''), data, { key }).toPromise(Promise).then((res) => {
return h.of(res).concat(rest.put(url, undefined, { key })).concat(h.of({ type: 'warning', message: 'Generated latest data for @published item', details: url }));
const published = isPublishable(url) ? rest.put(url, undefined, { key }) : h([]);

return h.of(res).concat(published).concat(h.of({ type: 'warning', message: 'Generated latest data for @published item', details: url }));
})).flatten();
} else if (options.publish) {
} else if (options.publish && isPublishable(url)) {
// PUT to latest and then PUT to @published
return h(rest.put(url, data, { key }).toPromise(Promise).then((res) => {
return h.of(res).concat(rest.put(`${url}@published`, undefined, { key }));
Expand Down
27 changes: 23 additions & 4 deletions lib/cmd/import.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,17 @@ describe('import', () => {
});

it('adds warning when importing @published item', () => {
fetch.mockResponseOnce('{}');
fetch.mockResponseOnce('{}');
return lib(h.of(yaml.dump({ _components: { a: { instances: { 'b@published': { c: 'd' }} }} })), url, { yaml: true, publish: true, key, concurrency }).collect().toPromise(Promise).then((res) => {
expect(res).toEqual([{ type: 'success', message: 'http://domain.com/_components/a/instances/b' }, { type: 'success', message: 'http://domain.com/_components/a/instances/b@published' }, { type: 'warning', message: 'Generated latest data for @published item', details: 'http://domain.com/_components/a/instances/b@published' }]);
expect(res).toEqual([{ type: 'success', message: 'http://domain.com/_components/a/instances/b' }, { type: 'warning', message: 'Generated latest data for @published item', details: 'http://domain.com/_components/a/instances/b@published' }]);
});
});

it('publishes @published page dispatches after generating latest', () => {
fetch.mockResponseOnce('{}');
fetch.mockResponseOnce('{}');
return lib(h.of(yaml.dump({ _pages: { 'foo@published': { layout: 'domain.com/_layouts/l/instances/a', main: ['domain.com/_components/a/instances/b'] }} })), url, { yaml: true, publish: true, key, concurrency }).collect().toPromise(Promise).then((res) => {
expect(res).toEqual([{ type: 'success', message: 'http://domain.com/_pages/foo' }, { type: 'success', message: 'http://domain.com/_pages/foo@published' }, { type: 'warning', message: 'Generated latest data for @published item', details: 'http://domain.com/_pages/foo@published' }]);
});
});

Expand Down Expand Up @@ -149,8 +156,20 @@ describe('import', () => {
});
});

it('publishes items', () => {
it('publishes page dispatches', () => {
fetch.mockResponseOnce('{}');
fetch.mockResponseOnce('{}');
return lib(JSON.stringify({
'/_pages/foo': {
layout: '/_layouts/layout/instances/default',
main: ['/_components/article/instances/foo']
}
}), url, { key, concurrency, publish: true }).collect().toPromise(Promise).then((res) => {
expect(res).toEqual([{ type: 'success', message: 'http://domain.com/_pages/foo' }, { type: 'success', message: 'http://domain.com/_pages/foo@published' }]);
});
});

it('does not solo-publish component dispatches', () => {
fetch.mockResponseOnce('{}');
return lib(JSON.stringify({
'/_components/article/instances/foo': {
Expand All @@ -161,7 +180,7 @@ describe('import', () => {
}]
}
}), url, { key, concurrency, publish: true }).collect().toPromise(Promise).then((res) => {
expect(res).toEqual([{ type: 'success', message: 'http://domain.com/_components/article/instances/foo' }, { type: 'success', message: 'http://domain.com/_components/article/instances/foo@published' }]);
expect(res).toEqual([{ type: 'success', message: 'http://domain.com/_components/article/instances/foo' }]);
});
});

Expand Down
Loading