This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
executable file
·303 lines (266 loc) · 7.44 KB
/
index.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env node
process.on('unhandledRejection', err => { throw err; });
process.on('SIGINT', () => process.exit());
process.on('SIGTERM', () => process.exit());
const path = require('path');
const commandLineCommands = require('command-line-commands');
const commandLineUsage = require('command-line-usage');
const fs = require('fs-extra');
const logger = require('loggy');
process.on('exit', () => process.exit(logger.errorHappened ? 1 : 0));
const commands = require('./commands/index.js');
const parseOptions = require('./lib/parseCli.js').parseOptions;
const pkgJson = require('./package.json');
const SETTINGS = require('./lib/settings.js').settings;
const utils = require('./lib/utils.js');
const pluralise = utils.pluralise;
const uppercaseFirstLetter = utils.uppercaseFirstLetter;
const validCommands = [null, 'download', 'install', 'launch', 'test', 'version', 'help'];
switch (process.argv[2]) {
case 'fetch':
process.argv[2] = 'download';
break;
case 'update':
case 'upgrade':
process.argv[2] = 'install';
break;
case 'open':
case 'run':
case 'serve':
case 'server':
case 'dev':
case 'url':
process.argv[2] = 'launch';
break;
case 'tests':
case 't':
process.argv[2] = 'test';
break;
}
let parsedCommands = {};
try {
parsedCommands = commandLineCommands(validCommands);
} catch (err) {
if (err.name === 'INVALID_COMMAND') {
help();
process.exit(1);
} else {
throw err;
}
}
const command = parsedCommands.command;
const argv = parsedCommands.argv;
function version () {
console.log(pkgJson.version);
process.exit();
}
function getHeaderLogo () {
const concat = require('concat-stream');
const pictureTube = require('picture-tube');
return new Promise((resolve, reject) => {
const imgPath = path.join(__dirname, 'assets', 'img', 'ff_logo.png');
const imgStream = fs.createReadStream(imgPath).pipe(pictureTube({cols: 30}));
const concatStream = concat(imgBuffer => {
resolve(imgBuffer.toString());
});
imgStream.on('error', reject);
imgStream.pipe(concatStream);
});
}
function help () {
const chalk = require('chalk');
const binName = pkgJson.libraryName || pkgJson.productName || Object.keys(pkgJson.bin)[0];
const binStr = chalk.cyan.bold(binName);
let logoContent = '';
let bulletCounters = {
examples: 0
};
let links = [];
if (pkgJson.homepage) {
links.push({name: 'Project homepage', summary: link(pkgJson.homepage)});
}
if (typeof pkgJson.bugs === 'string') {
pkgJson.bugs = {url: pkgJson.bugs};
}
if (pkgJson.bugs && pkgJson.bugs.url) {
links.push({name: 'File an issue', summary: link(pkgJson.bugs.url)});
}
return getHeaderLogo().then(content => {
logoContent = content;
displayUsage();
}).catch(() => {
displayUsage();
});
function link (url) {
return chalk.underline(url);
}
function bullet (type, str) {
return `${++bulletCounters[type]}. ${str}`;
}
function cmd (cmdName) {
return chalk.magenta(cmdName);
}
function displayUsage () {
const chalk = require('chalk');
const sections = [
{
content: logoContent,
raw: true
},
{
header: binName,
content: `${binStr} is a command-line tool for installing and automating the Firefox Reality virtual-reality browser.`
},
{
header: 'Usage',
content: `$ ${binStr} ${cmd('<command>')} ${chalk.blue('[options]')}`
},
{
header: 'Commands',
content: [
{name: cmd('install'), summary: 'Install Firefox Reality to your device.'},
{name: cmd('launch'), summary: 'Launch a URL in Firefox Reality.'},
{name: cmd('version'), summary: 'Output the version number.'},
{name: cmd('help'), summary: 'Output the usage information.'}
]
},
{
header: 'Examples',
content: [
{
desc: bullet('examples', 'Install Firefox Reality to your device.'),
example: `$ ${binStr} ${cmd('install')}`
},
{
desc: bullet('examples', 'Launch a URL in Firefox Reality.'),
example: `$ ${binStr} ${cmd('launch')} "http://example.com/"`
},
// TODO: See https://github.com/MozillaReality/fxr-cli/issues/5
// {
// desc: bullet('examples', 'Launch a local project in Firefox Reality.'),
// example: `$ ${binStr} ${cmd('launch')} path/to/project/`
// }
]
},
{
header: 'Links',
content: links
}
];
const usage = commandLineUsage(sections);
console.log(usage);
}
}
function download () {
return platformAction('download');
}
function install () {
return platformAction('install');
}
function launch (url) {
return platformAction('launch', url);
}
function test (url) {
return platformAction('test', null);
}
function platformAction (action, url, defaults = {}) {
return new Promise((resolve, reject) => {
if (!('indent' in defaults)) {
defaults.indent = 2;
}
const options = parseOptions(action, url, defaults);
const actionStr = action;
let actionPresentStr;
let actionPastStr;
let displayLogBefore = true;
let displayLogAfter = true;
let displayError = true;
switch (action) {
case 'download':
actionPresentStr = 'downloading';
actionPastStr = 'downloaded';
displayLogAfter = false;
break;
case 'install':
actionPresentStr = 'installing';
actionPastStr = 'installed';
displayLogAfter = false;
break;
case 'launch':
actionPresentStr = 'launching';
actionPastStr = 'launched';
displayLogAfter = false;
break;
case 'test':
actionPresentStr = 'testing';
actionPastStr = 'tested';
displayLogAfter = false;
break;
}
const platformStr = pluralise('platform', 'platforms', options.platformsSlugs.length);
const platformListStr = options.platformsSlugs.join('", "');
const platform = options.platformsSlugs[0];
const loggerPlatform = (str, level) => utils.loggerPlatform(platform, str, level);
if (displayLogBefore) {
loggerPlatform(uppercaseFirstLetter(actionPresentStr));
}
return commands[action].run({
platformsSlugs: options.platformsSlugs,
forceUpdate: options.forceUpdate,
url: options.url,
org: options.org || SETTINGS.github_org,
repo: options.repo || SETTINGS.github_repo,
indent: options.indent || ''
}).then(completed => {
if (action === 'test') {
return;
}
if (completed) {
if (displayLogAfter) {
loggerPlatform(uppercaseFirstLetter(actionPastStr));
}
}
}).catch(err => {
if (displayError) {
if (err) {
loggerPlatform(`Could not ${actionStr}: ${err}`, 'error');
} else {
loggerPlatform(`Could not ${actionStr}`, 'error');
}
} else {
throw err;
}
});
}).catch(err => {
logger.error(err);
});
}
switch (command) {
case 'download':
download();
break;
case 'install':
install();
break;
case 'launch':
launch();
break;
case 'test':
test();
break;
case 'help':
help();
break;
case 'version':
version();
break;
default:
if (argv.includes('-v') ||
argv.includes('--v') ||
argv.includes('--version')) {
version();
break;
}
help();
break;
}