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