Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit c17e6df

Browse files
manucorporatadamdbradley
authored andcommitted
feat(bonjour): adds service auto-discovery
1 parent d7a4d1e commit c17e6df

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"dependencies": {
3434
"autoprefixer": "6.7.2",
3535
"babili": "0.0.10",
36+
"bonjour": "^3.5.0",
3637
"chalk": "1.1.3",
3738
"chokidar": "1.6.1",
3839
"clean-css": "3.4.24",

src/dev-server/bonjour.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Logger } from '../logger/logger';
2+
import { getProjectJson } from '../util/ionic-project';
3+
import { ServeConfig } from './serve-config';
4+
5+
export function createBonjourService(config: ServeConfig) {
6+
if (!config.bonjour) {
7+
return;
8+
}
9+
getProjectJson()
10+
.then(project => project.name)
11+
.catch(() => 'ionic-app-scripts')
12+
.then(projectName => {
13+
Logger.info(`publishing bonjour service`);
14+
15+
const bonjour = require('bonjour')();
16+
bonjour.publish({
17+
name: projectName,
18+
type: 'ionicdev',
19+
port: config.httpPort
20+
});
21+
22+
const unpublish = function () {
23+
bonjour.unpublishAll();
24+
bonjour.destroy();
25+
};
26+
27+
process.on('exit', unpublish);
28+
process.on('SIGINT', unpublish);
29+
});
30+
}

src/dev-server/serve-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface ServeConfig {
1717
useServerLogs: boolean;
1818
notifyOnConsoleLog: boolean;
1919
useProxy: boolean;
20+
bonjour: boolean;
2021
}
2122
export const LOGGER_DIR = '__ion-dev-server';
2223
export const IONIC_LAB_URL = '/ionic-lab';

src/serve.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ describe('test serve', () => {
3737
notificationPort: 53703,
3838
useServerLogs: false,
3939
useProxy: true,
40-
notifyOnConsoleLog: false
40+
notifyOnConsoleLog: false,
41+
bonjour: false
4142
};
4243
spyOn(network, 'findClosestOpenPorts').and.callFake((host: string, ports: number[]) => Promise.resolve(ports));
4344
spyOn(notificationServer, 'createNotificationServer');

src/serve.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import open from './util/open';
88
import { createNotificationServer } from './dev-server/notification-server';
99
import { createHttpServer } from './dev-server/http-server';
1010
import { createLiveReloadServer } from './dev-server/live-reload';
11+
import { createBonjourService } from './dev-server/bonjour';
1112
import { ServeConfig, IONIC_LAB_URL } from './dev-server/serve-config';
1213
import { findClosestOpenPorts } from './util/network';
1314

@@ -45,7 +46,8 @@ export function serve(context: BuildContext) {
4546
notificationPort: notificationPortFound,
4647
useServerLogs: useServerLogs(context),
4748
useProxy: useProxy(context),
48-
notifyOnConsoleLog: sendClientConsoleLogs(context)
49+
notifyOnConsoleLog: sendClientConsoleLogs(context),
50+
bonjour: useBonjour(context)
4951
};
5052

5153
createNotificationServer(config);
@@ -55,6 +57,7 @@ export function serve(context: BuildContext) {
5557
return watch(context);
5658
})
5759
.then(() => {
60+
createBonjourService(config);
5861
onReady(config, context);
5962
return config;
6063
}, (err: BuildError) => {
@@ -139,6 +142,10 @@ function browserOption(context: BuildContext): string {
139142
return getConfigValue(context, '--browseroption', '-o', 'IONIC_BROWSEROPTION', 'ionic_browseroption', null);
140143
}
141144

145+
function useBonjour(context: BuildContext): boolean {
146+
return hasConfigValue(context, '--bonjour', '-B', 'bonjour', false);
147+
}
148+
142149
function launchLab(context: BuildContext): boolean {
143150
return hasConfigValue(context, '--lab', '-l', 'ionic_lab', false);
144151
}

0 commit comments

Comments
 (0)