Skip to content

Commit ee2de68

Browse files
committed
feat(): merge remote into serve and add qrcode option.
1 parent 2b18aaf commit ee2de68

File tree

4 files changed

+57
-122
lines changed

4 files changed

+57
-122
lines changed

packages/cli-build-ionic-angular/src/serve.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export default async function(cmdMetadata: CommandData, inputs: CommandLineInput
4040
const settings = await appScripts.serve(context);
4141
return {
4242
publicIp: chosenIP,
43+
protocol: 'http',
4344
...settings
4445
};
4546
}

packages/ionic/src/commands/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { ServeCommand } from './serve';
1313
import { GenerateCommand } from './generate';
1414
import { LinkCommand } from './link';
1515
import { UploadCommand } from './upload';
16-
import { RemoteCommand } from './remote';
1716

1817
export class IonicNamespace extends Namespace {
1918
name: 'global';
@@ -26,7 +25,6 @@ export class IonicNamespace extends Namespace {
2625
m.set('help', new HelpCommand());
2726
m.set('info', new InfoCommand());
2827
m.set('login', new LoginCommand());
29-
m.set('remote', new RemoteCommand());
3028
m.set('signup', new SignupCommand());
3129
m.set('version', new VersionCommand());
3230
m.set('telemetry', new TelemetryCommand());

packages/ionic/src/commands/remote.ts

Lines changed: 0 additions & 119 deletions
This file was deleted.

packages/ionic/src/commands/serve.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as dgram from 'dgram';
2+
import * as qrcode from 'qrcode-terminal';
13
import {
24
CommandLineInputs,
35
CommandLineOptions,
@@ -79,6 +81,11 @@ import {
7981
name: 'platform',
8082
description: 'Start serve with a specific platform (ios/android)',
8183
aliases: ['t']
84+
},
85+
{
86+
name: 'qrcode',
87+
description: 'Print a QR code for Ionic View instead of network broadcasting',
88+
type: Boolean
8289
}
8390
],
8491
requiresProject: true
@@ -90,13 +97,61 @@ export class ServeCommand extends Command {
9097

9198
var tasks = new TaskChain();
9299

93-
await this.env.emitEvent('serve', {
100+
var response = await this.env.emitEvent('serve', {
94101
metadata: this.metadata,
95102
inputs,
96103
options
97104
});
98105

99106
tasks.next(`Starting server`);
107+
108+
// If qrcode option then generate a qrcode on the Command Line.
109+
if (options.qrcode) {
110+
const codeString = await generateQrCode(
111+
`${response.protocol}://${response.publicIp}:${response.httpPort}`
112+
);
113+
this.env.log.msg(`\n\n\n${codeString}`);
114+
}
115+
116+
// If broadcast option then start udp server and broadcast info
117+
if (options.broadcast) {
118+
tasks.next(`Broadcasting server information`);
119+
const appDetails = await this.env.project.load();
120+
121+
const message = JSON.stringify({
122+
app_name: appDetails.name,
123+
app_id: appDetails.app_id,
124+
local_address: `${response.protocol}://${response.publicIp}:${response.httpPort}`
125+
});
126+
const server = dgram.createSocket('udp4');
127+
128+
server.on('listening', () => {
129+
server.setBroadcast(true);
130+
setInterval(() => {
131+
try {
132+
server.send(message, 41234, '255.255.255.255');
133+
} catch (e) {
134+
throw e;
135+
}
136+
}, 3000);
137+
});
138+
139+
server.bind();
140+
}
141+
100142
tasks.end();
101143
}
102144
}
145+
146+
function generateQrCode(input: string): Promise<string> {
147+
return new Promise((resolve, reject) => {
148+
149+
try {
150+
qrcode.generate(input, (response: any) => {
151+
resolve(response);
152+
});
153+
} catch (e) {
154+
reject(e);
155+
}
156+
});
157+
}

0 commit comments

Comments
 (0)