UNPKG

2.37 kBJavaScriptView Raw
1'use strict';
2
3// Node.js built-ins
4
5const path = require('path');
6
7// foreign modules
8
9const pify = require('pify');
10const mkdirp = require('mkdirp');
11
12// local modules
13
14const api = require('./api');
15const asyncp = require('./utils/asyncp');
16const progress = require('./progress');
17const resource = require('./resource');
18const values = require('./values');
19
20// this module
21
22const mkdirpp = pify(mkdirp);
23
24function pruneInteractions (options) {
25 const ids = options.interactions;
26 return resource.listInteractions({ cwd: options.cwd })
27 .then((interactions) => {
28 return interactions.filter((interaction) => !~ids.indexOf(interaction.id));
29 })
30 .then((interactions) => asyncp.eachSeries(interactions, asyncp.asyncify((i) => {
31 return resource.deleteInteraction({ cwd: options.cwd, name: i.name });
32 })));
33}
34
35function pullAnswerSpace (options) {
36 let interactions;
37 return api.getDashboard()
38 .then((obj) => obj.answerSpace.id)
39 .then((id) => api.getResource({ id, type: 'answerspaces' }))
40 .then((result) => {
41 interactions = result.answerspaces.links.interactions || [];
42 return resource.writeAnswerSpace({
43 cwd: options.cwd,
44 data: result.answerspaces
45 });
46 })
47 .then(() => interactions);
48}
49
50function pullInteraction (options) {
51 return api.getResource({ id: options.id, type: 'interactions' })
52 .then((result) => resource.writeInteraction({
53 cwd: options.cwd,
54 data: result.interactions,
55 name: result.interactions.name
56 }));
57}
58
59function pullInteractions (options) {
60 const cwd = options.cwd;
61 const interactions = options.interactions;
62
63 progress.addWork(interactions.length);
64 return mkdirpp(path.join(cwd, 'interactions'))
65 .then(() => asyncp.eachLimit(interactions, values.MAX_REQUESTS, asyncp.asyncify((id) => {
66 return pullInteraction({ cwd, id })
67 .then(() => progress.completeWork(1));
68 })));
69}
70
71function pullAll (options) {
72 options = options || {};
73 const cwd = process.env.BMP_WORKING_DIR || process.cwd();
74
75 return pullAnswerSpace({ cwd })
76 .then((interactions) => {
77 if (options.prune) {
78 return pruneInteractions({ cwd, interactions })
79 .then(() => interactions);
80 }
81 return interactions;
82 })
83 .then((interactions) => pullInteractions({ cwd, interactions }));
84}
85
86module.exports = {
87 pullAll
88};