UNPKG

3.05 kBJavaScriptView Raw
1'use strict';
2
3// Node.js built-ins
4
5const fs = require('fs');
6const path = require('path');
7
8// foreign modules
9
10const memoize = require('lodash.memoize');
11const pify = require('pify');
12
13// local modules
14
15const api = require('./api');
16const asyncp = require('./utils/asyncp');
17const progress = require('./progress');
18const resource = require('./resource');
19const values = require('./values');
20
21// this module
22
23const fsp = pify(fs);
24
25const readCachedAnswerSpace = memoize(resource.readAnswerSpace);
26
27function deployAnswerSpace (options) {
28 return readCachedAnswerSpace(options)
29 .then((data) => api.putResource({ id: data.id, type: 'answerspaces', data }));
30}
31
32function 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
56function deployInteractions (options) {
57 const cwd = options.cwd;
58 return fsp.readdir(path.join(cwd, 'interactions'))
59 .catch((err) => {
60 if (err.code === 'ENOENT') {
61 // it's not an error condition if we don't have any interactions
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
76function 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
91function 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
111module.exports = {
112 deployAll,
113 deployInteraction
114};