UNPKG

3.19 kBJavaScriptView Raw
1'use strict';
2
3// local modules
4
5const api = require('./api');
6const asyncp = require('./utils/asyncp');
7const progress = require('./progress');
8const resource = require('./resource');
9const scope = require('./scope');
10const validate = require('./validate');
11const values = require('./values');
12
13// this module
14
15function deployAnswerSpace (options) {
16 return resource.readCachedAnswerSpace(options)
17 .then((data) => api.putResource({
18 data,
19 type: 'answerspaces',
20 uid: data.name
21 }));
22}
23
24function 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
53function 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
69function 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
86function 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
110function wipeSiteMap (options) {
111 return resource.readCachedAnswerSpace(options)
112 .then((data) => api.wipeSiteMap({
113 data,
114 type: 'answerspaces',
115 uid: data.name
116 }));
117}
118
119module.exports = {
120 deployAll,
121 deployInteraction,
122 wipeSiteMap
123};