Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix import by making support more explicit #92

Merged
merged 2 commits into from
Sep 4, 2018
Merged
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
28 changes: 25 additions & 3 deletions lib/cmd/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,31 @@ function importDispatch(obj, prefix, key, options) {
.flatMap((dispatch) => sendDispatchToClay(dispatch, prefix, key, options));
}

/**
* format the source when parsing dispatches
* @param {string|Buffer|object|Stream} source
* @return {Stream}
*/
function formatDispatchSource(source) {
if (_.isString(source)) {
// handle importing from strings, e.g. when piping files into claycli
return h.of(source).split().compact();
} else if (Buffer.isBuffer(source)) {
// handle importing from buffers, e.g. when piping claycli export into claycli import
return h.of(source.toString('utf8')).split().compact();
} else if (_.isObject(source) && typeof source.pipe !== 'function') {
// handle importing from objects (that aren't streams), e.g. when programmatically piping claycli export into claycli import
// note: we're duck-typing for streams by checking if it has a .pipe function
return h.of(source);
} else {
// handle importing from streams of strings
return h(source).split().compact();
}
}

/**
* import data into clay
* @param {string|Stream} str (stream of) bootstraps or dispatches
* @param {string|object|Buffer|Stream} str (stream of) bootstraps or dispatches
* @param {string} url to import to (must be a site prefix)
* @param {Object} [options={}]
* @param {string} [options.key] api key or alias
Expand Down Expand Up @@ -141,13 +163,13 @@ function importItems(str, url, options = {}) {
}).parallel(concurrency);
} else {
// parse dispatches
return (_.isString(str) ? h.of(str).split().compact() : h(str))
return formatDispatchSource(str)
.map((res) => {
try {
return _.isString(res) ? JSON.parse(res) : res; // allow strings or objects
} catch (e) {
try {
yaml.safeLoad(str);
yaml.safeLoad(res);
// user accidentally tried to import dispatches from a yaml file!
return { type: 'error', message: 'Cannot import dispatch from yaml', details: 'Please use the --yaml argument to import from bootstraps' };
} catch (otherE) {
Expand Down
18 changes: 16 additions & 2 deletions lib/cmd/import.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,23 @@ describe('import', () => {
});
});

it('imports dispatch from stream of objects', () => {
it('imports dispatch from buffer', () => {
fetch.mockResponseOnce('{}');
return lib(h.of({ '/_components/obj': { b: 'c' } }), url, { key, concurrency }).collect().toPromise(Promise).then((res) => {
return lib(Buffer.from(JSON.stringify({ '/_components/a': { b: 'c' } })), url, { key, concurrency }).collect().toPromise(Promise).then((res) => {
expect(res).toEqual([{ type: 'success', message: 'http://domain.com/_components/a' }]);
});
});

it('imports dispatch from string', () => {
fetch.mockResponseOnce('{}');
return lib(JSON.stringify({ '/_components/a': { b: 'c' } }), url, { key, concurrency }).collect().toPromise(Promise).then((res) => {
expect(res).toEqual([{ type: 'success', message: 'http://domain.com/_components/a' }]);
});
});

it('imports dispatch from object', () => {
fetch.mockResponseOnce('{}');
return lib({ '/_components/obj': { b: 'c' } }, url, { key, concurrency }).collect().toPromise(Promise).then((res) => {
expect(res).toEqual([{ type: 'success', message: 'http://domain.com/_components/obj' }]);
});
});
Expand Down