forked from datamadic/openfin-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
137 lines (118 loc) · 3.64 KB
/
index.js
File metadata and controls
137 lines (118 loc) · 3.64 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict';
var configBuilder = require('openfin-config-builder'),
{ launch, connect } = require('hadouken-js-adapter'),
path = require('path'),
fs = require('fs'),
request = require('request'),
parseURLOrFile = require('./parse-url-or-file'),
meow;
function main(cli) {
meow = cli;
var flags = cli.flags,
name = flags.n || flags.name,
url = flags.u || flags.url,
config = flags.c || flags.config || 'app.json',
launch = flags.l || flags.launch,
devtools_port = flags.p || flags.devtoolsPort,
runtime_version = flags.v || flags.runtimeVersion,
parsedUrl = url ? parseURLOrFile(url) : url;
if (isEmpty(flags)) {
console.log(cli.help);
return;
}
try {
writeToConfig(name, parsedUrl, config, devtools_port, runtime_version, function (configObj) {
if (launch) launchOpenfin(config);
});
} catch (err) {
onError('Failed:', err);
}
}
function isURL(str) {
return (typeof str === 'string') && str.lastIndexOf('http') >= 0;
}
//makeshift is object empty function
function isEmpty(flags) {
for (var key in flags) {
if (flags.hasOwnProperty(key) && flags[key] !== false) {
return false;
}
}
return true;
}
function onError(message, err) {
console.log(message, err);
console.log(meow.help);
}
//will launch download the rvm and launch openfin
async function launchOpenfin(config) {
try {
const manifestUrl = isURL(config) ? config : path.resolve(config);
const port = await launch({ manifestUrl, installerUI: true });
const fin = await connect({
uuid: 'openfin-cli-server-connection',
address: `ws://localhost:${port}`,
nonPersistent: true,
});
fin.once('disconnected', process.exit);
} catch (err) {
console.error(err);
}
}
//write the specified config to disk.
function writeToConfig(name, url, config, devtools_port, runtime_version, callback) {
if (isURL(config)) {
request(config, function (err, response, body) {
if (!err && response.statusCode === 200) {
callback(JSON.parse(body));
}
});
return;
}
var shortcut = {},
startup_app = {},
runtime = {},
configAction,
actionMessage;
fs.exists(config, function (exists) {
if (exists) {
configAction = configBuilder.update;
actionMessage = 'using config';
} else {
configAction = configBuilder.create;
actionMessage = 'successfully created config';
}
if (config) {
if (name) {
startup_app.name = name;
shortcut.name = name;
shortcut.company = name;
shortcut.description = name;
}
if (url) {
startup_app.url = url;
}
if (runtime_version) {
runtime.version = runtime_version;
}
}
var appConfigObj = {
startup_app: url ? startup_app : null,
shortcut: shortcut
};
if (devtools_port) {
appConfigObj.devtools_port = devtools_port;
}
if (runtime_version) {
appConfigObj.runtime = runtime;
}
//create or update the config
configAction(appConfigObj, config).catch(function (err) {
console.log(err);
}).then(function (configObj) {
console.log(actionMessage, path.resolve(config));
callback(configObj);
});
});
}
module.exports = main;