This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
/
forms.js
81 lines (73 loc) · 2.33 KB
/
forms.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Generated by CoffeeScript 1.11.1
var Promise, SpinnerPromise, _, discoverLocalResinOsDevices, form;
Promise = require('bluebird');
_ = require('lodash');
form = require('resin-cli-form');
discoverLocalResinOsDevices = require('./discover').discoverLocalResinOsDevices;
SpinnerPromise = require('resin-cli-visuals').SpinnerPromise;
exports.selectAppName = function(preferredAppName) {
var validateAppName;
validateAppName = Promise.method(function(appName) {
var hasValidChars, validCharsRegExp;
validCharsRegExp = new RegExp('^[a-z0-9-]+$');
if (_.isEmpty(appName)) {
throw new Error('Application name should not be empty.');
}
hasValidChars = validCharsRegExp.test(appName);
if (!hasValidChars || _.startsWith(appName, '-') || _.endsWith(appName, '-')) {
throw new Error('Application name may only contain lowercase characters, digits and one or more dashes. It may not start or end with a dash.');
}
return appName;
});
return form.run([
{
message: 'Select a name for the application',
name: 'appname',
type: 'input'
}
], {
override: {
appname: preferredAppName
}
}).get('appname').call('trim').then(validateAppName);
};
exports.selectSyncDestination = function(preferredDestination) {
return form.run([
{
message: 'Destination directory on device container [/usr/src/app]',
name: 'destination',
type: 'input'
}
], {
override: {
destination: preferredDestination
}
}).get('destination').then(function(destination) {
return destination != null ? destination : '/usr/src/app';
});
};
exports.selectLocalResinOsDevice = function(timeout) {
if (timeout == null) {
timeout = 4000;
}
return new SpinnerPromise({
promise: discoverLocalResinOsDevices(timeout),
startMessage: 'Discovering local resinOS devices..',
stopMessage: 'Reporting discovered devices'
}).then(function(devices) {
if (_.isEmpty(devices)) {
throw new Error('Could not find any local resinOS devices');
}
return form.ask({
message: 'select a device',
type: 'list',
"default": devices[0].ip,
choices: _.map(devices, function(device) {
return {
name: (device.host || 'untitled') + " (" + device.address + ")",
value: device.address
};
})
});
});
};