Skip to content

Commit

Permalink
fix import by making support more explicit (#92)
Browse files Browse the repository at this point in the history
* fix import by making support more explicit

* pulled out complex function
  • Loading branch information
nelsonpecora authored Sep 4, 2018
1 parent 4ae8b2e commit 0b8fa3f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
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

0 comments on commit 0b8fa3f

Please sign in to comment.