1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 | const path = require('path');
|
6 |
|
7 |
|
8 |
|
9 | const pify = require('pify');
|
10 | const mkdirp = require('mkdirp');
|
11 |
|
12 |
|
13 |
|
14 | const api = require('./api');
|
15 | const asyncp = require('./utils/asyncp');
|
16 | const progress = require('./progress');
|
17 | const resource = require('./resource');
|
18 | const values = require('./values');
|
19 |
|
20 |
|
21 |
|
22 | const mkdirpp = pify(mkdirp);
|
23 |
|
24 | function 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 |
|
35 | function 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 |
|
50 | function 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 |
|
59 | function 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 |
|
71 | function 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 |
|
86 | module.exports = {
|
87 | pullAll
|
88 | };
|