1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 | const fs = require('fs');
|
6 | const path = require('path');
|
7 |
|
8 |
|
9 |
|
10 | const mkdirp = require('mkdirp');
|
11 | const pify = require('pify');
|
12 |
|
13 |
|
14 |
|
15 | const deploy = require('./deploy');
|
16 | const resource = require('./resource');
|
17 | const promise = require('./utils/promise');
|
18 |
|
19 |
|
20 |
|
21 | const fsp = pify(fs);
|
22 | const mkdirpp = pify(mkdirp);
|
23 |
|
24 | function assertUniqueInteractionName (options) {
|
25 | const interactionPath = options.interactionPath;
|
26 | const name = options.name;
|
27 | return promise.reverse(fsp.access(interactionPath, fs.F_OK))
|
28 | .catch(() => {
|
29 | throw new Error(`interaction "${name}" already exists`);
|
30 | });
|
31 | }
|
32 |
|
33 | function newInteraction (options) {
|
34 | const cwd = process.env.BMP_WORKING_DIR || process.cwd();
|
35 | const name = options.name;
|
36 | const interactionPath = path.join(cwd, 'interactions', name);
|
37 | const type = options.type;
|
38 |
|
39 | return assertUniqueInteractionName({ interactionPath, name })
|
40 | .then(() => mkdirpp(interactionPath))
|
41 |
|
42 | .then(() => resource.writeInteraction({
|
43 | cwd,
|
44 | data: resource.newInteraction({ name, type }),
|
45 | name
|
46 | }))
|
47 | .then(() => {
|
48 | if (options.remote) {
|
49 | return deploy.deployInteraction({ cwd, name });
|
50 | }
|
51 | });
|
52 | }
|
53 |
|
54 | module.exports = {
|
55 | newInteraction
|
56 | };
|