UNPKG

1.33 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 mkdirp = require('mkdirp');
11const pify = require('pify');
12
13// local modules
14
15const deploy = require('./deploy');
16const resource = require('./resource');
17const promise = require('./utils/promise');
18
19// this module
20
21const fsp = pify(fs);
22const mkdirpp = pify(mkdirp);
23
24function 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
33function 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 // follow mostly the same process as in pull
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
54module.exports = {
55 newInteraction
56};