1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.run = void 0;
|
4 | const cli_framework_1 = require("@ionic/cli-framework");
|
5 | const utils_fs_1 = require("@ionic/utils-fs");
|
6 | const chalk = require("chalk");
|
7 | const express = require("express");
|
8 | const http = require("http");
|
9 | const https = require("https");
|
10 | const path = require("path");
|
11 | const WWW_DIRECTORY = path.join(__dirname, '..', 'www');
|
12 | class DefaultCommand extends cli_framework_1.Command {
|
13 | async getMetadata() {
|
14 | return {
|
15 | name: 'default',
|
16 | summary: '',
|
17 | inputs: [
|
18 | {
|
19 | name: 'url',
|
20 | summary: 'The URL of the livereload server to use with lab',
|
21 | validators: [cli_framework_1.validators.required, cli_framework_1.validators.url],
|
22 | },
|
23 | ],
|
24 | options: [
|
25 | {
|
26 | name: 'host',
|
27 | summary: 'HTTP host of Ionic Lab',
|
28 | default: 'localhost',
|
29 | },
|
30 | {
|
31 | name: 'port',
|
32 | summary: 'HTTP port of Ionic Lab',
|
33 | default: '8200',
|
34 | },
|
35 | {
|
36 | name: 'project-type',
|
37 | summary: 'Ionic project type',
|
38 | },
|
39 | {
|
40 | name: 'ssl',
|
41 | summary: 'Host Ionic Lab with HTTPS',
|
42 | type: Boolean,
|
43 | },
|
44 | {
|
45 | name: 'ssl-key',
|
46 | summary: 'Path to SSL key',
|
47 | },
|
48 | {
|
49 | name: 'ssl-cert',
|
50 | summary: 'Path to SSL certificate',
|
51 | },
|
52 | {
|
53 | name: 'app-name',
|
54 | summary: 'App name to show in bottom left corner',
|
55 | },
|
56 | {
|
57 | name: 'app-version',
|
58 | summary: 'App version to show in bottom left corner',
|
59 | },
|
60 | ],
|
61 | };
|
62 | }
|
63 | async run(inputs, options) {
|
64 | const [url] = inputs;
|
65 | const { host, port, ssl } = options;
|
66 | const protocol = ssl ? 'https' : 'http';
|
67 | const projectType = options['project-type'];
|
68 | const name = options['app-name'];
|
69 | const version = options['app-version'];
|
70 | const app = express();
|
71 | app.use('/', express.static(WWW_DIRECTORY));
|
72 | app.get('/api/app', (req, res) => {
|
73 | res.json({ url, name, version, projectType });
|
74 | });
|
75 | const server = ssl ? https.createServer(await this.collectSecureContextOptions(options), app) : http.createServer(app);
|
76 | server.listen({ port, host });
|
77 | const labUrl = `${protocol}://${host}:${port}`;
|
78 | server.on('listening', () => {
|
79 | process.stdout.write('Ionic Lab running!\n' +
|
80 | `Lab: ${chalk.bold(labUrl)}\n` +
|
81 | `App: ${chalk.bold(url)}\n`);
|
82 | });
|
83 | }
|
84 | async collectSecureContextOptions(options) {
|
85 | const sslKeyPath = options['ssl-key'] ? String(options['ssl-key']) : undefined;
|
86 | const sslCertPath = options['ssl-cert'] ? String(options['ssl-cert']) : undefined;
|
87 | if (!sslKeyPath || !sslCertPath) {
|
88 | throw new Error('SSL key and cert required for serving SSL');
|
89 | }
|
90 | const [key, cert] = await Promise.all([this.readPem(sslKeyPath), this.readPem(sslCertPath)]);
|
91 | return { key, cert };
|
92 | }
|
93 | async readPem(p) {
|
94 | try {
|
95 | return await utils_fs_1.readFile(p, { encoding: 'utf8' });
|
96 | }
|
97 | catch (e) {
|
98 | process.stderr.write(String(e.stack ? e.stack : e) + '\n');
|
99 | throw new Error(`Error encountered with ${p}`);
|
100 | }
|
101 | }
|
102 | }
|
103 | class LabNamespace extends cli_framework_1.Namespace {
|
104 | async getMetadata() {
|
105 | return {
|
106 | name: 'ionic-lab',
|
107 | summary: '',
|
108 | };
|
109 | }
|
110 | async getCommands() {
|
111 | return new cli_framework_1.CommandMap([[cli_framework_1.CommandMapDefault, async () => new DefaultCommand(this)]]);
|
112 | }
|
113 | }
|
114 | const namespace = new LabNamespace();
|
115 | async function run(argv, env) {
|
116 | await cli_framework_1.execute({ namespace, argv, env });
|
117 | }
|
118 | exports.run = run;
|