1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 | const fs = require('fs');
|
6 | const path = require('path');
|
7 |
|
8 |
|
9 |
|
10 | const memoize = require('lodash.memoize');
|
11 | const pify = require('pify');
|
12 |
|
13 |
|
14 |
|
15 | const api = require('./api');
|
16 | const asyncp = require('./utils/asyncp');
|
17 | const progress = require('./progress');
|
18 | const resource = require('./resource');
|
19 | const values = require('./values');
|
20 |
|
21 |
|
22 |
|
23 | const fsp = pify(fs);
|
24 |
|
25 | const readCachedAnswerSpace = memoize(resource.readAnswerSpace);
|
26 |
|
27 | function deployAnswerSpace (options) {
|
28 | return readCachedAnswerSpace(options)
|
29 | .then((data) => api.putResource({ id: data.id, type: 'answerspaces', data }));
|
30 | }
|
31 |
|
32 | function deployInteraction (options) {
|
33 | let isNew;
|
34 | return Promise.all([
|
35 | readCachedAnswerSpace({ cwd: options.cwd }),
|
36 | resource.readInteraction(options)
|
37 | ])
|
38 | .then((results) => {
|
39 | const answerSpace = results[0];
|
40 | const data = results[1];
|
41 | isNew = !data.id;
|
42 | data.links = { answerspaces: answerSpace.id };
|
43 | return api.putResource({ id: data.id, type: 'interactions', data });
|
44 | })
|
45 | .then((result) => {
|
46 | if (isNew) {
|
47 | return resource.writeInteraction({
|
48 | cwd: options.cwd,
|
49 | data: result.interactions,
|
50 | name: result.interactions.name
|
51 | });
|
52 | }
|
53 | });
|
54 | }
|
55 |
|
56 | function deployInteractions (options) {
|
57 | const cwd = options.cwd;
|
58 | return fsp.readdir(path.join(cwd, 'interactions'))
|
59 | .catch((err) => {
|
60 | if (err.code === 'ENOENT') {
|
61 |
|
62 | return [];
|
63 | }
|
64 | throw err;
|
65 | })
|
66 | .then((names) => {
|
67 | progress.addWork(names.length);
|
68 | return names;
|
69 | })
|
70 | .then((names) => asyncp.eachLimit(names, values.MAX_REQUESTS, asyncp.asyncify((name) => {
|
71 | return deployInteraction({ cwd, name })
|
72 | .then(() => progress.completeWork(1));
|
73 | })));
|
74 | }
|
75 |
|
76 | function deployAll (options) {
|
77 | options = options || {};
|
78 | const cwd = process.env.BMP_WORKING_DIR || process.cwd();
|
79 |
|
80 | return Promise.all([
|
81 | deployAnswerSpace({ cwd }),
|
82 | deployInteractions({ cwd })
|
83 | ])
|
84 | .then(() => {
|
85 | if (options.prune) {
|
86 | return pruneInteractions({ cwd });
|
87 | }
|
88 | });
|
89 | }
|
90 |
|
91 | function pruneInteractions (options) {
|
92 | return api.getDashboard()
|
93 | .then((obj) => obj.answerSpace.id)
|
94 | .then((id) => Promise.all([
|
95 | api.getResource({ id, type: 'answerspaces' }),
|
96 | resource.listInteractions({ cwd: options.cwd })
|
97 | ]))
|
98 | .then((results) => {
|
99 | const remoteIds = results[0].answerspaces.links.interactions || [];
|
100 | const localIds = results[1].map((i) => i.id);
|
101 | const pruneIds = remoteIds.filter((id) => localIds.indexOf('' + id) === -1);
|
102 | progress.addWork(pruneIds.length);
|
103 | return pruneIds;
|
104 | })
|
105 | .then((pruneIds) => asyncp.eachLimit(pruneIds, values.MAX_REQUESTS, asyncp.asyncify((id) => {
|
106 | return api.deleteResource({ id, type: 'interactions' })
|
107 | .then(() => progress.completeWork(1));
|
108 | })));
|
109 | }
|
110 |
|
111 | module.exports = {
|
112 | deployAll,
|
113 | deployInteraction
|
114 | };
|