-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathhost-manager.js
More file actions
203 lines (168 loc) · 4.96 KB
/
host-manager.js
File metadata and controls
203 lines (168 loc) · 4.96 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
'use strict';
const fs = require('fs')
const os = require('os');
const path = require('path');
const chalk = require('chalk');
const Table = require('cli-table')
const Config = require('../lib/config')
const esh = require('eshost');
const head = [
'name',
'type',
'path',
'args',
'tags',
];
const colWidths = head.map(h => h.length + 2);
exports.list = ({configPath, hosts}) => {
const table = new Table({
colWidths, head
});
const names = Object.keys(hosts);
let output;
if (!names.length) {
output = 'No configured hosts';
} else {
output = names.reduce((accum, name) => {
const {
type,
path = '',
args = '',
tags = ''
} = hosts[name];
const row = [ name, type, path, args, tags ];
const {colWidths: widths} = accum.options;
// Update the stored colWidths for each cell, saving the largest/longest
accum.options.colWidths = Array.from(widths, (width, index) => Math.max(width, String(row[index]).length + 2));
accum.push(row);
return accum;
}, table).sort().toString();
}
console.log(output);
};
exports.add = (config, hostName, hostType, hostPath, hostArgs, hostTags) => {
if (config.hosts[hostName]) {
console.log(chalk.red(`Host "${hostName}" already exists`));
return;
}
if (!esh.supportedHosts.includes(hostType)) {
console.log(
chalk.red(
`Host type "${hostType}" not supported. Supported host types are: "${esh.supportedHosts.join(", ")}"`
)
);
return;
}
if (hostPath && !path.isAbsolute(hostPath)) {
hostPath = path.join(process.cwd(), hostPath);
}
config.hosts[hostName] = {
type: hostType,
path: hostPath,
args: hostArgs,
tags: hostTags,
};
config.save();
};
exports.edit = (config, hostName, hostArgs, hostTags) => {
if (!config.hosts[hostName]) {
console.log(chalk.red(`Host "${hostName}" does not exist`));
process.exit(1);
return;
}
if (hostArgs) {
config.hosts[hostName].args = hostArgs;
}
if (hostTags) {
config.hosts[hostName].tags = hostTags;
}
config.save();
};
exports.delete = (config, hostName) => {
if (!hostName) {
Object.keys(config.hosts).forEach(hostName => exports.delete(config, hostName));
} else {
if (config.hosts[hostName]) {
delete config.hosts[hostName];
config.save();
console.log(`Host "${hostName}" removed`);
} else {
console.log(`Host "${hostName}" not found`);
}
}
};
const VU_BIN_PATH = {
esvu: 'bin',
jsvu: 'bin',
};
const VU_HOST_DETAILS = {
/*
hostName
binaryName
hostType
hostTags
*/
esvu: {
ch: ['ChakraCore', 'chakra', 'ch', ['web']],
engine262: ['engine262', 'engine262', 'engine262', ['']],
graaljs: ['GraalJS', 'graaljs', 'graaljs', ['']],
hermes: ['Hermes', 'hermes', 'hermes', ['']],
jsc: ['JavaScriptCore', 'jsc', 'jsc', ['web']],
jsshell: ['SpiderMonkey', 'sm', 'jsshell', ['web']],
qjs: ['QuickJS', 'quickjs-run-test262', 'qjs', ['embedded']],
v8: ['V8', 'v8', 'd8', ['web']],
xs: ['Moddable XS', 'xs', 'xs', ['embedded']],
},
jsvu: {
chakra: ['ChakraCore', 'chakra', 'ch', ['web']],
hermes: ['Hermes', 'hermes', 'hermes', ['']],
javascriptcore: ['JavaScriptCore', 'jsc', 'jsc', ['web']],
// This version of quickjs does not include the necessary
// host defined environment extensions to execute code in
// eshost.
quickjs: ['QuickJS', 'quickjs', ['']],
spidermonkey: ['SpiderMonkey', 'sm', 'jsshell', ['web']],
v8: ['V8', 'v8', 'd8', ['web']],
xs: ['Moddable XS', 'xs', 'xs', ['embedded']],
}
};
exports.configureFromVersionUpdater = (config, vu, vuRoot, hostPrefix = '') => {
if (!fs.existsSync(vuRoot)) {
throw Error(`Version Updater "${vu}" config root "${vuRoot}" not found`);
}
const status = require(path.join(vuRoot, 'status.json'));
const engines = status[vu === 'esvu' ? 'selectedEngines' : 'engines'];
let extension = '';
if (os.platform() === 'win32') {
extension = '.cmd';
}
for (let engine of engines) {
if (!VU_HOST_DETAILS[vu] ||
(VU_HOST_DETAILS[vu] && !VU_HOST_DETAILS[vu][engine])) {
continue;
}
let [
hostName,
binaryName,
hostType,
hostTags
] = VU_HOST_DETAILS[vu][engine];
if (!hostName) {
continue;
}
const hostPath = path.resolve(
vuRoot, VU_BIN_PATH[vu], binaryName + extension
);
if (!fs.existsSync(hostPath)) {
console.log(chalk.red(`"${hostName}" could not be configured because ${hostPath} was not found.`));
continue;
}
const version = status[engine] || (status.installed[engine] && status.installed[engine].version);
if (hostPrefix) {
hostName = `${hostPrefix}-${hostName}`;
}
hostTags.unshift(version);
console.log(`Configuring ${vu} host with name "${hostName}" (type "${hostType}") at "${hostPath}"`);
exports.add(config, hostName, hostType, hostPath, undefined, hostTags.filter(Boolean));
}
};